2019-07-26 16:54:25 -05:00
|
|
|
// run-pass
|
2013-08-13 14:46:08 -05:00
|
|
|
// Test that we can coerce an `@Object` to an `&Object`
|
|
|
|
|
2015-03-22 15:13:15 -05:00
|
|
|
|
2013-08-13 14:46:08 -05:00
|
|
|
trait Foo {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn foo(&self) -> usize;
|
|
|
|
fn bar(&mut self) -> usize;
|
2013-08-13 14:46:08 -05:00
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
impl Foo for usize {
|
|
|
|
fn foo(&self) -> usize {
|
2013-08-13 14:46:08 -05:00
|
|
|
*self
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn bar(&mut self) -> usize {
|
2013-08-13 14:46:08 -05:00
|
|
|
*self += 1;
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 13:47:21 -05:00
|
|
|
fn do_it_mut(obj: &mut dyn Foo) {
|
2013-08-13 14:46:08 -05:00
|
|
|
let x = obj.bar();
|
|
|
|
let y = obj.foo();
|
|
|
|
assert_eq!(x, y);
|
|
|
|
|
|
|
|
do_it_imm(obj, y);
|
|
|
|
}
|
|
|
|
|
2019-05-28 13:47:21 -05:00
|
|
|
fn do_it_imm(obj: &dyn Foo, v: usize) {
|
2013-08-13 14:46:08 -05:00
|
|
|
let y = obj.foo();
|
|
|
|
assert_eq!(v, y);
|
|
|
|
}
|
|
|
|
|
2013-09-25 02:43:37 -05:00
|
|
|
pub fn main() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let mut x: usize = 22;
|
2019-05-28 13:47:21 -05:00
|
|
|
let obj = &mut x as &mut dyn Foo;
|
2013-08-13 14:46:08 -05:00
|
|
|
do_it_mut(obj);
|
2015-03-03 02:42:26 -06:00
|
|
|
do_it_imm(obj, 23);
|
2013-08-13 14:46:08 -05:00
|
|
|
do_it_mut(obj);
|
|
|
|
}
|