neon::thread

Struct LocalKey

Source
pub struct LocalKey<T> { /* private fields */ }
Available on crate feature 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> LocalKey<T>

Source

pub const fn new() -> Self

Creates a new local value. This method is const, so it can be assigned to static variables.

Source§

impl<T: Any + Send + 'static> LocalKey<T>

Source

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.

Source

pub fn get_or_init<'cx, 'a, C, F>(&self, cx: &'a mut C, f: F) -> &'cx T
where C: Context<'cx>, F: FnOnce() -> T,

Gets the current value of the cell, initializing it with the result of calling f if it has not yet been initialized.

Source

pub fn get_or_try_init<'cx, 'a, C, E, F>( &self, cx: &'a mut C, f: F, ) -> Result<&'cx T, E>
where C: Context<'cx>, F: FnOnce(&mut C) -> Result<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.

Source§

impl<T: Any + Send + Default + 'static> LocalKey<T>

Source

pub fn get_or_init_default<'cx, 'a, C>(&self, cx: &'a mut C) -> &'cx T
where C: Context<'cx>,

Gets the current value of the cell, initializing it with the default value if it has not yet been initialized.

Trait Implementations§

Source§

impl<T: Default> Default for LocalKey<T>

Source§

fn default() -> LocalKey<T>

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.