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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::hash::Hasher;
#[derive(Default)]
pub struct ComponentTypeIdHasher(u64);
impl Hasher for ComponentTypeIdHasher {
#[inline]
fn finish(&self) -> u64 {
self.0
}
#[inline]
fn write_u64(&mut self, seed: u64) {
debug_assert_eq!(self.0, 0);
self.0 = seed;
}
fn write(&mut self, _bytes: &[u8]) {
unimplemented!()
}
}
#[derive(Default)]
pub struct U64Hasher(u64);
impl Hasher for U64Hasher {
#[inline]
fn finish(&self) -> u64 {
self.0
}
#[inline]
fn write_u64(&mut self, seed: u64) {
debug_assert_eq!(self.0, 0);
let max_prime = 11_400_714_819_323_198_549u64;
self.0 = max_prime.wrapping_mul(seed);
}
fn write(&mut self, _bytes: &[u8]) {
unimplemented!()
}
}
#[test]
fn hasher() {
fn verify<T: 'static + ?Sized>() {
use core::{any::TypeId, hash::Hash};
let mut hasher = ComponentTypeIdHasher::default();
let type_id = TypeId::of::<T>();
type_id.hash(&mut hasher);
assert_eq!(hasher.finish(), unsafe {
core::mem::transmute::<TypeId, u64>(type_id)
});
}
verify::<usize>();
verify::<()>();
verify::<str>();
verify::<&'static str>();
verify::<[u8; 20]>();
}