Skip to content

Commit f3129d9

Browse files
Roba1993Copilotpaberr
authored
Make ChannelTask public, but hidden in docu (#7)
* Make ChannelTask public, but hidden in documentation * Added pot as optional dependency * Add codec documentation to README and extend CI for pot feature Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Pascal Berrang <git@p4l.dev>
1 parent f3406b3 commit f3129d9

6 files changed

Lines changed: 86 additions & 5 deletions

File tree

.github/workflows/test.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,30 @@ jobs:
1515

1616
clippy:
1717
runs-on: ubuntu-latest
18+
strategy:
19+
matrix:
20+
features: ["--all-features", "--no-default-features --features serde,codec-pot"]
1821

1922
steps:
2023
- uses: actions/checkout@v4
2124
- uses: dtolnay/rust-toolchain@nightly
2225
with:
2326
components: clippy
2427
- uses: Swatinem/rust-cache@v2
25-
- run: cargo clippy --all-features -- -D warnings
28+
- run: cargo clippy ${{ matrix.features }} -- -D warnings
2629

2730
doc-test:
2831
runs-on: ubuntu-latest
32+
strategy:
33+
matrix:
34+
features: ["--all-features", "--no-default-features --features serde,macros,codec-pot"]
2935

3036
steps:
3137
- uses: actions/checkout@v4
3238
- uses: dtolnay/rust-toolchain@stable
3339
- uses: Swatinem/rust-cache@v2
3440
- name: Run doctest
35-
run: cargo test --doc --all-features
41+
run: cargo test --doc ${{ matrix.features }}
3642

3743
test:
3844
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ keywords.workspace = true
3030
[dependencies]
3131
futures = "0.3"
3232
js-sys = { version = "0.3" }
33-
postcard = { version = "1.1", features = ["alloc"] }
3433
send_wrapper = "0.6"
3534
serde = { version = "1.0", features = ["derive"] }
3635
serde_bytes = "0.11"
@@ -40,6 +39,8 @@ tokio = { version = "1.4", features = ["sync"] }
4039
wasm-bindgen = "0.2"
4140
wasm-bindgen-futures = "0.4"
4241
log = "0.4"
42+
postcard = { version = "1.1", features = ["alloc"], optional = true }
43+
pot = { version = "3.0.1", optional = true }
4344

4445
[dependencies.web-sys]
4546
features = [
@@ -63,8 +64,10 @@ version = "0.3"
6364
wasmworker-proc-macro = { workspace = true }
6465

6566
[features]
66-
default = ["serde"]
67+
default = ["serde", "codec-postcard"]
6768
serde = []
69+
codec-postcard = ["dep:postcard"]
70+
codec-pot = ["dep:pot"]
6871
macros = ["wasmworker-proc-macro"]
6972

7073
[dependencies.wasmworker-proc-macro]

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ In contrast to many other libraries like [wasm-bindgen-rayon](https://github.com
1111

1212
- [Usage](#usage)
1313
- [Setting up](#setting-up)
14+
- [Serialization codec](#serialization-codec)
1415
- [Outsourcing tasks](#outsourcing-tasks)
1516
- [WebWorker](#webworker)
1617
- [WebWorkerPool](#webworkerpool)
@@ -38,6 +39,18 @@ The `wasmworker` crate comes with a default feature called `serde`, which allows
3839
Without the `serde` feature, only functions with the type `fn(Box<[u8]>) -> Box<[u8]>` can be run on a worker.
3940
This is useful for users that do not want a direct serde dependency. Internally, the library always uses serde, though.
4041

42+
#### Serialization codec
43+
By default, `wasmworker` uses [postcard](https://crates.io/crates/postcard) for internal serialization.
44+
Postcard is compact and fast, making it ideal for the typical WebWorker use case (passing `Vec<T>`, structs, primitives).
45+
46+
For complex types like `Rc<T>` or cyclic structures, you can use [pot](https://crates.io/crates/pot) instead.
47+
Note that pot has significantly higher serialization overhead and larger output sizes, so it should only be used when postcard cannot handle your data types.
48+
49+
```toml
50+
[dependencies]
51+
wasmworker = { version = "0.3", default-features = false, features = ["serde", "macros", "codec-pot"] }
52+
```
53+
4154
You can then start using the library without further setup.
4255
If you plan on using the global `WebWorkerPool` (using the iterator extensions or `worker_pool()`), you can *optionally* configure this pool:
4356
```rust

src/channel_task.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ pub struct ChannelTask<R> {
3434

3535
impl<R: DeserializeOwned> ChannelTask<R> {
3636
/// Create a new `ChannelTask` from a channel and a result receiver.
37-
pub(crate) fn new(channel: Channel, result_rx: oneshot::Receiver<Vec<u8>>) -> Self {
37+
#[doc(hidden)]
38+
pub fn new(channel: Channel, result_rx: oneshot::Receiver<Vec<u8>>) -> Self {
3839
Self {
3940
channel,
4041
result_rx,

src/convert.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
33
/// This wrapper function encapsulates our internal serialization format.
44
/// It is used internally to prepare values before sending them to a worker
55
/// or back to the main thread via `postMessage`.
6+
#[cfg(feature = "codec-postcard")]
67
pub fn to_bytes<T: Serialize>(value: &T) -> Box<[u8]> {
78
postcard::to_allocvec(value)
89
.expect("WebWorker serialization failed")
@@ -12,6 +13,34 @@ pub fn to_bytes<T: Serialize>(value: &T) -> Box<[u8]> {
1213
/// This wrapper function encapsulates our internal serialization format.
1314
/// It is used internally to prepare values after receiving them from a worker
1415
/// or the main thread via `postMessage`.
16+
#[cfg(feature = "codec-postcard")]
1517
pub fn from_bytes<'de, T: Deserialize<'de>>(bytes: &'de [u8]) -> T {
1618
postcard::from_bytes(bytes).expect("WebWorker deserialization failed")
1719
}
20+
21+
#[cfg(all(feature = "codec-pot", not(feature = "codec-postcard")))]
22+
const POT_CONFIG: pot::Config = pot::Config::new().compatibility(pot::Compatibility::V4);
23+
24+
/// This wrapper function encapsulates our internal serialization format.
25+
/// It is used internally to prepare values before sending them to a worker
26+
/// or back to the main thread via `postMessage`.
27+
#[cfg(all(feature = "codec-pot", not(feature = "codec-postcard")))]
28+
pub fn to_bytes<T: Serialize>(value: &T) -> Box<[u8]> {
29+
POT_CONFIG
30+
.serialize(value)
31+
.expect("WebWorker serialization failed")
32+
.into()
33+
}
34+
35+
/// This wrapper function encapsulates our internal serialization format.
36+
/// It is used internally to prepare values after receiving them from a worker
37+
/// or the main thread via `postMessage`.
38+
#[cfg(all(feature = "codec-pot", not(feature = "codec-postcard")))]
39+
pub fn from_bytes<'de, T: Deserialize<'de>>(bytes: &'de [u8]) -> T {
40+
POT_CONFIG
41+
.deserialize(bytes)
42+
.expect("WebWorker deserialization failed")
43+
}
44+
45+
#[cfg(not(any(feature = "codec-postcard", feature = "codec-pot")))]
46+
compile_error!("No codec selected. Enable `codec-postcard` (default) or `codec-pot`.");

0 commit comments

Comments
 (0)