]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-15734.rs
Merge branch 'master' into cfg_tmp_dir
[rust.git] / src / test / run-pass / issue-15734.rs
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.
4 //
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.
10
11 struct Mat<T> { data: Vec<T>, cols: uint, }
12
13 impl<T> Mat<T> {
14     fn new(data: Vec<T>, cols: uint) -> Mat<T> {
15         Mat { data: data, cols: cols }
16     }
17     fn row<'a>(&'a self, row: uint) -> Row<&'a Mat<T>> {
18         Row { mat: self, row: row, }
19     }
20 }
21
22 impl<T> Index<(uint, uint), T> for Mat<T> {
23     fn index<'a>(&'a self, &(row, col): &(uint, uint)) -> &'a T {
24         &self.data[row * self.cols + col]
25     }
26 }
27
28 impl<'a, T> Index<(uint, uint), T> for &'a Mat<T> {
29     fn index<'b>(&'b self, index: &(uint, uint)) -> &'b T {
30         (*self).index(index)
31     }
32 }
33
34 struct Row<M> { mat: M, row: uint, }
35
36 impl<T, M: Index<(uint, uint), T>> Index<uint, T> for Row<M> {
37     fn index<'a>(&'a self, col: &uint) -> &'a T {
38         &self.mat[(self.row, *col)]
39     }
40 }
41
42 fn main() {
43     let m = Mat::new(vec!(1u, 2, 3, 4, 5, 6), 3);
44     let r = m.row(1);
45
46     assert!(r.index(&2) == &6);
47     assert!(r[2] == 6);
48     assert!(r[2u] == 6u);
49     assert!(6 == r[2]);
50
51     let e = r[2];
52     assert!(e == 6);
53
54     let e: uint = r[2];
55     assert!(e == 6);
56 }