protocol Hashable<Self, H> { ... }
H: Hasher<H>
Types that can be hashed.
The hash method can be implemented directly or a default implementation can be mixed in, in which case the memory representation of the type is used as an input to the hash function.
This is only recommended if the memory layout of the type does not contain padding, as the value of the padding bytes may be unpredictable and cause undefined behavior.
Examples
A default implementation
struct FancyI32 { a: i32 }
impl FancyI32 {
use std::hash::{Hashable, Hasher};
mixin<H: Hasher<H>> Hashable<FancyI32, H>;
}
use std::hash::hash_of_val;
let x = FancyI32 { a: 1 };
let y = FancyI32 { a: 1 };
let z = FancyI32 { a: 2 };
assert_eq!(hash_of_val(x), hash_of_val(y));
assert_ne!(hash_of_val(x), hash_of_val(z));
Run this example
A custom implementation
struct Foo { a: i32, b: &[u8], extra: u64 }
impl Foo {
use std::hash::Hasher;
fn hash<H: Hasher<H>>(self: &Foo, hasher: &mut H) {
self.a.hash(hasher);
self.b.hash(hasher);
// Skip the `extra`, if two instances of `Foo` are otherwise equal,
// but differ in the value of `extra`, they will hash to the same value.
}
}
use std::hash::hash_of_val;
let v1 = Foo { a: 1, b: "foo", extra: 0 };
let v2 = Foo { a: 1, b: "foo", extra: 1 };
let v3 = Foo { a: 1, b: "bar", extra: 0 };
assert_eq!(hash_of_val(v1), hash_of_val(v2));
assert_ne!(hash_of_val(v1), hash_of_val(v3));
Run this example
Provided methods
-
fn hash(a: &Self, hasher: &mut H)