xref: /DragonOS/kernel/crates/intertrait/tests/on-enum.rs (revision a8753f8fffb992e4d3bbd21eda431081b395af6b)
1 use intertrait::cast::*;
2 use intertrait::*;
3 
4 #[cast_to(Greet)]
5 #[allow(dead_code)]
6 enum Data {
7     Var1,
8     Var2(u32),
9 }
10 
11 trait Source: CastFrom {}
12 
13 trait Greet {
14     fn greet(&self);
15 }
16 
17 impl Greet for Data {
18     fn greet(&self) {
19         println!("Hello");
20     }
21 }
22 
23 impl Source for Data {}
24 
25 #[test]
26 fn test_cast_to_on_enum() {
27     let data = Data::Var2(1);
28     let source: &dyn Source = &data;
29     let greet = source.cast::<dyn Greet>();
30     greet.unwrap().greet();
31 }
32