thread_map

Struct ThreadMap

Source
pub struct ThreadMap<V> { /* private fields */ }
Expand description

This type encapsulates the association of ThreadIds to values of type V. It is a simple and easy-to-use alternative to the std::thread_local macro and the thread_local crate.

Implementations§

Source§

impl<V> ThreadMap<V>

Source

pub fn new(value_init: fn() -> V) -> Self

Creates a new ThreadMap instance, with value_init used to create the initial value for each thread.

Source

pub fn with_mut<W>(&self, f: impl FnOnce(&mut V) -> W) -> W

Invokes f mutably on the value associated with the ThreadId of the current thread and returns the invocation result. If there is no value associated with the current thread then the value_init argument of Self::new is used to instantiate an initial associated value before f is applied.

Source

pub fn with<W>(&self, f: impl FnOnce(&V) -> W) -> W

Invokes f on the value associated with the ThreadId of the current thread and returns the invocation result. If there is no value associated with the current thread then the value_init argument of Self::new is used to instantiate an initial associated value before f is applied.

Source

pub fn drain(&self) -> Result<HashMap<ThreadId, V>, ThreadMapLockError>

Returns a HashMap with the values associated with each ThreadId key and clears self’s state.

§Errors
Source

pub fn fold<W>( &self, z: W, f: impl FnMut(W, (ThreadId, &V)) -> W, ) -> Result<W, ThreadMapLockError>

Folds every association in self into an accumulator (with initial value z) by applying an operation f, returning the final result.

§Errors
Source

pub fn fold_values<W>( &self, z: W, f: impl FnMut(W, &V) -> W, ) -> Result<W, ThreadMapLockError>

Folds every value in self into an accumulator (with initial value z) by applying an operation f, returning the final result.

§Errors
Source

pub fn probe(&self) -> Result<HashMap<ThreadId, V>, ThreadMapLockError>
where V: Clone,

Returns a HashMap with clones of the values associated with each ThreadId key at the time the probe was executed.

§Errors

Trait Implementations§

Source§

impl<V: Debug> Debug for ThreadMap<V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<V> !Freeze for ThreadMap<V>

§

impl<V> RefUnwindSafe for ThreadMap<V>

§

impl<V> Send for ThreadMap<V>
where V: Send,

§

impl<V> Sync for ThreadMap<V>
where V: Send,

§

impl<V> Unpin for ThreadMap<V>
where V: Unpin,

§

impl<V> UnwindSafe for ThreadMap<V>

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.