Skip to content

Commit

Permalink
fix bincode serialization (#223)
Browse files Browse the repository at this point in the history
* fix bincode serialization

* fmt

* re-use visitor

* fix macro indention
  • Loading branch information
Wollac authored Aug 3, 2023
1 parent 4f7a72c commit 58e2259
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ alloy-rlp = { version = "0.3.0", default-features = false }
alloy-rlp-derive = { version = "0.3.0", default-features = false }
arbitrary = "1.3"
arrayvec = { version = "0.7", default-features = false }
bincode = "1.3"
bytes = { version = "1.4", default-features = false }
criterion = "0.5"
derive_arbitrary = "1.3"
Expand Down
1 change: 1 addition & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }

[dev-dependencies]
bincode.workspace = true
serde_json.workspace = true
serde = { workspace = true, features = ["derive"] }

Expand Down
22 changes: 21 additions & 1 deletion crates/primitives/src/bits/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,18 @@ impl<'de, const N: usize> Deserialize<'de> for FixedBytes<N> {
}
}

deserializer.deserialize_any(FixedVisitor::<N>)
if deserializer.is_human_readable() {
deserializer.deserialize_any(FixedVisitor::<N>)
} else {
deserializer.deserialize_bytes(FixedVisitor::<N>)
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use bincode as _;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct TestCase<const N: usize> {
Expand All @@ -82,6 +87,13 @@ mod tests {
let ser = serde_json::to_string(&bytes).unwrap();
assert_eq!(ser, "\"0x000000000123456789abcdef\"");
assert_eq!(serde_json::from_str::<FixedBytes<12>>(&ser).unwrap(), bytes);

let val = serde_json::to_value(&bytes).unwrap();
assert_eq!(val, serde_json::json! {"0x000000000123456789abcdef"});
assert_eq!(
serde_json::from_value::<FixedBytes<12>>(val).unwrap(),
bytes
);
}

#[test]
Expand All @@ -101,4 +113,12 @@ mod tests {
)
.contains("invalid length 5, expected exactly 4 bytes"),);
}

#[test]
fn test_bincode_roundtrip() {
let bytes = FixedBytes([0, 0, 0, 0, 1, 35, 69, 103, 137, 171, 205, 239]);

let bin = bincode::serialize(&bytes).unwrap();
assert_eq!(bincode::deserialize::<FixedBytes<12>>(&bin).unwrap(), bytes);
}
}
19 changes: 18 additions & 1 deletion crates/primitives/src/bytes/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ impl<'de> serde::Deserialize<'de> for Bytes {
}
}

deserializer.deserialize_any(BytesVisitor)
if deserializer.is_human_readable() {
deserializer.deserialize_any(BytesVisitor)
} else {
deserializer.deserialize_byte_buf(BytesVisitor)
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use bincode as _;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct TestCase {
Expand All @@ -72,6 +77,10 @@ mod tests {
let ser = serde_json::to_string(&bytes).unwrap();
assert_eq!(ser, "\"0x0123456789abcdef\"");
assert_eq!(serde_json::from_str::<Bytes>(&ser).unwrap(), bytes);

let val = serde_json::to_value(&bytes).unwrap();
assert_eq!(val, serde_json::json! {"0x0123456789abcdef"});
assert_eq!(serde_json::from_value::<Bytes>(val).unwrap(), bytes);
}

#[test]
Expand All @@ -85,4 +94,12 @@ mod tests {
Bytes::from(Vec::from([0, 1, 2, 3, 4]))
);
}

#[test]
fn test_bincode_roundtrip() {
let bytes = Bytes::from_static(&[1, 35, 69, 103, 137, 171, 205, 239]);

let bin = bincode::serialize(&bytes).unwrap();
assert_eq!(bincode::deserialize::<Bytes>(&bin).unwrap(), bytes);
}
}

0 comments on commit 58e2259

Please sign in to comment.