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 turbo_prehash::{BuildHasherExt, PreHashed};
use std::collections::HashMap;
use std::hash::{BuildHasherDefault, RandomState, Hash};

/// 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: HashMap<PreHashed<String>, String> = Default::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§

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

Traits§