OnceMap

Struct OnceMap 

Source
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>

Source

pub fn with_hasher(hasher: H) -> Self

Create a OnceMap with the specified hasher.

Source

pub fn with_capacity_and_hasher(capacity: usize, hasher: H) -> Self

Create a OnceMap with the specified capacity and hasher.

Source

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.

Source

pub fn done(&self, key: K, value: V)

Submit the result of a job you registered.

Source

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.

Source

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.

Source

pub fn get<Q: ?Sized + Hash + Eq>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>,

Return the result of a previous job, if any.

Source

pub fn remove<Q: ?Sized + Hash + Eq>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>,

Remove the result of a previous job, if any.

Trait Implementations§

Source§

impl<K: Eq + Hash + Clone, V, H: Default + BuildHasher + Clone> Default for OnceMap<K, V, H>

Source§

fn default() -> Self

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

impl<K, V, H> FromIterator<(K, V)> for OnceMap<K, V, H>
where K: Eq + Hash, H: Default + Clone + BuildHasher,

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<K, V, S> Freeze for OnceMap<K, V, S>
where S: Freeze,

§

impl<K, V, S = RandomState> !RefUnwindSafe for OnceMap<K, V, S>

§

impl<K, V, S> Send for OnceMap<K, V, S>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Sync for OnceMap<K, V, S>
where S: Sync, K: Send + Sync, V: Send + Sync,

§

impl<K, V, S> Unpin for OnceMap<K, V, S>
where S: Unpin,

§

impl<K, V, S> UnwindSafe for OnceMap<K, V, S>
where S: UnwindSafe, K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.