Skip to content

Commit

Permalink
Fix/issue 500 (#503)
Browse files Browse the repository at this point in the history
* Fixed an issue where serde would decode a sequence with an u32 instead of a usize

* Added serde to the `the_same` test for roundtrips, fixed several issues.

* Fixed backwards compatibility for result, ipv4addr and ipv6addr

* Processed feedback
  • Loading branch information
VictorKoenders authored Feb 7, 2022
1 parent 3127ff5 commit 75a2e0b
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 43 deletions.
3 changes: 3 additions & 0 deletions compatibility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use ::rand::Rng;
use bincode_1::Options;

mod misc;
mod rand;
mod sway;

Expand All @@ -20,6 +21,8 @@ where
// This is what bincode 1 serializes to. This will be our comparison value.
let encoded = bincode_1_options.serialize(t).unwrap();

println!("Encoded {:?} as {:?}", t, encoded);

// Test bincode 2 encode
let bincode_2_output = bincode_2::encode_to_vec(t, bincode_2_config).unwrap();
assert_eq!(encoded, bincode_2_output, "{:?} serializes differently", t);
Expand Down
10 changes: 10 additions & 0 deletions compatibility/src/misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[test]
fn test() {
super::test_same((1,));
super::test_same(Option::<u32>::Some(5));
super::test_same(Option::<u32>::None);
super::test_same(Result::<u32, u8>::Ok(5));
super::test_same(Result::<u32, u8>::Err(5));
super::test_same(std::net::Ipv4Addr::LOCALHOST);
super::test_same(std::net::Ipv6Addr::LOCALHOST);
}
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ impl<E, I, A, L> Configuration<E, I, A, L> {
}

/// Skip writing the length of fixed size arrays (`[u8; N]`) before writing the array
///
/// **NOTE:** This is not supported if you're using the `bincode::serde::*` functions, the `#[bincode(with_serde)]` attribute, or the `Compat` struct.
pub const fn skip_fixed_array_length(self) -> Configuration<E, I, SkipFixedArrayLength, L> {
generate()
}
Expand Down
2 changes: 1 addition & 1 deletion src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ where
U: Decode,
{
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
let is_ok = u8::decode(decoder)?;
let is_ok = u32::decode(decoder)?;
match is_ok {
0 => {
let t = T::decode(decoder)?;
Expand Down
4 changes: 2 additions & 2 deletions src/enc/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ where
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
match self {
Ok(val) => {
0u8.encode(encoder)?;
0u32.encode(encoder)?;
val.encode(encoder)
}
Err(err) => {
1u8.encode(encoder)?;
1u32.encode(encoder)?;
err.encode(encoder)
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/features/impl_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,25 +268,29 @@ impl Decode for IpAddr {

impl Encode for Ipv4Addr {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
self.octets().encode(encoder)
encoder.writer().write(&self.octets())
}
}

impl Decode for Ipv4Addr {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
Ok(Self::from(<[u8; 4]>::decode(decoder)?))
let mut buff = [0u8; 4];
decoder.reader().read(&mut buff)?;
Ok(Self::from(buff))
}
}

impl Encode for Ipv6Addr {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
self.octets().encode(encoder)
encoder.writer().write(&self.octets())
}
}

impl Decode for Ipv6Addr {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
Ok(Self::from(<[u8; 16]>::decode(decoder)?))
let mut buff = [0u8; 16];
decoder.reader().read(&mut buff)?;
Ok(Self::from(buff))
}
}

Expand Down
29 changes: 27 additions & 2 deletions src/features/serde/de_borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ where
T: Deserialize<'de>,
C: Config,
{
if C::SKIP_FIXED_ARRAY_LENGTH {
return Err(SerdeDecodeError::SkipFixedArrayLengthNotSupported.into());
}
let reader = crate::de::read::SliceReader::new(slice);
let mut decoder = crate::de::DecoderImpl::new(reader, config);
let serde_decoder = SerdeDecoder {
Expand Down Expand Up @@ -72,6 +75,15 @@ impl<'a, 'de, DE: BorrowDecoder<'de>> Deserializer<'de> for SerdeDecoder<'a, 'de
visitor.visit_i64(Decode::decode(&mut self.de)?)
}

serde_incl::serde_if_integer128! {
fn deserialize_i128<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde_incl::de::Visitor<'de>,
{
visitor.visit_i128(Decode::decode(&mut self.de)?)
}
}

fn deserialize_u8<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde_incl::de::Visitor<'de>,
Expand Down Expand Up @@ -100,6 +112,15 @@ impl<'a, 'de, DE: BorrowDecoder<'de>> Deserializer<'de> for SerdeDecoder<'a, 'de
visitor.visit_u64(Decode::decode(&mut self.de)?)
}

serde_incl::serde_if_integer128! {
fn deserialize_u128<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde_incl::de::Visitor<'de>,
{
visitor.visit_u128(Decode::decode(&mut self.de)?)
}
}

fn deserialize_f32<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde_incl::de::Visitor<'de>,
Expand Down Expand Up @@ -214,8 +235,8 @@ impl<'a, 'de, DE: BorrowDecoder<'de>> Deserializer<'de> for SerdeDecoder<'a, 'de
where
V: serde_incl::de::Visitor<'de>,
{
let len = u32::decode(&mut self.de)?;
self.deserialize_tuple(len as usize, visitor)
let len = usize::decode(&mut self.de)?;
self.deserialize_tuple(len, visitor)
}

fn deserialize_tuple<V>(mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
Expand Down Expand Up @@ -367,6 +388,10 @@ impl<'a, 'de, DE: BorrowDecoder<'de>> Deserializer<'de> for SerdeDecoder<'a, 'de
{
Err(SerdeDecodeError::IgnoredAnyNotSupported.into())
}

fn is_human_readable(&self) -> bool {
false
}
}

impl<'de, 'a, DE: BorrowDecoder<'de>> EnumAccess<'de> for SerdeDecoder<'a, 'de, DE> {
Expand Down
35 changes: 33 additions & 2 deletions src/features/serde/de_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ where
T: DeserializeOwned,
C: Config,
{
if C::SKIP_FIXED_ARRAY_LENGTH {
return Err(SerdeDecodeError::SkipFixedArrayLengthNotSupported.into());
}
let reader = crate::de::read::SliceReader::new(slice);
let mut decoder = crate::de::DecoderImpl::new(reader, config);
let serde_decoder = SerdeDecoder { de: &mut decoder };
Expand All @@ -31,6 +34,9 @@ pub fn decode_from_std_read<D: DeserializeOwned, C: Config, R: std::io::Read>(
src: &mut R,
config: C,
) -> Result<D, DecodeError> {
if C::SKIP_FIXED_ARRAY_LENGTH {
return Err(SerdeDecodeError::SkipFixedArrayLengthNotSupported.into());
}
let reader = crate::IoReader::new(src);
let mut decoder = crate::de::DecoderImpl::new(reader, config);
let serde_decoder = SerdeDecoder { de: &mut decoder };
Expand All @@ -46,6 +52,9 @@ pub fn decode_from_reader<D: DeserializeOwned, R: Reader, C: Config>(
reader: R,
config: C,
) -> Result<D, DecodeError> {
if C::SKIP_FIXED_ARRAY_LENGTH {
return Err(SerdeDecodeError::SkipFixedArrayLengthNotSupported.into());
}
let mut decoder = crate::de::DecoderImpl::<_, C>::new(reader, config);
let serde_decoder = SerdeDecoder { de: &mut decoder };
D::deserialize(serde_decoder)
Expand Down Expand Up @@ -100,6 +109,15 @@ impl<'a, 'de, DE: Decoder> Deserializer<'de> for SerdeDecoder<'a, DE> {
visitor.visit_i64(Decode::decode(&mut self.de)?)
}

serde_incl::serde_if_integer128! {
fn deserialize_i128<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde_incl::de::Visitor<'de>,
{
visitor.visit_i128(Decode::decode(&mut self.de)?)
}
}

fn deserialize_u8<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde_incl::de::Visitor<'de>,
Expand Down Expand Up @@ -128,6 +146,15 @@ impl<'a, 'de, DE: Decoder> Deserializer<'de> for SerdeDecoder<'a, DE> {
visitor.visit_u64(Decode::decode(&mut self.de)?)
}

serde_incl::serde_if_integer128! {
fn deserialize_u128<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde_incl::de::Visitor<'de>,
{
visitor.visit_u128(Decode::decode(&mut self.de)?)
}
}

fn deserialize_f32<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde_incl::de::Visitor<'de>,
Expand Down Expand Up @@ -257,8 +284,8 @@ impl<'a, 'de, DE: Decoder> Deserializer<'de> for SerdeDecoder<'a, DE> {
where
V: serde_incl::de::Visitor<'de>,
{
let len = u32::decode(&mut self.de)?;
self.deserialize_tuple(len as usize, visitor)
let len = usize::decode(&mut self.de)?;
self.deserialize_tuple(len, visitor)
}

fn deserialize_tuple<V>(mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
Expand Down Expand Up @@ -407,6 +434,10 @@ impl<'a, 'de, DE: Decoder> Deserializer<'de> for SerdeDecoder<'a, DE> {
{
Err(SerdeDecodeError::IgnoredAnyNotSupported.into())
}

fn is_human_readable(&self) -> bool {
false
}
}

impl<'de, 'a, DE: Decoder> EnumAccess<'de> for SerdeDecoder<'a, DE> {
Expand Down
6 changes: 6 additions & 0 deletions src/features/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub enum DecodeError {
/// Serde tried decoding a borrowed value from an owned reader. Use `serde_decode_borrowed_from_*` instead
CannotBorrowOwnedData,

/// Serde does not support skipping fixed array lengths
SkipFixedArrayLengthNotSupported,

/// Could not allocate data like `String` and `Vec<u8>`
#[cfg(not(feature = "alloc"))]
CannotAllocate,
Expand Down Expand Up @@ -136,6 +139,9 @@ pub enum EncodeError {
/// Serde provided bincode with a sequence without a length, which is not supported in bincode
SequenceMustHaveLength,

/// Serde does not support skipping fixed array lengths
SkipFixedArrayLengthNotSupported,

/// [Serializer::collect_str] got called but bincode was unable to allocate memory.
#[cfg(not(feature = "alloc"))]
CannotCollectStr,
Expand Down
31 changes: 29 additions & 2 deletions src/features/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ where
T: Serialize,
C: Config,
{
if C::SKIP_FIXED_ARRAY_LENGTH {
return Err(SerdeEncodeError::SkipFixedArrayLengthNotSupported.into());
}
let mut encoder = crate::enc::EncoderImpl::new(crate::VecWriter::default(), config);
let serializer = SerdeEncoder { enc: &mut encoder };
t.serialize(serializer)?;
Expand All @@ -28,6 +31,9 @@ where
T: Serialize,
C: Config,
{
if C::SKIP_FIXED_ARRAY_LENGTH {
return Err(SerdeEncodeError::SkipFixedArrayLengthNotSupported.into());
}
let mut encoder =
crate::enc::EncoderImpl::new(crate::enc::write::SliceWriter::new(slice), config);
let serializer = SerdeEncoder { enc: &mut encoder };
Expand All @@ -45,6 +51,9 @@ pub fn encode_into_writer<E: Serialize, W: Writer, C: Config>(
writer: W,
config: C,
) -> Result<(), EncodeError> {
if C::SKIP_FIXED_ARRAY_LENGTH {
return Err(SerdeEncodeError::SkipFixedArrayLengthNotSupported.into());
}
let mut encoder = crate::enc::EncoderImpl::<_, C>::new(writer, config);
let serializer = SerdeEncoder { enc: &mut encoder };
val.serialize(serializer)?;
Expand All @@ -62,6 +71,9 @@ pub fn encode_into_std_write<E: Serialize, C: Config, W: std::io::Write>(
dst: &mut W,
config: C,
) -> Result<usize, EncodeError> {
if C::SKIP_FIXED_ARRAY_LENGTH {
return Err(SerdeEncodeError::SkipFixedArrayLengthNotSupported.into());
}
let writer = crate::IoWriter::new(dst);
let mut encoder = crate::enc::EncoderImpl::<_, C>::new(writer, config);
let serializer = SerdeEncoder { enc: &mut encoder };
Expand Down Expand Up @@ -109,6 +121,12 @@ where
v.encode(self.enc)
}

serde_incl::serde_if_integer128! {
fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
v.encode(self.enc)
}
}

fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
v.encode(self.enc)
}
Expand All @@ -125,6 +143,12 @@ where
v.encode(self.enc)
}

serde_incl::serde_if_integer128! {
fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
v.encode(self.enc)
}
}

fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
v.encode(self.enc)
}
Expand Down Expand Up @@ -205,8 +229,7 @@ where
Ok(Compound { enc: self.enc })
}

fn serialize_tuple(mut self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
len.encode(&mut self.enc)?;
fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
Ok(self)
}

Expand Down Expand Up @@ -262,6 +285,10 @@ where
{
Err(SerdeEncodeError::CannotCollectStr.into())
}

fn is_human_readable(&self) -> bool {
false
}
}

type Compound<'a, ENC> = SerdeEncoder<'a, ENC>;
Expand Down
8 changes: 6 additions & 2 deletions tests/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ mod utils;

use alloc::borrow::Cow;
use alloc::collections::*;
#[cfg(not(feature = "serde"))]
use alloc::rc::Rc;
#[cfg(feature = "atomic")]
#[cfg(all(feature = "atomic", not(feature = "serde")))]
use alloc::sync::Arc;
use utils::{the_same, the_same_with_comparer};

Expand Down Expand Up @@ -58,8 +59,11 @@ fn test_alloc_commons() {
the_same(Box::<[u32]>::from(vec![1, 2, 3, 4, 5]));
the_same(Cow::<u32>::Owned(5));
the_same(Cow::<u32>::Borrowed(&5));
// Serde doesn't support Rc<u32>
#[cfg(not(feature = "serde"))]
the_same(Rc::<u32>::new(5));
#[cfg(feature = "atomic")]
// serde doesn't support Arc<u32>
#[cfg(all(feature = "atomic", not(feature = "serde")))]
the_same(Arc::<u32>::new(5));
the_same_with_comparer(
{
Expand Down
Loading

0 comments on commit 75a2e0b

Please sign in to comment.