xref: /DragonOS/kernel/crates/intertrait/tests/castable_to.rs (revision bd70d2d1f490aabd570a5301b858bd5eb04149fa)
1 use intertrait::cast::*;
2 use intertrait::*;
3 
4 struct Data;
5 
6 trait Source: CastFrom {}
7 
8 trait Greet {
greet(&self)9     fn greet(&self);
10 }
11 
12 impl Greet for Data {
greet(&self)13     fn greet(&self) {
14         println!("Hello");
15     }
16 }
17 
18 trait Greet1 {
greet1(&self)19     fn greet1(&self);
20 }
21 
22 impl Greet1 for Data {
greet1(&self)23     fn greet1(&self) {
24         println!("Hello1");
25     }
26 }
27 
28 trait Greet2 {
greet2(&self)29     fn greet2(&self);
30 }
31 
32 impl Greet2 for Data {
greet2(&self)33     fn greet2(&self) {
34         println!("Hello2");
35     }
36 }
37 
38 impl Source for Data {}
39 
40 castable_to! { Data => crate::Greet, Greet1, Greet2 }
41 
42 #[test]
test_multi_traits_on_struct()43 fn test_multi_traits_on_struct() {
44     let data = Data;
45     let source: &dyn Source = &data;
46 
47     let greet = source.cast::<dyn Greet>();
48     greet.unwrap().greet();
49 
50     let greet1 = source.cast::<dyn Greet1>();
51     greet1.unwrap().greet1();
52 
53     let greet2 = source.cast::<dyn Greet2>();
54     greet2.unwrap().greet2();
55 }
56