@@ -32,7 +32,7 @@ This is useful for users that do not want a direct serde dependency. Internally,
3232
3333You can then start using the library without further setup.
3434If you plan on using the global ` WebWorkerPool ` (using the iterator extensions or ` worker_pool() ` ), you can * optionally* configure this pool:
35- ``` rust,no_run
35+ ``` rust
3636// Importing it publicly will also expose the function on the JavaScript side.
3737// You can instantiate the pool both via Rust and JS.
3838pub use wasmworker :: {init_worker_pool, WorkerPoolOptions };
@@ -42,7 +42,6 @@ async fn startup() {
4242 options . num_workers = Some (2 ); // Default is navigator.hardwareConcurrency
4343 init_worker_pool (options ). await . expect (" Worker pool already initialized" );
4444}
45- # fn main() {}
4645```
4746
4847### Outsourcing tasks
@@ -52,11 +51,12 @@ The library offers three ways of outsourcing function calls onto concurrent work
52513 . ` par_map ` : an extension to regular iterators, which allows to execute a function on every element of the iterator in parallel using the default worker pool.
5352
5453All approaches require the functions that should be executed to be annotated with the ` #[webworker_fn] ` macro.
55- This macro ensures that the functions are available to the web worker instances:
54+ This macro ensures that the functions are available to the web worker instances.
55+ To execute such a function, pass its ` WebWorkerFn ` handle (obtained via the ` webworker!() ` macro) to a worker:
5656
57- ``` rust,no_run
57+ ``` rust
5858use serde :: {Deserialize , Serialize };
59- use wasmworker::webworker_fn;
59+ use wasmworker :: {webworker, webworker_fn} ;
6060
6161/// An arbitrary type that is (de)serializable.
6262#[derive(Serialize , Deserialize )]
@@ -68,65 +68,52 @@ pub fn sort_vec(mut v: VecType) -> VecType {
6868 v . 0 . sort ();
6969 v
7070}
71- # fn main() {}
72- ```
73-
74- Whenever we want to execute a function, we need to pass the corresponding ` WebWorkerFn ` object to the worker.
75- This object describes the function to the worker and can be safely obtained via the ` webworker!() ` macro:
7671
77- ``` rust,no_run
78- # use serde::{Deserialize, Serialize};
79- # use wasmworker::webworker_fn;
80- # #[derive(Serialize, Deserialize)]
81- # pub struct VecType(Vec<u8>);
82- # #[webworker_fn]
83- # pub fn sort_vec(mut v: VecType) -> VecType { v.0.sort(); v }
84- use wasmworker::webworker;
85-
86- # fn main() {
72+ // Obtain a type-safe handle to the function:
8773let ww_sort = webworker! (sort_vec );
88- # }
8974```
9075
9176#### WebWorker
9277We can instantiate our own workers and run functions on them:
93- ``` rust,no_run
94- # use serde::{Deserialize, Serialize};
95- # use wasmworker::webworker_fn;
96- # #[derive(Serialize, Deserialize, PartialEq, Debug)]
97- # pub struct VecType(Vec<u8>);
98- # #[webworker_fn]
99- # pub fn sort_vec(mut v: VecType) -> VecType { v.0.sort(); v }
100- use wasmworker::{webworker, WebWorker};
101-
102- # async fn example() {
78+ ``` rust
79+ use serde :: {Deserialize , Serialize };
80+ use wasmworker :: {webworker, webworker_fn, WebWorker };
81+
82+ #[derive(Serialize , Deserialize )]
83+ pub struct VecType (Vec <u8 >);
84+
85+ #[webworker_fn]
86+ pub fn sort_vec (mut v : VecType ) -> VecType {
87+ v . 0 . sort ();
88+ v
89+ }
90+
10391let worker = WebWorker :: new (None ). await . expect (" Couldn't create worker" );
104- let res = worker.run(webworker!(sort_vec), &VecType(vec![5, 2, 8])).await;
105- assert_eq!(res.0, vec![2, 5, 8]);
106- # }
107- # fn main() {}
92+ let sorted = worker . run (webworker! (sort_vec ), & VecType (vec! [3 , 1 , 2 ])). await ;
93+ assert_eq! (sorted . 0 , vec! [1 , 2 , 3 ]);
10894```
10995
11096#### WebWorkerPool
11197Most of the time, we probably want to schedule tasks to a pool of workers, though.
11298The default worker pool is instantiated on first use and can be configured using ` init_worker_pool() ` as described above.
11399It uses a round-robin scheduler (with the second option being a load based scheduler), a number of ` navigator.hardwareConcurrency ` separate workers, and the default inferred path.
114100
115- ``` rust,no_run
116- # use serde::{Deserialize, Serialize};
117- # use wasmworker::webworker_fn;
118- # #[derive(Serialize, Deserialize, PartialEq, Debug)]
119- # pub struct VecType(Vec<u8>);
120- # #[webworker_fn]
121- # pub fn sort_vec(mut v: VecType) -> VecType { v.0.sort(); v }
122- use wasmworker::{webworker, worker_pool};
101+ ``` rust
102+ use serde :: {Deserialize , Serialize };
103+ use wasmworker :: {webworker, webworker_fn, worker_pool};
104+
105+ #[derive(Serialize , Deserialize )]
106+ pub struct VecType (Vec <u8 >);
107+
108+ #[webworker_fn]
109+ pub fn sort_vec (mut v : VecType ) -> VecType {
110+ v . 0 . sort ();
111+ v
112+ }
123113
124- # async fn example() {
125114let worker_pool = worker_pool (). await ;
126- let res = worker_pool.run(webworker!(sort_vec), &VecType(vec![5, 2, 8])).await;
127- assert_eq!(res.0, vec![2, 5, 8]);
128- # }
129- # fn main() {}
115+ let sorted = worker_pool . run (webworker! (sort_vec ), & VecType (vec! [3 , 1 , 2 ])). await ;
116+ assert_eq! (sorted . 0 , vec! [1 , 2 , 3 ]);
130117```
131118
132119#### Iterator extension
@@ -242,34 +229,27 @@ No Rust-side changes are needed — `import.meta.url` resolves correctly when th
242229If your build setup places the wasm-bindgen glue or WASM binary at non-standard locations
243230(e.g., hashed filenames, nested directories), you can override the paths explicitly:
244231
245- ``` rust,no_run
232+ ``` rust
246233use wasmworker :: {init_worker_pool, WorkerPoolOptions };
247234
248- # async fn example() -> Result<(), wasmworker::AlreadyInitialized> {
249235let mut options = WorkerPoolOptions :: new ();
250236// Path to the wasm-bindgen glue file (used by worker blob's import())
251237options . path = Some (" /assets/myapp.js" . to_string ());
252238// Path to the WASM binary (passed to wasm-bindgen's init function)
253239options . path_bg = Some (" /assets/myapp_bg.wasm" . to_string ());
254- init_worker_pool(options).await?;
255- # Ok(())
256- # }
257- # fn main() {}
240+ init_worker_pool (options ). await . unwrap ();
258241```
259242
260243#### Precompiling WASM
261244
262245To reduce bandwidth (fetch WASM once instead of once per worker), you can precompile and share the module:
263246
264- ``` rust,no_run
265- # use wasmworker::{init_worker_pool, WorkerPoolOptions};
266- # async fn example() -> Result<(), wasmworker::AlreadyInitialized> {
247+ ``` rust
248+ use wasmworker :: {init_worker_pool, WorkerPoolOptions };
249+
267250let mut options = WorkerPoolOptions :: new ();
268251options . precompile_wasm = Some (true );
269- init_worker_pool(options).await?;
270- # Ok(())
271- # }
272- # fn main() {}
252+ init_worker_pool (options ). await . unwrap ();
273253```
274254
275255## FAQ
0 commit comments