|
1 | 1 | use alloc::boxed::Box; |
2 | 2 | use core::any::TypeId; |
3 | | -use core::marker::PhantomData; |
4 | 3 | use core::mem::{self, MaybeUninit}; |
5 | 4 | use core::ptr; |
6 | 5 |
|
@@ -41,7 +40,7 @@ impl Any { |
41 | 40 | pub(crate) unsafe fn new<T>(t: T) -> Self { |
42 | 41 | let value: Value; |
43 | 42 | let drop: unsafe fn(&mut Value); |
44 | | - let type_id = non_static_type_id::<T>(); |
| 43 | + let type_id = typeid::of::<T>(); |
45 | 44 |
|
46 | 45 | if is_small::<T>() { |
47 | 46 | let mut inline = [MaybeUninit::uninit(); 2]; |
@@ -71,7 +70,7 @@ impl Any { |
71 | 70 |
|
72 | 71 | // This is unsafe -- caller is responsible that T is the correct type. |
73 | 72 | pub(crate) unsafe fn take<T>(mut self) -> T { |
74 | | - if self.type_id != non_static_type_id::<T>() { |
| 73 | + if self.type_id != typeid::of::<T>() { |
75 | 74 | self.invalid_cast_to::<T>(); |
76 | 75 | } |
77 | 76 |
|
@@ -106,59 +105,3 @@ impl Drop for Any { |
106 | 105 | unsafe { (self.drop)(&mut self.value) } |
107 | 106 | } |
108 | 107 | } |
109 | | - |
110 | | -trait NonStaticAny { |
111 | | - fn get_type_id(&self) -> TypeId |
112 | | - where |
113 | | - Self: 'static; |
114 | | -} |
115 | | - |
116 | | -impl<T: ?Sized> NonStaticAny for PhantomData<T> { |
117 | | - fn get_type_id(&self) -> TypeId |
118 | | - where |
119 | | - Self: 'static, |
120 | | - { |
121 | | - TypeId::of::<T>() |
122 | | - } |
123 | | -} |
124 | | - |
125 | | -fn non_static_type_id<T: ?Sized>() -> TypeId { |
126 | | - let non_static_thing = PhantomData::<T>; |
127 | | - let thing = unsafe { |
128 | | - mem::transmute::<&dyn NonStaticAny, &(dyn NonStaticAny + 'static)>(&non_static_thing) |
129 | | - }; |
130 | | - NonStaticAny::get_type_id(thing) |
131 | | -} |
132 | | - |
133 | | -#[test] |
134 | | -fn test_non_static_type_id() { |
135 | | - assert_eq!(non_static_type_id::<usize>(), non_static_type_id::<usize>()); |
136 | | - assert_eq!( |
137 | | - non_static_type_id::<&str>(), |
138 | | - non_static_type_id::<&'static str>() |
139 | | - ); |
140 | | - |
141 | | - assert_ne!(non_static_type_id::<u32>(), non_static_type_id::<[u8; 4]>()); |
142 | | - assert_ne!( |
143 | | - non_static_type_id::<u32>(), |
144 | | - non_static_type_id::<[u32; 2]>() |
145 | | - ); |
146 | | - |
147 | | - assert_ne!(non_static_type_id::<usize>(), non_static_type_id::<isize>()); |
148 | | - assert_ne!( |
149 | | - non_static_type_id::<usize>(), |
150 | | - non_static_type_id::<&usize>() |
151 | | - ); |
152 | | - assert_ne!( |
153 | | - non_static_type_id::<&usize>(), |
154 | | - non_static_type_id::<&&usize>() |
155 | | - ); |
156 | | - assert_ne!( |
157 | | - non_static_type_id::<&usize>(), |
158 | | - non_static_type_id::<&mut usize>() |
159 | | - ); |
160 | | - |
161 | | - struct A; |
162 | | - struct B; |
163 | | - assert_ne!(non_static_type_id::<A>(), non_static_type_id::<B>()); |
164 | | -} |
0 commit comments