pub struct ScopedKey<T> { /* 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> ScopedKey<T>
impl<T> ScopedKey<T>
Sourcepub fn set<F, R>(&'static self, t: &T, f: F) -> Rwhere
F: FnOnce() -> R,
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 f is running, the value t will be returned by get unless
this function is called recursively inside of f.
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);
});Sourcepub fn with<F, R>(&'static self, f: F) -> R
pub fn with<F, R>(&'static self, f: F) -> 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`
});Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for ScopedKey<T>
impl<T> RefUnwindSafe for ScopedKey<T>where
T: RefUnwindSafe,
impl<T> Send for ScopedKey<T>where
T: Send,
impl<T> Unpin for ScopedKey<T>where
T: Unpin,
impl<T> UnwindSafe for ScopedKey<T>where
T: UnwindSafe,
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: 8 bytes