use dyn_dyn::{dyn_dyn_base, dyn_dyn_cast, dyn_dyn_impl}; #[test] fn test_generic_base() { #[dyn_dyn_base] trait Base {} trait TestTraitA {} trait TestTraitB {} struct TestStruct; #[dyn_dyn_impl(TestTraitA)] impl Base for TestStruct {} #[dyn_dyn_impl(TestTraitB)] impl Base for TestStruct {} impl TestTraitA for TestStruct {} impl TestTraitB for TestStruct {} assert!(dyn_dyn_cast!(Base => TestTraitA, &TestStruct as &dyn Base).is_ok()); assert!(dyn_dyn_cast!(Base => TestTraitB, &TestStruct as &dyn Base).is_err()); assert!(dyn_dyn_cast!(Base => TestTraitA, &TestStruct as &dyn Base).is_err()); assert!(dyn_dyn_cast!(Base => TestTraitB, &TestStruct as &dyn Base).is_ok()); } #[test] fn test_generic_trait() { #[dyn_dyn_base] trait Base {} trait GenericTrait { fn test(&self) -> u32; } struct TestStruct; #[dyn_dyn_impl(GenericTrait, GenericTrait)] impl Base for TestStruct {} impl GenericTrait for TestStruct { fn test(&self) -> u32 { 0 } } impl GenericTrait for TestStruct { fn test(&self) -> u32 { 1 } } assert_eq!( Ok(0), dyn_dyn_cast!(Base => GenericTrait, &TestStruct as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); assert_eq!( Ok(1), dyn_dyn_cast!(Base => GenericTrait, &TestStruct as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); assert_eq!( Err(()), dyn_dyn_cast!(Base => GenericTrait, &TestStruct as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); } #[test] fn test_generic_trait_from_param() { #[dyn_dyn_base] trait Base {} trait GenericTrait { fn test(&self) -> u32; } struct TestStruct(T); #[dyn_dyn_impl(GenericTrait)] impl Base for TestStruct {} impl GenericTrait for TestStruct { fn test(&self) -> u32 { 1234 } } assert_eq!( Ok(1234), dyn_dyn_cast!(Base => GenericTrait, &TestStruct(0_u32) as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); assert_eq!( Err(()), dyn_dyn_cast!(Base => GenericTrait, &TestStruct(0_u32) as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); assert_eq!( Err(()), dyn_dyn_cast!(Base => GenericTrait, &TestStruct(0_u64) as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); assert_eq!( Ok(1234), dyn_dyn_cast!(Base => GenericTrait, &TestStruct(0_u64) as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); } #[test] fn test_generic_base_from_param() { #[dyn_dyn_base] trait Base {} trait TestTrait {} struct TestStruct(T); #[dyn_dyn_impl(TestTrait)] impl Base for TestStruct {} impl TestTrait for TestStruct {} assert!( dyn_dyn_cast!(Base => TestTrait, &TestStruct(0_u32) as &dyn Base).is_ok() ); assert!( dyn_dyn_cast!(Base => TestTrait, &TestStruct(0_u32) as &dyn Base).is_err() ); assert!( dyn_dyn_cast!(Base => TestTrait, &TestStruct(0_u64) as &dyn Base).is_err() ); assert!( dyn_dyn_cast!(Base => TestTrait, &TestStruct(0_u64) as &dyn Base).is_ok() ); } #[test] fn test_generic_base_blanket_impl() { #[dyn_dyn_base] trait Base {} trait TestTrait {} struct TestStruct; #[dyn_dyn_impl(TestTrait)] impl Base for TestStruct {} impl TestTrait for TestStruct {} assert!(dyn_dyn_cast!(Base => TestTrait, &TestStruct as &dyn Base).is_ok()); assert!(dyn_dyn_cast!(Base => TestTrait, &TestStruct as &dyn Base).is_err()); assert!(dyn_dyn_cast!(Base => TestTrait, &TestStruct as &dyn Base).is_err()); assert!(dyn_dyn_cast!(Base => TestTrait, &TestStruct as &dyn Base).is_ok()); } #[test] fn test_where_clause_on_base() { #[dyn_dyn_base] trait Base where T: 'static, { } trait TestTrait {} struct TestStruct; #[dyn_dyn_impl(TestTrait)] impl Base for TestStruct {} impl TestTrait for TestStruct {} assert!(dyn_dyn_cast!(Base => TestTrait, &TestStruct as &dyn Base).is_ok()); } #[test] fn test_where_clause_on_derived() { #[dyn_dyn_base] trait Base {} trait GenericTrait { fn test(&self) -> u32; } struct TestStruct(T) where T: 'static; #[dyn_dyn_impl(GenericTrait)] impl Base for TestStruct where T: 'static {} impl GenericTrait for TestStruct { fn test(&self) -> u32 { 1234 } } assert_eq!( Ok(1234), dyn_dyn_cast!(Base => GenericTrait, &TestStruct(0_u32) as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); assert_eq!( Err(()), dyn_dyn_cast!(Base => GenericTrait, &TestStruct(0_u32) as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); assert_eq!( Err(()), dyn_dyn_cast!(Base => GenericTrait, &TestStruct(0_u64) as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); assert_eq!( Ok(1234), dyn_dyn_cast!(Base => GenericTrait, &TestStruct(0_u64) as &dyn Base) .map(|b| b.test()) .map_err(|_| ()) ); }