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::convert::TryInto;
use std::hash::{BuildHasherDefault, Hasher};
use std::mem::size_of;
#[derive(Default)]
pub struct FastHasher(u64);
pub type BuildFastHasher = BuildHasherDefault<FastHasher>;
impl Hasher for FastHasher {
fn finish(&self) -> u64 {
self.0
}
fn write(&mut self, bytes: &[u8]) {
let mut bytes = bytes;
while bytes.len() > size_of::<u64>() {
let (u64_bytes, remaining) = bytes.split_at(size_of::<u64>());
self.0 ^= u64::from_ne_bytes(u64_bytes.try_into().unwrap());
bytes = remaining
}
self.0 ^= bytes
.iter()
.fold(0u64, |result, b| (result << 8) | *b as u64);
}
}