Struct ThreadLocal

Source
pub struct ThreadLocal<T: 'static> { /* private fields */ }
Expand description

A thread local variable wrapper.

Implementations§

Source§

impl<T: 'static> ThreadLocal<T>

Source

pub fn new() -> ThreadLocal<T>

Creates a new ThreadLocal with no values for any threads.

§Panics

Panics if more than usize::max_value() ThreadLocal objects have already been created. This can only ever realistically happen on 32 bit platforms.

Source

pub fn set(&self, value: T) -> Option<T>

Sets this thread’s value, returning the previous value if present.

§Panics

Panics if called from within the execution of a closure provided to another method on this value.

Source

pub fn remove(&self) -> Option<T>

Removes this thread’s value, returning it if it existed.

§Panics

Panics if called from within the execution of a closure provided to another method on this value.

Source

pub fn entry<F, R>(&self, f: F) -> R
where F: FnOnce(Entry<'_, T>) -> R,

Passes a handle to the current thread’s value to a closure for in-place manipulation.

The closure is required for the same soundness reasons it is required for the standard library’s thread_local! values.

§Panics

Panics if called from within the execution of a closure provided to another method on this value.

Source

pub fn get<F, R>(&self, f: F) -> R
where F: FnOnce(Option<&T>) -> R,

Passes a mutable reference to the current thread’s value to a closure.

The closure is required for the same soundness reasons it is required for the standard library’s thread_local! values.

§Panics

Panics if called from within the execution of a closure passed to entry or get_mut on this value.

Source

pub fn get_mut<F, R>(&self, f: F) -> R
where F: FnOnce(Option<&mut T>) -> R,

Passes a mutable reference to the current thread’s value to a closure.

The closure is required for the same soundness reasons it is required for the standard library’s thread_local! values.

§Panics

Panics if called from within the execution of a closure provided to another method on this value.

Source§

impl<T> ThreadLocal<T>
where T: 'static + Clone,

Source

pub fn get_cloned(&self) -> Option<T>

Returns a copy of the current thread’s value.

§Panics

Panics if called from within the execution of a closure passed to entry or get_mut on this value.

Auto Trait Implementations§

§

impl<T> Freeze for ThreadLocal<T>

§

impl<T> RefUnwindSafe for ThreadLocal<T>
where T: RefUnwindSafe,

§

impl<T> Send for ThreadLocal<T>
where T: Send,

§

impl<T> Sync for ThreadLocal<T>
where T: Sync,

§

impl<T> Unpin for ThreadLocal<T>
where T: Unpin,

§

impl<T> UnwindSafe for ThreadLocal<T>
where T: UnwindSafe,

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.
Source§

impl<T> UnsafeAny for T
where T: Any,