Skip to content

Update to bincode v2 #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ rocket = { default-features = false, optional = true, version = "0.5.0-rc.3" }
serde = { default-features = false, optional = true, version = "1.0" }
serde_json = { default-features = false, optional = true, version = "1.0" }
tokio-postgres = { default-features = false, optional = true, version = "0.7" }
bincode = { default-features = false, features = ["derive"], optional = true, version = "2.0.1" }

[dev-dependencies]
bincode = { default-features = false, version = "1.0" }
bincode = { default-features = false, version = "2.0.1", features = ["derive", "alloc", "serde"] }
bytes = { default-features = false, version = "1.0" }
criterion = { default-features = false, version = "0.5" }
csv = "1"
Expand Down Expand Up @@ -85,6 +86,7 @@ serde-with-float = ["serde"]
serde-with-str = ["serde"]
std = ["arrayvec/std", "borsh?/std", "bytes?/std", "rand?/std", "rkyv?/std", "serde?/std", "serde_json?/std"]
tokio-pg = ["db-tokio-postgres"] # Backwards compatability
bincode = ["dep:bincode"]

[[bench]]
harness = false
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ assert_eq!(total, dec!(27.26));

**Behavior / Functionality**

* bincode
* [borsh](#borsh)
* [c-repr](#c-repr)
* [legacy-ops](#legacy-ops)
Expand All @@ -123,6 +124,10 @@ assert_eq!(total, dec!(27.26));
* [serde-with-str](#serde-with-str)
* [serde-with-arbitrary-precision](#serde-with-arbitrary-precision)

### `bincode`

Derives `bincode::Encode` and `bincode::Decode` for `Decimal`.

### `borsh`

Enables [Borsh](https://borsh.io/) serialization for `Decimal`.
Expand Down
1 change: 1 addition & 0 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub struct UnpackedDecimal {
archive_attr(derive(Clone, Copy, Debug))
)]
#[cfg_attr(feature = "rkyv-safe", archive(check_bytes))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
pub struct Decimal {
// Bits 0-15: unused
// Bits 16-23: Contains "e", a value between 0-28 that indicates the scale
Expand Down
50 changes: 33 additions & 17 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,10 @@ mod test {
#[test]
#[cfg(all(feature = "serde-str", not(feature = "serde-float")))]
fn bincode_serialization_not_float() {
use bincode::{deserialize, serialize};
use bincode::{
config,
serde::{decode_from_slice, encode_to_vec},
};

let data = [
"0",
Expand All @@ -675,17 +678,20 @@ mod test {
];
for &raw in data.iter() {
let value = Decimal::from_str(raw).unwrap();
let encoded = serialize(&value).unwrap();
let decoded: Decimal = deserialize(&encoded[..]).unwrap();
let encoded = encode_to_vec(&value, config::standard()).unwrap();
let decoded: Decimal = decode_from_slice(&encoded[..], config::standard()).unwrap().0;
assert_eq!(value, decoded);
assert_eq!(8usize + raw.len(), encoded.len());
assert_eq!(1usize + raw.len(), encoded.len());
Copy link
Author

@intendednull intendednull Apr 6, 2025

Choose a reason for hiding this comment

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

Not sure this is correct. I checked the str serialization path is running, but I'm fuzzy with the internal representation. Not sure why it would be smaller tbh

}
}

#[test]
#[cfg(all(feature = "serde-str", feature = "serde-float"))]
fn bincode_serialization_serde_float() {
use bincode::{deserialize, serialize};
use bincode::{
config,
serde::{decode_from_slice, encode_to_vec},
};

let data = [
("0", "0"),
Expand All @@ -698,8 +704,8 @@ mod test {
for &(value, expected) in data.iter() {
let value = Decimal::from_str(value).unwrap();
let expected = Decimal::from_str(expected).unwrap();
let encoded = serialize(&value).unwrap();
let decoded: Decimal = deserialize(&encoded[..]).unwrap();
let encoded = encode_to_vec(&value, config::standard()).unwrap();
let decoded: Decimal = decode_from_slice(&encoded[..], config::standard()).unwrap().0;
assert_eq!(expected, decoded);
assert_eq!(8usize, encoded.len());
}
Expand All @@ -708,6 +714,10 @@ mod test {
#[test]
#[cfg(all(feature = "serde-str", not(feature = "serde-float")))]
fn bincode_nested_serialization() {
use bincode::{
config,
serde::{decode_from_slice, encode_to_vec},
};
// Issue #361
#[derive(Deserialize, Serialize, Debug)]
pub struct Foo {
Expand All @@ -717,8 +727,8 @@ mod test {
let s = Foo {
value: Decimal::new(-1, 3).round_dp(0),
};
let ser = bincode::serialize(&s).unwrap();
let des: Foo = bincode::deserialize(&ser).unwrap();
let ser = encode_to_vec(&s, config::standard()).unwrap();
let des: Foo = decode_from_slice(&ser, config::standard()).unwrap().0;
assert_eq!(des.value, s.value);
}

Expand Down Expand Up @@ -783,7 +793,10 @@ mod test {
#[test]
#[cfg(feature = "serde-with-str")]
fn with_str_bincode() {
use bincode::{deserialize, serialize};
use bincode::{
config,
serde::{decode_from_slice, encode_to_vec},
};

#[derive(Serialize, Deserialize)]
struct BincodeExample {
Expand All @@ -805,16 +818,19 @@ mod test {
let expected = Decimal::from_str(expected).unwrap();
let input = BincodeExample { value };

let encoded = serialize(&input).unwrap();
let decoded: BincodeExample = deserialize(&encoded[..]).unwrap();
let encoded = encode_to_vec(&input, config::standard()).unwrap();
let decoded: BincodeExample = decode_from_slice(&encoded[..], config::standard()).unwrap().0;
assert_eq!(expected, decoded.value);
}
}

#[test]
#[cfg(feature = "serde-with-str")]
fn with_str_bincode_optional() {
use bincode::{deserialize, serialize};
use bincode::{
config,
serde::{decode_from_slice, encode_to_vec},
};

#[derive(Serialize, Deserialize)]
struct BincodeExample {
Expand All @@ -825,14 +841,14 @@ mod test {
// Some(value)
let value = Some(Decimal::new(1234, 3));
let input = BincodeExample { value };
let encoded = serialize(&input).unwrap();
let decoded: BincodeExample = deserialize(&encoded[..]).unwrap();
let encoded = encode_to_vec(&input, config::standard()).unwrap();
let decoded: BincodeExample = decode_from_slice(&encoded[..], config::standard()).unwrap().0;
assert_eq!(value, decoded.value, "Some(value)");

// None
let input = BincodeExample { value: None };
let encoded = serialize(&input).unwrap();
let decoded: BincodeExample = deserialize(&encoded[..]).unwrap();
let encoded = encode_to_vec(&input, config::standard()).unwrap();
let decoded: BincodeExample = decode_from_slice(&encoded[..], config::standard()).unwrap().0;
assert_eq!(None, decoded.value, "None");
}

Expand Down