diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f0424c6..3fa11db4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -422,6 +422,10 @@ ## `worker` +### Version 0.7.0 + +- **BREAKING:** Replace `Bincode` with `Postcard` (#540) by @ranile + ### Version 0.6.0 - Add option to spawn workers as a module; default worker type is now ES module (#421) by @JonasAlaif diff --git a/Cargo.lock b/Cargo.lock index 3e9bf8f6..ea50e595 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -123,6 +123,15 @@ dependencies = [ "windows-link", ] +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror 2.0.18", +] + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -168,6 +177,18 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + [[package]] name = "equivalent" version = "1.0.2" @@ -883,12 +904,12 @@ dependencies = [ name = "gloo-worker" version = "0.6.0" dependencies = [ - "bincode", "futures", "gloo-utils 0.3.0", "gloo-worker-macros 0.2.0", "js-sys", "pinned", + "postcard", "serde", "thiserror 2.0.18", "wasm-bindgen", @@ -1327,6 +1348,18 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "serde", +] + [[package]] name = "prettyplease" version = "0.2.37" diff --git a/crates/worker/Cargo.toml b/crates/worker/Cargo.toml index 623a5382..dec8ff44 100644 --- a/crates/worker/Cargo.toml +++ b/crates/worker/Cargo.toml @@ -18,7 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"] [dependencies] -bincode = "1.3.3" +postcard = { version = "1.1.3", default-features = false, features = ["use-std"] } gloo-utils = { path = "../utils", version = "0.3" } gloo-worker-macros = { path = "../worker-macros", version = "0.2" } js-sys = { workspace = true } diff --git a/crates/worker/src/actor/registrar.rs b/crates/worker/src/actor/registrar.rs index 576ef2ba..ed517c7b 100644 --- a/crates/worker/src/actor/registrar.rs +++ b/crates/worker/src/actor/registrar.rs @@ -9,10 +9,10 @@ use super::messages::{FromWorker, ToWorker}; use super::native_worker::{DedicatedWorker, NativeWorkerExt, WorkerSelf}; use super::scope::WorkerScope; use super::traits::Worker; -use crate::codec::{Bincode, Codec}; +use crate::codec::{Codec, Postcard}; /// A Worker Registrar. -pub struct WorkerRegistrar +pub struct WorkerRegistrar where W: Worker, CODEC: Codec, diff --git a/crates/worker/src/actor/spawner.rs b/crates/worker/src/actor/spawner.rs index 9e198a01..9be34584 100644 --- a/crates/worker/src/actor/spawner.rs +++ b/crates/worker/src/actor/spawner.rs @@ -16,11 +16,11 @@ use super::messages::FromWorker; use super::native_worker::{DedicatedWorker, NativeWorkerExt}; use super::traits::Worker; use super::{Callback, Shared}; -use crate::codec::{Bincode, Codec}; +use crate::codec::{Codec, Postcard}; /// A spawner to create workers. #[derive(Clone)] -pub struct WorkerSpawner +pub struct WorkerSpawner where W: Worker, CODEC: Codec, diff --git a/crates/worker/src/codec.rs b/crates/worker/src/codec.rs index f8b8c0d8..e53ed7cc 100644 --- a/crates/worker/src/codec.rs +++ b/crates/worker/src/codec.rs @@ -15,16 +15,16 @@ pub trait Codec { O: for<'de> Deserialize<'de>; } -/// Default message encoding with [bincode]. +/// Default message encoding with [postcard]. #[derive(Debug)] -pub struct Bincode; +pub struct Postcard; -impl Codec for Bincode { +impl Codec for Postcard { fn encode(input: I) -> JsValue where I: Serialize, { - let buf = bincode::serialize(&input).expect("can't serialize a worker message"); + let buf = postcard::to_stdvec(&input).expect("can't serialize a worker message"); Uint8Array::from(buf.as_slice()).into() } @@ -33,6 +33,6 @@ impl Codec for Bincode { O: for<'de> Deserialize<'de>, { let data = Uint8Array::from(input).to_vec(); - bincode::deserialize(&data).expect("can't deserialize a worker message") + postcard::from_bytes(&data).expect("can't deserialize a worker message") } } diff --git a/crates/worker/src/lib.rs b/crates/worker/src/lib.rs index c264100d..141dbeac 100644 --- a/crates/worker/src/lib.rs +++ b/crates/worker/src/lib.rs @@ -17,7 +17,7 @@ //! ### Overhead //! //! Gloo Workers use web workers. They incur a serialization overhead on the -//! messages they send and receive. Bridges use [bincode](https://github.com/servo/bincode) +//! messages they send and receive. Bridges use [postcard](https://github.com/jamesmunns/postcard) //! by default to communicate with workers, so the cost is substantially higher //! than just calling a function. //! @@ -60,5 +60,5 @@ pub mod reactor; mod traits; pub use actor::*; -pub use codec::{Bincode, Codec}; +pub use codec::{Codec, Postcard}; pub use traits::*; diff --git a/crates/worker/src/oneshot/registrar.rs b/crates/worker/src/oneshot/registrar.rs index 75b9a296..146c80a2 100644 --- a/crates/worker/src/oneshot/registrar.rs +++ b/crates/worker/src/oneshot/registrar.rs @@ -6,11 +6,11 @@ use serde::ser::Serialize; use super::traits::Oneshot; use super::worker::OneshotWorker; use crate::actor::WorkerRegistrar; -use crate::codec::{Bincode, Codec}; +use crate::codec::{Codec, Postcard}; use crate::traits::Registrable; /// A registrar for oneshot workers. -pub struct OneshotRegistrar +pub struct OneshotRegistrar where T: Oneshot + 'static, CODEC: Codec + 'static, diff --git a/crates/worker/src/oneshot/spawner.rs b/crates/worker/src/oneshot/spawner.rs index e5abcb6b..4b41df7e 100644 --- a/crates/worker/src/oneshot/spawner.rs +++ b/crates/worker/src/oneshot/spawner.rs @@ -5,11 +5,11 @@ use super::bridge::OneshotBridge; use super::traits::Oneshot; use super::worker::OneshotWorker; use crate::actor::WorkerSpawner; -use crate::codec::{Bincode, Codec}; +use crate::codec::{Codec, Postcard}; /// A spawner to create oneshot workers. #[derive(Debug, Default)] -pub struct OneshotSpawner +pub struct OneshotSpawner where N: Oneshot + 'static, CODEC: Codec, diff --git a/crates/worker/src/reactor/registrar.rs b/crates/worker/src/reactor/registrar.rs index 8bb97743..7c69d759 100644 --- a/crates/worker/src/reactor/registrar.rs +++ b/crates/worker/src/reactor/registrar.rs @@ -7,11 +7,11 @@ use super::scope::ReactorScoped; use super::traits::Reactor; use super::worker::ReactorWorker; use crate::actor::WorkerRegistrar; -use crate::codec::{Bincode, Codec}; +use crate::codec::{Codec, Postcard}; use crate::traits::Registrable; /// A registrar for reactor workers. -pub struct ReactorRegistrar +pub struct ReactorRegistrar where R: Reactor + 'static, CODEC: Codec + 'static, diff --git a/crates/worker/src/reactor/spawner.rs b/crates/worker/src/reactor/spawner.rs index df8a2704..2fad5b82 100644 --- a/crates/worker/src/reactor/spawner.rs +++ b/crates/worker/src/reactor/spawner.rs @@ -6,11 +6,11 @@ use super::scope::ReactorScoped; use super::traits::Reactor; use super::worker::ReactorWorker; use crate::actor::WorkerSpawner; -use crate::codec::{Bincode, Codec}; +use crate::codec::{Codec, Postcard}; /// A spawner to create oneshot workers. #[derive(Debug, Default)] -pub struct ReactorSpawner +pub struct ReactorSpawner where R: Reactor + 'static, CODEC: Codec,