Debian Codegraph @

gcc/testsuite/rust/execute/torture/trait5.rs

raw
1 /* { dg-output "123\r*\n123\r*\n" } */
2 extern "C" {
3 fn printf(s: *const i8, ...);
4 }
5
6 #[lang = "sized"]
7 pub trait Sized {}
8
9 struct Foo(i32);
10 trait Bar {
11 fn baz(&self);
12 }
13
14 impl Bar for Foo {
15 fn baz(&self) {
16 unsafe {
17 let a = "%i\n\0";
18 let b = a as *const str;
19 let c = b as *const i8;
20
21 printf(c, self.0);
22 }
23 }
24 }
25
26 fn static_dispatch<T: Bar>(t: &T) {
27 t.baz();
28 }
29
30 fn dynamic_dispatch(t: &dyn Bar) {
31 t.baz();
32 }
33
34 fn main() -> i32 {
35 let a = &Foo(123);
36 static_dispatch(a);
37
38 let b: &dyn Bar = a;
39 dynamic_dispatch(b);
40
41 0
42 }