Skip to content

Commit 66581ef

Browse files
committed
Bincode codec without serde
1 parent e76b565 commit 66581ef

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
44
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

6+
## [0.3.1] - 2025-04-19
7+
8+
### New Codecs
9+
10+
- Add a `bincode` feature to encode with `bincode v2` without serde and native `bincode::Encode` and `bincode::Decode` derive macros (thanks to @zakstucke)
11+
612
## [0.3.0] - 2025-01-09
713

814
### Breaking Changes

Cargo.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "codee"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition = "2021"
55
authors = ["Marc-Stefan Cassola"]
66
categories = ["encoding"]
@@ -13,7 +13,6 @@ repository = "https://github.com/Synphonyte/codee"
1313

1414
[dependencies]
1515
base64 = { version = "0.22", optional = true }
16-
bincode = { version = "1", optional = true }
1716
js-sys = { version = "0.3", optional = true }
1817
miniserde = { version = "0.1", optional = true }
1918
prost = { version = "0.13", optional = true }
@@ -26,11 +25,18 @@ serde-wasm-bindgen = { version = "0.6", optional = true }
2625
thiserror = "2.0"
2726
wasm-bindgen = { version = "0.2", optional = true }
2827

28+
# Optional bincode v1 for bincode_serde feature
29+
bincode_v1 = { package = "bincode", version = "1", optional = true }
30+
31+
# Optional bincode v2 for bincode feature
32+
bincode = { package = "bincode", version = "2.0.1", optional = true }
33+
2934
[features]
3035
prost = ["dep:prost"]
3136
json_serde = ["dep:serde_json", "dep:serde"]
3237
msgpack_serde = ["dep:rmp-serde", "dep:serde"]
33-
bincode_serde = ["dep:bincode", "dep:serde"]
38+
bincode = ["dep:bincode"]
39+
bincode_serde = ["dep:bincode_v1", "dep:serde"]
3440
serde_lite = ["dep:serde-lite"]
3541
json_serde_wasm = ["dep:serde", "dep:serde_json", "dep:js-sys", "dep:serde-wasm-bindgen", "dep:wasm-bindgen"]
3642

src/binary/bincode.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/// A codec that relies on `bincode` to encode data.
2+
///
3+
/// This does not use serde but the bincode derive macros `bincode::Encode` and `bincode::Decode` instead.
4+
/// bincode recommends not using `serde` because of [known issues](https://docs.rs/bincode/latest/bincode/serde/index.html#known-issues).
5+
///
6+
/// This is only available with the **`bincode` feature** feature enabled.
7+
///
8+
/// If you want to use `serde` with bincode, please use the `bincode_serde` feature instead.
9+
pub struct BincodeCodec;
10+
11+
impl<T: bincode::Encode> crate::Encoder<T> for BincodeCodec {
12+
type Error = bincode::error::EncodeError;
13+
type Encoded = Vec<u8>;
14+
15+
fn encode(val: &T) -> Result<Self::Encoded, Self::Error> {
16+
bincode::encode_to_vec(val, bincode::config::standard())
17+
}
18+
}
19+
20+
impl<T: for<'a> bincode::de::BorrowDecode<'a, ()>> crate::Decoder<T> for BincodeCodec {
21+
type Error = bincode::error::DecodeError;
22+
type Encoded = [u8];
23+
24+
fn decode(val: &Self::Encoded) -> Result<T, Self::Error> {
25+
let (data, _bytes_read) =
26+
bincode::borrow_decode_from_slice(val, bincode::config::standard())?;
27+
Ok(data)
28+
}
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use crate::{Decoder, Encoder};
34+
35+
use super::*;
36+
37+
#[test]
38+
fn test_bincode_codec() {
39+
#[derive(Clone, Debug, PartialEq, bincode::Encode, bincode::Decode)]
40+
struct Test {
41+
s: String,
42+
i: i32,
43+
}
44+
let t = Test {
45+
s: String::from("party time 🎉"),
46+
i: 42,
47+
};
48+
let enc = BincodeCodec::encode(&t).unwrap();
49+
let dec: Test = BincodeCodec::decode(&enc).unwrap();
50+
assert_eq!(dec, t);
51+
}
52+
}

src/binary/bincode_serde.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{Decoder, Encoder};
2+
use bincode_v1 as bincode;
23

3-
/// A codec that relies on `bincode` adn `serde` to encode data in the bincode format.
4+
/// A codec that relies on `bincode` and `serde` to encode data in the bincode format.
45
///
56
/// This is only available with the **`bincode_serde` feature** enabled.
67
pub struct BincodeSerdeCodec;

src/binary/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(feature = "bincode")]
2+
mod bincode;
13
#[cfg(feature = "bincode_serde")]
24
mod bincode_serde;
35
mod from_to_bytes;
@@ -8,6 +10,8 @@ mod prost;
810
#[cfg(feature = "rkyv")]
911
mod rkyv;
1012

13+
#[cfg(feature = "bincode")]
14+
pub use bincode::*;
1115
#[cfg(feature = "bincode_serde")]
1216
pub use bincode_serde::*;
1317
#[allow(unused_imports)]

0 commit comments

Comments
 (0)