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 // Test the `Provider` API.
136 struct SomeConcreteType {
140 impl Provider for SomeConcreteType {
141 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
143 .provide_ref::<String>(&self.some_string)
144 .provide_ref::<str>(&self.some_string)
145 .provide_value_with::<String>(|| "bye".to_owned());
149 // Test the provide and request mechanisms with a by-reference trait object.
152 let obj: &dyn Provider = &SomeConcreteType { some_string: "hello".to_owned() };
154 assert_eq!(&**request_ref::<String>(obj).unwrap(), "hello");
155 assert_eq!(&*request_value::<String>(obj).unwrap(), "bye");
156 assert_eq!(request_value::<u8>(obj), None);
159 // Test the provide and request mechanisms with a boxed trait object.
161 fn test_provider_boxed() {
162 let obj: Box<dyn Provider> = Box::new(SomeConcreteType { some_string: "hello".to_owned() });
164 assert_eq!(&**request_ref::<String>(&*obj).unwrap(), "hello");
165 assert_eq!(&*request_value::<String>(&*obj).unwrap(), "bye");
166 assert_eq!(request_value::<u8>(&*obj), None);
169 // Test the provide and request mechanisms with a concrete object.
171 fn test_provider_concrete() {
172 let obj = SomeConcreteType { some_string: "hello".to_owned() };
174 assert_eq!(&**request_ref::<String>(&obj).unwrap(), "hello");
175 assert_eq!(&*request_value::<String>(&obj).unwrap(), "bye");
176 assert_eq!(request_value::<u8>(&obj), None);
179 trait OtherTrait: Provider {}
181 impl OtherTrait for SomeConcreteType {}
183 impl dyn OtherTrait {
184 fn get_ref<T: 'static + ?Sized>(&self) -> Option<&T> {
185 request_ref::<T>(self)
189 // Test the provide and request mechanisms via an intermediate trait.
191 fn test_provider_intermediate() {
192 let obj: &dyn OtherTrait = &SomeConcreteType { some_string: "hello".to_owned() };
193 assert_eq!(obj.get_ref::<str>().unwrap(), "hello");