xref: /DragonOS/kernel/crates/intertrait/tests/on-trait-impl-assoc-type2.rs (revision 2b7818e80e00fcfe4d03533f587cc125ea5e4bec)
1 use std::fmt::Debug;
2 
3 use intertrait::cast::*;
4 use intertrait::*;
5 
6 struct Data;
7 
8 trait Source: CastFrom {}
9 
10 trait Concat {
11     type I1: Debug;
12     type I2: Debug;
13 
14     fn concat(&self, a: Self::I1, b: Self::I2) -> String;
15 }
16 
17 #[cast_to]
18 impl Concat for Data {
19     type I1 = i32;
20     type I2 = &'static str;
21 
22     fn concat(&self, a: Self::I1, b: Self::I2) -> String {
23         format!("Data: {} - {}", a, b)
24     }
25 }
26 
27 impl Source for Data {}
28 
29 #[test]
30 fn test_cast_to_on_trait_impl_with_assoc_type2() {
31     let data = Data;
32     let source: &dyn Source = &data;
33     let concat = source.cast::<dyn Concat<I1 = i32, I2 = &'static str>>();
34     assert_eq!(concat.unwrap().concat(101, "hello"), "Data: 101 - hello");
35 }
36