• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // run-pass
2 // Test using overloaded indexing when the "map" is stored in a
3 // field. This caused problems at some point.
4 
5 use std::ops::Index;
6 
7 struct Foo {
8     x: isize,
9     y: isize,
10 }
11 
12 struct Bar {
13     foo: Foo
14 }
15 
16 impl Index<isize> for Foo {
17     type Output = isize;
18 
index(&self, z: isize) -> &isize19     fn index(&self, z: isize) -> &isize {
20         if z == 0 {
21             &self.x
22         } else {
23             &self.y
24         }
25     }
26 }
27 
28 trait Int {
get(self) -> isize29     fn get(self) -> isize;
get_from_ref(&self) -> isize30     fn get_from_ref(&self) -> isize;
inc(&mut self)31     fn inc(&mut self);
32 }
33 
34 impl Int for isize {
get(self) -> isize35     fn get(self) -> isize { self }
get_from_ref(&self) -> isize36     fn get_from_ref(&self) -> isize { *self }
inc(&mut self)37     fn inc(&mut self) { *self += 1; }
38 }
39 
main()40 fn main() {
41     let f = Bar { foo: Foo {
42         x: 1,
43         y: 2,
44     } };
45     assert_eq!(f.foo[1].get(), 2);
46 }
47