Struct NapiAsyncWork

Source
pub struct NapiAsyncWork<T>(/* private fields */);

Implementations§

Source§

impl<T> NapiAsyncWork<T>

Source

pub fn env(&self) -> NapiEnv

Source

pub fn raw(&self) -> napi_async_work

Source

pub fn new( env: NapiEnv, name: impl AsRef<str>, state: T, execute: impl FnMut(&mut T) + Send + 'static, complete: impl FnMut(NapiEnv, NapiStatus, T) -> NapiResult<()> + 'static, ) -> NapiResult<NapiAsyncWork<T>>

This API allocates a work object that is used to execute logic asynchronously. It should be freed using napi_delete_async_work once the work is no longer required. async_resource_name should be a null-terminated, UTF-8-encoded string.

The async_resource_name identifier is provided by the user and should be representative of the type of async work being performed. It is also recommended to apply namespacing to the identifier, e.g. by including the module name. See the async_hooks documentation for more information.

§Arguments
  • env - napi_env
  • name - napi async work identifier
  • state - The state shared between execute & complete
  • execute - The native function which should be called to execute the logic asynchronously. The given function is called from a worker pool thread and can execute in parallel with the main event loop thread.
  • complete - The native function which will be called when the asynchronous logic is completed or is cancelled. The given function is called from the main event loop thread.
Source

pub fn queue(&mut self) -> NapiResult<Option<()>>

This API requests that the previously allocated work be scheduled for execution. Once it returns successfully, this API must not be called again with the same napi_async_work item or the result will be undefined.

NB: The NapiAsyncWork can not be queued more than once.

Source

pub fn cancel(&self) -> NapiResult<()>

This API cancels queued work if it has not yet been started. If it has already started executing, it cannot be cancelled and napi_generic_failure will be returned. If successful, the complete callback will be invoked with a status value of napi_cancelled. The work should not be deleted before the complete callback invocation, even if it has been successfully cancelled.

This API can be called even if there is a pending JavaScript exception.

Source

pub fn delete(self) -> NapiResult<()>

This API frees a previously allocated work object. This API can be called even if there is a pending JavaScript exception.

NB: should not delete a queued task.

Trait Implementations§

Source§

impl<T: Debug> Debug for NapiAsyncWork<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for NapiAsyncWork<T>

§

impl<T> RefUnwindSafe for NapiAsyncWork<T>
where T: RefUnwindSafe,

§

impl<T> !Send for NapiAsyncWork<T>

§

impl<T> !Sync for NapiAsyncWork<T>

§

impl<T> Unpin for NapiAsyncWork<T>
where T: Unpin,

§

impl<T> UnwindSafe for NapiAsyncWork<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>,

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.