1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // If `Index` used an associated type for its output, this test would
12 // work more smoothly.
19 struct Mat<T> { data: Vec<T>, cols: usize, }
22 fn new(data: Vec<T>, cols: usize) -> Mat<T> {
23 Mat { data: data, cols: cols }
25 fn row<'a>(&'a self, row: usize) -> Row<&'a Mat<T>> {
26 Row { mat: self, row: row, }
30 impl<T> Index<(usize, usize)> for Mat<T> {
33 fn index<'a>(&'a self, (row, col): (usize, usize)) -> &'a T {
34 &self.data[row * self.cols + col]
38 impl<'a, T> Index<(usize, usize)> for &'a Mat<T> {
41 fn index<'b>(&'b self, index: (usize, usize)) -> &'b T {
46 struct Row<M> { mat: M, row: usize, }
48 impl<T, M: Index<(usize, usize), Output=T>> Index<usize> for Row<M> {
51 fn index<'a>(&'a self, col: usize) -> &'a T {
52 &self.mat[(self.row, col)]
57 let m = Mat::new(vec![1, 2, 3, 4, 5, 6], 3);
60 assert_eq!(r.index(2), &6);