1 use core::convert::TryInto;
2 use core::hash::{BuildHasherDefault, Hasher};
3 use core::mem::size_of;
4 
5 /// A simple `Hasher` implementation tuned for performance.
6 #[derive(Default)]
7 pub struct FastHasher(u64);
8 
9 /// A `BuildHasher` for `FastHasher`.
10 pub type BuildFastHasher = BuildHasherDefault<FastHasher>;
11 
12 impl Hasher for FastHasher {
finish(&self) -> u6413     fn finish(&self) -> u64 {
14         self.0
15     }
16 
write(&mut self, bytes: &[u8])17     fn write(&mut self, bytes: &[u8]) {
18         let mut bytes = bytes;
19         while bytes.len() > size_of::<u64>() {
20             let (u64_bytes, remaining) = bytes.split_at(size_of::<u64>());
21             self.0 ^= u64::from_ne_bytes(u64_bytes.try_into().unwrap());
22             bytes = remaining
23         }
24         self.0 ^= bytes
25             .iter()
26             .fold(0u64, |result, b| (result << 8) | *b as u64);
27     }
28 }
29