Added an option to encode a fixed array length or to skip it

This commit is contained in:
Victor Koenders 2021-10-14 21:31:47 +02:00
parent 19fed15463
commit 960b6066cd
5 changed files with 415 additions and 287 deletions

View File

@ -12,7 +12,10 @@
//! .with_little_endian()
//! // pick one of:
//! .with_variable_int_encoding()
//! .with_fixed_int_encoding();
//! .with_fixed_int_encoding()
//! // pick one of:
//! .skip_fixed_array_length()
//! .write_fixed_array_length();
//! ```
//!
//! See [Config] for more information on the configuration options.
@ -26,12 +29,15 @@ 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
pub trait Config: InternalConfig {
/// Makes bincode encode all integer types in big endian.
fn with_big_endian(self) -> BigEndian<Self> {
@ -97,6 +103,16 @@ pub trait Config: InternalConfig {
fn with_fixed_int_encoding(self) -> Fixint<Self> {
Fixint { _pd: PhantomData }
}
/// Skip writing the length of fixed size arrays (`[u8; N]`) before writing the array
fn skip_fixed_array_length(self) -> SkipFixedArrayLength<Self> {
SkipFixedArrayLength { _pd: PhantomData }
}
/// Write the length of fixed size arrays (`[u8; N]`) before writing the array
fn write_fixed_array_length(self) -> WriteFixedArrayLength<Self> {
WriteFixedArrayLength { _pd: PhantomData }
}
}
impl<T: InternalConfig> Config for T {}
@ -104,6 +120,7 @@ impl<T: InternalConfig> Config for T {}
/// The default config. By default this will be:
/// - Little endian
/// - Variable int encoding
/// - Skip fixed array length
#[derive(Copy, Clone)]
pub struct Default;
@ -112,6 +129,7 @@ impl InternalConfig for Default {
const INT_ENCODING: IntEncoding = IntEncoding::Variable;
const LIMIT: Option<u64> = None;
const ALLOW_TRAILING: bool = true;
const SKIP_FIXED_ARRAY_LENGTH: bool = true;
}
#[doc(hidden)]
@ -125,6 +143,7 @@ impl<C: InternalConfig> InternalConfig for BigEndian<C> {
const INT_ENCODING: IntEncoding = C::INT_ENCODING;
const LIMIT: Option<u64> = C::LIMIT;
const ALLOW_TRAILING: bool = C::ALLOW_TRAILING;
const SKIP_FIXED_ARRAY_LENGTH: bool = C::SKIP_FIXED_ARRAY_LENGTH;
}
#[doc(hidden)]
@ -138,6 +157,7 @@ impl<C: InternalConfig> InternalConfig for LittleEndian<C> {
const INT_ENCODING: IntEncoding = C::INT_ENCODING;
const LIMIT: Option<u64> = C::LIMIT;
const ALLOW_TRAILING: bool = C::ALLOW_TRAILING;
const SKIP_FIXED_ARRAY_LENGTH: bool = C::SKIP_FIXED_ARRAY_LENGTH;
}
#[doc(hidden)]
@ -151,6 +171,7 @@ impl<C: InternalConfig> InternalConfig for Fixint<C> {
const INT_ENCODING: IntEncoding = IntEncoding::Fixed;
const LIMIT: Option<u64> = C::LIMIT;
const ALLOW_TRAILING: bool = C::ALLOW_TRAILING;
const SKIP_FIXED_ARRAY_LENGTH: bool = C::SKIP_FIXED_ARRAY_LENGTH;
}
#[doc(hidden)]
@ -164,6 +185,35 @@ impl<C: InternalConfig> InternalConfig for Varint<C> {
const INT_ENCODING: IntEncoding = IntEncoding::Variable;
const LIMIT: Option<u64> = C::LIMIT;
const ALLOW_TRAILING: bool = C::ALLOW_TRAILING;
const SKIP_FIXED_ARRAY_LENGTH: bool = C::SKIP_FIXED_ARRAY_LENGTH;
}
#[doc(hidden)]
#[derive(Copy, Clone)]
pub struct SkipFixedArrayLength<C: Config> {
_pd: PhantomData<C>,
}
impl<C: InternalConfig> InternalConfig for SkipFixedArrayLength<C> {
const ENDIAN: Endian = C::ENDIAN;
const INT_ENCODING: IntEncoding = C::INT_ENCODING;
const LIMIT: Option<u64> = C::LIMIT;
const ALLOW_TRAILING: bool = C::ALLOW_TRAILING;
const SKIP_FIXED_ARRAY_LENGTH: bool = true;
}
#[doc(hidden)]
#[derive(Copy, Clone)]
pub struct WriteFixedArrayLength<C: Config> {
_pd: PhantomData<C>,
}
impl<C: InternalConfig> InternalConfig for WriteFixedArrayLength<C> {
const ENDIAN: Endian = C::ENDIAN;
const INT_ENCODING: IntEncoding = C::INT_ENCODING;
const LIMIT: Option<u64> = C::LIMIT;
const ALLOW_TRAILING: bool = C::ALLOW_TRAILING;
const SKIP_FIXED_ARRAY_LENGTH: bool = false;
}
mod internal {
@ -172,6 +222,7 @@ mod internal {
const INT_ENCODING: IntEncoding;
const LIMIT: Option<u64>;
const ALLOW_TRAILING: bool;
const SKIP_FIXED_ARRAY_LENGTH: bool;
}
#[derive(PartialEq, Eq)]
@ -194,5 +245,6 @@ mod internal {
const INT_ENCODING: IntEncoding = C::INT_ENCODING;
const LIMIT: Option<u64> = C::LIMIT;
const ALLOW_TRAILING: bool = C::ALLOW_TRAILING;
const SKIP_FIXED_ARRAY_LENGTH: bool = C::SKIP_FIXED_ARRAY_LENGTH;
}
}

View File

@ -228,6 +228,15 @@ impl<'a, 'de, R: Reader<'de>, C: Config> Decode for &'a mut Decoder<R, C> {
fn decode_array<const N: usize>(&mut self) -> Result<[u8; N], DecodeError> {
let mut array = [0u8; N];
if !C::SKIP_FIXED_ARRAY_LENGTH {
let length = self.decode_usize()?;
if length != N {
return Err(DecodeError::ArrayLengthMismatch {
found: length,
required: N,
});
}
}
self.reader.read(&mut array)?;
Ok(array)
}

View File

@ -195,6 +195,9 @@ impl<'a, W: Writer, C: Config> Encode for &'a mut Encoder<W, C> {
}
fn encode_array<const N: usize>(&mut self, val: [u8; N]) -> Result<(), EncodeError> {
if !C::SKIP_FIXED_ARRAY_LENGTH {
self.encode_usize(N)?;
}
self.writer.write(&val)
}

View File

@ -52,6 +52,14 @@ pub enum DecodeError {
/// The decoder tried to decode a `bool` and failed. The given value is what is actually read.
InvalidBooleanValue(u8),
/// The decoder tried to decode an array of length `required`, but the binary data contained an array of length `found`.
ArrayLengthMismatch {
/// The length of the array required by the rust type.
required: usize,
/// The length of the array found in the binary format.
found: usize,
},
}
/// Integer types. Used by [DecodeError]. These types have no purpose other than being shown in errors.

View File

@ -11,7 +11,7 @@ where
+ 'static,
C: Config,
{
let mut buffer = [0u8; 32];
let mut buffer = [0u8; 1024];
let len = bincode::encode_into_slice_with_config(element.clone(), &mut buffer, config).unwrap();
println!(
"{:?}: {:?} ({:?})",
@ -32,27 +32,62 @@ where
+ Clone
+ 'static,
{
// A matrix of each different config option possible
the_same_with_config(
element.clone(),
config::Default
.with_little_endian()
.with_fixed_int_encoding(),
.with_fixed_int_encoding()
.skip_fixed_array_length(),
);
the_same_with_config(
element.clone(),
config::Default.with_big_endian().with_fixed_int_encoding(),
config::Default
.with_big_endian()
.with_fixed_int_encoding()
.skip_fixed_array_length(),
);
the_same_with_config(
element.clone(),
config::Default
.with_little_endian()
.with_variable_int_encoding(),
.with_variable_int_encoding()
.skip_fixed_array_length(),
);
the_same_with_config(
element.clone(),
config::Default
.with_big_endian()
.with_variable_int_encoding()
.skip_fixed_array_length(),
);
the_same_with_config(
element.clone(),
config::Default
.with_little_endian()
.with_fixed_int_encoding()
.write_fixed_array_length(),
);
the_same_with_config(
element.clone(),
config::Default
.with_big_endian()
.with_fixed_int_encoding()
.write_fixed_array_length(),
);
the_same_with_config(
element.clone(),
config::Default
.with_little_endian()
.with_variable_int_encoding()
.write_fixed_array_length(),
);
the_same_with_config(
element,
config::Default
.with_big_endian()
.with_variable_int_encoding(),
.with_variable_int_encoding()
.write_fixed_array_length(),
);
}
@ -95,6 +130,27 @@ fn test_numbers() {
the_same((1u8, 2u8, 3u8, 4u8, 5u8, 6u8));
the_same((1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8));
the_same((1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8));
// arrays
#[rustfmt::skip]
the_same([
0, 1, 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,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144,
145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176,
177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192,
193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208,
209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224,
225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
]);
}
#[test]