pub struct LocalKey<T: 'static> { /* private fields */ }
Expand description
A TLS key that owns its data.
Can be created with the os_task_local
macro.
Implementations§
source§impl<T: 'static> LocalKey<T>
impl<T: 'static> LocalKey<T>
sourcepub const fn new(init: fn() -> T) -> Self
pub const fn new(init: fn() -> T) -> Self
Creates a new local key that lazily initializes its data. init is called to initialize the data when it is first accessed from a new thread.
sourcepub fn with<F, R>(&'static self, f: F) -> Rwhere
F: FnOnce(&'static T) -> R,
pub fn with<F, R>(&'static self, f: F) -> Rwhere
F: FnOnce(&'static T) -> R,
Passes a reference to the value of this key to the given closure. If the value has not been initialized yet, it will be initialized.
source§impl<T: 'static> LocalKey<Cell<T>>
impl<T: 'static> LocalKey<Cell<T>>
sourcepub fn set(&'static self, value: T)
pub fn set(&'static self, value: T)
Sets or initializes the value of this key.
If the value was already initialized, it is overwritten.
If the value was not initialized, it is initialized with value
.
source§impl<T: 'static> LocalKey<RefCell<T>>
impl<T: 'static> LocalKey<RefCell<T>>
sourcepub fn with_borrow<F, R>(&'static self, f: F) -> R
pub fn with_borrow<F, R>(&'static self, f: F) -> R
Acquires a reference to the contained value, initializing it if required.
§Panics
Panics if the value is currently mutably borrowed.
sourcepub fn with_borrow_mut<F, R>(&'static self, f: F) -> R
pub fn with_borrow_mut<F, R>(&'static self, f: F) -> R
Acquires a mutable reference to the contained value, initializing it if required.
§Panics
Panics if the value is currently borrowed.
sourcepub fn set(&'static self, value: T)
pub fn set(&'static self, value: T)
Sets or initializes the value of this key, without running the initializer.
§Panics
Panics if the value is currently borrowed.