Struct thread_local::CachedThreadLocal
source · [−]pub struct CachedThreadLocal<T: 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
sourceimpl<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) -> &T where
F: FnOnce() -> T,
pub fn get_or<F>(&self, create: F) -> &T where
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> where
F: FnOnce() -> Result<T, E>,
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.
sourcepub fn iter_mut(&mut self) -> CachedIterMut<'_, T>ⓘNotable traits for CachedIterMut<'a, T>impl<'a, T: Send + 'a> Iterator for CachedIterMut<'a, T> type Item = &'a mut T;
pub fn iter_mut(&mut self) -> CachedIterMut<'_, T>ⓘNotable traits for CachedIterMut<'a, T>impl<'a, T: Send + 'a> Iterator for CachedIterMut<'a, T> type Item = &'a mut 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.
sourceimpl<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.
Trait Implementations
sourceimpl<T: Send + Debug> Debug for CachedThreadLocal<T>
impl<T: Send + Debug> Debug for CachedThreadLocal<T>
sourceimpl<T: Send> Default for CachedThreadLocal<T>
impl<T: Send> Default for CachedThreadLocal<T>
sourcefn default() -> CachedThreadLocal<T>
fn default() -> CachedThreadLocal<T>
Returns the “default value” for a type. Read more
sourceimpl<T: Send> IntoIterator for CachedThreadLocal<T>
impl<T: Send> IntoIterator for CachedThreadLocal<T>
type Item = T
type Item = T
The type of the elements being iterated over.
type IntoIter = CachedIntoIter<T>
type IntoIter = CachedIntoIter<T>
Which kind of iterator are we turning this into?
sourcefn into_iter(self) -> CachedIntoIter<T>ⓘNotable traits for CachedIntoIter<T>impl<T: Send> Iterator for CachedIntoIter<T> type Item = T;
fn into_iter(self) -> CachedIntoIter<T>ⓘNotable traits for CachedIntoIter<T>impl<T: Send> Iterator for CachedIntoIter<T> type Item = T;
Creates an iterator from a value. Read more
sourceimpl<'a, T: Send + 'a> IntoIterator for &'a mut CachedThreadLocal<T>
impl<'a, T: Send + 'a> IntoIterator for &'a mut CachedThreadLocal<T>
type IntoIter = CachedIterMut<'a, T>
type IntoIter = CachedIterMut<'a, T>
Which kind of iterator are we turning this into?
sourcefn into_iter(self) -> CachedIterMut<'a, T>ⓘNotable traits for CachedIterMut<'a, T>impl<'a, T: Send + 'a> Iterator for CachedIterMut<'a, T> type Item = &'a mut T;
fn into_iter(self) -> CachedIterMut<'a, T>ⓘNotable traits for CachedIterMut<'a, T>impl<'a, T: Send + 'a> Iterator for CachedIterMut<'a, T> type Item = &'a mut T;
Creates an iterator from a value. Read more
impl<T: Send> Sync for CachedThreadLocal<T>
impl<T: Send + UnwindSafe> UnwindSafe for CachedThreadLocal<T>
Auto Trait Implementations
impl<T> !RefUnwindSafe for CachedThreadLocal<T>
impl<T> Send for CachedThreadLocal<T>
impl<T> Unpin for CachedThreadLocal<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more