Expand description

A concurrent insert only hash map.

This crate implements a “memo map” which is in many ways similar to a HashMap with some crucial differences:

  • Unlike a regular hash map, a memo map is thread safe and synchronized.
  • Once a value has been placed in the memo map it can be neither removed nor replaced.
  • Retrieving a value from a memo map returns a plain old reference.

Together these purposes allow one to use this type of structure to implement something similar to lazy loading in places where the API has been constrained to references before.

For this to work the value placed in the MemoMap has to implement StableDeref. If the value you want to place there does not implement it you can generally wrap it in a Box.

use memo_map::MemoMap;

let memo = MemoMap::new();
let one = memo.get_or_insert(&1, || "one".to_string());
let one2 = memo.get_or_insert(&1, || "not one".to_string());
assert_eq!(one, "one");
assert_eq!(one2, "one");

Notes on Iteration

Because the memo map internally uses a mutex it needs to be held during iteration. This is potentially dangerous as it means you can easily deadlock yourself when trying to use the memo map while iterating. The iteration functionality thus has to be used with great care.

Structs

An iterator over the items of a MemoMap.

An iterator over the keys of a MemoMap.

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