From b34ee9541d0054a839b67fa8e39a488e4ff4dc8b Mon Sep 17 00:00:00 2001 From: Trangar Date: Thu, 30 Mar 2023 15:09:33 +0200 Subject: [PATCH] Made arrays never encode their length (#625) * Made arrays with 32 elements or less never encode their length * Removed `write_fixed_array_length` and `skip_fixed_array_length` as this was based on incorrect assumptions on how serde and bincode 1 works --------- Co-authored-by: Victor Koenders --- compatibility/src/lib.rs | 12 ++-- compatibility/src/misc.rs | 77 +++++++++++++++++++++++++ docs/spec.md | 16 +----- src/config.rs | 94 ++++++------------------------- src/de/impls.rs | 25 +------- src/enc/impls.rs | 8 +-- src/features/impl_alloc.rs | 13 +++++ src/features/serde/de_borrowed.rs | 6 -- src/features/serde/de_owned.rs | 9 --- src/features/serde/mod.rs | 6 -- src/features/serde/ser.rs | 12 ---- tests/basic_types.rs | 17 ++---- tests/derive.rs | 2 +- tests/utils.rs | 55 +++++++----------- 14 files changed, 146 insertions(+), 206 deletions(-) diff --git a/compatibility/src/lib.rs b/compatibility/src/lib.rs index 96fde3f5..05d4dbe2 100644 --- a/compatibility/src/lib.rs +++ b/compatibility/src/lib.rs @@ -22,18 +22,22 @@ 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); + println!("Encoded {t:?} as {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); + assert_eq!( + encoded, + bincode_2_output, + "{t:?} serializes differently\nbincode 2 config {:?}", + core::any::type_name::(), + ); // Test bincode 2 serde serialize let bincode_2_serde_output = bincode_2::serde::encode_to_vec(t, bincode_2_config).unwrap(); assert_eq!( encoded, bincode_2_serde_output, - "{:?} serializes differently", - t + "{t:?} serializes differently" ); // Test bincode 1 deserialize diff --git a/compatibility/src/misc.rs b/compatibility/src/misc.rs index 2ab756d4..280b9060 100644 --- a/compatibility/src/misc.rs +++ b/compatibility/src/misc.rs @@ -10,6 +10,83 @@ fn test() { super::test_same(std::net::Ipv6Addr::LOCALHOST); } +#[test] +fn test_arrays() { + // serde is known to be weird with arrays + // Arrays of length 32 and less are encoded as tuples, but arrays 33 and up are encoded as slices + // we need to make sure we're compatible with this + super::test_same([0u8; 0]); + super::test_same([1u8; 1]); + super::test_same([1u8, 2]); + super::test_same([1u8, 2, 3]); + super::test_same([1u8, 2, 3, 4]); + super::test_same([1u8, 2, 3, 4, 5]); + super::test_same([1u8, 2, 3, 4, 5, 6]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7, 8]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7, 8, 9]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); + super::test_same([1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, + ]); + super::test_same([ + 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, + ]); +} + #[derive( bincode_2::Encode, bincode_2::Decode, serde::Serialize, serde::Deserialize, Debug, PartialEq, )] diff --git a/docs/spec.md b/docs/spec.md index bcf1fdc5..fa26e8b2 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -126,7 +126,7 @@ assert_eq!(encoded.as_slice(), &[ # Arrays -Array length is encoded based on the `.write_fixed_array_length` and `.skip_fixed_array_length()` config. When an array length is written, it will be encoded as a `u64`. +Array length is never encoded. Note that `&[T]` is encoded as a [Collection](#collections). @@ -135,15 +135,9 @@ Note that `&[T]` is encoded as a [Collection](#collections). let arr: [u8; 5] = [10, 20, 30, 40, 50]; let encoded = bincode::encode_to_vec(arr, bincode::config::legacy()).unwrap(); assert_eq!(encoded.as_slice(), &[ - 5, 0, 0, 0, 0, 0, 0, 0, // The length, as a u64 10, 20, 30, 40, 50, // the bytes ]); -let encoded = bincode::encode_to_vec(arr, bincode::config::legacy().skip_fixed_array_length()).unwrap(); -assert_eq!(encoded.as_slice(), &[ - // no length - 10, 20, 30, 40, 50, // the bytes -]); ``` This applies to any type `T` that implements `Encode`/`Decode` @@ -168,14 +162,6 @@ let arr: [Foo; 2] = [ let encoded = bincode::encode_to_vec(&arr, bincode::config::legacy()).unwrap(); assert_eq!(encoded.as_slice(), &[ - 2, 0, 0, 0, 0, 0, 0, 0, // Length of the array - 10, 20, // First Foo - 30, 40, // Second Foo -]); - -let encoded = bincode::encode_to_vec(&arr, bincode::config::legacy().skip_fixed_array_length()).unwrap(); -assert_eq!(encoded.as_slice(), &[ - // no length 10, 20, // First Foo 30, 40, // Second Foo ]); diff --git a/src/config.rs b/src/config.rs index bb3129fa..60b847fc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,10 +11,7 @@ //! .with_little_endian() //! // pick one of: //! .with_variable_int_encoding() -//! .with_fixed_int_encoding() -//! // pick one of: -//! .skip_fixed_array_length() -//! .write_fixed_array_length(); +//! .with_fixed_int_encoding(); //! ``` //! //! See [Configuration] for more information on the configuration options. @@ -29,20 +26,16 @@ use core::marker::PhantomData; /// /// - [with_little_endian] and [with_big_endian] /// - [with_fixed_int_encoding] and [with_variable_int_encoding] -/// - [skip_fixed_array_length] and [write_fixed_array_length] /// /// /// [with_little_endian]: #method.with_little_endian /// [with_big_endian]: #method.with_big_endian /// [with_fixed_int_encoding]: #method.with_fixed_int_encoding /// [with_variable_int_encoding]: #method.with_variable_int_encoding -/// [skip_fixed_array_length]: #method.skip_fixed_array_length -/// [write_fixed_array_length]: #method.write_fixed_array_length #[derive(Copy, Clone)] -pub struct Configuration { +pub struct Configuration { _e: PhantomData, _i: PhantomData, - _a: PhantomData, _l: PhantomData, } @@ -59,7 +52,6 @@ pub struct Configuration Configuration { generate() } @@ -67,34 +59,32 @@ pub const fn standard() -> Configuration { /// Creates the "legacy" default config. This is the default config that was present in bincode 1.0 /// - Little endian /// - Fixed int length encoding -/// - Write fixed array length -pub const fn legacy() -> Configuration { +pub const fn legacy() -> Configuration { generate() } -impl Default for Configuration { +impl Default for Configuration { fn default() -> Self { generate() } } -const fn generate() -> Configuration { +const fn generate() -> Configuration { Configuration { _e: PhantomData, _i: PhantomData, - _a: PhantomData, _l: PhantomData, } } -impl Configuration { +impl Configuration { /// Makes bincode encode all integer types in big endian. - pub const fn with_big_endian(self) -> Configuration { + pub const fn with_big_endian(self) -> Configuration { generate() } /// Makes bincode encode all integer types in little endian. - pub const fn with_little_endian(self) -> Configuration { + pub const fn with_little_endian(self) -> Configuration { generate() } @@ -155,7 +145,7 @@ impl Configuration { /// /// Note that u256 and the like are unsupported by this format; if and when they are added to the /// language, they may be supported via the extension point given by the 255 byte. - pub const fn with_variable_int_encoding(self) -> Configuration { + pub const fn with_variable_int_encoding(self) -> Configuration { generate() } @@ -164,51 +154,29 @@ impl Configuration { /// * Fixed size integers are encoded directly /// * Enum discriminants are encoded as u32 /// * Lengths and usize are encoded as u64 - pub const fn with_fixed_int_encoding(self) -> Configuration { - generate() - } - - /// 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 { - generate() - } - - /// Write the length of fixed size arrays (`[u8; N]`) before writing the array - pub const fn write_fixed_array_length(self) -> Configuration { + pub const fn with_fixed_int_encoding(self) -> Configuration { generate() } /// Sets the byte limit to `limit`. - pub const fn with_limit(self) -> Configuration> { + pub const fn with_limit(self) -> Configuration> { generate() } /// Clear the byte limit. - pub const fn with_no_limit(self) -> Configuration { + pub const fn with_no_limit(self) -> Configuration { generate() } } /// Indicates a type is valid for controlling the bincode configuration pub trait Config: - InternalEndianConfig - + InternalArrayLengthConfig - + InternalIntEncodingConfig - + InternalLimitConfig - + Copy - + Clone + InternalEndianConfig + InternalIntEncodingConfig + InternalLimitConfig + Copy + Clone { } impl Config for T where - T: InternalEndianConfig - + InternalArrayLengthConfig - + InternalIntEncodingConfig - + InternalLimitConfig - + Copy - + Clone + T: InternalEndianConfig + InternalIntEncodingConfig + InternalLimitConfig + Copy + Clone { } @@ -244,22 +212,6 @@ impl InternalIntEncodingConfig for Varint { const INT_ENCODING: IntEncoding = IntEncoding::Variable; } -/// Skip writing the length of fixed size arrays (`[u8; N]`) before writing the array. -#[derive(Copy, Clone)] -pub struct SkipFixedArrayLength {} - -impl InternalArrayLengthConfig for SkipFixedArrayLength { - const SKIP_FIXED_ARRAY_LENGTH: bool = true; -} - -/// Write the length of fixed size arrays (`[u8; N]`) before writing the array. -#[derive(Copy, Clone)] -pub struct WriteFixedArrayLength {} - -impl InternalArrayLengthConfig for WriteFixedArrayLength { - const SKIP_FIXED_ARRAY_LENGTH: bool = false; -} - /// Sets an unlimited byte limit. #[derive(Copy, Clone)] pub struct NoLimit {} @@ -281,7 +233,7 @@ mod internal { const ENDIAN: Endian; } - impl InternalEndianConfig for Configuration { + impl InternalEndianConfig for Configuration { const ENDIAN: Endian = E::ENDIAN; } @@ -295,9 +247,7 @@ mod internal { const INT_ENCODING: IntEncoding; } - impl InternalIntEncodingConfig - for Configuration - { + impl InternalIntEncodingConfig for Configuration { const INT_ENCODING: IntEncoding = I::INT_ENCODING; } @@ -307,21 +257,11 @@ mod internal { Variable, } - pub trait InternalArrayLengthConfig { - const SKIP_FIXED_ARRAY_LENGTH: bool; - } - - impl InternalArrayLengthConfig - for Configuration - { - const SKIP_FIXED_ARRAY_LENGTH: bool = A::SKIP_FIXED_ARRAY_LENGTH; - } - pub trait InternalLimitConfig { const LIMIT: Option; } - impl InternalLimitConfig for Configuration { + impl InternalLimitConfig for Configuration { const LIMIT: Option = L::LIMIT; } } diff --git a/src/de/impls.rs b/src/de/impls.rs index 8b592f57..b1c80b0b 100644 --- a/src/de/impls.rs +++ b/src/de/impls.rs @@ -3,10 +3,7 @@ use super::{ BorrowDecode, BorrowDecoder, Decode, Decoder, }; use crate::{ - config::{ - Endian, IntEncoding, InternalArrayLengthConfig, InternalEndianConfig, - InternalIntEncodingConfig, - }, + config::{Endian, IntEncoding, InternalEndianConfig, InternalIntEncodingConfig}, error::{DecodeError, IntegerType}, impl_borrow_decode, }; @@ -448,16 +445,6 @@ where T: Decode + Sized + 'static, { fn decode(decoder: &mut D) -> Result { - if !D::C::SKIP_FIXED_ARRAY_LENGTH { - let length = super::decode_slice_len(decoder)?; - if length != N { - return Err(DecodeError::ArrayLengthMismatch { - found: length, - required: N, - }); - } - } - decoder.claim_bytes_read(core::mem::size_of::<[T; N]>())?; // Optimize for `[u8; N]` @@ -489,16 +476,6 @@ where T: BorrowDecode<'de> + Sized + 'static, { fn borrow_decode>(decoder: &mut D) -> Result { - if !D::C::SKIP_FIXED_ARRAY_LENGTH { - let length = super::decode_slice_len(decoder)?; - if length != N { - return Err(DecodeError::ArrayLengthMismatch { - found: length, - required: N, - }); - } - } - decoder.claim_bytes_read(core::mem::size_of::<[T; N]>())?; // Optimize for `[u8; N]` diff --git a/src/enc/impls.rs b/src/enc/impls.rs index 4c8df9ec..2609eb62 100644 --- a/src/enc/impls.rs +++ b/src/enc/impls.rs @@ -1,9 +1,6 @@ use super::{write::Writer, Encode, Encoder}; use crate::{ - config::{ - Endian, IntEncoding, InternalArrayLengthConfig, InternalEndianConfig, - InternalIntEncodingConfig, - }, + config::{Endian, IntEncoding, InternalEndianConfig, InternalIntEncodingConfig}, error::EncodeError, }; use core::{ @@ -358,9 +355,6 @@ where T: Encode, { fn encode(&self, encoder: &mut E) -> Result<(), EncodeError> { - if !E::C::SKIP_FIXED_ARRAY_LENGTH { - super::encode_slice_len(encoder, N)?; - } for item in self.iter() { item.encode(encoder)?; } diff --git a/src/features/impl_alloc.rs b/src/features/impl_alloc.rs index 0ba11b0f..f307451f 100644 --- a/src/features/impl_alloc.rs +++ b/src/features/impl_alloc.rs @@ -444,6 +444,19 @@ where } } +#[test] +fn test_cow_round_trip() { + let start = Cow::Borrowed("Foo"); + let encoded = crate::encode_to_vec(&start, crate::config::standard()).unwrap(); + let (end, _) = + crate::borrow_decode_from_slice::, _>(&encoded, crate::config::standard()) + .unwrap(); + assert_eq!(start, end); + let (end, _) = + crate::decode_from_slice::, _>(&encoded, crate::config::standard()).unwrap(); + assert_eq!(start, end); +} + impl Decode for Rc where T: Decode, diff --git a/src/features/serde/de_borrowed.rs b/src/features/serde/de_borrowed.rs index 5488b098..45495f15 100644 --- a/src/features/serde/de_borrowed.rs +++ b/src/features/serde/de_borrowed.rs @@ -13,9 +13,6 @@ 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 { @@ -35,9 +32,6 @@ where T: DeserializeSeed<'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 { diff --git a/src/features/serde/de_owned.rs b/src/features/serde/de_owned.rs index 9a3ba780..87287712 100644 --- a/src/features/serde/de_owned.rs +++ b/src/features/serde/de_owned.rs @@ -16,9 +16,6 @@ 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 }; @@ -34,9 +31,6 @@ pub fn decode_from_std_read( src: &mut R, config: C, ) -> Result { - 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 }; @@ -52,9 +46,6 @@ pub fn decode_from_reader( reader: R, config: C, ) -> Result { - 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) diff --git a/src/features/serde/mod.rs b/src/features/serde/mod.rs index 04a50699..51b9b012 100644 --- a/src/features/serde/mod.rs +++ b/src/features/serde/mod.rs @@ -83,9 +83,6 @@ 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` #[cfg(not(feature = "alloc"))] CannotAllocate, @@ -133,9 +130,6 @@ 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, diff --git a/src/features/serde/ser.rs b/src/features/serde/ser.rs index feede35c..d3686602 100644 --- a/src/features/serde/ser.rs +++ b/src/features/serde/ser.rs @@ -16,9 +16,6 @@ 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)?; @@ -31,9 +28,6 @@ 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 }; @@ -51,9 +45,6 @@ pub fn encode_into_writer( 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)?; @@ -71,9 +62,6 @@ pub fn encode_into_std_write( dst: &mut W, config: C, ) -> Result { - 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 }; diff --git a/tests/basic_types.rs b/tests/basic_types.rs index b269f8e8..f63416e6 100644 --- a/tests/basic_types.rs +++ b/tests/basic_types.rs @@ -214,30 +214,25 @@ fn test_array() { let mut buffer = [0u8; 32]; let input: [u8; 10] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]; bincode::encode_into_slice(input, &mut buffer, bincode::config::standard()).unwrap(); - assert_eq!( - &buffer[..11], - &[10, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] - ); + assert_eq!(&buffer[..10], &[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]); let (output, len): ([u8; 10], usize) = bincode::decode_from_slice(&buffer[..11], bincode::config::standard()).unwrap(); assert_eq!(input, output); - assert_eq!(len, 11); + assert_eq!(len, 10); let mut buffer = [0u8; 32]; let input: [u8; 1] = [1]; let config = bincode::config::standard() - .write_fixed_array_length() .with_fixed_int_encoding() .with_little_endian(); let len = bincode::encode_into_slice(input, &mut buffer, config).unwrap(); - assert_eq!(len, 9); - assert_eq!(&buffer[..9], &[1, 0, 0, 0, 0, 0, 0, 0, 1]); + assert_eq!(len, 1); + assert_eq!(&buffer[..1], &[1]); - let (output, len): (&[u8], usize) = - bincode::borrow_decode_from_slice(&buffer[..9], config).unwrap(); + let (output, len): ([u8; 1], usize) = bincode::decode_from_slice(&buffer[..9], config).unwrap(); + assert_eq!(len, 1); assert_eq!(input, output); - assert_eq!(len, 9); } #[test] diff --git a/tests/derive.rs b/tests/derive.rs index 009312b4..07f6be2d 100644 --- a/tests/derive.rs +++ b/tests/derive.rs @@ -422,7 +422,7 @@ mod zoxide { #[test] fn test() { - let dirs = &[ + let dirs = vec![ Dir { path: Cow::Borrowed("Foo"), rank: 1.23, diff --git a/tests/utils.rs b/tests/utils.rs index c336bd5f..6c8392b8 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -37,38 +37,29 @@ where C: bincode::config::Config, CMP: Fn(&V, &V) -> bool, { - use bincode::error::EncodeError; - let mut buffer = [0u8; 2048]; let len = bincode::serde::encode_into_slice(element, &mut buffer, config); let decoded = bincode::serde::decode_from_slice(&buffer, config); - if !C::SKIP_FIXED_ARRAY_LENGTH { - let len = len.unwrap(); - let (decoded, decoded_len): (V, usize) = decoded.unwrap(); - println!( - "{:?} ({}): {:?} ({:?})", - element, - core::any::type_name::(), - &buffer[..len], - core::any::type_name::() - ); + let len = len.unwrap(); + let (decoded, decoded_len): (V, usize) = decoded.unwrap(); + println!( + "{:?} ({}): {:?} ({:?})", + element, + core::any::type_name::(), + &buffer[..len], + core::any::type_name::() + ); - assert!( - cmp(element, &decoded), - "Comparison failed\nDecoded: {:?}\nExpected: {:?}\nBytes: {:?}", - decoded, - element, - &buffer[..len], - ); - assert_eq!(len, decoded_len); - } else { - match len.unwrap_err() { - EncodeError::Serde(bincode::serde::EncodeError::SkipFixedArrayLengthNotSupported) => {} - err => panic!("Unexpected error: {:?}", err), - } - } + assert!( + cmp(element, &decoded), + "Comparison failed\nDecoded: {:?}\nExpected: {:?}\nBytes: {:?}", + decoded, + element, + &buffer[..len], + ); + assert_eq!(len, decoded_len); } pub fn the_same_with_comparer(element: V, cmp: CMP) @@ -81,32 +72,28 @@ where &element, bincode::config::standard() .with_little_endian() - .with_fixed_int_encoding() - .skip_fixed_array_length(), + .with_fixed_int_encoding(), &cmp, ); the_same_with_config( &element, bincode::config::standard() .with_big_endian() - .with_fixed_int_encoding() - .skip_fixed_array_length(), + .with_fixed_int_encoding(), &cmp, ); the_same_with_config( &element, bincode::config::standard() .with_little_endian() - .with_variable_int_encoding() - .skip_fixed_array_length(), + .with_variable_int_encoding(), &cmp, ); the_same_with_config( &element, bincode::config::standard() .with_big_endian() - .with_variable_int_encoding() - .skip_fixed_array_length(), + .with_variable_int_encoding(), &cmp, ); the_same_with_config(