Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Test

on: [push, pull_request]
on:
push:
branches: [main]
pull_request:

jobs:
rustfmt:
Expand All @@ -17,7 +20,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
features: ["--all-features", "--no-default-features --features serde,codec-pot"]
include:
- codec: postcard
features: "--all-features"
- codec: pot
features: "--no-default-features --features iter-ext,codec-pot"
name: clippy (${{ matrix.codec }})

steps:
- uses: actions/checkout@v4
Expand All @@ -31,7 +39,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
features: ["--all-features", "--no-default-features --features serde,macros,codec-pot"]
include:
- codec: postcard
features: "--all-features"
- codec: pot
features: "--no-default-features --features iter-ext,macros,codec-pot"
name: doc-test (${{ matrix.codec }})

steps:
- uses: actions/checkout@v4
Expand All @@ -42,6 +55,14 @@ jobs:

test:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- codec: postcard
script: "npm test"
- codec: pot
script: "npm run test:pot"
name: test (${{ matrix.codec }})

steps:
- uses: actions/checkout@v4
Expand All @@ -52,4 +73,4 @@ jobs:
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- name: Run tests
working-directory: ./test
run: npm test
run: ${{ matrix.script }}
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"
keywords = ["webworker", "parallelism", "wasm"]

[workspace.dependencies]
wasmworker = { version = "0.3", path = ".", features = ["serde", "macros"] }
wasmworker = { version = "0.3", path = ".", default-features = false, features = ["iter-ext", "macros"] }
wasmworker-proc-macro = { version = "0.3", path = "proc-macro" }

[package]
Expand Down Expand Up @@ -64,8 +64,8 @@ version = "0.3"
wasmworker-proc-macro = { workspace = true }

[features]
default = ["serde", "codec-postcard"]
serde = []
default = ["iter-ext", "codec-postcard"]
iter-ext = []
codec-postcard = ["dep:postcard"]
codec-pot = ["dep:pot"]
macros = ["wasmworker-proc-macro"]
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ Enable the `macros` feature to get access to the `#[webworker_fn]` and `#[webwor
wasmworker = { version = "0.3", features = ["macros"] }
```

The `wasmworker` crate comes with a default feature called `serde`, which allows running any function on a web worker under the following two conditions:
1. The function takes a single argument, which implements `serde::Serialize + serde::Deserialize<'de>`.
2. The return type implements `serde::Serialize + serde::Deserialize<'de>`.
Function arguments and return types must implement `serde::Serialize + serde::Deserialize<'de>`.
Alternatively, functions with the type `fn(Box<[u8]>) -> Box<[u8]>` can be used via `run_bytes()` for manual serialization.

Without the `serde` feature, only functions with the type `fn(Box<[u8]>) -> Box<[u8]>` can be run on a worker.
This is useful for users that do not want a direct serde dependency. Internally, the library always uses serde, though.
The `iter-ext` feature (enabled by default) adds the `par_map` and `try_par_map` iterator extensions for convenient parallel map operations on the default worker pool.

#### Serialization codec
By default, `wasmworker` uses [postcard](https://crates.io/crates/postcard) for internal serialization.
Expand All @@ -48,7 +46,7 @@ Note that pot has significantly higher serialization overhead and larger output

```toml
[dependencies]
wasmworker = { version = "0.3", default-features = false, features = ["serde", "macros", "codec-pot"] }
wasmworker = { version = "0.3", default-features = false, features = ["iter-ext", "macros", "codec-pot"] }
```

You can then start using the library without further setup.
Expand Down
2 changes: 1 addition & 1 deletion demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.43", features = ["sync"] }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
wasmworker = { workspace = true }
wasmworker = { workspace = true, features = ["codec-postcard"] }

[dependencies.web-sys]
features = [
Expand Down
1 change: 1 addition & 0 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct WebWorkerFn<T, R> {
/// The worker will automatically add the `__webworker_` prefix.
pub(crate) name: &'static str,
/// The original function, which can be used as a fallback.
#[cfg_attr(not(feature = "iter-ext"), allow(dead_code))]
pub(crate) func: fn(T) -> R,
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub mod convert;
pub mod error;
pub mod func;
mod global;
#[cfg(feature = "serde")]
#[cfg(feature = "iter-ext")]
pub mod iter_ext;
pub mod pool;
mod webworker;
2 changes: 1 addition & 1 deletion src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl WebWorkerPool {

/// This function can outsource a task on a [`WebWorkerPool`] 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 [`WebWorkerPool::run`].
/// For more convenient access, use [`WebWorkerPool::run`] instead.
///
/// 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.
Expand Down
4 changes: 2 additions & 2 deletions src/webworker/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl WebWorker {

/// 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`].
/// For more convenient access, use [`WebWorker::run`] instead.
///
/// 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.
Expand All @@ -318,7 +318,7 @@ impl WebWorker {
/// 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`].
/// For more convenient access, use [`WebWorker::try_run`] instead.
///
/// 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.
Expand Down
5 changes: 5 additions & 0 deletions test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ js-sys = "0.3"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", features = ["MessagePort", "Window"] }
wasmworker = { workspace = true }

[features]
default = ["codec-postcard"]
codec-postcard = ["wasmworker/codec-postcard"]
codec-pot = ["wasmworker/codec-pot"]
6 changes: 5 additions & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
"scripts": {
"postinstall": "playwright install chromium firefox",
"build:web": "wasm-pack build --target web --out-name test --out-dir out/web/pkg && shx cp index.* out/web",
"build:web:pot": "wasm-pack build --target web --out-name test --out-dir out/web/pkg -- --no-default-features --features codec-pot && shx cp index.* out/web",
"build:vite": "wasm-pack build --target web --out-name test --out-dir vite-app/pkg && cd vite-app && npx vite build --outDir ../out/vite",
"build:vite:pot": "wasm-pack build --target web --out-name test --out-dir vite-app/pkg -- --no-default-features --features codec-pot && cd vite-app && npx vite build --outDir ../out/vite",
"pretest": "npm run build:web && npm run build:vite",
"test": "playwright test"
"test": "playwright test",
"pretest:pot": "npm run build:web:pot && npm run build:vite:pot",
"test:pot": "playwright test"
},
"devDependencies": {
"@playwright/test": "^1.49.0",
Expand Down
Loading