intuicio_data/
type_hash.rs1use 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 pub fn is_valid(&self) -> bool {
23 self.hash != Self::INVALID.hash
24 }
25
26 pub unsafe fn raw(name: &str) -> Self {
28 let mut hasher = FxHasher::default();
29 name.hash(&mut hasher);
30 Self {
31 hash: hasher.finish(),
32 }
33 }
34
35 pub fn hash(&self) -> u64 {
36 self.hash
37 }
38}
39
40impl std::fmt::Display for TypeHash {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 write!(f, "#{:X}", self.hash)
43 }
44}