]> git.proxmox.com Git - rustc.git/blame - src/test/ui/issues/issue-15734.rs
New upstream version 1.49.0+dfsg1
[rustc.git] / src / test / ui / issues / issue-15734.rs
CommitLineData
b7449926 1// run-pass
1a4d82fc
JJ
2// If `Index` used an associated type for its output, this test would
3// work more smoothly.
4
1a4d82fc
JJ
5use std::ops::Index;
6
c34b1796 7struct Mat<T> { data: Vec<T>, cols: usize, }
1a4d82fc
JJ
8
9impl<T> Mat<T> {
c34b1796 10 fn new(data: Vec<T>, cols: usize) -> Mat<T> {
1a4d82fc
JJ
11 Mat { data: data, cols: cols }
12 }
c34b1796 13 fn row<'a>(&'a self, row: usize) -> Row<&'a Mat<T>> {
1a4d82fc
JJ
14 Row { mat: self, row: row, }
15 }
16}
17
c34b1796 18impl<T> Index<(usize, usize)> for Mat<T> {
1a4d82fc
JJ
19 type Output = T;
20
c34b1796 21 fn index<'a>(&'a self, (row, col): (usize, usize)) -> &'a T {
1a4d82fc
JJ
22 &self.data[row * self.cols + col]
23 }
24}
25
c34b1796 26impl<'a, T> Index<(usize, usize)> for &'a Mat<T> {
1a4d82fc
JJ
27 type Output = T;
28
c34b1796 29 fn index<'b>(&'b self, index: (usize, usize)) -> &'b T {
1a4d82fc
JJ
30 (*self).index(index)
31 }
32}
33
c34b1796 34struct Row<M> { mat: M, row: usize, }
1a4d82fc 35
c34b1796 36impl<T, M: Index<(usize, usize), Output=T>> Index<usize> for Row<M> {
1a4d82fc
JJ
37 type Output = T;
38
c34b1796
AL
39 fn index<'a>(&'a self, col: usize) -> &'a T {
40 &self.mat[(self.row, col)]
1a4d82fc
JJ
41 }
42}
43
44fn main() {
c30ab7b3 45 let m = Mat::new(vec![1, 2, 3, 4, 5, 6], 3);
1a4d82fc
JJ
46 let r = m.row(1);
47
62682a34
SL
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]);
1a4d82fc
JJ
52
53 let e = r[2];
62682a34 54 assert_eq!(e, 6);
1a4d82fc 55
c34b1796 56 let e: usize = r[2];
62682a34 57 assert_eq!(e, 6);
1a4d82fc 58}