1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::{
    collections::hash_map::DefaultHasher,
    hash::{Hash, Hasher},
};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct TypeHash {
    hash: u64,
}

impl TypeHash {
    pub fn of<T: 'static>() -> Self {
        unsafe { Self::raw(std::any::type_name::<T>()) }
    }

    /// # Safety
    pub unsafe fn raw(name: &str) -> Self {
        let mut hasher = DefaultHasher::default();
        name.hash(&mut hasher);
        Self {
            hash: hasher.finish(),
        }
    }

    pub fn hash(&self) -> u64 {
        self.hash
    }
}