Trait grafix_toolbox::uses::hash::BuildHasher1.7.0[][src]

pub trait BuildHasher {
    type Hasher: Hasher;
    fn build_hasher(&self) -> Self::Hasher;
}
Expand description

A trait for creating instances of Hasher.

A BuildHasher is typically used (e.g., by HashMap) to create Hashers for each key such that they are hashed independently of one another, since Hashers contain state.

For each instance of BuildHasher, the Hashers 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

Type of the hasher that will be created.

Required methods

Creates a new hasher.

Each call to build_hasher on the same instance should produce identical Hashers.

Examples

use std::collections::hash_map::RandomState;
use std::hash::BuildHasher;

let s = RandomState::new();
let new_s = s.build_hasher();

Implementations on Foreign Types

Constructs a new [AHasher] with keys based on this [RandomState] object. This means that two different [RandomState]s will will generate [AHasher]s 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());

Implementors