pub struct MemoMap<K, V, S = RandomState> { /* private fields */ }Expand description
An insert only, thread safe hash map to memoize values.
Implementations
sourceimpl<K, V> MemoMap<K, V, RandomState>
impl<K, V> MemoMap<K, V, RandomState>
sourcepub fn new() -> MemoMap<K, V, RandomState>
pub fn new() -> MemoMap<K, V, RandomState>
Creates an empty MemoMap.
sourceimpl<K, V, S> MemoMap<K, V, S>
impl<K, V, S> MemoMap<K, V, S>
sourcepub fn with_hasher(hash_builder: S) -> MemoMap<K, V, S>
pub fn with_hasher(hash_builder: S) -> MemoMap<K, V, S>
Creates an empty MemoMap which will use the given hash builder to hash
keys.
sourceimpl<K, V, S> MemoMap<K, V, S> where
K: Eq + Hash,
V: StableDeref,
S: BuildHasher,
impl<K, V, S> MemoMap<K, V, S> where
K: Eq + Hash,
V: StableDeref,
S: BuildHasher,
sourcepub fn insert(&self, key: K, value: V) -> bool
pub fn insert(&self, key: K, value: V) -> bool
Inserts a value into the memo map.
This inserts a value for a specific key into the memo map. If the
key already exists, this method does nothing and instead returns false.
Otherwise the value is inserted and true is returned. It’s generally
recommended to instead use get_or_insert or
it’s sibling get_or_try_insert.
sourcepub fn get_or_try_insert<Q, F, E>(&self, key: &Q, creator: F) -> Result<&V, E> where
Q: Hash + Eq + ToOwned<Owned = K> + ?Sized,
K: Borrow<Q>,
F: FnOnce() -> Result<V, E>,
pub fn get_or_try_insert<Q, F, E>(&self, key: &Q, creator: F) -> Result<&V, E> where
Q: Hash + Eq + ToOwned<Owned = K> + ?Sized,
K: Borrow<Q>,
F: FnOnce() -> Result<V, E>,
Returns a reference to the value corresponding to the key or inserts.
This is the preferred way to work with a memo map: if the value has not been in the map yet the creator function is invoked to create the value, otherwise the already stored value is returned. The creator function itself can be falliable and the error is passed through.
If the creator is infallible, get_or_insert can be used.
sourcepub fn get_or_insert<Q, F>(&self, key: &Q, creator: F) -> &V where
Q: Hash + Eq + ToOwned<Owned = K> + ?Sized,
K: Borrow<Q>,
F: FnOnce() -> V,
pub fn get_or_insert<Q, F>(&self, key: &Q, creator: F) -> &V where
Q: Hash + Eq + ToOwned<Owned = K> + ?Sized,
K: Borrow<Q>,
F: FnOnce() -> V,
Returns a reference to the value corresponding to the key or inserts.
This is the preferred way to work with a memo map: if the value has not been in the map yet the creator function is invoked to create the value, otherwise the already stored value is returned.
If the creator is fallible, get_or_try_insert can be used.
Example
let memo = MemoMap::new();
// first time inserts
let value = memo.get_or_insert("key", || "23");
assert_eq!(*value, "23");
// second time returns old value
let value = memo.get_or_insert("key", || "24");
assert_eq!(*value, "23");sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of items in the map.
Example
let memo = MemoMap::new();
assert_eq!(memo.len(), 0);
memo.insert(1, "a");
memo.insert(2, "b");
memo.insert(2, "not b");
assert_eq!(memo.len(), 2);sourcepub fn iter(&self) -> Iter<'_, K, V, S>ⓘNotable traits for Iter<'a, K, V, S>impl<'a, K, V, S> Iterator for Iter<'a, K, V, S> type Item = (&'a K, &'a V);
pub fn iter(&self) -> Iter<'_, K, V, S>ⓘNotable traits for Iter<'a, K, V, S>impl<'a, K, V, S> Iterator for Iter<'a, K, V, S> type Item = (&'a K, &'a V);
An iterator visiting all key-value pairs in arbitrary order. The
iterator element type is (&'a K, &'a V).
Important note: during iteration the map is locked! This means that you must not perform modifications to the map or you will run into deadlocks.
Trait Implementations
Auto Trait Implementations
impl<K, V, S> RefUnwindSafe for MemoMap<K, V, S>
impl<K, V, S> Send for MemoMap<K, V, S> where
K: Send,
S: Send,
V: Send,
impl<K, V, S> Sync for MemoMap<K, V, S> where
K: Send,
S: Send,
V: Send,
impl<K, V, S> Unpin for MemoMap<K, V, S> where
K: Unpin,
S: Unpin,
V: Unpin,
impl<K, V, S> UnwindSafe for MemoMap<K, V, S>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more