wasmworker

Struct WebWorker

Source
pub struct WebWorker { /* private fields */ }
Expand description

This struct represents a single web worker instance. It can be created using WebWorker::new or WebWorker::with_path. When an instance of this type is dropped, it also terminates the corresponding web worker.

Example usage:

use wasmworker::{webworker, WebWorker};

let worker = WebWorker::new(None).await;
let res = worker.run(webworker!(sort_vec), &VecType(vec![5, 2, 8])).await;
assert_eq!(res.0, vec![2, 5, 8]);

Implementations§

Source§

impl WebWorker

Source

pub async fn new(task_limit: Option<usize>) -> Result<WebWorker, InitError>

Create a new WebWorker with an optional limit on the number of tasks queued. This can fail with an InitError, for example, if the automatically inferred path for the wasm-bindgen glue is wrong.

Source

pub async fn with_path( main_js: Option<&str>, task_limit: Option<usize>, ) -> Result<WebWorker, InitError>

Create a new WebWorker with an optional limit on the number of tasks queued. This function also receives an optional main_js path, which should point to the glue file generated by wasm-bindgen.

In the standard setup, the path is automatically inferred. In more complex setups, it might be necessary to manually set this path. If a wrong path is given, a InitError will be returned.

Source

pub async fn run<T, R>(&self, func: WebWorkerFn<T, R>, arg: &T) -> R
where T: Serialize + for<'de> Deserialize<'de>, R: Serialize + for<'de> Deserialize<'de>,

This is the most general function to outsource a task on a WebWorker. It will automatically handle serialization of the argument, scheduling of the task on the worker, and deserialization of the return value.

The func: WebWorkerFn argument should normally be instantiated using the crate::webworker! macro. This ensures type safety and that the function is correctly exposed to the worker.

If a task limit has been set, this function will yield until previous tasks have been finished. This is achieved by a semaphore before task submission to the worker.

Example:

worker.run(webworker!(sort_vec), &my_vec).await
Source

pub async fn try_run<T, R>( &self, func: WebWorkerFn<T, R>, arg: &T, ) -> Result<R, Full>
where T: Serialize + for<'de> Deserialize<'de>, R: Serialize + for<'de> Deserialize<'de>,

This function differs from WebWorker::run by returning early if the given task limit is reached. In this case a Full error is returned.

The func: WebWorkerFn argument should normally be instantiated using the crate::webworker! macro. This ensures type safety and that the function is correctly exposed to the worker.

If no task limit has been set, this function can never return an error.

Example:

worker.try_run(webworker!(sort_vec), &my_vec).await
Source

pub async fn run_bytes( &self, func: WebWorkerFn<Box<[u8]>, Box<[u8]>>, arg: &Box<[u8]>, ) -> Box<[u8]>

This function can outsource a task on a WebWorker which has Box<[u8]> both as input and output. (De)serialization of values needs to be handled by the caller. For more convenient access, make sure the serde feature is enabled and use WebWorker::run.

The func: WebWorkerFn argument should normally be instantiated using the crate::webworker! macro. This ensures type safety and that the function is correctly exposed to the worker.

If a task limit has been set, this function will yield until previous tasks have been finished. This is achieved by a semaphore before task submission to the worker.

Example:

worker.run_bytes(webworker!(sort), &my_box).await
Source

pub async fn try_run_bytes( &self, func: WebWorkerFn<Box<[u8]>, Box<[u8]>>, arg: &Box<[u8]>, ) -> Result<Box<[u8]>, Full>

This function differs from WebWorker::run_bytes by returning early if the given task limit is reached. In this case a Full error is returned. (De)serialization of values needs to be handled by the caller. For more convenient access, make sure the serde feature is enabled and use WebWorker::try_run.

The func: WebWorkerFn argument should normally be instantiated using the crate::webworker! macro. This ensures type safety and that the function is correctly exposed to the worker.

If no task limit has been set, this function can never return an error.

Example:

worker.try_run_bytes(webworker!(sort), &my_box).await
Source

pub fn capacity(&self) -> Option<usize>

Return the current capacity for new tasks.

Source

pub fn current_load(&self) -> usize

Return the number of tasks currently queued to this worker.

Trait Implementations§

Source§

impl Drop for WebWorker

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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.