xref: /DragonOS/kernel/crates/intertrait/tests/castable_to.rs (revision 86ee1395de7c614865236ee15071c3603b794e44)
1 use intertrait::cast::*;
2 use intertrait::*;
3 
4 struct Data;
5 
6 trait Source: CastFrom {}
7 
8 trait Greet {
9     fn greet(&self);
10 }
11 
12 impl Greet for Data {
13     fn greet(&self) {
14         println!("Hello");
15     }
16 }
17 
18 trait Greet1 {
19     fn greet1(&self);
20 }
21 
22 impl Greet1 for Data {
23     fn greet1(&self) {
24         println!("Hello1");
25     }
26 }
27 
28 trait Greet2 {
29     fn greet2(&self);
30 }
31 
32 impl Greet2 for Data {
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]
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