pub struct MemoMap<K, V, S = RandomState> { /* private fields */ }
Expand description

An insert only, thread safe hash map to memoize values.

Implementations

Creates an empty MemoMap.

Creates an empty MemoMap which will use the given hash builder to hash keys.

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.

Returns true if the map contains a value for the specified key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

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.

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");

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);

Returns true if the memo map contains no items.

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.

An iterator visiting all keys in arbitrary order. The iterator element type is &'a K.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.