]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2012 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 that we can coerce an `@Object` to an `&Object` | |
12 | ||
c34b1796 | 13 | |
1a4d82fc | 14 | trait Foo { |
c34b1796 AL |
15 | fn foo(&self) -> usize; |
16 | fn bar(&mut self) -> usize; | |
1a4d82fc JJ |
17 | } |
18 | ||
c34b1796 AL |
19 | impl Foo for usize { |
20 | fn foo(&self) -> usize { | |
1a4d82fc JJ |
21 | *self |
22 | } | |
23 | ||
c34b1796 | 24 | fn bar(&mut self) -> usize { |
1a4d82fc JJ |
25 | *self += 1; |
26 | *self | |
27 | } | |
28 | } | |
29 | ||
30 | fn do_it_mut(obj: &mut Foo) { | |
31 | let x = obj.bar(); | |
32 | let y = obj.foo(); | |
33 | assert_eq!(x, y); | |
34 | ||
35 | do_it_imm(obj, y); | |
36 | } | |
37 | ||
c34b1796 | 38 | fn do_it_imm(obj: &Foo, v: usize) { |
1a4d82fc JJ |
39 | let y = obj.foo(); |
40 | assert_eq!(v, y); | |
41 | } | |
42 | ||
43 | pub fn main() { | |
c34b1796 | 44 | let mut x: usize = 22; |
1a4d82fc JJ |
45 | let obj = &mut x as &mut Foo; |
46 | do_it_mut(obj); | |
c34b1796 | 47 | do_it_imm(obj, 23); |
1a4d82fc JJ |
48 | do_it_mut(obj); |
49 | } |