Struct CachedThreadLocal

Source
pub struct CachedThreadLocal<T: ?Sized + Send> { /* private fields */ }
Expand description

Wrapper around ThreadLocal which adds a fast path for a single thread.

This has the same API as ThreadLocal, but will register the first thread that sets a value as its owner. All accesses by the owner will go through a special fast path which is much faster than the normal ThreadLocal path.

Implementations§

Source§

impl<T: ?Sized + Send> CachedThreadLocal<T>

Source

pub fn new() -> CachedThreadLocal<T>

Creates a new empty CachedThreadLocal.

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) -> &T
where F: FnOnce() -> Box<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<Box<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_mut(&mut self) -> CachedIterMut<'_, T>

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

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> CachedThreadLocal<T>

Source

pub fn get_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: ?Sized + Send + Debug> Debug for CachedThreadLocal<T>

Source§

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

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

impl<T: ?Sized + Send> Default for CachedThreadLocal<T>

Source§

fn default() -> CachedThreadLocal<T>

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

impl<'a, T: ?Sized + Send + 'a> IntoIterator for &'a mut CachedThreadLocal<T>

Source§

type Item = &'a mut Box<T>

The type of the elements being iterated over.
Source§

type IntoIter = Chain<IntoIter<&'a mut Box<T>>, IterMut<'a, T>>

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

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

Creates an iterator from a value. Read more
Source§

impl<T: ?Sized + Send> IntoIterator for CachedThreadLocal<T>

Source§

type Item = Box<T>

The type of the elements being iterated over.
Source§

type IntoIter = Chain<IntoIter<Box<T>>, IntoIter<T>>

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

fn into_iter(self) -> CachedIntoIter<T>

Creates an iterator from a value. Read more
Source§

impl<T: ?Sized + Send> Sync for CachedThreadLocal<T>

Source§

impl<T: ?Sized + Send + UnwindSafe> UnwindSafe for CachedThreadLocal<T>

Auto Trait Implementations§

§

impl<T> !Freeze for CachedThreadLocal<T>

§

impl<T> !RefUnwindSafe for CachedThreadLocal<T>

§

impl<T> Send for CachedThreadLocal<T>
where T: ?Sized,

§

impl<T> Unpin for CachedThreadLocal<T>
where T: Unpin + ?Sized,

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.