Crate turbo_prehash

Source
Expand description

turbo-prehash

A small wrapper around std::hash::Hasher that allows you to pre-hash a value before hashing it.

This is useful for when you want to hash a value that is expensive to compute (e.g. a large string) but you want to avoid re-hashing it every time.

§Example

use std::hash::{BuildHasherDefault, RandomState, Hash};

use rustc_hash::FxHashMap;
use turbo_prehash::{BuildHasherExt, PreHashed};

/// hash a key, returning a prehashed value
fn hash_key<T: Hash>(key: T) -> PreHashed<T> {
    RandomState::new().prehash(key)
}

// create hashmap to hold pre-hashed values
let mut map: FxHashMap<PreHashed<String>, String> = FxHashMap::default();

// insert a prehashed value
let hashed_key = hash_key("hello".to_string());
map.insert(hashed_key.clone(), "world".to_string());

// get the value
assert_eq!(map.get(&hashed_key), Some(&"world".to_string()));

Structs§

PassThroughHash
An implementer of Hash that simply returns the pre-computed hash.
PreHashed
A wrapper type that hashes some inner on creation, implementing Hash by simply returning the pre-computed hash.

Traits§

BuildHasherExt
An extension trait for BuildHasher that provides the BuildHasherExt::prehash method.