]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/overloaded-index-in-field.rs
New upstream version 1.19.0+dfsg1
[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
c34b1796
AL
14
15#![feature(core)]
16
1a4d82fc
JJ
17use std::ops::Index;
18
19struct Foo {
c34b1796
AL
20 x: isize,
21 y: isize,
1a4d82fc
JJ
22}
23
24struct Bar {
25 foo: Foo
26}
27
c34b1796
AL
28impl 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
40trait 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
46impl 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
52fn main() {
53 let f = Bar { foo: Foo {
54 x: 1,
55 y: 2,
56 } };
57 assert_eq!(f.foo[1].get(), 2);
58}