| 1 |
// Testing multiple supertraits and calling supertrait methods |
| 2 |
|
| 3 |
struct Foo { |
| 4 |
my_int: u32, |
| 5 |
} |
| 6 |
|
| 7 |
trait GrandParent { |
| 8 |
fn grandparent(&self) -> u32; |
| 9 |
} |
| 10 |
|
| 11 |
trait Parent : GrandParent { |
| 12 |
fn parent(&self) -> bool; |
| 13 |
} |
| 14 |
|
| 15 |
trait Child : Parent { |
| 16 |
fn child(&self); |
| 17 |
} |
| 18 |
|
| 19 |
impl GrandParent for Foo { |
| 20 |
fn grandparent(&self) -> u32 { |
| 21 |
self.my_int |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
impl Parent for Foo { |
| 26 |
fn parent(&self) -> bool { |
| 27 |
// Call supertrait method |
| 28 |
return self.grandparent() != 0; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
impl Child for Foo { |
| 33 |
fn child(&self) { |
| 34 |
let _ = self; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
pub fn main() { |
| 39 |
let a = Foo{my_int: 0xfeedf00d}; |
| 40 |
let b: &dyn Child = &a; |
| 41 |
|
| 42 |
b.parent(); |
| 43 |
b.child(); |
| 44 |
|
| 45 |
// Here to silence bogus compiler warning |
| 46 |
let _ = a.my_int; |
| 47 |
} |