pub struct LocalKey<T> { /* private fields */ }
Expand description
A JavaScript thread-local container that owns its contents, similar to
std::thread::LocalKey
but tied to a JavaScript thread rather
than a system thread.
§Initialization and Destruction
Initialization is dynamically performed on the first call to one of the init
methods
of LocalKey
, and values that implement Drop
get destructed when
the JavaScript thread exits, i.e. when a worker thread terminates or the main thread
terminates on process exit.
Implementations§
source§impl<T: Any + Send + 'static> LocalKey<T>
impl<T: Any + Send + 'static> LocalKey<T>
sourcepub fn get<'cx, 'a, C>(&self, cx: &'a mut C) -> Option<&'cx T>where
C: Context<'cx>,
pub fn get<'cx, 'a, C>(&self, cx: &'a mut C) -> Option<&'cx T>where
C: Context<'cx>,
Gets the current value of the cell. Returns None
if the cell has not
yet been initialized.
sourcepub fn get_or_init<'cx, 'a, C, F>(&self, cx: &'a mut C, f: F) -> &'cx T
pub fn get_or_init<'cx, 'a, C, F>(&self, cx: &'a mut C, f: F) -> &'cx T
Gets the current value of the cell, initializing it with the result of
calling f
if it has not yet been initialized.
sourcepub fn get_or_try_init<'cx, 'a, C, E, F>(
&self,
cx: &'a mut C,
f: F
) -> Result<&'cx T, E>
pub fn get_or_try_init<'cx, 'a, C, E, F>( &self, cx: &'a mut C, f: F ) -> Result<&'cx T, E>
Gets the current value of the cell, initializing it with the result of
calling f
if it has not yet been initialized. Returns Err
if the
callback triggers a JavaScript exception.
§Panics
During the execution of f
, calling any methods on this LocalKey
that
attempt to initialize it will panic.