File: supertraits.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (79 lines) | stat: -rw-r--r-- 1,625 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#![feature(trait_upcasting)]
// Check that super-traits are callable.

//@ revisions: cfi kcfi
// FIXME(#122848) Remove only-linux once OSX CFI binaries work
//@ only-linux
//@ [cfi] needs-sanitizer-cfi
//@ [kcfi] needs-sanitizer-kcfi
//@ compile-flags: -C target-feature=-crt-static
//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0
//@ [cfi] compile-flags: -Z sanitizer=cfi
//@ [kcfi] compile-flags: -Z sanitizer=kcfi
//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off
//@ run-pass

trait Parent1 {
    type P1;
    fn p1(&self) -> Self::P1;
    fn d(&self) -> i32 {
        42
    }
}

trait Parent2 {
    type P2;
    fn p2(&self) -> Self::P2;
}

trait Child : Parent1 + Parent2 {
    type C;
    fn c(&self) -> Self::C;
}

struct Foo;

impl Parent1 for Foo {
    type P1 = u16;
    fn p1(&self) -> Self::P1 {
        println!("p1");
        1
    }
}

impl Parent2 for Foo {
    type P2 = u32;
    fn p2(&self) -> Self::P2 {
        println!("p2");
        2
    }
}

impl Child for Foo {
    type C = u8;
    fn c(&self) -> Self::C {
        println!("c");
        0
    }
}

fn main() {
    // Child can access its own methods and super methods.
    let x = &Foo as &dyn Child<C=u8,P1=u16,P2=u32>;
    x.c();
    x.p1();
    x.p2();
    x.d();
    // Parents can be created and access their methods.
    let y = &Foo as &dyn Parent1<P1=u16>;
    y.p1();
    y.d();
    let z = &Foo as &dyn Parent2<P2=u32>;
    z.p2();
    // Trait upcasting works
    let x1 = x as &dyn Parent1<P1=u16>;
    x1.p1();
    x1.d();
    let x2 = x as &dyn Parent2<P2=u32>;
    x2.p2();
}