use std::fmt::Debug; use supply::prelude::*; #[cfg(feature = "std")] #[test] fn example_1() { // Source: https://doc.rust-lang.org/std/any/index.html#smart-pointers-and-dyn-any let boxed: Box = Box::new(3_i32); let actual_id = (*boxed).tag_id(); let boxed_id = boxed.tag_id(); assert_eq!(actual_id, TagTypeId::of::()); assert_eq!(boxed_id, TagTypeId::::of::>()); } #[cfg(feature = "std")] #[test] fn example_2() { // Source: https://doc.rust-lang.org/std/any/index.html#examples fn log(value: &T) -> String { let value_any = value as &dyn Anything; match value_any.downcast_ref::() { Some(as_string) => { format!("String ({}): {}", as_string.len(), as_string) } None => { format!("{value:?}") } } } fn do_work(value: &T) -> String { log(value) } let my_string = "Hello World".to_string(); let x = do_work(&my_string); assert_eq!(x, "String (11): Hello World"); let my_i8: i8 = 100; let x = do_work(&my_i8); assert_eq!(x, "100"); } #[test] fn example_3() { assert!(<() as AnythingExt>::is::<()>(&())); assert!(::is::("test")); } #[test] fn any_static() { struct A { name: String, } let a = A { name: String::from("bob"), }; let x: &dyn Anything = AnyStatic::from_ref(&a); let y = x.downcast_ref::>(); assert_eq!(y.unwrap().name, "bob"); }