hrw_hash/
hasher.rs

1use {rapidhash::v3::rapidhash_v3, std::hash::Hasher};
2
3/// Default hasher used in the library.
4///
5/// Relies on the `rapidhash`, which is a portable and fast hashing.
6/// Additionally, it is designed to stay stable across different platforms, Rust
7/// versions, and package releases --- thus, it is safe to assume that the
8/// key-hash pairs will not change over time, and the same key will always
9/// hash to the same value.
10#[derive(Default)]
11pub struct DefaultHasher(Vec<u8>);
12
13impl Hasher for DefaultHasher {
14    fn write(&mut self, bytes: &[u8]) {
15        self.0.extend_from_slice(bytes);
16    }
17
18    fn finish(&self) -> u64 {
19        rapidhash_v3(&self.0)
20    }
21}
22
23#[inline]
24pub const fn merge(a: u64, b: u64) -> u64 {
25    let mut distance = a ^ b;
26    distance ^= distance >> 33;
27    distance = distance.wrapping_mul(0xff51_afd7_ed55_8ccd);
28    distance ^= distance >> 33;
29    distance = distance.wrapping_mul(0xc4ce_b9fe_1a85_ec53);
30    distance ^= distance >> 33;
31    distance
32}