Skip to content

Commit 52b9d21

Browse files
committed
Use non-static TypeId implementation from typeid crate
1 parent 6516c7e commit 52b9d21

File tree

2 files changed

+3
-59
lines changed

2 files changed

+3
-59
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rust-version = "1.61"
1414

1515
[dependencies]
1616
serde = { version = "1.0.194", default-features = false }
17+
typeid = "1"
1718

1819
[dev-dependencies]
1920
rustversion = "1.0.13"

src/any.rs

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use alloc::boxed::Box;
22
use core::any::TypeId;
3-
use core::marker::PhantomData;
43
use core::mem::{self, MaybeUninit};
54
use core::ptr;
65

@@ -41,7 +40,7 @@ impl Any {
4140
pub(crate) unsafe fn new<T>(t: T) -> Self {
4241
let value: Value;
4342
let drop: unsafe fn(&mut Value);
44-
let type_id = non_static_type_id::<T>();
43+
let type_id = typeid::of::<T>();
4544

4645
if is_small::<T>() {
4746
let mut inline = [MaybeUninit::uninit(); 2];
@@ -71,7 +70,7 @@ impl Any {
7170

7271
// This is unsafe -- caller is responsible that T is the correct type.
7372
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>() {
7574
self.invalid_cast_to::<T>();
7675
}
7776

@@ -106,59 +105,3 @@ impl Drop for Any {
106105
unsafe { (self.drop)(&mut self.value) }
107106
}
108107
}
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

Comments
 (0)