xref: /DragonOS/kernel/crates/intertrait/tests/on-trait-impl-assoc-type2.rs (revision bd70d2d1f490aabd570a5301b858bd5eb04149fa)
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 
concat(&self, a: Self::I1, b: Self::I2) -> String14     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 
concat(&self, a: Self::I1, b: Self::I2) -> String22     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]
test_cast_to_on_trait_impl_with_assoc_type2()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