Skip to content

Commit 13e90e1

Browse files
committed
Improve readability of README (markdown doesn't support hidden code like rustdoc)
1 parent 9f033f1 commit 13e90e1

2 files changed

Lines changed: 137 additions & 62 deletions

File tree

README.md

Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ This is useful for users that do not want a direct serde dependency. Internally,
3232

3333
You can then start using the library without further setup.
3434
If 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.
3838
pub 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
5251
3. `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

5453
All 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
5858
use 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:
8773
let ww_sort = webworker!(sort_vec);
88-
# }
8974
```
9075

9176
#### WebWorker
9277
We 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+
10391
let 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
11197
Most of the time, we probably want to schedule tasks to a pool of workers, though.
11298
The default worker pool is instantiated on first use and can be configured using `init_worker_pool()` as described above.
11399
It 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() {
125114
let 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
242229
If 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
246233
use wasmworker::{init_worker_pool, WorkerPoolOptions};
247234

248-
# async fn example() -> Result<(), wasmworker::AlreadyInitialized> {
249235
let mut options = WorkerPoolOptions::new();
250236
// Path to the wasm-bindgen glue file (used by worker blob's import())
251237
options.path = Some("/assets/myapp.js".to_string());
252238
// Path to the WASM binary (passed to wasm-bindgen's init function)
253239
options.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

262245
To 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+
267250
let mut options = WorkerPoolOptions::new();
268251
options.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

src/lib.rs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,99 @@
1-
#![doc = include_str!("../README.md")]
1+
//! # wasmworker
2+
//!
3+
//! Parallelize tasks on WebAssembly without `SharedArrayBuffer`.
4+
//!
5+
//! See the [README](https://github.com/paberr/wasmworker) for full documentation
6+
//! including bundler setup and FAQ.
7+
//!
8+
//! ## Quick start
9+
//!
10+
//! Add to your `Cargo.toml`:
11+
//!
12+
//! ```toml
13+
//! [dependencies]
14+
//! wasmworker = { version = "0.2", features = ["macros"] }
15+
//! ```
16+
//!
17+
//! ### Defining worker functions
18+
//!
19+
//! ```no_run
20+
//! use serde::{Deserialize, Serialize};
21+
//! use wasmworker::{webworker, webworker_fn};
22+
//!
23+
//! /// An arbitrary type that is (de)serializable.
24+
//! #[derive(Serialize, Deserialize)]
25+
//! pub struct VecType(Vec<u8>);
26+
//!
27+
//! /// A sort function on a custom type.
28+
//! #[webworker_fn]
29+
//! pub fn sort_vec(mut v: VecType) -> VecType {
30+
//! v.0.sort();
31+
//! v
32+
//! }
33+
//!
34+
//! # fn main() {
35+
//! // Obtain a type-safe handle to the function:
36+
//! let ww_sort = webworker!(sort_vec);
37+
//! # }
38+
//! ```
39+
//!
40+
//! ### Running tasks
41+
//!
42+
//! ```no_run
43+
//! # use serde::{Deserialize, Serialize};
44+
//! # use wasmworker::{webworker, webworker_fn, WebWorker};
45+
//! #
46+
//! # #[derive(Serialize, Deserialize, PartialEq, Debug)]
47+
//! # pub struct VecType(Vec<u8>);
48+
//! #
49+
//! # #[webworker_fn]
50+
//! # pub fn sort_vec(mut v: VecType) -> VecType {
51+
//! # v.0.sort();
52+
//! # v
53+
//! # }
54+
//! #
55+
//! # async fn example() {
56+
//! let worker = WebWorker::new(None).await.expect("Couldn't create worker");
57+
//! let sorted = worker.run(webworker!(sort_vec), &VecType(vec![3, 1, 2])).await;
58+
//! assert_eq!(sorted.0, vec![1, 2, 3]);
59+
//! # }
60+
//! # fn main() {}
61+
//! ```
62+
//!
63+
//! ```no_run
64+
//! # use serde::{Deserialize, Serialize};
65+
//! # use wasmworker::{webworker, webworker_fn, worker_pool};
66+
//! #
67+
//! # #[derive(Serialize, Deserialize, PartialEq, Debug)]
68+
//! # pub struct VecType(Vec<u8>);
69+
//! #
70+
//! # #[webworker_fn]
71+
//! # pub fn sort_vec(mut v: VecType) -> VecType {
72+
//! # v.0.sort();
73+
//! # v
74+
//! # }
75+
//! #
76+
//! # async fn example() {
77+
//! let worker_pool = worker_pool().await;
78+
//! let sorted = worker_pool.run(webworker!(sort_vec), &VecType(vec![3, 1, 2])).await;
79+
//! assert_eq!(sorted.0, vec![1, 2, 3]);
80+
//! # }
81+
//! # fn main() {}
82+
//! ```
83+
//!
84+
//! ### Configuring the worker pool
85+
//!
86+
//! ```no_run
87+
//! # use wasmworker::{init_worker_pool, WorkerPoolOptions};
88+
//! #
89+
//! # async fn startup() {
90+
//! let mut options = WorkerPoolOptions::new();
91+
//! options.num_workers = Some(2); // Default is navigator.hardwareConcurrency
92+
//! init_worker_pool(options).await.expect("Worker pool already initialized");
93+
//! # }
94+
//! # fn main() {}
95+
//! ```
96+
297
#![allow(clippy::borrowed_box)]
398
pub use channel::Channel;
499
pub use channel_task::ChannelTask;

0 commit comments

Comments
 (0)