2 // If `Index` used an associated type for its output, this test would
7 struct Mat<T> { data: Vec<T>, cols: usize, }
10 fn new(data: Vec<T>, cols: usize) -> Mat<T> {
11 Mat { data: data, cols: cols }
13 fn row<'a>(&'a self, row: usize) -> Row<&'a Mat<T>> {
14 Row { mat: self, row: row, }
18 impl<T> Index<(usize, usize)> for Mat<T> {
21 fn index<'a>(&'a self, (row, col): (usize, usize)) -> &'a T {
22 &self.data[row * self.cols + col]
26 impl<'a, T> Index<(usize, usize)> for &'a Mat<T> {
29 fn index<'b>(&'b self, index: (usize, usize)) -> &'b T {
34 struct Row<M> { mat: M, row: usize, }
36 impl<T, M: Index<(usize, usize), Output=T>> Index<usize> for Row<M> {
39 fn index<'a>(&'a self, col: usize) -> &'a T {
40 &self.mat[(self.row, col)]
45 let m = Mat::new(vec![1, 2, 3, 4, 5, 6], 3);
48 assert_eq!(r.index(2), &6);