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
impl WebWorker
Sourcepub async fn with_path(
main_js: Option<&str>,
task_limit: Option<usize>,
) -> Result<WebWorker, InitError>
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.
Sourcepub async fn run<T, R>(&self, func: WebWorkerFn<T, R>, arg: &T) -> R
pub async fn run<T, R>(&self, func: WebWorkerFn<T, R>, arg: &T) -> R
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
Sourcepub async fn try_run<T, R>(
&self,
func: WebWorkerFn<T, R>,
arg: &T,
) -> Result<R, Full>
pub async fn try_run<T, R>( &self, func: WebWorkerFn<T, R>, arg: &T, ) -> Result<R, Full>
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
Sourcepub async fn run_bytes(
&self,
func: WebWorkerFn<Box<[u8]>, Box<[u8]>>,
arg: &Box<[u8]>,
) -> Box<[u8]>
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
Sourcepub async fn try_run_bytes(
&self,
func: WebWorkerFn<Box<[u8]>, Box<[u8]>>,
arg: &Box<[u8]>,
) -> Result<Box<[u8]>, Full>
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
Sourcepub fn current_load(&self) -> usize
pub fn current_load(&self) -> usize
Return the number of tasks currently queued to this worker.