Struct neon::thread::LocalKey

source ·
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> 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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.