pub struct LocalKey<T> { /* private fields */ }napi-6 only.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.
Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for LocalKey<T>
impl<T> RefUnwindSafe for LocalKey<T>where
T: RefUnwindSafe,
impl<T> Send for LocalKey<T>where
T: Send,
impl<T> Sync for LocalKey<T>where
T: Sync,
impl<T> Unpin for LocalKey<T>where
T: Unpin,
impl<T> UnwindSafe for LocalKey<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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more