3 #[derive(PartialEq, Debug)]
6 static TEST: &'static str = "Test";
10 let (a, b, c) = (&5 as &dyn Any, &TEST as &dyn Any, &Test as &dyn Any);
12 assert!(a.is::<i32>());
13 assert!(!b.is::<i32>());
14 assert!(!c.is::<i32>());
16 assert!(!a.is::<&'static str>());
17 assert!(b.is::<&'static str>());
18 assert!(!c.is::<&'static str>());
20 assert!(!a.is::<Test>());
21 assert!(!b.is::<Test>());
22 assert!(c.is::<Test>());
28 Box::new(5_usize) as Box<dyn Any>,
29 Box::new(TEST) as Box<dyn Any>,
30 Box::new(Test) as Box<dyn Any>,
33 assert!(a.is::<usize>());
34 assert!(!b.is::<usize>());
35 assert!(!c.is::<usize>());
37 assert!(!a.is::<&'static str>());
38 assert!(b.is::<&'static str>());
39 assert!(!c.is::<&'static str>());
41 assert!(!a.is::<Test>());
42 assert!(!b.is::<Test>());
43 assert!(c.is::<Test>());
47 fn any_downcast_ref() {
48 let a = &5_usize as &dyn Any;
50 match a.downcast_ref::<usize>() {
52 x => panic!("Unexpected value {x:?}"),
55 match a.downcast_ref::<Test>() {
57 x => panic!("Unexpected value {x:?}"),
62 fn any_downcast_mut() {
64 let mut b: Box<_> = Box::new(7_usize);
66 let a_r = &mut a as &mut dyn Any;
67 let tmp: &mut usize = &mut *b;
68 let b_r = tmp as &mut dyn Any;
70 match a_r.downcast_mut::<usize>() {
75 x => panic!("Unexpected value {x:?}"),
78 match b_r.downcast_mut::<usize>() {
83 x => panic!("Unexpected value {x:?}"),
86 match a_r.downcast_mut::<Test>() {
88 x => panic!("Unexpected value {x:?}"),
91 match b_r.downcast_mut::<Test>() {
93 x => panic!("Unexpected value {x:?}"),
96 match a_r.downcast_mut::<usize>() {
98 x => panic!("Unexpected value {x:?}"),
101 match b_r.downcast_mut::<usize>() {
103 x => panic!("Unexpected value {x:?}"),
109 let test = [0_usize; 8];
110 let test = &test as &dyn Any;
111 assert!(test.is::<[usize; 8]>());
112 assert!(!test.is::<[usize; 10]>());
117 fn is_any<T: Any + ?Sized>() {}
122 fn distinct_type_names() {
123 // https://github.com/rust-lang/rust/issues/84666
125 struct Velocity(f32, f32);
127 fn type_name_of_val<T>(_: T) -> &'static str {
131 assert_ne!(type_name_of_val(Velocity), type_name_of_val(Velocity(0.0, -9.8)),);
134 #[cfg(not(bootstrap))]
142 "dyn core::ops::function::Fn(i32, i32) -> i32",
143 std::any::type_name::<dyn Fn(i32, i32) -> i32>()
146 "dyn coretests::any::dyn_type_name::Foo<Bar = i32> \
147 + core::marker::Send + core::marker::Sync",
148 std::any::type_name::<dyn Foo<Bar = i32> + Send + Sync>()
152 // Test the `Provider` API.
154 struct SomeConcreteType {
158 impl Provider for SomeConcreteType {
159 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
161 .provide_ref::<String>(&self.some_string)
162 .provide_ref::<str>(&self.some_string)
163 .provide_value_with::<String>(|| "bye".to_owned());
167 // Test the provide and request mechanisms with a by-reference trait object.
170 let obj: &dyn Provider = &SomeConcreteType { some_string: "hello".to_owned() };
172 assert_eq!(&**request_ref::<String>(obj).unwrap(), "hello");
173 assert_eq!(&*request_value::<String>(obj).unwrap(), "bye");
174 assert_eq!(request_value::<u8>(obj), None);
177 // Test the provide and request mechanisms with a boxed trait object.
179 fn test_provider_boxed() {
180 let obj: Box<dyn Provider> = Box::new(SomeConcreteType { some_string: "hello".to_owned() });
182 assert_eq!(&**request_ref::<String>(&*obj).unwrap(), "hello");
183 assert_eq!(&*request_value::<String>(&*obj).unwrap(), "bye");
184 assert_eq!(request_value::<u8>(&*obj), None);
187 // Test the provide and request mechanisms with a concrete object.
189 fn test_provider_concrete() {
190 let obj = SomeConcreteType { some_string: "hello".to_owned() };
192 assert_eq!(&**request_ref::<String>(&obj).unwrap(), "hello");
193 assert_eq!(&*request_value::<String>(&obj).unwrap(), "bye");
194 assert_eq!(request_value::<u8>(&obj), None);
197 trait OtherTrait: Provider {}
199 impl OtherTrait for SomeConcreteType {}
201 impl dyn OtherTrait {
202 fn get_ref<T: 'static + ?Sized>(&self) -> Option<&T> {
203 request_ref::<T>(self)
207 // Test the provide and request mechanisms via an intermediate trait.
209 fn test_provider_intermediate() {
210 let obj: &dyn OtherTrait = &SomeConcreteType { some_string: "hello".to_owned() };
211 assert_eq!(obj.get_ref::<str>().unwrap(), "hello");