Skip to content

Commit 6707155

Browse files
committed
Replace Bincode with Postcard
fixes #529
1 parent 40b4c5f commit 6707155

11 files changed

Lines changed: 58 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@
422422

423423
## `worker`
424424

425+
### Version 0.7.0
426+
427+
- **BREAKING:** Replace `Bincode` with `Postcard` (#540) by @ranile
428+
425429
### Version 0.6.0
426430

427431
- Add option to spawn workers as a module; default worker type is now ES module (#421) by @JonasAlaif

Cargo.lock

Lines changed: 34 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/worker/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1818

1919

2020
[dependencies]
21-
bincode = "1.3.3"
21+
postcard = { version = "1.1.3", default-features = false, features = ["use-std"] }
2222
gloo-utils = { path = "../utils", version = "0.3" }
2323
gloo-worker-macros = { path = "../worker-macros", version = "0.2" }
2424
js-sys = { workspace = true }

crates/worker/src/actor/registrar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use super::messages::{FromWorker, ToWorker};
99
use super::native_worker::{DedicatedWorker, NativeWorkerExt, WorkerSelf};
1010
use super::scope::WorkerScope;
1111
use super::traits::Worker;
12-
use crate::codec::{Bincode, Codec};
12+
use crate::codec::{Postcard, Codec};
1313

1414
/// A Worker Registrar.
15-
pub struct WorkerRegistrar<W, CODEC = Bincode>
15+
pub struct WorkerRegistrar<W, CODEC = Postcard>
1616
where
1717
W: Worker,
1818
CODEC: Codec,

crates/worker/src/actor/spawner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use super::messages::FromWorker;
1616
use super::native_worker::{DedicatedWorker, NativeWorkerExt};
1717
use super::traits::Worker;
1818
use super::{Callback, Shared};
19-
use crate::codec::{Bincode, Codec};
19+
use crate::codec::{Postcard, Codec};
2020

2121
/// A spawner to create workers.
2222
#[derive(Clone)]
23-
pub struct WorkerSpawner<W, CODEC = Bincode>
23+
pub struct WorkerSpawner<W, CODEC = Postcard>
2424
where
2525
W: Worker,
2626
CODEC: Codec,

crates/worker/src/codec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ pub trait Codec {
1515
O: for<'de> Deserialize<'de>;
1616
}
1717

18-
/// Default message encoding with [bincode].
18+
/// Default message encoding with [postcard].
1919
#[derive(Debug)]
20-
pub struct Bincode;
20+
pub struct Postcard;
2121

22-
impl Codec for Bincode {
22+
impl Codec for Postcard {
2323
fn encode<I>(input: I) -> JsValue
2424
where
2525
I: Serialize,
2626
{
27-
let buf = bincode::serialize(&input).expect("can't serialize a worker message");
27+
let buf = postcard::to_stdvec(&input).expect("can't serialize a worker message");
2828
Uint8Array::from(buf.as_slice()).into()
2929
}
3030

@@ -33,6 +33,6 @@ impl Codec for Bincode {
3333
O: for<'de> Deserialize<'de>,
3434
{
3535
let data = Uint8Array::from(input).to_vec();
36-
bincode::deserialize(&data).expect("can't deserialize a worker message")
36+
postcard::from_bytes(&data).expect("can't deserialize a worker message")
3737
}
3838
}

crates/worker/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! ### Overhead
1818
//!
1919
//! Gloo Workers use web workers. They incur a serialization overhead on the
20-
//! messages they send and receive. Bridges use [bincode](https://github.com/servo/bincode)
20+
//! messages they send and receive. Bridges use [postcard](https://github.com/jamesmunns/postcard)
2121
//! by default to communicate with workers, so the cost is substantially higher
2222
//! than just calling a function.
2323
//!
@@ -60,5 +60,5 @@ pub mod reactor;
6060
mod traits;
6161

6262
pub use actor::*;
63-
pub use codec::{Bincode, Codec};
63+
pub use codec::{Postcard, Codec};
6464
pub use traits::*;

crates/worker/src/oneshot/registrar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use serde::ser::Serialize;
66
use super::traits::Oneshot;
77
use super::worker::OneshotWorker;
88
use crate::actor::WorkerRegistrar;
9-
use crate::codec::{Bincode, Codec};
9+
use crate::codec::{Postcard, Codec};
1010
use crate::traits::Registrable;
1111

1212
/// A registrar for oneshot workers.
13-
pub struct OneshotRegistrar<T, CODEC = Bincode>
13+
pub struct OneshotRegistrar<T, CODEC = Postcard>
1414
where
1515
T: Oneshot + 'static,
1616
CODEC: Codec + 'static,

crates/worker/src/oneshot/spawner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use super::bridge::OneshotBridge;
55
use super::traits::Oneshot;
66
use super::worker::OneshotWorker;
77
use crate::actor::WorkerSpawner;
8-
use crate::codec::{Bincode, Codec};
8+
use crate::codec::{Postcard, Codec};
99

1010
/// A spawner to create oneshot workers.
1111
#[derive(Debug, Default)]
12-
pub struct OneshotSpawner<N, CODEC = Bincode>
12+
pub struct OneshotSpawner<N, CODEC = Postcard>
1313
where
1414
N: Oneshot + 'static,
1515
CODEC: Codec,

crates/worker/src/reactor/registrar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use super::scope::ReactorScoped;
77
use super::traits::Reactor;
88
use super::worker::ReactorWorker;
99
use crate::actor::WorkerRegistrar;
10-
use crate::codec::{Bincode, Codec};
10+
use crate::codec::{Postcard, Codec};
1111
use crate::traits::Registrable;
1212

1313
/// A registrar for reactor workers.
14-
pub struct ReactorRegistrar<R, CODEC = Bincode>
14+
pub struct ReactorRegistrar<R, CODEC = Postcard>
1515
where
1616
R: Reactor + 'static,
1717
CODEC: Codec + 'static,

0 commit comments

Comments
 (0)