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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@

## `worker`

### Version 0.7.0

- **BREAKING:** Replace `Bincode` with `Postcard` (#540) by @ranile

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

The breakage is removing the public Bincode type. Anyone who explicitly wrote:

use gloo_worker::Bincode;
WorkerSpawner<MyWorker, Bincode>

will get a compile error. But in practice, I think nobody does this.

The whole point of the CODEC = Bincode default type parameter is that users never name it.

I think it's worth mentioning.

@ranile ranile Mar 27, 2026

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I'll edit the PR description so anyone following the changelog can see it and include it in commit too


### Version 0.6.0

- Add option to spawn workers as a module; default worker type is now ES module (#421) by @JonasAlaif
Expand Down
35 changes: 34 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions crates/worker/src/actor/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W, CODEC = Bincode>
pub struct WorkerRegistrar<W, CODEC = Postcard>
where
W: Worker,
CODEC: Codec,
Expand Down
4 changes: 2 additions & 2 deletions crates/worker/src/actor/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W, CODEC = Bincode>
pub struct WorkerSpawner<W, CODEC = Postcard>
where
W: Worker,
CODEC: Codec,
Expand Down
10 changes: 5 additions & 5 deletions crates/worker/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I>(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()
}

Expand All @@ -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")
}
}
4 changes: 2 additions & 2 deletions crates/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//!
Expand Down Expand Up @@ -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::*;
4 changes: 2 additions & 2 deletions crates/worker/src/oneshot/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, CODEC = Bincode>
pub struct OneshotRegistrar<T, CODEC = Postcard>
where
T: Oneshot + 'static,
CODEC: Codec + 'static,
Expand Down
4 changes: 2 additions & 2 deletions crates/worker/src/oneshot/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<N, CODEC = Bincode>
pub struct OneshotSpawner<N, CODEC = Postcard>
where
N: Oneshot + 'static,
CODEC: Codec,
Expand Down
4 changes: 2 additions & 2 deletions crates/worker/src/reactor/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R, CODEC = Bincode>
pub struct ReactorRegistrar<R, CODEC = Postcard>
where
R: Reactor + 'static,
CODEC: Codec + 'static,
Expand Down
4 changes: 2 additions & 2 deletions crates/worker/src/reactor/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R, CODEC = Bincode>
pub struct ReactorSpawner<R, CODEC = Postcard>
where
R: Reactor + 'static,
CODEC: Codec,
Expand Down
Loading