[−][src]Struct scoped_tls::ScopedKey
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.
Methods
impl<T> ScopedKey<T>
[src]
impl<T> ScopedKey<T>
pub fn set<F, R>(&'static self, t: &T, f: F) -> R where
F: FnOnce() -> R,
[src]
pub fn set<F, R>(&'static self, t: &T, f: F) -> R where
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
#[macro_use] extern crate scoped_tls; 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); });
pub fn with<F, R>(&'static self, f: F) -> R where
F: FnOnce(&T) -> R,
[src]
pub fn with<F, R>(&'static self, f: F) -> R where
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
#[macro_use] extern crate scoped_tls; scoped_thread_local!(static FOO: u32); FOO.with(|slot| { // work with `slot` });
pub fn is_set(&'static self) -> bool
[src]
pub fn is_set(&'static self) -> bool
Test whether this TLS key has been set
for the current thread.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
try_from
)Performs the conversion.
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more