pub struct ThreadLocal<T: Send> { /* private fields */ }
Expand description

Thread-local variable wrapper

See the module-level documentation for more.

Implementations§

source§

impl<T: Send> ThreadLocal<T>

source

pub fn new() -> ThreadLocal<T>

Creates a new empty ThreadLocal.

source

pub fn with_capacity(capacity: usize) -> ThreadLocal<T>

Creates a new ThreadLocal with an initial capacity. If less than the capacity threads access the thread local it will never reallocate. The capacity may be rounded up to the nearest power of two.

source

pub fn get(&self) -> Option<&T>

Returns the element for the current thread, if it exists.

source

pub fn get_or<F>(&self, create: F) -> &Twhere F: FnOnce() -> T,

Returns the element for the current thread, or creates it if it doesn’t exist.

source

pub fn get_or_try<F, E>(&self, create: F) -> Result<&T, E>where F: FnOnce() -> Result<T, E>,

Returns the element for the current thread, or creates it if it doesn’t exist. If create fails, that error is returned and no element is added.

source

pub fn iter(&self) -> Iter<'_, T> where T: Sync,

Returns an iterator over the local values of all threads in unspecified order.

This call can be done safely, as T is required to implement Sync.

source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns a mutable iterator over the local values of all threads in unspecified order.

Since this call borrows the ThreadLocal mutably, this operation can be done safely—the mutable borrow statically guarantees no other threads are currently accessing their associated values.

source

pub fn clear(&mut self)

Removes all thread-specific values from the ThreadLocal, effectively reseting it to its original state.

Since this call borrows the ThreadLocal mutably, this operation can be done safely—the mutable borrow statically guarantees no other threads are currently accessing their associated values.

source§

impl<T: Send + Default> ThreadLocal<T>

source

pub fn get_or_default(&self) -> &T

Returns the element for the current thread, or creates a default one if it doesn’t exist.

Trait Implementations§

source§

impl<T: Send + Debug> Debug for ThreadLocal<T>

source§

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

Formats the value using the given formatter. Read more
source§

impl<T: Send> Default for ThreadLocal<T>

source§

fn default() -> ThreadLocal<T>

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

impl<T: Send> Drop for ThreadLocal<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a, T: Send + Sync> IntoIterator for &'a ThreadLocal<T>

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, T: Send> IntoIterator for &'a mut ThreadLocal<T>

§

type Item = &'a mut T

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IterMut<'a, T>

Creates an iterator from a value. Read more
source§

impl<T: Send> IntoIterator for ThreadLocal<T>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IntoIter<T>

Creates an iterator from a value. Read more
source§

impl<T: Send> Sync for ThreadLocal<T>

source§

impl<T: Send + UnwindSafe> UnwindSafe for ThreadLocal<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for ThreadLocal<T>

§

impl<T> Send for ThreadLocal<T>

§

impl<T> Unpin for ThreadLocal<T>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.