]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/overloaded-index.rs
Account for --remap-path-prefix in save-analysis
[rust.git] / src / test / run-pass / overloaded-index.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
12 #![feature(core)]
13
14 use std::ops::{Index, IndexMut};
15
16 struct Foo {
17     x: isize,
18     y: isize,
19 }
20
21 impl Index<isize> for Foo {
22     type Output = isize;
23
24     fn index(&self, z: isize) -> &isize {
25         if z == 0 {
26             &self.x
27         } else {
28             &self.y
29         }
30     }
31 }
32
33 impl IndexMut<isize> for Foo {
34     fn index_mut(&mut self, z: isize) -> &mut isize {
35         if z == 0 {
36             &mut self.x
37         } else {
38             &mut self.y
39         }
40     }
41 }
42
43 trait Int {
44     fn get(self) -> isize;
45     fn get_from_ref(&self) -> isize;
46     fn inc(&mut self);
47 }
48
49 impl Int for isize {
50     fn get(self) -> isize { self }
51     fn get_from_ref(&self) -> isize { *self }
52     fn inc(&mut self) { *self += 1; }
53 }
54
55 fn main() {
56     let mut f = Foo {
57         x: 1,
58         y: 2,
59     };
60     assert_eq!(f[1], 2);
61     f[0] = 3;
62     assert_eq!(f[0], 3);
63     {
64         let p = &mut f[1];
65         *p = 4;
66     }
67     {
68         let p = &f[1];
69         assert_eq!(*p, 4);
70     }
71
72     // Test calling methods with `&mut self`, `self, and `&self` receivers:
73     f[1].inc();
74     assert_eq!(f[1].get(), 5);
75     assert_eq!(f[1].get_from_ref(), 5);
76 }