]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/trait-inheritance-cast.rs
51bc2751873faa566dc41c1b0f465248360a5574
[rust.git] / src / test / run-pass / trait-inheritance-cast.rs
1 // Copyright 2012-2014 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 // Testing that supertrait methods can be called on subtrait object types
12
13 trait Foo {
14     fn f(&self) -> int;
15 }
16
17 trait Bar : Foo {
18     fn g(&self) -> int;
19 }
20
21 struct A {
22     x: int
23 }
24
25 impl Foo for A {
26     fn f(&self) -> int { 10 }
27 }
28
29 impl Bar for A {
30     fn g(&self) -> int { 20 }
31 }
32
33 pub fn main() {
34     let a = &A { x: 3 };
35     let afoo = a as &Foo;
36     let abar = a as &Bar;
37     assert_eq!(afoo.f(), 10);
38     assert_eq!(abar.g(), 20);
39     assert_eq!(abar.f(), 10);
40 }