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.
14 #![feature(old_orphan_check)]
18 struct Mat<T> { data: Vec<T>, cols: uint, }
21 fn new(data: Vec<T>, cols: uint) -> Mat<T> {
22 Mat { data: data, cols: cols }
24 fn row<'a>(&'a self, row: uint) -> Row<&'a Mat<T>> {
25 Row { mat: self, row: row, }
29 impl<T> Index<(uint, uint)> for Mat<T> {
32 fn index<'a>(&'a self, &(row, col): &(uint, uint)) -> &'a T {
33 &self.data[row * self.cols + col]
37 impl<'a, T> Index<(uint, uint)> for &'a Mat<T> {
40 fn index<'b>(&'b self, index: &(uint, uint)) -> &'b T {
45 struct Row<M> { mat: M, row: uint, }
47 impl<T, M: Index<(uint, uint), Output=T>> Index<uint> for Row<M> {
50 fn index<'a>(&'a self, col: &uint) -> &'a T {
51 &self.mat[(self.row, *col)]
56 let m = Mat::new(vec!(1, 2, 3, 4, 5, 6), 3);
59 assert!(r.index(&2) == &6);