Skip to content
Open
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
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ jobs:
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- run: cargo clippy --all-features -- -D warnings
- run: cargo clippy --no-default-features --features "serde,codec-postcard" -- -D warnings
- run: cargo clippy --no-default-features --features "serde,codec-pot" -- -D warnings

doc-test:
runs-on: ubuntu-latest
Expand Down
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ keywords.workspace = true
[dependencies]
futures = "0.3"
js-sys = { version = "0.3" }
postcard = { version = "1.1", features = ["alloc"] }
send_wrapper = "0.6"
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11"
Expand All @@ -40,6 +39,8 @@ tokio = { version = "1.4", features = ["sync"] }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
log = "0.4"
postcard = { version = "1.1", features = ["alloc"], optional = true }
pot = { version = "3.0.1", optional = true }

[dependencies.web-sys]
features = [
Expand All @@ -63,5 +64,7 @@ version = "0.3"
wasmworker-proc-macro = { workspace = true }

[features]
default = ["serde"]
default = ["serde", "codec-postcard"]
serde = []
codec-postcard = ["dep:postcard"]
codec-pot = ["dep:pot"]
3 changes: 2 additions & 1 deletion src/channel_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub struct ChannelTask<R> {

impl<R: DeserializeOwned> ChannelTask<R> {
/// Create a new `ChannelTask` from a channel and a result receiver.
pub(crate) fn new(channel: Channel, result_rx: oneshot::Receiver<Vec<u8>>) -> Self {
#[doc(hidden)]
pub fn new(channel: Channel, result_rx: oneshot::Receiver<Vec<u8>>) -> Self {
Self {
channel,
result_rx,
Expand Down
33 changes: 33 additions & 0 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
/// This wrapper function encapsulates our internal serialization format.
/// It is used internally to prepare values before sending them to a worker
/// or back to the main thread via `postMessage`.
#[cfg(feature = "codec-postcard")]
pub fn to_bytes<T: Serialize>(value: &T) -> Box<[u8]> {
postcard::to_allocvec(value)
.expect("WebWorker serialization failed")
Expand All @@ -12,6 +13,38 @@ pub fn to_bytes<T: Serialize>(value: &T) -> Box<[u8]> {
/// This wrapper function encapsulates our internal serialization format.
/// It is used internally to prepare values after receiving them from a worker
/// or the main thread via `postMessage`.
#[cfg(feature = "codec-postcard")]
pub fn from_bytes<'de, T: Deserialize<'de>>(bytes: &'de [u8]) -> T {
postcard::from_bytes(bytes).expect("WebWorker deserialization failed")
}

#[cfg(feature = "codec-pot")]
pub const POT_CONFIG: pot::Config = pot::Config::new().compatibility(pot::Compatibility::V4);

/// This wrapper function encapsulates our internal serialization format.
/// It is used internally to prepare values before sending them to a worker
/// or back to the main thread via `postMessage`.
#[cfg(feature = "codec-pot")]
pub fn to_bytes<T: Serialize>(value: &T) -> Box<[u8]> {
POT_CONFIG
.serialize(value)
.expect("WebWorker serialization failed")
.into()
}

/// This wrapper function encapsulates our internal serialization format.
/// It is used internally to prepare values after receiving them from a worker
/// or the main thread via `postMessage`.
#[cfg(feature = "codec-pot")]
pub fn from_bytes<'de, T: Deserialize<'de>>(bytes: &'de [u8]) -> T {
POT_CONFIG
.deserialize(bytes)
.expect("WebWorker deserialization failed")
}

// Enforce exactly one:
#[cfg(all(feature = "codec-postcard", feature = "codec-pot"))]
compile_error!("Enable only one of: codec-postcard, codec-pot");

#[cfg(not(any(feature = "codec-postcard", feature = "codec-pot")))]
compile_error!("Enable one of: codec-postcard, codec-pot");
Loading