2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(dead_code)]
|
2012-11-28 20:34:30 +00:00
|
|
|
// Testing that supertrait methods can be called on subtrait object types
|
|
|
|
|
2015-03-22 20:13:15 +00:00
|
|
|
|
2012-11-28 20:34:30 +00:00
|
|
|
trait Foo {
|
2015-03-26 00:06:52 +00:00
|
|
|
fn f(&self) -> isize;
|
2012-11-28 20:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Bar : Foo {
|
2015-03-26 00:06:52 +00:00
|
|
|
fn g(&self) -> isize;
|
2012-11-28 20:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct A {
|
2015-03-26 00:06:52 +00:00
|
|
|
x: isize
|
2012-11-28 20:34:30 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl Foo for A {
|
2015-03-26 00:06:52 +00:00
|
|
|
fn f(&self) -> isize { 10 }
|
2012-11-28 20:34:30 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl Bar for A {
|
2015-03-26 00:06:52 +00:00
|
|
|
fn g(&self) -> isize { 20 }
|
2012-11-28 20:34:30 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2012-11-28 20:34:30 +00:00
|
|
|
let a = &A { x: 3 };
|
2019-05-28 18:47:21 +00:00
|
|
|
let afoo = a as &dyn Foo;
|
|
|
|
let abar = a as &dyn Bar;
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(afoo.f(), 10);
|
|
|
|
assert_eq!(abar.g(), 20);
|
|
|
|
assert_eq!(abar.f(), 10);
|
2012-11-28 20:34:30 +00:00
|
|
|
}
|