Hashing

Writing a custom hasher in Rust

Implementing FNV Hash,
1
struct FnvHasher(u64);
2
​
3
impl Default for FnvHasher {
4
fn default() -> Self {
5
FnvHasher(0x811c9dc5)
6
}
7
}
8
​
9
impl Hasher for FnvHasher {
10
fn finish(&self) -> u64 {
11
self.0
12
}
13
​
14
fn write(&mut self, bytes: &[u8]) {
15
let FnvHasher(mut hash) = *self;
16
​
17
for c in bytes.iter() {
18
hash = hash.wrapping_mul(0x01000193);
19
hash ^= *c as u64;
20
}
21
}
22
}
Copied!
Last modified 1yr ago
Copy link