Added support for Vec<T>, Box<T> and Box<[T]>

This commit is contained in:
Victor Koenders 2021-10-16 11:41:59 +02:00
parent db963bd705
commit 07f49e878e
5 changed files with 207 additions and 96 deletions

View File

@ -103,6 +103,27 @@ impl Encodeable for &'_ [u8] {
}
}
// BlockedTODO: https://github.com/rust-lang/rust/issues/37653
//
// We'll want to implement encoding for both &[u8] and &[T: Encodeable],
// but those implementations overlap because u8 also implements Encodeabl
//
// default impl Encodeable for &'_ [u8] {
// fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
// encoder.encode_slice(*self)
// }
// }
//
// impl<T: Encodeable> Encodeable for &'_ [T] {
// fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
// self.len().encode(&mut encoder)?;
// for item in self.iter() {
// item.encode(&mut encoder)?;
// }
// Ok(())
// }
// }
impl Encodeable for &'_ str {
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
encoder.encode_slice(self.as_bytes())

View File

@ -1,5 +1,11 @@
use crate::{config, enc, error, Config};
use alloc::vec::Vec;
use crate::{
config,
de::{Decodable, Decode},
enc::{self, Encode, Encodeable},
error::{DecodeError, EncodeError},
Config,
};
use alloc::{boxed::Box, vec::Vec};
#[derive(Default)]
struct VecWriter {
@ -7,14 +13,14 @@ struct VecWriter {
}
impl enc::write::Writer for VecWriter {
fn write(&mut self, bytes: &[u8]) -> Result<(), error::EncodeError> {
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
self.inner.extend_from_slice(bytes);
Ok(())
}
}
/// Encode the given value into a `Vec<u8>`.
pub fn encode_to_vec<E: enc::Encodeable>(val: E) -> Result<Vec<u8>, error::EncodeError> {
pub fn encode_to_vec<E: enc::Encodeable>(val: E) -> Result<Vec<u8>, EncodeError> {
encode_to_vec_with_config(val, config::Default)
}
@ -22,9 +28,78 @@ pub fn encode_to_vec<E: enc::Encodeable>(val: E) -> Result<Vec<u8>, error::Encod
pub fn encode_to_vec_with_config<E: enc::Encodeable, C: Config>(
val: E,
config: C,
) -> Result<Vec<u8>, error::EncodeError> {
) -> Result<Vec<u8>, EncodeError> {
let writer = VecWriter::default();
let mut encoder = enc::Encoder::<_, C>::new(writer, config);
val.encode(&mut encoder)?;
Ok(encoder.into_writer().inner)
}
impl<'de, T> Decodable for Vec<T>
where
T: Decodable,
{
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
let len = usize::decode(&mut decoder)?;
let mut vec = Vec::with_capacity(len);
for _ in 0..len {
vec.push(T::decode(&mut decoder)?);
}
Ok(vec)
}
}
impl<T> Encodeable for Vec<T>
where
T: Encodeable,
{
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
self.len().encode(&mut encoder)?;
for item in self.iter() {
item.encode(&mut encoder)?;
}
Ok(())
}
}
impl<'de, T> Decodable for Box<T>
where
T: Decodable,
{
fn decode<D: Decode>(decoder: D) -> Result<Self, DecodeError> {
let t = T::decode(decoder)?;
Ok(Box::new(t))
}
}
impl<T> Encodeable for Box<T>
where
T: Encodeable,
{
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
T::encode(self, encoder)
}
}
impl<'de, T> Decodable for Box<[T]>
where
T: Decodable,
{
fn decode<D: Decode>(decoder: D) -> Result<Self, DecodeError> {
let vec = Vec::decode(decoder)?;
Ok(vec.into_boxed_slice())
}
}
impl<T> Encodeable for Box<[T]>
where
T: Encodeable,
{
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
self.len().encode(&mut encoder)?;
for item in self.iter() {
item.encode(&mut encoder)?;
}
Ok(())
}
}

View File

@ -1,5 +1,9 @@
#![cfg(feature = "alloc")]
mod utils;
use utils::the_same;
struct Foo {
pub a: u32,
pub b: u32,
@ -34,3 +38,10 @@ fn test_vec() {
assert_eq!(foo.a, 5);
assert_eq!(foo.b, 10);
}
#[test]
fn test_alloc_commons() {
the_same::<Vec<u32>>(vec![1, 2, 3, 4, 5]);
the_same(Box::<u32>::new(5));
the_same(Box::<[u32]>::from(vec![1, 2, 3, 4, 5]));
}

View File

@ -1,95 +1,6 @@
use bincode::config::{self, Config};
use core::fmt::Debug;
mod utils;
fn the_same_with_config<V, C>(element: V, config: C)
where
V: bincode::enc::Encodeable
+ for<'de> bincode::de::Decodable
+ PartialEq
+ Debug
+ Clone
+ 'static,
C: Config,
{
let mut buffer = [0u8; 1024];
let len = bincode::encode_into_slice_with_config(element.clone(), &mut buffer, config).unwrap();
println!(
"{:?}: {:?} ({:?})",
element,
&buffer[..len],
core::any::type_name::<C>()
);
let decoded: V = bincode::decode_with_config(&mut buffer, config).unwrap();
assert_eq!(element, decoded);
}
fn the_same<V>(element: V)
where
V: bincode::enc::Encodeable
+ for<'de> bincode::de::Decodable
+ PartialEq
+ Debug
+ 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()
.skip_fixed_array_length(),
);
the_same_with_config(
element.clone(),
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()
.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()
.write_fixed_array_length(),
);
}
use utils::the_same;
#[test]
fn test_numbers() {

93
tests/utils.rs Normal file
View File

@ -0,0 +1,93 @@
use bincode::config::{self, Config};
use core::fmt::Debug;
fn the_same_with_config<V, C>(element: V, config: C)
where
V: bincode::enc::Encodeable
+ for<'de> bincode::de::Decodable
+ PartialEq
+ Debug
+ Clone
+ 'static,
C: Config,
{
let mut buffer = [0u8; 1024];
let len = bincode::encode_into_slice_with_config(element.clone(), &mut buffer, config).unwrap();
println!(
"{:?}: {:?} ({:?})",
element,
&buffer[..len],
core::any::type_name::<C>()
);
let decoded: V = bincode::decode_with_config(&mut buffer, config).unwrap();
assert_eq!(element, decoded);
}
pub fn the_same<V>(element: V)
where
V: bincode::enc::Encodeable
+ for<'de> bincode::de::Decodable
+ PartialEq
+ Debug
+ 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()
.skip_fixed_array_length(),
);
the_same_with_config(
element.clone(),
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()
.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()
.write_fixed_array_length(),
);
}