pub struct OnceMap<K, V, S = RandomState> { /* private fields */ }Expand description
Run tasks only once and store the results in a parallel hash map.
We often have jobs Fn(K) -> V that we only want to run once and memoize, e.g. network
requests for metadata. When multiple tasks start the same query in parallel, e.g. through source
dist builds, we want to wait until the other task is done and get a reference to the same
result.
Note that this always clones the value out of the underlying map. Because
of this, it’s common to wrap the V in an Arc<V> to make cloning cheap.
Implementations§
Source§impl<K: Eq + Hash, V: Clone, H: BuildHasher + Clone> OnceMap<K, V, H>
impl<K: Eq + Hash, V: Clone, H: BuildHasher + Clone> OnceMap<K, V, H>
Sourcepub fn with_hasher(hasher: H) -> Self
pub fn with_hasher(hasher: H) -> Self
Create a OnceMap with the specified hasher.
Sourcepub fn with_capacity_and_hasher(capacity: usize, hasher: H) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hasher: H) -> Self
Create a OnceMap with the specified capacity and hasher.
Sourcepub fn register(&self, key: K) -> bool
pub fn register(&self, key: K) -> bool
Register that you want to start a job.
If this method returns true, you need to start a job and call OnceMap::done eventually
or other tasks will hang. If it returns false, this job is already in progress and you
can OnceMap::wait for the result.
Sourcepub async fn wait(&self, key: &K) -> Option<V>
pub async fn wait(&self, key: &K) -> Option<V>
Wait for the result of a job that is running.
Will hang if OnceMap::done isn’t called for this key.
Sourcepub fn wait_blocking(&self, key: &K) -> Option<V>
pub fn wait_blocking(&self, key: &K) -> Option<V>
Wait for the result of a job that is running, in a blocking context.
Will hang if OnceMap::done isn’t called for this key.