pub struct StableHasher {
state: u64,
}
const OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
const PRIME: u64 = 0x0000_0100_0000_01b3;
impl StableHasher {
#[must_use]
pub const fn new() -> Self {
Self {
state: OFFSET_BASIS,
}
}
pub const fn write_bytes(&mut self, bytes: &[u8]) {
let mut index = 0;
while index < bytes.len() {
self.state ^= bytes[index] as u64;
self.state = self.state.wrapping_mul(PRIME);
index += 1;
}
}
pub const fn write_u8(&mut self, value: u8) {
self.write_bytes(&[value]);
}
pub const fn write_u64(&mut self, value: u64) {
self.write_bytes(&value.to_be_bytes());
}
pub const fn write_str(&mut self, value: &str) {
self.write_u64(value.len() as u64);
self.write_bytes(value.as_bytes());
}
#[must_use]
pub const fn finish(&self) -> u64 {
self.state
}
}
impl Default for StableHasher {
fn default() -> Self {
Self::new()
}
}