pub struct ScopedKey<T: ?Sized + 'static> { /* private fields */ }
Expand description

Type representing a thread local storage key corresponding to a reference to the type parameter T.

Keys are statically allocated and can contain a reference to an instance of type T scoped to a particular lifetime. Keys provides two methods, set and with, both of which currently use closures to control the scope of their contents.

Implementations§

source§

impl<T: ?Sized + 'static> ScopedKey<T>

source

pub fn set<F, R>(&'static self, t: &T, f: F) -> Rwhere F: FnOnce() -> R,

Inserts a value into this scoped thread local storage slot for a duration of a closure.

While cb is running, the value t will be returned by get unless this function is called recursively inside of cb.

Upon return, this function will restore the previous value, if any was available.

Examples
use scoped_tls_hkt::scoped_thread_local;

scoped_thread_local!(static FOO: u32);

FOO.set(&100, || {
    let val = FOO.with(|v| *v);
    assert_eq!(val, 100);

    // set can be called recursively
    FOO.set(&101, || {
        // ...
    });

    // Recursive calls restore the previous value.
    let val = FOO.with(|v| *v);
    assert_eq!(val, 100);
});
source

pub fn with<F, R>(&'static self, f: F) -> Rwhere F: FnOnce(&T) -> R,

Gets a value out of this scoped variable.

This function takes a closure which receives the value of this variable.

Panics

This function will panic if set has not previously been called.

Examples
use scoped_tls_hkt::scoped_thread_local;

scoped_thread_local!(static FOO: u32);

FOO.with(|slot| {
    // work with `slot`
});
source

pub fn is_set(&'static self) -> bool

Test whether this TLS key has been set for the current thread.

Trait Implementations§

source§

impl<T: ?Sized + 'static> Sync for ScopedKey<T>

Auto Trait Implementations§

§

impl<T: ?Sized> RefUnwindSafe for ScopedKey<T>

§

impl<T: ?Sized> Send for ScopedKey<T>

§

impl<T: ?Sized> Unpin for ScopedKey<T>

§

impl<T: ?Sized> UnwindSafe for ScopedKey<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.