Hashing algorithms.
Hasher is a visitor that is accepted by a hashable type through the hash method. Each hasher is a disposable object that is used to hash a single value.
Example
use std::hash::hash_of;
// A bad hash, do not use
struct CountingHash {
length: usize
}
impl CountingHash {
fn new() -> CountingHash { CountingHash { length: 0 } }
fn write(self: &mut CountingHash, val: &[u8]) { self.length += val.len() }
fn finish(self: &mut CountingHash) -> u64 { self.length as u64 }
}
assert_eq!(hash_of::<&[u8], CountingHash>(&""), 0);
assert_eq!(hash_of::<&[u8], CountingHash>(&"a"), 1);
assert_eq!(hash_of::<&[u8], CountingHash>(&"ab"), 2);
Run this example
Required methods
-
fn new() -> Self
Create a new hasher.
-
Write a byte slice into a hasher.
-
Finalize the hashing and return the hash value.