use crate::data::{key_bytes, RawKey};
use crate::Result;
pub(super) fn hash_bytes(hash: &mut u64, bytes: &[u8]) -> Result<()> {
crate::operation::work(bytes.len())?;
for byte in bytes {
*hash ^= u64::from(*byte);
*hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
Ok(())
}
fn hash_field(hash: &mut u64, bytes: &[u8]) -> Result<()> {
hash_bytes(
hash,
&u64::try_from(bytes.len())
.expect("byte length")
.to_le_bytes(),
)?;
hash_bytes(hash, bytes)
}
pub(super) fn hash_key(hash: &mut u64, key: &RawKey) -> Result<()> {
if let RawKey::String(value) = key {
return hash_field(hash, value.as_bytes());
}
hash_bytes(hash, &u64::MAX.to_le_bytes())?;
hash_field(hash, &key_bytes(key)?)
}