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