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>
impl<T: ?Sized + Send> CachedThreadLocal<T>
Sourcepub fn new() -> CachedThreadLocal<T>
pub fn new() -> CachedThreadLocal<T>
Creates a new empty CachedThreadLocal
.
Sourcepub fn get_or<F>(&self, create: F) -> &T
pub fn get_or<F>(&self, create: F) -> &T
Returns the element for the current thread, or creates it if it doesn’t exist.
Sourcepub fn get_or_try<F, E>(&self, create: F) -> Result<&T, E>
pub fn get_or_try<F, E>(&self, create: F) -> 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.
Sourcepub fn iter_mut(&mut self) -> CachedIterMut<'_, T>
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.
Sourcepub fn clear(&mut self)
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>
impl<T: Send + Default> CachedThreadLocal<T>
Sourcepub fn get_default(&self) -> &T
pub fn get_default(&self) -> &T
Returns the element for the current thread, or creates a default one if it doesn’t exist.