]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/objects-coerce-freeze-borrored.rs
d2523bc4f246f34ec0fb1c4e4383aeed577fe930
[rust.git] / src / test / run-pass / objects-coerce-freeze-borrored.rs
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
13 trait Foo {
14     fn foo(&self) -> uint;
15     fn bar(&mut self) -> uint;
16 }
17
18 impl Foo for uint {
19     fn foo(&self) -> uint {
20         *self
21     }
22
23     fn bar(&mut self) -> uint {
24         *self += 1;
25         *self
26     }
27 }
28
29 fn do_it_mut(obj: &mut Foo) {
30     let x = obj.bar();
31     let y = obj.foo();
32     assert_eq!(x, y);
33
34     do_it_imm(obj, y);
35 }
36
37 fn do_it_imm(obj: &Foo, v: uint) {
38     let y = obj.foo();
39     assert_eq!(v, y);
40 }
41
42 pub fn main() {
43     let mut x = 22;
44     let obj = &mut x as &mut Foo;
45     do_it_mut(obj);
46     do_it_imm(obj, 23);
47     do_it_mut(obj);
48 }