about summary refs log tree commit diff
path: root/tests/ui/traits/index-trait-multiple-impls.rs
diff options
context:
space:
mode:
authorKivooeo <Kivooeo123@gmail.com>2025-07-24 18:29:09 +0500
committerKivooeo <Kivooeo123@gmail.com>2025-07-25 15:34:28 +0500
commit90bb5cacb5c1a5fe20ba821d28e7eb7a21e35d09 (patch)
treefa18c4e8fc12592084b8d16f71a8838e44a116ac /tests/ui/traits/index-trait-multiple-impls.rs
parent3c30dbbe31bfbf6029f4534170165ba573ff0fd1 (diff)
downloadrust-90bb5cacb5c1a5fe20ba821d28e7eb7a21e35d09.tar.gz
rust-90bb5cacb5c1a5fe20ba821d28e7eb7a21e35d09.zip
moved 34 tests to organized locations
Diffstat (limited to 'tests/ui/traits/index-trait-multiple-impls.rs')
-rw-r--r--tests/ui/traits/index-trait-multiple-impls.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/ui/traits/index-trait-multiple-impls.rs b/tests/ui/traits/index-trait-multiple-impls.rs
new file mode 100644
index 00000000000..26fb7061664
--- /dev/null
+++ b/tests/ui/traits/index-trait-multiple-impls.rs
@@ -0,0 +1,59 @@
+//@ run-pass
+//@ revisions: current next
+//@ ignore-compare-mode-next-solver (explicit revisions)
+//@[next] compile-flags: -Znext-solver
+
+use std::ops::Index;
+
+struct Mat<T> { data: Vec<T>, cols: usize, }
+
+impl<T> Mat<T> {
+    fn new(data: Vec<T>, cols: usize) -> Mat<T> {
+        Mat { data: data, cols: cols }
+    }
+    fn row<'a>(&'a self, row: usize) -> Row<&'a Mat<T>> {
+        Row { mat: self, row: row, }
+    }
+}
+
+impl<T> Index<(usize, usize)> for Mat<T> {
+    type Output = T;
+
+    fn index<'a>(&'a self, (row, col): (usize, usize)) -> &'a T {
+        &self.data[row * self.cols + col]
+    }
+}
+
+impl<'a, T> Index<(usize, usize)> for &'a Mat<T> {
+    type Output = T;
+
+    fn index<'b>(&'b self, index: (usize, usize)) -> &'b T {
+        (*self).index(index)
+    }
+}
+
+struct Row<M> { mat: M, row: usize, }
+
+impl<T, M: Index<(usize, usize), Output=T>> Index<usize> for Row<M> {
+    type Output = T;
+
+    fn index<'a>(&'a self, col: usize) -> &'a T {
+        &self.mat[(self.row, col)]
+    }
+}
+
+fn main() {
+    let m = Mat::new(vec![1, 2, 3, 4, 5, 6], 3);
+    let r = m.row(1);
+
+    assert_eq!(r.index(2), &6);
+    assert_eq!(r[2], 6);
+    assert_eq!(r[2], 6);
+    assert_eq!(6, r[2]);
+
+    let e = r[2];
+    assert_eq!(e, 6);
+
+    let e: usize = r[2];
+    assert_eq!(e, 6);
+}