intuicio_data/
type_hash.rs

1use rustc_hash::FxHasher;
2use std::hash::{Hash, Hasher};
3
4#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub struct TypeHash {
6    hash: u64,
7}
8
9impl Default for TypeHash {
10    fn default() -> Self {
11        Self::INVALID
12    }
13}
14
15impl TypeHash {
16    pub const INVALID: Self = Self { hash: 0 };
17
18    pub fn of<T: ?Sized>() -> Self {
19        unsafe { Self::raw(std::any::type_name::<T>()) }
20    }
21
22    /// # Safety
23    pub unsafe fn raw(name: &str) -> Self {
24        let mut hasher = FxHasher::default();
25        name.hash(&mut hasher);
26        Self {
27            hash: hasher.finish(),
28        }
29    }
30
31    pub fn hash(&self) -> u64 {
32        self.hash
33    }
34}
35
36impl std::fmt::Display for TypeHash {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        write!(f, "#{:X}", self.hash)
39    }
40}