]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/overloaded-index-in-field.rs
66f8c5c4238fa66fc37c24631bd5edaafde3d245
[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 #![feature(core)]
15
16 use std::ops::Index;
17
18 struct Foo {
19     x: int,
20     y: int,
21 }
22
23 struct Bar {
24     foo: Foo
25 }
26
27 impl Index<int> for Foo {
28     type Output = int;
29
30     fn index(&self, z: &int) -> &int {
31         if *z == 0 {
32             &self.x
33         } else {
34             &self.y
35         }
36     }
37 }
38
39 trait Int {
40     fn get(self) -> int;
41     fn get_from_ref(&self) -> int;
42     fn inc(&mut self);
43 }
44
45 impl Int for int {
46     fn get(self) -> int { self }
47     fn get_from_ref(&self) -> int { *self }
48     fn inc(&mut self) { *self += 1; }
49 }
50
51 fn main() {
52     let f = Bar { foo: Foo {
53         x: 1,
54         y: 2,
55     } };
56     assert_eq!(f.foo[1].get(), 2);
57 }