From 09f820f638420458eaf7a4997b6a2c5da45ff647 Mon Sep 17 00:00:00 2001 From: leudz Date: Wed, 21 Jun 2023 22:16:51 +0200 Subject: [PATCH] Change TypeId size to 128 bits https://github.com/rust-lang/rust/pull/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 --- src/scheduler/into_workload.rs | 6 +++--- src/type_id/hasher.rs | 1 + src/type_id/mod.rs | 27 ++++++++++++++++++--------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/scheduler/into_workload.rs b/src/scheduler/into_workload.rs index a7e0d6d4..030f992f 100644 --- a/src/scheduler/into_workload.rs +++ b/src/scheduler/into_workload.rs @@ -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(), }); @@ -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(), }); @@ -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(), }); diff --git a/src/type_id/hasher.rs b/src/type_id/hasher.rs index 441d7a32..72f20aba 100644 --- a/src/type_id/hasher.rs +++ b/src/type_id/hasher.rs @@ -15,6 +15,7 @@ impl Hasher for TypeIdHasher { } } +#[cfg(feature = "std")] #[test] fn hasher() { fn verify() { diff --git a/src/type_id/mod.rs b/src/type_id/mod.rs index 8e3713ea..eb84ccdf 100644 --- a/src/type_id/mod.rs +++ b/src/type_id/mod.rs @@ -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() -> Self { @@ -22,21 +22,30 @@ impl TypeId { impl From for TypeId { fn from(type_id: core::any::TypeId) -> Self { - let mut hasher = TypeIdHasher::default(); + match core::mem::size_of::() { + 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() } }