//! These are some tests derived from better_any. //! //! Source: https://github.com/rrevenantt/better_any/blob/main/better_any/tests/test.rs //! //! The tests have been modified to follow supply's API but to also retain the API of better_any as //! much as possible. We do not aim to provide 1-to-1 API to better_any, but we can provide a //! similar one. //! //! better_any only operates on one lifetime, but supply works on an arbitrary number so we //! get to see where supply adds complexity to have this feature. #![allow(clippy::extra_unused_lifetimes)] use supply::prelude::*; struct S1(usize); impl Tagged for S1 { type Tag = tag_for!(Self); } struct S2<'a>(&'a str); impl<'a> Tagged for S2<'a> { type Tag = tag_for!(<'l0> S2<'l0>); } #[allow(unused)] struct S2_2<'a, 'b>(&'a &'b str); impl<'a, 'b> Tagged for S2_2<'a, 'b> { type Tag = tag_for!(<'l0> S2_2<'l0, 'l0>); } #[allow(unused)] struct S3<'a, T>(&'a T); impl_tagged! { struct S3<'a, T> where T::Reified: 'a } pub struct S7(pub T); impl_tagged! { pub struct S7 } fn _test<'a>() { fn test_bound() {} test_bound::(); test_bound::>(); } #[test] fn test_downcast_trait_object() { trait T<'a>: Anything {} #[derive(Clone)] struct S<'a>(&'a str); impl<'a> Tagged for S<'a> { type Tag = tag_for!(<'l0> S<'l0>); } impl<'a> T<'a> for S<'a> {} let s = String::from("xx"); let orig = S(&s); let to: &dyn T<'_> = &orig as &dyn T; let downcasted = to.downcast_ref::().unwrap(); assert_eq!(orig.0, downcasted.0); assert!(!to.is::()); } #[test] fn test_static() { let a = S1(5); let a = &a as &dyn Anything; assert_eq!(a.downcast_ref::().unwrap().0, 5); } #[test] fn test_simple() { let s7 = S7(S1(5)); let s7 = &s7 as &dyn Anything; assert_eq!(s7.downcast_ref::>().unwrap().0 .0, 5); let s = String::from("test"); let a = S2(&s); let a = &a as &dyn Anything; assert_eq!(a.downcast_ref::().unwrap().0, "test"); } #[test] fn test_generic_context() { use std::borrow::Cow; fn generic<'a, H: Anything>(x: H) -> Cow<'a, str> { if let Some(s1) = x.downcast_ref::() { return Cow::Owned(s1.0.to_string()); } if let Some(s2) = x.downcast_ref::() { return Cow::Borrowed(s2.0); } panic!("unsupported type") } assert_eq!(generic(S1(5)).as_ref(), "5"); assert_eq!(generic(S2("x")).as_ref(), "x"); }