Skip to content

Commit

Permalink
Change TypeId size to 128 bits
Browse files Browse the repository at this point in the history
rust-lang/rust#109953 made std TypeId 128 bits
and its new Hash impl prevents the use of TypeIdHasher
so I'll be using a pointer cast going forward
  • Loading branch information
leudz committed Jun 21, 2023
1 parent 59912c3 commit 09f820f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/scheduler/into_workload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ where
let unique_id = unique_id();

let name = Box::new(WorkloadLabel {
type_id: TypeId(unique_id),
type_id: TypeId(unique_id as u128),
name: unique_id.to_string().as_label(),
});

Expand Down Expand Up @@ -211,7 +211,7 @@ macro_rules! impl_into_workload {
let unique_id = unique_id();

let name = Box::new(WorkloadLabel {
type_id: TypeId(unique_id),
type_id: TypeId(unique_id as u128),
name: unique_id.to_string().as_label(),
});

Expand Down Expand Up @@ -241,7 +241,7 @@ macro_rules! impl_into_workload {
let unique_id = unique_id();

let name = Box::new(WorkloadLabel {
type_id: TypeId(unique_id),
type_id: TypeId(unique_id as u128),
name: unique_id.to_string().as_label(),
});

Expand Down
1 change: 1 addition & 0 deletions src/type_id/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl Hasher for TypeIdHasher {
}
}

#[cfg(feature = "std")]
#[test]
fn hasher() {
fn verify<T: 'static + ?Sized>() {
Expand Down
27 changes: 18 additions & 9 deletions src/type_id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::hash::{Hash, Hasher};
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]

pub struct TypeId(pub(crate) u64);
pub struct TypeId(pub(crate) u128);

impl TypeId {
pub(crate) fn of<T: ?Sized + 'static>() -> Self {
Expand All @@ -22,21 +22,30 @@ impl TypeId {

impl From<core::any::TypeId> for TypeId {
fn from(type_id: core::any::TypeId) -> Self {
let mut hasher = TypeIdHasher::default();
match core::mem::size_of::<core::any::TypeId>() {
8 => {
let mut hasher = TypeIdHasher::default();

type_id.hash(&mut hasher);
type_id.hash(&mut hasher);

TypeId(hasher.finish())
TypeId(hasher.finish() as u128)
}
16 => unsafe {
// This is technically unsound, core::any::TypeId has rust layout
// but there is no other way to get the full value anymore

let type_id_ptr: *const core::any::TypeId = &type_id;
let type_id_ptr = type_id_ptr as *const TypeId;
*type_id_ptr
},
_ => panic!("Compiler version not supported"),
}
}
}

impl From<&core::any::TypeId> for TypeId {
fn from(type_id: &core::any::TypeId) -> Self {
let mut hasher = TypeIdHasher::default();

type_id.hash(&mut hasher);

TypeId(hasher.finish())
type_id.into()
}
}

Expand Down

0 comments on commit 09f820f

Please sign in to comment.