#![allow(missing_docs)]
#![allow(unused_imports)]
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
use gloss_utils::abi_stable_aliases::StableAbi;
use identity_hash::IdentityHasher;
use std::any::TypeId;
#[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord)]
#[repr(C)]
#[cfg_attr(not(target_arch = "wasm32"), derive(StableAbi))]
pub struct StableTypeId {
t: u64,
}
impl PartialEq for StableTypeId {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.t == other.t
}
}
impl Hash for StableTypeId {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
(self.t).hash(state);
}
}
impl StableTypeId {
#[allow(clippy::similar_names)]
pub fn of<T: ?Sized + 'static>() -> Self {
#[cfg(not(feature = "unsafe_typeid"))]
{
let name = core::any::type_name::<T>();
let mut s = DefaultHasher::new();
name.hash(&mut s);
let hashed = s.finish();
Self { t: hashed }
}
#[cfg(feature = "unsafe_typeid")]
{
let ty = TypeId::of::<T>();
let mut hasher = IdentityHasher::<u64>::default();
ty.hash(&mut hasher);
let hashed = hasher.finish();
Self { t: hashed }
}
}
}