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