pub struct CacheMap<K, V, S = RandomState> { /* private fields */ }
Expand description
An insert-only map for caching the result of functions
Implementations§
source§impl<K: Eq + Hash, V, S: BuildHasher> CacheMap<K, V, S>
impl<K: Eq + Hash, V, S: BuildHasher> CacheMap<K, V, S>
sourcepub fn cache<F: FnOnce() -> V>(&self, key: K, f: F) -> &V
pub fn cache<F: FnOnce() -> V>(&self, key: K, f: F) -> &V
Fetch the value associated with the key, or run the provided function to insert one.
Example
use cachemap2::CacheMap;
let m = CacheMap::new();
let fst = m.cache("key", || 5u32);
let snd = m.cache("key", || 7u32);
assert_eq!(*fst, *snd);
assert_eq!(*fst, 5u32);
sourcepub fn cache_default(&self, key: K) -> &Vwhere
V: Default,
pub fn cache_default(&self, key: K) -> &Vwhere
V: Default,
Fetch the value associated with the key, or insert a default value.
sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Return whether the map contains the given key.
source§impl<K: Eq + Hash, V, S: BuildHasher + Default> CacheMap<K, V, S>
impl<K: Eq + Hash, V, S: BuildHasher + Default> CacheMap<K, V, S>
sourcepub fn with_hasher(hash_builder: S) -> Self
pub fn with_hasher(hash_builder: S) -> Self
Creates a new CacheMap with the provided hasher
Trait Implementations§
source§impl<K: Eq + Hash, V, S: BuildHasher + Default> FromIterator<(K, V)> for CacheMap<K, V, S>
impl<K: Eq + Hash, V, S: BuildHasher + Default> FromIterator<(K, V)> for CacheMap<K, V, S>
source§impl<'a, K, V, S> IntoIterator for &'a CacheMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a CacheMap<K, V, S>
Auto Trait Implementations§
impl<K, V, S> RefUnwindSafe for CacheMap<K, V, S>
impl<K, V, S> Send for CacheMap<K, V, S>
impl<K, V, S> Sync for CacheMap<K, V, S>
impl<K, V, S> Unpin for CacheMap<K, V, S>
impl<K, V, S> UnwindSafe for CacheMap<K, V, S>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more