]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-15734.rs
Test enabling MIR inliner
[rust.git] / src / test / ui / issues / issue-15734.rs
1 // run-pass
2 // If `Index` used an associated type for its output, this test would
3 // work more smoothly.
4
5 use std::ops::Index;
6
7 struct Mat<T> { data: Vec<T>, cols: usize, }
8
9 impl<T> Mat<T> {
10     fn new(data: Vec<T>, cols: usize) -> Mat<T> {
11         Mat { data: data, cols: cols }
12     }
13     fn row<'a>(&'a self, row: usize) -> Row<&'a Mat<T>> {
14         Row { mat: self, row: row, }
15     }
16 }
17
18 impl<T> Index<(usize, usize)> for Mat<T> {
19     type Output = T;
20
21     fn index<'a>(&'a self, (row, col): (usize, usize)) -> &'a T {
22         &self.data[row * self.cols + col]
23     }
24 }
25
26 impl<'a, T> Index<(usize, usize)> for &'a Mat<T> {
27     type Output = T;
28
29     fn index<'b>(&'b self, index: (usize, usize)) -> &'b T {
30         (*self).index(index)
31     }
32 }
33
34 struct Row<M> { mat: M, row: usize, }
35
36 impl<T, M: Index<(usize, usize), Output=T>> Index<usize> for Row<M> {
37     type Output = T;
38
39     fn index<'a>(&'a self, col: usize) -> &'a T {
40         &self.mat[(self.row, col)]
41     }
42 }
43
44 fn main() {
45     let m = Mat::new(vec![1, 2, 3, 4, 5, 6], 3);
46     let r = m.row(1);
47
48     assert_eq!(r.index(2), &6);
49     assert_eq!(r[2], 6);
50     assert_eq!(r[2], 6);
51     assert_eq!(6, r[2]);
52
53     let e = r[2];
54     assert_eq!(e, 6);
55
56     let e: usize = r[2];
57     assert_eq!(e, 6);
58 }