Trait wasmer_types::lib::std::hash::BuildHasher
1.7.0 · source · [−]pub trait BuildHasher {
type Hasher: Hasher;
fn build_hasher(&self) -> Self::Hasher;
fn hash_one<T>(&self, x: T) -> u64
where
T: Hash,
{ ... }
}
Expand description
A trait for creating instances of Hasher
.
A BuildHasher
is typically used (e.g., by HashMap
) to create
Hasher
s for each key such that they are hashed independently of one
another, since Hasher
s contain state.
For each instance of BuildHasher
, the Hasher
s created by
build_hasher
should be identical. That is, if the same stream of bytes
is fed into each hasher, the same output will also be generated.
Examples
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hasher};
let s = RandomState::new();
let mut hasher_1 = s.build_hasher();
let mut hasher_2 = s.build_hasher();
hasher_1.write_u32(8128);
hasher_2.write_u32(8128);
assert_eq!(hasher_1.finish(), hasher_2.finish());
Associated Types
Required methods
fn build_hasher(&self) -> Self::Hasher
fn build_hasher(&self) -> Self::Hasher
Provided methods
build_hasher_simple_hash_one
)Calculates the hash of a single value.
This is intended as a convenience for code which consumes hashes, such
as the implementation of a hash table or in unit tests that check
whether a custom Hash
implementation behaves as expected.
This must not be used in any code which creates hashes, such as in an
implementation of Hash
. The way to create a combined hash of
multiple values is to call Hash::hash
multiple times using the same
Hasher
, not to call this method repeatedly and combine the results.
Example
#![feature(build_hasher_simple_hash_one)]
use std::cmp::{max, min};
use std::hash::{BuildHasher, Hash, Hasher};
struct OrderAmbivalentPair<T: Ord>(T, T);
impl<T: Ord + Hash> Hash for OrderAmbivalentPair<T> {
fn hash<H: Hasher>(&self, hasher: &mut H) {
min(&self.0, &self.1).hash(hasher);
max(&self.0, &self.1).hash(hasher);
}
}
// Then later, in a `#[test]` for the type...
let bh = std::collections::hash_map::RandomState::new();
assert_eq!(
bh.hash_one(OrderAmbivalentPair(1, 2)),
bh.hash_one(OrderAmbivalentPair(2, 1))
);
assert_eq!(
bh.hash_one(OrderAmbivalentPair(10, 2)),
bh.hash_one(&OrderAmbivalentPair(2, 10))
);
Implementations on Foreign Types
sourceimpl BuildHasher for RandomState
impl BuildHasher for RandomState
type Hasher = DefaultHasher
pub fn build_hasher(&self) -> DefaultHasher
sourceimpl BuildHasher for RandomState
impl BuildHasher for RandomState
sourcepub fn build_hasher(&self) -> AHasher
pub fn build_hasher(&self) -> AHasher
Constructs a new AHasher with keys based on this RandomState object. This means that two different RandomStates will will generate AHashers that will return different hashcodes, but Hashers created from the same BuildHasher will generate the same hashes for the same input data.
Examples
use ahash::{AHasher, RandomState};
use std::hash::{Hasher, BuildHasher};
let build_hasher = RandomState::new();
let mut hasher_1 = build_hasher.build_hasher();
let mut hasher_2 = build_hasher.build_hasher();
hasher_1.write_u32(1234);
hasher_2.write_u32(1234);
assert_eq!(hasher_1.finish(), hasher_2.finish());
let other_build_hasher = RandomState::new();
let mut different_hasher = other_build_hasher.build_hasher();
different_hasher.write_u32(1234);
assert_ne!(different_hasher.finish(), hasher_1.finish());