]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | // Test using overloaded indexing when the "map" is stored in a | |
12 | // field. This caused problems at some point. | |
13 | ||
c34b1796 AL |
14 | |
15 | #![feature(core)] | |
16 | ||
1a4d82fc JJ |
17 | use std::ops::Index; |
18 | ||
19 | struct Foo { | |
c34b1796 AL |
20 | x: isize, |
21 | y: isize, | |
1a4d82fc JJ |
22 | } |
23 | ||
24 | struct Bar { | |
25 | foo: Foo | |
26 | } | |
27 | ||
c34b1796 AL |
28 | impl Index<isize> for Foo { |
29 | type Output = isize; | |
1a4d82fc | 30 | |
c34b1796 AL |
31 | fn index(&self, z: isize) -> &isize { |
32 | if z == 0 { | |
1a4d82fc JJ |
33 | &self.x |
34 | } else { | |
35 | &self.y | |
36 | } | |
37 | } | |
38 | } | |
39 | ||
40 | trait Int { | |
c34b1796 AL |
41 | fn get(self) -> isize; |
42 | fn get_from_ref(&self) -> isize; | |
1a4d82fc JJ |
43 | fn inc(&mut self); |
44 | } | |
45 | ||
c34b1796 AL |
46 | impl Int for isize { |
47 | fn get(self) -> isize { self } | |
48 | fn get_from_ref(&self) -> isize { *self } | |
1a4d82fc JJ |
49 | fn inc(&mut self) { *self += 1; } |
50 | } | |
51 | ||
52 | fn main() { | |
53 | let f = Bar { foo: Foo { | |
54 | x: 1, | |
55 | y: 2, | |
56 | } }; | |
57 | assert_eq!(f.foo[1].get(), 2); | |
58 | } |