Struct thread_local::CachedThreadLocal
source · pub struct CachedThreadLocal<T: Send> { /* private fields */ }
ThreadLocal
insteadExpand description
Wrapper around ThreadLocal
.
This used to add a fast path for a single thread, however that has been
obsoleted by performance improvements to ThreadLocal
itself.
Implementations§
source§impl<T: Send> CachedThreadLocal<T>
impl<T: 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) -> &Twhere
F: FnOnce() -> T,
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.
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_or_default(&self) -> &T
pub fn get_or_default(&self) -> &T
Returns the element for the current thread, or creates a default one if it doesn’t exist.