use std::hash::{Hash, Hasher};
use rustc_hash::FxHasher;
#[must_use]
pub fn hash_string(s: &str) -> u64 {
let mut hasher = FxHasher::default();
s.as_bytes().hash(&mut hasher);
hasher.finish()
}
#[must_use]
pub fn hash_blob(data: &[u8]) -> u64 {
let mut hasher = FxHasher::default();
data.hash(&mut hasher);
hasher.finish()
}
#[must_use]
pub fn hash_guid(guid: &[u8; 16]) -> u64 {
let mut hasher = FxHasher::default();
guid.hash(&mut hasher);
hasher.finish()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hash_string_deterministic() {
let hash1 = hash_string("hello");
let hash2 = hash_string("hello");
let hash3 = hash_string("world");
assert_eq!(hash1, hash2);
assert_ne!(hash1, hash3);
}
#[test]
fn test_hash_string_empty() {
let hash = hash_string("");
assert_eq!(hash, hash_string(""));
}
#[test]
fn test_hash_blob_deterministic() {
let blob1 = hash_blob(&[1, 2, 3]);
let blob2 = hash_blob(&[1, 2, 3]);
let blob3 = hash_blob(&[4, 5, 6]);
assert_eq!(blob1, blob2);
assert_ne!(blob1, blob3);
}
#[test]
fn test_hash_blob_empty() {
let hash = hash_blob(&[]);
assert_eq!(hash, hash_blob(&[]));
}
#[test]
fn test_hash_guid_deterministic() {
let guid1 = [1u8; 16];
let guid2 = [1u8; 16];
let guid3 = [2u8; 16];
assert_eq!(hash_guid(&guid1), hash_guid(&guid2));
assert_ne!(hash_guid(&guid1), hash_guid(&guid3));
}
#[test]
fn test_hash_guid_zero() {
let zero_guid = [0u8; 16];
assert_eq!(hash_guid(&zero_guid), hash_guid(&zero_guid));
}
}