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>
impl<T: Send> ThreadLocal<T>
Sourcepub const fn new() -> ThreadLocal<T>
pub const fn new() -> ThreadLocal<T>
Creates a new empty ThreadLocal
.
Sourcepub fn with_capacity(capacity: usize) -> ThreadLocal<T>
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.
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(&self) -> Iter<'_, T> ⓘwhere
T: Sync,
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
.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
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.
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> ThreadLocal<T>
impl<T: Send + Default> ThreadLocal<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§
Source§impl<T: Send> Default for ThreadLocal<T>
impl<T: Send> Default for ThreadLocal<T>
Source§fn default() -> ThreadLocal<T>
fn default() -> ThreadLocal<T>
Source§impl<T: Send> Drop for ThreadLocal<T>
impl<T: Send> Drop for ThreadLocal<T>
Source§impl<'a, T: Send + Sync> IntoIterator for &'a ThreadLocal<T>
impl<'a, T: Send + Sync> IntoIterator for &'a ThreadLocal<T>
Source§impl<'a, T: Send> IntoIterator for &'a mut ThreadLocal<T>
impl<'a, T: Send> IntoIterator for &'a mut ThreadLocal<T>
Source§impl<T: Send> IntoIterator for ThreadLocal<T>
impl<T: Send> IntoIterator for ThreadLocal<T>
impl<T: Send> Sync for ThreadLocal<T>
impl<T: Send + UnwindSafe> UnwindSafe for ThreadLocal<T>
Auto Trait Implementations§
impl<T> !Freeze for ThreadLocal<T>
impl<T> RefUnwindSafe for ThreadLocal<T>
impl<T> Send for ThreadLocal<T>
impl<T> Unpin for ThreadLocal<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 512 bytes