]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/overloaded-index-in-field.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / overloaded-index-in-field.rs
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
14
15 #![feature(core)]
16
17 use std::ops::Index;
18
19 struct Foo {
20     x: isize,
21     y: isize,
22 }
23
24 struct Bar {
25     foo: Foo
26 }
27
28 impl Index<isize> for Foo {
29     type Output = isize;
30
31     fn index(&self, z: isize) -> &isize {
32         if z == 0 {
33             &self.x
34         } else {
35             &self.y
36         }
37     }
38 }
39
40 trait Int {
41     fn get(self) -> isize;
42     fn get_from_ref(&self) -> isize;
43     fn inc(&mut self);
44 }
45
46 impl Int for isize {
47     fn get(self) -> isize { self }
48     fn get_from_ref(&self) -> isize { *self }
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 }