2nd implementation of the Config Trait (#214)

* add option and config traits

* thread options everywhere

* add WithOtherLimit, WithOtherEndian, and update internal to take advantage of it

* wip

* add rest of the public API and fix tests

* dtolnay feedback

* remove serialized_size_bounded and replace it with a use of config

* remove inline from trait method

* finish documentation and add custom reader support

* minor config_map refactor

* doc changes

* add with_(de)serializer functions and their associated modules
This commit is contained in:
Ty Overby 2018-02-07 18:26:46 -08:00 committed by GitHub
parent 093bc24a13
commit b31151a605
9 changed files with 811 additions and 568 deletions

View File

@ -2,7 +2,7 @@
extern crate serde_derive; extern crate serde_derive;
extern crate bincode; extern crate bincode;
use bincode::{deserialize, serialize, Infinite}; use bincode::{deserialize, serialize};
#[derive(Serialize, Deserialize, PartialEq, Debug)] #[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Entity { struct Entity {
@ -16,7 +16,7 @@ struct World(Vec<Entity>);
fn main() { fn main() {
let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]); let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]);
let encoded: Vec<u8> = serialize(&world, Infinite).unwrap(); let encoded: Vec<u8> = serialize(&world).unwrap();
// 8 bytes for the length of the vector (usize), 4 bytes per float. // 8 bytes for the length of the vector (usize), 4 bytes per float.
assert_eq!(encoded.len(), 8 + 4 * 4); assert_eq!(encoded.len(), 8 + 4 * 4);

297
src/config.rs Normal file
View File

@ -0,0 +1,297 @@
use super::internal::{Bounded, Infinite, SizeLimit};
use ::error::Result;
use byteorder::{BigEndian, ByteOrder, LittleEndian, NativeEndian};
use {DeserializerAcceptor, SerializerAcceptor};
use serde;
use std::io::{Write, Read};
use std::marker::PhantomData;
use de::read::BincodeRead;
use self::LimitOption::*;
use self::EndianOption::*;
struct DefaultOptions(Infinite);
pub(crate) trait Options {
type Limit: SizeLimit + 'static;
type Endian: ByteOrder + 'static;
fn limit(&mut self) -> &mut Self::Limit;
}
pub(crate) trait OptionsExt: Options + Sized {
fn with_no_limit(self) -> WithOtherLimit<Self, Infinite> {
WithOtherLimit::new(self, Infinite)
}
fn with_limit(self, limit: u64) -> WithOtherLimit<Self, Bounded> {
WithOtherLimit::new(self, Bounded(limit))
}
fn with_little_endian(self) -> WithOtherEndian<Self, LittleEndian> {
WithOtherEndian::new(self)
}
fn with_big_endian(self) -> WithOtherEndian<Self, BigEndian> {
WithOtherEndian::new(self)
}
fn with_native_endian(self) -> WithOtherEndian<Self, NativeEndian> {
WithOtherEndian::new(self)
}
}
impl<'a, O: Options> Options for &'a mut O {
type Limit = O::Limit;
type Endian = O::Endian;
#[inline(always)]
fn limit(&mut self) -> &mut Self::Limit {
(*self).limit()
}
}
impl<T: Options> OptionsExt for T {}
impl DefaultOptions {
fn new() -> DefaultOptions {
DefaultOptions(Infinite)
}
}
impl Options for DefaultOptions {
type Limit = Infinite;
type Endian = LittleEndian;
#[inline(always)]
fn limit(&mut self) -> &mut Infinite {
&mut self.0
}
}
#[derive(Clone, Copy)]
enum LimitOption {
Unlimited,
Limited(u64),
}
#[derive(Clone, Copy)]
enum EndianOption {
Big,
Little,
Native,
}
/// A configuration builder whose options Bincode will use
/// while serializing and deserializing.
///
/// ### Options
/// Endianness: The endianness with which multi-byte integers will be read/written. *default: little endian*
/// Limit: The maximum number of bytes that will be read/written in a bincode serialize/deserialize. *default: unlimited*
///
/// ### Byte Limit Details
/// The purpose of byte-limiting is to prevent Denial-Of-Service attacks whereby malicious attackers get bincode
/// deserialization to crash your process by allocating too much memory or keeping a connection open for too long.
///
/// When a byte limit is set, bincode will return `Err` on any deserialization that goes over the limit, or any
/// serialization that goes over the limit.
pub struct Config {
limit: LimitOption,
endian: EndianOption,
}
pub(crate) struct WithOtherLimit<O: Options, L: SizeLimit> {
_options: O,
pub(crate) new_limit: L,
}
pub(crate) struct WithOtherEndian<O: Options, E: ByteOrder> {
options: O,
_endian: PhantomData<E>,
}
impl<O: Options, L: SizeLimit> WithOtherLimit<O, L> {
#[inline(always)]
pub(crate) fn new(options: O, limit: L) -> WithOtherLimit<O, L> {
WithOtherLimit {
_options: options,
new_limit: limit,
}
}
}
impl<O: Options, E: ByteOrder> WithOtherEndian<O, E> {
#[inline(always)]
pub(crate) fn new(options: O) -> WithOtherEndian<O, E> {
WithOtherEndian {
options: options,
_endian: PhantomData,
}
}
}
impl<O: Options, E: ByteOrder + 'static> Options for WithOtherEndian<O, E> {
type Limit = O::Limit;
type Endian = E;
#[inline(always)]
fn limit(&mut self) -> &mut O::Limit {
self.options.limit()
}
}
impl<O: Options, L: SizeLimit + 'static> Options for WithOtherLimit<O, L> {
type Limit = L;
type Endian = O::Endian;
fn limit(&mut self) -> &mut L {
&mut self.new_limit
}
}
macro_rules! config_map {
($self:expr, $opts:ident => $call:expr) => {
match ($self.limit, $self.endian) {
(Unlimited, Little) => {
let $opts = DefaultOptions::new().with_no_limit().with_little_endian();
$call
}
(Unlimited, Big) => {
let $opts = DefaultOptions::new().with_no_limit().with_big_endian();
$call
}
(Unlimited, Native) => {
let $opts = DefaultOptions::new().with_no_limit().with_native_endian();
$call
}
(Limited(l), Little) => {
let $opts = DefaultOptions::new().with_limit(l).with_little_endian();
$call
}
(Limited(l), Big) => {
let $opts = DefaultOptions::new().with_limit(l).with_big_endian();
$call
}
(Limited(l), Native) => {
let $opts = DefaultOptions::new().with_limit(l).with_native_endian();
$call
}
}
}
}
impl Config {
#[inline(always)]
pub(crate) fn new() -> Config {
Config {
limit: LimitOption::Unlimited,
endian: EndianOption::Little,
}
}
/// Sets the byte limit to be unlimited.
/// This is the default.
#[inline(always)]
pub fn no_limit(&mut self) -> &mut Self {
self.limit = LimitOption::Unlimited;
self
}
/// Sets the byte limit to `limit`.
#[inline(always)]
pub fn limit(&mut self, limit: u64) -> &mut Self {
self.limit = LimitOption::Limited(limit);
self
}
/// Sets the endianness to little-endian
/// This is the default.
#[inline(always)]
pub fn little_endian(&mut self) -> &mut Self {
self.endian = EndianOption::Little;
self
}
/// Sets the endianness to big-endian
#[inline(always)]
pub fn big_endian(&mut self) -> &mut Self {
self.endian= EndianOption::Big;
self
}
/// Sets the endianness to the the machine-native endianness
#[inline(always)]
pub fn native_endian(&mut self) -> &mut Self {
self.endian = EndianOption::Native;
self
}
/// Serializes a serializable object into a `Vec` of bytes using this configuration
#[inline(always)]
pub fn serialize<T: ?Sized + serde::Serialize>(&self, t: &T) -> Result<Vec<u8>> {
config_map!(self, opts => ::internal::serialize(t, opts))
}
/// Returns the size that an object would be if serialized using Bincode with this configuration
#[inline(always)]
pub fn serialized_size<T: ?Sized + serde::Serialize>(&self, t: &T) -> Result<u64> {
config_map!(self, opts => ::internal::serialized_size(t, opts))
}
/// Serializes an object directly into a `Writer` using this configuration
///
/// If the serialization would take more bytes than allowed by the size limit, an error
/// is returned and *no bytes* will be written into the `Writer`
#[inline(always)]
pub fn serialize_into<W: Write, T: ?Sized + serde::Serialize>(&self, w: W, t: &T) -> Result<()> {
config_map!(self, opts => ::internal::serialize_into(w, t, opts))
}
/// Deserializes a slice of bytes into an instance of `T` using this configuration
#[inline(always)]
pub fn deserialize<'a, T: serde::Deserialize<'a>>(&self, bytes: &'a [u8]) -> Result<T> {
config_map!(self, opts => ::internal::deserialize(bytes, opts))
}
/// Deserializes an object directly from a `Read`er using this configuration
///
/// If this returns an `Error`, `reader` may be in an invalid state.
#[inline(always)]
pub fn deserialize_from<R: Read, T: serde::de::DeserializeOwned>(&self, reader: R) -> Result<T> {
config_map!(self, opts => ::internal::deserialize_from(reader, opts))
}
/// Deserializes an object from a custom `BincodeRead`er using the default configuration.
/// It is highly recommended to use `deserialize_from` unless you need to implement
/// `BincodeRead` for performance reasons.
///
/// If this returns an `Error`, `reader` may be in an invalid state.
#[inline(always)]
pub fn deserialize_from_custom<'a, R: BincodeRead<'a>, T: serde::de::DeserializeOwned>(&self, reader: R) -> Result<T> {
config_map!(self, opts => ::internal::deserialize_from_custom(reader, opts))
}
/// Executes the acceptor with a serde::Deserializer instance.
pub fn with_deserializer<'a, A, R>(&self, reader: R, acceptor: A) -> A::Output
where A: DeserializerAcceptor<'a>,
R: BincodeRead<'a>
{
config_map!(self, opts => {
let mut deserializer = ::de::Deserializer::new(reader, opts);
acceptor.accept(&mut deserializer)
})
}
/// Executes the acceptor with a serde::Serializer instance.
pub fn with_serializer<A, W>(&self, writer: W, acceptor: A) -> A::Output
where A: SerializerAcceptor,
W: Write
{
config_map!(self, opts => {
let mut serializer = ::ser::Serializer::new(writer, opts);
acceptor.accept(&mut serializer)
})
}
}

View File

@ -1,12 +1,12 @@
use std::io::Read; use std::io::Read;
use std::marker::PhantomData; use ::config::Options;
use byteorder::{ReadBytesExt, ByteOrder}; use serde;
use serde_crate as serde; use byteorder::ReadBytesExt;
use serde_crate::de::IntoDeserializer; use serde::de::IntoDeserializer;
use serde_crate::de::Error as DeError; use serde::de::Error as DeError;
use SizeLimit; use ::{Error, ErrorKind, Result};
use super::{Result, Error, ErrorKind}; use ::internal::SizeLimit;
use self::read::BincodeRead; use self::read::BincodeRead;
pub mod read; pub mod read;
@ -24,24 +24,22 @@ pub mod read;
/// serde::Deserialize::deserialize(&mut deserializer); /// serde::Deserialize::deserialize(&mut deserializer);
/// let bytes_read = d.bytes_read(); /// let bytes_read = d.bytes_read();
/// ``` /// ```
pub struct Deserializer<R, S: SizeLimit, E: ByteOrder> { pub(crate) struct Deserializer<R, O: Options> {
reader: R, reader: R,
size_limit: S, options: O,
_phantom: PhantomData<E>,
} }
impl<'de, R: BincodeRead<'de>, E: ByteOrder, S: SizeLimit> Deserializer<R, S, E> { impl<'de, R: BincodeRead<'de>, O: Options> Deserializer<R, O> {
/// Creates a new Deserializer with a given `Read`er and a size_limit. /// Creates a new Deserializer with a given `Read`er and a size_limit.
pub fn new(r: R, size_limit: S) -> Deserializer<R, S, E> { pub(crate) fn new(r: R, options: O) -> Deserializer<R, O> {
Deserializer { Deserializer {
reader: r, reader: r,
size_limit: size_limit, options: options,
_phantom: PhantomData,
} }
} }
fn read_bytes(&mut self, count: u64) -> Result<()> { fn read_bytes(&mut self, count: u64) -> Result<()> {
self.size_limit.add(count) self.options.limit().add(count)
} }
fn read_type<T>(&mut self) -> Result<()> { fn read_type<T>(&mut self) -> Result<()> {
@ -68,17 +66,16 @@ macro_rules! impl_nums {
where V: serde::de::Visitor<'de>, where V: serde::de::Visitor<'de>,
{ {
try!(self.read_type::<$ty>()); try!(self.read_type::<$ty>());
let value = try!(self.reader.$reader_method::<E>()); let value = try!(self.reader.$reader_method::<O::Endian>());
visitor.$visitor_method(value) visitor.$visitor_method(value)
} }
} }
} }
impl<'de, 'a, R, S, E> serde::Deserializer<'de> for &'a mut Deserializer<R, S, E> impl<'de, 'a, R, O> serde::Deserializer<'de> for &'a mut Deserializer<R, O>
where where
R: BincodeRead<'de>, R: BincodeRead<'de>,
S: SizeLimit, O: Options,
E: ByteOrder,
{ {
type Error = Error; type Error = Error;
@ -211,8 +208,8 @@ where
where where
V: serde::de::Visitor<'de>, V: serde::de::Visitor<'de>,
{ {
impl<'de, 'a, R: 'a, S, E> serde::de::EnumAccess<'de> for &'a mut Deserializer<R, S, E> impl<'de, 'a, R: 'a, O> serde::de::EnumAccess<'de> for &'a mut Deserializer<R, O>
where R: BincodeRead<'de>, S: SizeLimit, E: ByteOrder { where R: BincodeRead<'de>, O: Options {
type Error = Error; type Error = Error;
type Variant = Self; type Variant = Self;
@ -232,8 +229,8 @@ where
where where
V: serde::de::Visitor<'de>, V: serde::de::Visitor<'de>,
{ {
struct Access<'a, R: Read + 'a, S: SizeLimit + 'a, E: ByteOrder + 'a> { struct Access<'a, R: Read + 'a, O: Options + 'a> {
deserializer: &'a mut Deserializer<R, S, E>, deserializer: &'a mut Deserializer<R, O>,
len: usize, len: usize,
} }
@ -242,9 +239,8 @@ where
'a, 'a,
'b: 'a, 'b: 'a,
R: BincodeRead<'de> + 'b, R: BincodeRead<'de> + 'b,
S: SizeLimit, O: Options,
E: ByteOrder, > serde::de::SeqAccess<'de> for Access<'a, R, O> {
> serde::de::SeqAccess<'de> for Access<'a, R, S, E> {
type Error = Error; type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>> fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
@ -299,8 +295,8 @@ where
where where
V: serde::de::Visitor<'de>, V: serde::de::Visitor<'de>,
{ {
struct Access<'a, R: Read + 'a, S: SizeLimit + 'a, E: ByteOrder + 'a> { struct Access<'a, R: Read + 'a, O: Options + 'a> {
deserializer: &'a mut Deserializer<R, S, E>, deserializer: &'a mut Deserializer<R, O>,
len: usize, len: usize,
} }
@ -309,9 +305,8 @@ where
'a, 'a,
'b: 'a, 'b: 'a,
R: BincodeRead<'de> + 'b, R: BincodeRead<'de> + 'b,
S: SizeLimit, O: Options,
E: ByteOrder, > serde::de::MapAccess<'de> for Access<'a, R, O> {
> serde::de::MapAccess<'de> for Access<'a, R, S, E> {
type Error = Error; type Error = Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
@ -413,8 +408,8 @@ where
} }
} }
impl<'de, 'a, R, S, E> serde::de::VariantAccess<'de> for &'a mut Deserializer<R, S, E> impl<'de, 'a, R, O> serde::de::VariantAccess<'de> for &'a mut Deserializer<R, O>
where R: BincodeRead<'de>, S: SizeLimit, E: ByteOrder { where R: BincodeRead<'de>, O: Options{
type Error = Error; type Error = Error;
fn unit_variant(self) -> Result<()> { fn unit_variant(self) -> Result<()> {

View File

@ -1,19 +1,21 @@
use std::io; use std::io;
use Result; use error::Result;
use serde_crate as serde; use serde;
/// A byte-oriented reading trait that is specialized for /// An optional Read trait for advanced Bincode usage.
/// slices and generic readers. ///
pub trait BincodeRead<'storage>: io::Read + ::private::Sealed { /// It is highly recommended to use bincode with `io::Read` or `&[u8]` before
#[doc(hidden)] /// implementing a custom `BincodeRead`.
pub trait BincodeRead<'storage>: io::Read {
/// Forwards reading `length` bytes of a string on to the serde reader.
fn forward_read_str<V>(&mut self, length: usize, visitor: V) -> Result<V::Value> fn forward_read_str<V>(&mut self, length: usize, visitor: V) -> Result<V::Value>
where where
V: serde::de::Visitor<'storage>; V: serde::de::Visitor<'storage>;
#[doc(hidden)] /// Return the first `length` bytes of the internal byte buffer.
fn get_byte_buffer(&mut self, length: usize) -> Result<Vec<u8>>; fn get_byte_buffer(&mut self, length: usize) -> Result<Vec<u8>>;
#[doc(hidden)] /// Forwards reading `length` bytes on to the serde reader.
fn forward_read_bytes<V>(&mut self, length: usize, visitor: V) -> Result<V::Value> fn forward_read_bytes<V>(&mut self, length: usize, visitor: V) -> Result<V::Value>
where where
V: serde::de::Visitor<'storage>; V: serde::de::Visitor<'storage>;
@ -48,24 +50,29 @@ impl<R> IoReader<R> {
} }
impl<'storage> io::Read for SliceReader<'storage> { impl<'storage> io::Read for SliceReader<'storage> {
#[inline(always)]
fn read(&mut self, out: &mut [u8]) -> io::Result<usize> { fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
(&mut self.slice).read(out) (&mut self.slice).read(out)
} }
#[inline(always)]
fn read_exact(&mut self, out: &mut [u8]) -> io::Result<()> { fn read_exact(&mut self, out: &mut [u8]) -> io::Result<()> {
(&mut self.slice).read_exact(out) (&mut self.slice).read_exact(out)
} }
} }
impl<R: io::Read> io::Read for IoReader<R> { impl<R: io::Read> io::Read for IoReader<R> {
#[inline(always)]
fn read(&mut self, out: &mut [u8]) -> io::Result<usize> { fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
self.reader.read(out) self.reader.read(out)
} }
#[inline(always)]
fn read_exact(&mut self, out: &mut [u8]) -> io::Result<()> { fn read_exact(&mut self, out: &mut [u8]) -> io::Result<()> {
self.reader.read_exact(out) self.reader.read_exact(out)
} }
} }
impl<'storage> SliceReader<'storage> { impl<'storage> SliceReader<'storage> {
#[inline(always)]
fn unexpected_eof() -> Box<::ErrorKind> { fn unexpected_eof() -> Box<::ErrorKind> {
return Box::new(::ErrorKind::Io( return Box::new(::ErrorKind::Io(
io::Error::new(io::ErrorKind::UnexpectedEof, ""), io::Error::new(io::ErrorKind::UnexpectedEof, ""),
@ -74,6 +81,7 @@ impl<'storage> SliceReader<'storage> {
} }
impl<'storage> BincodeRead<'storage> for SliceReader<'storage> { impl<'storage> BincodeRead<'storage> for SliceReader<'storage> {
#[inline(always)]
fn forward_read_str<V>(&mut self, length: usize, visitor: V) -> Result<V::Value> fn forward_read_str<V>(&mut self, length: usize, visitor: V) -> Result<V::Value>
where where
V: serde::de::Visitor<'storage>, V: serde::de::Visitor<'storage>,
@ -92,6 +100,7 @@ impl<'storage> BincodeRead<'storage> for SliceReader<'storage> {
r r
} }
#[inline(always)]
fn get_byte_buffer(&mut self, length: usize) -> Result<Vec<u8>> { fn get_byte_buffer(&mut self, length: usize) -> Result<Vec<u8>> {
if length > self.slice.len() { if length > self.slice.len() {
return Err(SliceReader::unexpected_eof()); return Err(SliceReader::unexpected_eof());
@ -102,6 +111,7 @@ impl<'storage> BincodeRead<'storage> for SliceReader<'storage> {
Ok(r.to_vec()) Ok(r.to_vec())
} }
#[inline(always)]
fn forward_read_bytes<V>(&mut self, length: usize, visitor: V) -> Result<V::Value> fn forward_read_bytes<V>(&mut self, length: usize, visitor: V) -> Result<V::Value>
where where
V: serde::de::Visitor<'storage>, V: serde::de::Visitor<'storage>,

119
src/error.rs Normal file
View File

@ -0,0 +1,119 @@
use std::io;
use std::{error, fmt};
use std::str::Utf8Error;
use std::error::Error as StdError;
use serde;
/// The result of a serialization or deserialization operation.
pub type Result<T> = ::std::result::Result<T, Error>;
/// An error that can be produced during (de)serializing.
pub type Error = Box<ErrorKind>;
/// The kind of error that can be produced during a serialization or deserialization.
#[derive(Debug)]
pub enum ErrorKind {
/// If the error stems from the reader/writer that is being used
/// during (de)serialization, that error will be stored and returned here.
Io(io::Error),
/// Returned if the deserializer attempts to deserialize a string that is not valid utf8
InvalidUtf8Encoding(Utf8Error),
/// Returned if the deserializer attempts to deserialize a bool that was
/// not encoded as either a 1 or a 0
InvalidBoolEncoding(u8),
/// Returned if the deserializer attempts to deserialize a char that is not in the correct format.
InvalidCharEncoding,
/// Returned if the deserializer attempts to deserialize the tag of an enum that is
/// not in the expected ranges
InvalidTagEncoding(usize),
/// Serde has a deserialize_any method that lets the format hint to the
/// object which route to take in deserializing.
DeserializeAnyNotSupported,
/// If (de)serializing a message takes more than the provided size limit, this
/// error is returned.
SizeLimit,
/// Bincode can not encode sequences of unknown length (like iterators).
SequenceMustHaveLength,
/// A custom error message from Serde.
Custom(String),
}
impl StdError for ErrorKind {
fn description(&self) -> &str {
match *self {
ErrorKind::Io(ref err) => error::Error::description(err),
ErrorKind::InvalidUtf8Encoding(_) => "string is not valid utf8",
ErrorKind::InvalidBoolEncoding(_) => "invalid u8 while decoding bool",
ErrorKind::InvalidCharEncoding => "char is not valid",
ErrorKind::InvalidTagEncoding(_) => "tag for enum is not valid",
ErrorKind::SequenceMustHaveLength =>
"Bincode can only encode sequences and maps that have a knowable size ahead of time",
ErrorKind::DeserializeAnyNotSupported => {
"Bincode doesn't support serde::Deserializer::deserialize_any"
}
ErrorKind::SizeLimit => "the size limit has been reached",
ErrorKind::Custom(ref msg) => msg,
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
ErrorKind::Io(ref err) => Some(err),
ErrorKind::InvalidUtf8Encoding(_) => None,
ErrorKind::InvalidBoolEncoding(_) => None,
ErrorKind::InvalidCharEncoding => None,
ErrorKind::InvalidTagEncoding(_) => None,
ErrorKind::SequenceMustHaveLength => None,
ErrorKind::DeserializeAnyNotSupported => None,
ErrorKind::SizeLimit => None,
ErrorKind::Custom(_) => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
ErrorKind::Io(err).into()
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
ErrorKind::Io(ref ioerr) => write!(fmt, "io error: {}", ioerr),
ErrorKind::InvalidUtf8Encoding(ref e) => write!(fmt, "{}: {}", self.description(), e),
ErrorKind::InvalidBoolEncoding(b) => {
write!(fmt, "{}, expected 0 or 1, found {}", self.description(), b)
}
ErrorKind::InvalidCharEncoding => write!(fmt, "{}", self.description()),
ErrorKind::InvalidTagEncoding(tag) => {
write!(fmt, "{}, found {}", self.description(), tag)
}
ErrorKind::SequenceMustHaveLength => {
write!(fmt, "{}", self.description())
}
ErrorKind::SizeLimit => write!(fmt, "{}", self.description()),
ErrorKind::DeserializeAnyNotSupported => {
write!(
fmt,
"Bincode does not support the serde::Deserializer::deserialize_any method"
)
}
ErrorKind::Custom(ref s) => s.fmt(fmt),
}
}
}
impl serde::de::Error for Error {
fn custom<T: fmt::Display>(desc: T) -> Error {
ErrorKind::Custom(desc.to_string()).into()
}
}
impl serde::ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
ErrorKind::Custom(msg.to_string()).into()
}
}

View File

@ -1,199 +1,50 @@
//! A collection of serialization and deserialization functions use std::io::{Read, Write};
//! that use the `serde` crate for the serializable and deserializable use serde;
//! implementation.
use std::io::{self, Write, Read}; use config::{Options, OptionsExt};
use std::{error, fmt, result}; use de::read::BincodeRead;
use std::str::Utf8Error; use {ErrorKind, Result};
use {CountSize, SizeLimit};
use byteorder::ByteOrder;
use std::error::Error as StdError;
pub use super::de::Deserializer; #[derive(Clone)]
struct CountSize<L: SizeLimit> {
pub use super::ser::Serializer; total: u64,
other_limit: L,
use super::ser::SizeChecker;
use serde_crate as serde;
/// The result of a serialization or deserialization operation.
pub type Result<T> = result::Result<T, Error>;
/// An error that can be produced during (de)serializing.
pub type Error = Box<ErrorKind>;
/// The kind of error that can be produced during a serialization or deserialization.
#[derive(Debug)]
pub enum ErrorKind {
/// If the error stems from the reader/writer that is being used
/// during (de)serialization, that error will be stored and returned here.
Io(io::Error),
/// Returned if the deserializer attempts to deserialize a string that is not valid utf8
InvalidUtf8Encoding(Utf8Error),
/// Returned if the deserializer attempts to deserialize a bool that was
/// not encoded as either a 1 or a 0
InvalidBoolEncoding(u8),
/// Returned if the deserializer attempts to deserialize a char that is not in the correct format.
InvalidCharEncoding,
/// Returned if the deserializer attempts to deserialize the tag of an enum that is
/// not in the expected ranges
InvalidTagEncoding(usize),
/// Serde has a deserialize_any method that lets the format hint to the
/// object which route to take in deserializing.
DeserializeAnyNotSupported,
/// If (de)serializing a message takes more than the provided size limit, this
/// error is returned.
SizeLimit,
/// Bincode can not encode sequences of unknown length (like iterators).
SequenceMustHaveLength,
/// A custom error message from Serde.
Custom(String),
} }
impl StdError for ErrorKind { pub(crate) fn serialize_into<W, T: ?Sized, O>(writer: W, value: &T, mut options: O) -> Result<()>
fn description(&self) -> &str {
match *self {
ErrorKind::Io(ref err) => error::Error::description(err),
ErrorKind::InvalidUtf8Encoding(_) => "string is not valid utf8",
ErrorKind::InvalidBoolEncoding(_) => "invalid u8 while decoding bool",
ErrorKind::InvalidCharEncoding => "char is not valid",
ErrorKind::InvalidTagEncoding(_) => "tag for enum is not valid",
ErrorKind::SequenceMustHaveLength =>
"Bincode can only encode sequences and maps that have a knowable size ahead of time",
ErrorKind::DeserializeAnyNotSupported => {
"Bincode doesn't support serde::Deserializer::deserialize_any"
}
ErrorKind::SizeLimit => "the size limit has been reached",
ErrorKind::Custom(ref msg) => msg,
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
ErrorKind::Io(ref err) => Some(err),
ErrorKind::InvalidUtf8Encoding(_) => None,
ErrorKind::InvalidBoolEncoding(_) => None,
ErrorKind::InvalidCharEncoding => None,
ErrorKind::InvalidTagEncoding(_) => None,
ErrorKind::SequenceMustHaveLength => None,
ErrorKind::DeserializeAnyNotSupported => None,
ErrorKind::SizeLimit => None,
ErrorKind::Custom(_) => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
ErrorKind::Io(err).into()
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
ErrorKind::Io(ref ioerr) => write!(fmt, "io error: {}", ioerr),
ErrorKind::InvalidUtf8Encoding(ref e) => write!(fmt, "{}: {}", self.description(), e),
ErrorKind::InvalidBoolEncoding(b) => {
write!(fmt, "{}, expected 0 or 1, found {}", self.description(), b)
}
ErrorKind::InvalidCharEncoding => write!(fmt, "{}", self.description()),
ErrorKind::InvalidTagEncoding(tag) => {
write!(fmt, "{}, found {}", self.description(), tag)
}
ErrorKind::SequenceMustHaveLength => {
write!(fmt, "{}", self.description())
}
ErrorKind::SizeLimit => write!(fmt, "{}", self.description()),
ErrorKind::DeserializeAnyNotSupported => {
write!(
fmt,
"Bincode does not support the serde::Deserializer::deserialize_any method"
)
}
ErrorKind::Custom(ref s) => s.fmt(fmt),
}
}
}
impl serde::de::Error for Error {
fn custom<T: fmt::Display>(desc: T) -> Error {
ErrorKind::Custom(desc.to_string()).into()
}
}
impl serde::ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
ErrorKind::Custom(msg.to_string()).into()
}
}
/// Serializes an object directly into a `Writer`.
///
/// If the serialization would take more bytes than allowed by `size_limit`, an error
/// is returned and *no bytes* will be written into the `Writer`.
///
/// If this returns an `Error` (other than SizeLimit), assume that the
/// writer is in an invalid state, as writing could bail out in the middle of
/// serializing.
pub fn serialize_into<W, T: ?Sized, S, E>(writer: W, value: &T, size_limit: S) -> Result<()>
where where
W: Write, W: Write,
T: serde::Serialize, T: serde::Serialize,
S: SizeLimit, O: Options,
E: ByteOrder,
{ {
if let Some(limit) = size_limit.limit() { if options.limit().limit().is_some() {
try!(serialized_size_bounded(value, limit).ok_or( // "compute" the size for the side-effect
ErrorKind::SizeLimit, // of returning Err if the bound was reached.
)); serialized_size(value, &mut options)?;
} }
let mut serializer = Serializer::<_, E>::new(writer); let mut serializer = ::ser::Serializer::<_, O>::new(writer, options);
serde::Serialize::serialize(value, &mut serializer) serde::Serialize::serialize(value, &mut serializer)
} }
/// Serializes a serializable object into a `Vec` of bytes. pub(crate) fn serialize<T: ?Sized, O>(value: &T, mut options: O) -> Result<Vec<u8>>
///
/// If the serialization would take more bytes than allowed by `size_limit`,
/// an error is returned.
pub fn serialize<T: ?Sized, S, E>(value: &T, size_limit: S) -> Result<Vec<u8>>
where where
T: serde::Serialize, T: serde::Serialize,
S: SizeLimit, O: Options,
E: ByteOrder,
{ {
let mut writer = match size_limit.limit() { let mut writer = {
Some(size_limit) => { let actual_size = serialized_size(value, &mut options)?;
let actual_size = try!(serialized_size_bounded(value, size_limit).ok_or(
ErrorKind::SizeLimit,
));
Vec::with_capacity(actual_size as usize) Vec::with_capacity(actual_size as usize)
}
None => {
let size = serialized_size(value) as usize;
Vec::with_capacity(size)
}
}; };
try!(serialize_into::<_, _, _, E>( serialize_into(&mut writer, value, options.with_no_limit())?;
&mut writer,
value,
super::Infinite,
));
Ok(writer) Ok(writer)
} }
impl SizeLimit for CountSize { impl<L: SizeLimit> SizeLimit for CountSize<L> {
fn add(&mut self, c: u64) -> Result<()> { fn add(&mut self, c: u64) -> Result<()> {
self.other_limit.add(c)?;
self.total += c; self.total += c;
if let Some(limit) = self.limit {
if self.total > limit {
return Err(Box::new(ErrorKind::SizeLimit));
}
}
Ok(()) Ok(())
} }
@ -202,77 +53,102 @@ impl SizeLimit for CountSize {
} }
} }
/// Returns the size that an object would be if serialized using Bincode. pub(crate) fn serialized_size<T: ?Sized, O: Options>(value: &T, mut options: O) -> Result<u64>
///
/// This is used internally as part of the check for encode_into, but it can
/// be useful for preallocating buffers if thats your style.
pub fn serialized_size<T: ?Sized>(value: &T) -> u64
where where
T: serde::Serialize, T: serde::Serialize,
{ {
let mut size_counter = SizeChecker { let old_limiter = options.limit().clone();
size_limit: CountSize { let mut size_counter = ::ser::SizeChecker {
options: ::config::WithOtherLimit::new(
options,
CountSize {
total: 0, total: 0,
limit: None, other_limit: old_limiter,
}, },
),
}; };
value.serialize(&mut size_counter).ok(); let result = value.serialize(&mut size_counter);
size_counter.size_limit.total result.map(|_| size_counter.options.new_limit.total)
} }
/// Given a maximum size limit, check how large an object would be if it pub(crate) fn deserialize_from<R, T, O>(reader: R, options: O) -> Result<T>
/// were to be serialized.
///
/// If it can be serialized in `max` or fewer bytes, that number will be returned
/// inside `Some`. If it goes over bounds, then None is returned.
pub fn serialized_size_bounded<T: ?Sized>(value: &T, max: u64) -> Option<u64>
where
T: serde::Serialize,
{
let mut size_counter = SizeChecker {
size_limit: CountSize {
total: 0,
limit: Some(max),
},
};
match value.serialize(&mut size_counter) {
Ok(_) => Some(size_counter.size_limit.total),
Err(_) => None,
}
}
/// Deserializes an object directly from a `Read`er.
///
/// If the provided `SizeLimit` is reached, the deserialization will bail immediately.
/// A SizeLimit can help prevent an attacker from flooding your server with
/// a neverending stream of values that runs your server out of memory.
///
/// If this returns an `Error`, assume that the buffer that you passed
/// in is in an invalid state, as the error could be returned during any point
/// in the reading.
pub fn deserialize_from<R, T, S, E>(reader: R, size_limit: S) -> Result<T>
where where
R: Read, R: Read,
T: serde::de::DeserializeOwned, T: serde::de::DeserializeOwned,
S: SizeLimit, O: Options,
E: ByteOrder,
{ {
let reader = ::de::read::IoReader::new(reader); let reader = ::de::read::IoReader::new(reader);
let mut deserializer = Deserializer::<_, S, E>::new(reader, size_limit); let mut deserializer = ::de::Deserializer::<_, O>::new(reader, options);
serde::Deserialize::deserialize(&mut deserializer) serde::Deserialize::deserialize(&mut deserializer)
} }
/// Deserializes a slice of bytes into an object. pub(crate) fn deserialize_from_custom<'a, R, T, O>(reader: R, options: O) -> Result<T>
///
/// This method does not have a size-limit because if you already have the bytes
/// in memory, then you don't gain anything by having a limiter.
pub fn deserialize<'a, T, E: ByteOrder>(bytes: &'a [u8]) -> Result<T>
where where
T: serde::de::Deserialize<'a>, R: BincodeRead<'a>,
T: serde::de::DeserializeOwned,
O: Options,
{ {
let reader = ::de::read::SliceReader::new(bytes); let mut deserializer = ::de::Deserializer::<_, O>::new(reader, options);
let mut deserializer = Deserializer::<_, _, E>::new(reader, super::Infinite);
serde::Deserialize::deserialize(&mut deserializer) serde::Deserialize::deserialize(&mut deserializer)
} }
pub(crate) fn deserialize<'a, T, O>(bytes: &'a [u8], options: O) -> Result<T>
where
T: serde::de::Deserialize<'a>,
O: Options,
{
let reader = ::de::read::SliceReader::new(bytes);
let options = ::config::WithOtherLimit::new(options, Infinite);
let mut deserializer = ::de::Deserializer::new(reader, options);
serde::Deserialize::deserialize(&mut deserializer)
}
pub(crate) trait SizeLimit: Clone {
/// Tells the SizeLimit that a certain number of bytes has been
/// read or written. Returns Err if the limit has been exceeded.
fn add(&mut self, n: u64) -> Result<()>;
/// Returns the hard limit (if one exists)
fn limit(&self) -> Option<u64>;
}
/// A SizeLimit that restricts serialized or deserialized messages from
/// exceeding a certain byte length.
#[derive(Copy, Clone)]
pub struct Bounded(pub u64);
/// A SizeLimit without a limit!
/// Use this if you don't care about the size of encoded or decoded messages.
#[derive(Copy, Clone)]
pub struct Infinite;
impl SizeLimit for Bounded {
#[inline(always)]
fn add(&mut self, n: u64) -> Result<()> {
if self.0 >= n {
self.0 -= n;
Ok(())
} else {
Err(Box::new(ErrorKind::SizeLimit))
}
}
#[inline(always)]
fn limit(&self) -> Option<u64> {
Some(self.0)
}
}
impl SizeLimit for Infinite {
#[inline(always)]
fn add(&mut self, _: u64) -> Result<()> {
Ok(())
}
#[inline(always)]
fn limit(&self) -> Option<u64> {
None
}
}

View File

@ -1,32 +1,20 @@
#![deny(missing_docs)] #![deny(missing_docs)]
//! Bincode is a crate for encoding and decoding using a tiny binary //! Bincode is a crate for encoding and decoding using a tiny binary
//! serialization strategy. //! serialization strategy. Using it, you can easily go from having
//! //! an object in memory, quickly serialize it to bytes, and then
//! There are simple functions for encoding to `Vec<u8>` and decoding from //! deserialize it back just as fast!
//! `&[u8]`, but the meat of the library is the `serialize_into` and `deserialize_from`
//! functions which respectively allow encoding into any `std::io::Write`
//! or decode from any `std::io::Read`.
//!
//! ## Modules
//! Until "default type parameters" lands, we have an extra module called `endian_choice`
//! that duplicates all of the core Bincode functionality but with the option to choose
//! which endianness the integers are encoded using.
//!
//! The default endianness is little.
//! //!
//! ### Using Basic Functions //! ### Using Basic Functions
//! //!
//! ```rust //! ```rust
//! extern crate bincode; //! extern crate bincode;
//! use bincode::{serialize, deserialize, Bounded}; //! use bincode::{serialize, deserialize};
//! fn main() { //! fn main() {
//! // The object that we will serialize. //! // The object that we will serialize.
//! let target = Some("hello world".to_string()); //! let target: Option<String> = Some("hello world".to_string());
//! // The maximum size of the encoded message.
//! let limit = Bounded(20);
//! //!
//! let encoded: Vec<u8> = serialize(&target, limit).unwrap(); //! let encoded: Vec<u8> = serialize(&target).unwrap();
//! let decoded: Option<String> = deserialize(&encoded[..]).unwrap(); //! let decoded: Option<String> = deserialize(&encoded[..]).unwrap();
//! assert_eq!(target, decoded); //! assert_eq!(target, decoded);
//! } //! }
@ -37,164 +25,121 @@
#![crate_type = "dylib"] #![crate_type = "dylib"]
extern crate byteorder; extern crate byteorder;
extern crate serde as serde_crate; extern crate serde;
mod config;
mod ser; mod ser;
mod error;
mod de; mod de;
pub mod internal; mod internal;
pub mod read { pub use error::{Error, ErrorKind, Result};
//! The types that the deserializer uses for optimizations pub use config::Config;
pub use de::read::{SliceReader, BincodeRead, IoReader}; pub use de::read::BincodeRead;
/// An object that implements this trait can be passed a
/// serde::Deserializer without knowing its concrete type.
///
/// This trait should be used only for `with_deserializer` functions.
pub trait DeserializerAcceptor<'a> {
/// The return type for the accept method
type Output;
/// Accept a serde::Deserializer and do whatever you want with it.
fn accept<T: serde::Deserializer<'a>>(self, T) -> Self::Output;
} }
use std::io::{Read, Write}; /// An object that implements this trait can be passed a
/// serde::Serializer without knowing its concrete type.
pub use internal::{ErrorKind, Error, Result, serialized_size, serialized_size_bounded};
/// A Deserializer that uses LittleEndian byteorder
pub type Deserializer<R, S> = internal::Deserializer<R, S, byteorder::LittleEndian>;
/// A Serializer that uses LittleEndian byteorder
pub type Serializer<W> = internal::Serializer<W, byteorder::LittleEndian>;
/// Deserializes a slice of bytes into an object.
/// ///
/// This method does not have a size-limit because if you already have the bytes /// This trait should be used only for `with_serializer` functions.
/// in memory, then you don't gain anything by having a limiter. pub trait SerializerAcceptor {
pub fn deserialize<'a, T>(bytes: &'a [u8]) -> internal::Result<T> /// The return type for the accept method
where type Output;
T: serde_crate::de::Deserialize<'a>, /// Accept a serde::Serializer and do whatever you want with it.
{ fn accept<T: serde::Serializer>(self, T) -> Self::Output;
internal::deserialize::<_, byteorder::LittleEndian>(bytes)
} }
/// Deserializes an object directly from a `Buffer`ed Reader. /// Get a default configuration object.
/// ///
/// If the provided `SizeLimit` is reached, the deserialization will bail immediately. /// ### Default Configuration:
/// A SizeLimit can help prevent an attacker from flooding your server with /// Byte limit: Unlimited
/// a neverending stream of values that runs your server out of memory. /// Endianness: Little
/// pub fn config() -> Config {
/// If this returns an `Error`, assume that the buffer that you passed Config::new()
/// in is in an invalid state, as the error could be returned during any point
/// in the reading.
pub fn deserialize_from<R: ?Sized, T, S>(reader: &mut R, size_limit: S) -> internal::Result<T>
where
R: Read,
T: serde_crate::de::DeserializeOwned,
S: SizeLimit,
{
internal::deserialize_from::<_, _, _, byteorder::LittleEndian>(reader, size_limit)
} }
/// Serializes an object directly into a `Writer`. /// Serializes an object directly into a `Writer` using the default configuration.
/// ///
/// If the serialization would take more bytes than allowed by `size_limit`, an error /// If the serialization would take more bytes than allowed by the size limit, an error
/// is returned and *no bytes* will be written into the `Writer`. /// is returned and *no bytes* will be written into the `Writer`.
/// pub fn serialize_into<W, T: ?Sized, O>(writer: W, value: &T) -> Result<()>
/// If this returns an `Error` (other than SizeLimit), assume that the
/// writer is in an invalid state, as writing could bail out in the middle of
/// serializing.
pub fn serialize_into<W: ?Sized, T: ?Sized, S>(
writer: &mut W,
value: &T,
size_limit: S,
) -> internal::Result<()>
where where
W: Write, W: std::io::Write,
T: serde_crate::Serialize, T: serde::Serialize,
S: SizeLimit,
{ {
internal::serialize_into::<_, _, _, byteorder::LittleEndian>(writer, value, size_limit) config().serialize_into(writer, value)
} }
/// Serializes a serializable object into a `Vec` of bytes. /// Serializes a serializable object into a `Vec` of bytes using the default configuration.
/// pub fn serialize<T: ?Sized>(value: &T) -> Result<Vec<u8>>
/// If the serialization would take more bytes than allowed by `size_limit`,
/// an error is returned.
pub fn serialize<T: ?Sized, S>(value: &T, size_limit: S) -> internal::Result<Vec<u8>>
where where
T: serde_crate::Serialize, T: serde::Serialize,
S: SizeLimit,
{ {
internal::serialize::<_, _, byteorder::LittleEndian>(value, size_limit) config().serialize(value)
} }
/// A limit on the amount of bytes that can be read or written. /// Deserializes an object directly from a `Read`er using the default configuration.
/// ///
/// Size limits are an incredibly important part of both encoding and decoding. /// If this returns an `Error`, `reader` may be in an invalid state.
pub fn deserialize_from<R, T>(reader: R) -> Result<T>
where
R: std::io::Read,
T: serde::de::DeserializeOwned,
{
config().deserialize_from(reader)
}
/// Deserializes an object from a custom `BincodeRead`er using the default configuration.
/// It is highly recommended to use `deserialize_from` unless you need to implement
/// `BincodeRead` for performance reasons.
/// ///
/// In order to prevent DOS attacks on a decoder, it is important to limit the /// If this returns an `Error`, `reader` may be in an invalid state.
/// amount of bytes that a single encoded message can be; otherwise, if you pub fn deserialize_from_custom<'a, R, T>(reader: R) -> Result<T>
/// are decoding bytes right off of a TCP stream for example, it would be where
/// possible for an attacker to flood your server with a 3TB vec, causing the R: de::read::BincodeRead<'a>,
/// decoder to run out of memory and crash your application! T: serde::de::DeserializeOwned,
/// Because of this, you can provide a maximum-number-of-bytes that can be read {
/// during decoding, and the decoder will explicitly fail if it has to read config().deserialize_from_custom(reader)
/// any more than that.
///
/// On the other side, you want to make sure that you aren't encoding a message
/// that is larger than your decoder expects. By supplying a size limit to an
/// encoding function, the encoder will verify that the structure can be encoded
/// within that limit. This verification occurs before any bytes are written to
/// the Writer, so recovering from an error is easy.
pub trait SizeLimit: private::Sealed {
/// Tells the SizeLimit that a certain number of bytes has been
/// read or written. Returns Err if the limit has been exceeded.
fn add(&mut self, n: u64) -> Result<()>;
/// Returns the hard limit (if one exists)
fn limit(&self) -> Option<u64>;
} }
/// A SizeLimit that restricts serialized or deserialized messages from /// Deserializes a slice of bytes into an instance of `T` using the default configuration.
/// exceeding a certain byte length. pub fn deserialize<'a, T>(bytes: &'a [u8]) -> Result<T>
#[derive(Copy, Clone)] where
pub struct Bounded(pub u64); T: serde::de::Deserialize<'a>,
{
/// A SizeLimit without a limit! config().deserialize(bytes)
/// Use this if you don't care about the size of encoded or decoded messages.
#[derive(Copy, Clone)]
pub struct Infinite;
struct CountSize {
total: u64,
limit: Option<u64>,
} }
impl SizeLimit for Bounded { /// Returns the size that an object would be if serialized using Bincode with the default configuration.
#[inline(always)] pub fn serialized_size<T: ?Sized>(value: &T) -> Result<u64>
fn add(&mut self, n: u64) -> Result<()> { where
if self.0 >= n { T: serde::Serialize,
self.0 -= n; {
Ok(()) config().serialized_size(value)
} else {
Err(Box::new(ErrorKind::SizeLimit))
}
}
#[inline(always)]
fn limit(&self) -> Option<u64> {
Some(self.0)
}
} }
impl SizeLimit for Infinite { /// Executes the acceptor with a serde::Deserializer instance.
#[inline(always)] pub fn with_deserializer<'a, A, R>(reader: R, acceptor: A) -> A::Output
fn add(&mut self, _: u64) -> Result<()> { where A: DeserializerAcceptor<'a>,
Ok(()) R: BincodeRead<'a>
} {
config().with_deserializer(reader, acceptor)
#[inline(always)]
fn limit(&self) -> Option<u64> {
None
}
} }
mod private { /// Executes the acceptor with a serde::Serializer instance.
pub trait Sealed {} pub fn with_serializer<A, W>(writer: W, acceptor: A) -> A::Output
where A: SerializerAcceptor,
impl<'a> Sealed for super::de::read::SliceReader<'a> {} W: std::io::Write
impl<R> Sealed for super::de::read::IoReader<R> {} {
impl Sealed for super::Infinite {} config().with_serializer(writer, acceptor)
impl Sealed for super::Bounded {}
impl Sealed for super::CountSize {}
} }

View File

@ -1,13 +1,13 @@
use std::io::Write; use std::io::Write;
use std::u32; use std::u32;
use std::marker::PhantomData;
use serde_crate as serde; use serde;
use byteorder::{WriteBytesExt, ByteOrder}; use byteorder::WriteBytesExt;
use super::{Result, Error, ErrorKind}; use super::{Result, Error, ErrorKind};
use super::SizeLimit; use ::config::Options;
use super::internal::SizeLimit;
/// An Serializer that encodes values directly into a Writer. /// An Serializer that encodes values directly into a Writer.
/// ///
@ -16,31 +16,31 @@ use super::SizeLimit;
/// ///
/// This struct should not be used often. /// This struct should not be used often.
/// For most cases, prefer the `encode_into` function. /// For most cases, prefer the `encode_into` function.
pub struct Serializer<W, E: ByteOrder> { pub(crate) struct Serializer<W, O: Options> {
writer: W, writer: W,
_phantom: PhantomData<E>, _options: O,
} }
impl<W: Write, E: ByteOrder> Serializer<W, E> { impl<W: Write, O: Options> Serializer<W, O> {
/// Creates a new Serializer with the given `Write`r. /// Creates a new Serializer with the given `Write`r.
pub fn new(w: W) -> Serializer<W, E> { pub fn new(w: W, options: O) -> Serializer<W, O> {
Serializer { Serializer {
writer: w, writer: w,
_phantom: PhantomData, _options: options,
} }
} }
} }
impl<'a, W: Write, E: ByteOrder> serde::Serializer for &'a mut Serializer<W, E> { impl<'a, W: Write, O: Options> serde::Serializer for &'a mut Serializer<W, O> {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
type SerializeSeq = Compound<'a, W, E>; type SerializeSeq = Compound<'a, W, O>;
type SerializeTuple = Compound<'a, W, E>; type SerializeTuple = Compound<'a, W, O>;
type SerializeTupleStruct = Compound<'a, W, E>; type SerializeTupleStruct = Compound<'a, W, O>;
type SerializeTupleVariant = Compound<'a, W, E>; type SerializeTupleVariant = Compound<'a, W, O>;
type SerializeMap = Compound<'a, W, E>; type SerializeMap = Compound<'a, W, O>;
type SerializeStruct = Compound<'a, W, E>; type SerializeStruct = Compound<'a, W, O>;
type SerializeStructVariant = Compound<'a, W, E>; type SerializeStructVariant = Compound<'a, W, O>;
fn serialize_unit(self) -> Result<()> { fn serialize_unit(self) -> Result<()> {
Ok(()) Ok(())
@ -61,15 +61,15 @@ impl<'a, W: Write, E: ByteOrder> serde::Serializer for &'a mut Serializer<W, E>
} }
fn serialize_u16(self, v: u16) -> Result<()> { fn serialize_u16(self, v: u16) -> Result<()> {
self.writer.write_u16::<E>(v).map_err(Into::into) self.writer.write_u16::<O::Endian>(v).map_err(Into::into)
} }
fn serialize_u32(self, v: u32) -> Result<()> { fn serialize_u32(self, v: u32) -> Result<()> {
self.writer.write_u32::<E>(v).map_err(Into::into) self.writer.write_u32::<O::Endian>(v).map_err(Into::into)
} }
fn serialize_u64(self, v: u64) -> Result<()> { fn serialize_u64(self, v: u64) -> Result<()> {
self.writer.write_u64::<E>(v).map_err(Into::into) self.writer.write_u64::<O::Endian>(v).map_err(Into::into)
} }
fn serialize_i8(self, v: i8) -> Result<()> { fn serialize_i8(self, v: i8) -> Result<()> {
@ -77,23 +77,23 @@ impl<'a, W: Write, E: ByteOrder> serde::Serializer for &'a mut Serializer<W, E>
} }
fn serialize_i16(self, v: i16) -> Result<()> { fn serialize_i16(self, v: i16) -> Result<()> {
self.writer.write_i16::<E>(v).map_err(Into::into) self.writer.write_i16::<O::Endian>(v).map_err(Into::into)
} }
fn serialize_i32(self, v: i32) -> Result<()> { fn serialize_i32(self, v: i32) -> Result<()> {
self.writer.write_i32::<E>(v).map_err(Into::into) self.writer.write_i32::<O::Endian>(v).map_err(Into::into)
} }
fn serialize_i64(self, v: i64) -> Result<()> { fn serialize_i64(self, v: i64) -> Result<()> {
self.writer.write_i64::<E>(v).map_err(Into::into) self.writer.write_i64::<O::Endian>(v).map_err(Into::into)
} }
fn serialize_f32(self, v: f32) -> Result<()> { fn serialize_f32(self, v: f32) -> Result<()> {
self.writer.write_f32::<E>(v).map_err(Into::into) self.writer.write_f32::<O::Endian>(v).map_err(Into::into)
} }
fn serialize_f64(self, v: f64) -> Result<()> { fn serialize_f64(self, v: f64) -> Result<()> {
self.writer.write_f64::<E>(v).map_err(Into::into) self.writer.write_f64::<O::Endian>(v).map_err(Into::into)
} }
fn serialize_str(self, v: &str) -> Result<()> { fn serialize_str(self, v: &str) -> Result<()> {
@ -209,17 +209,17 @@ impl<'a, W: Write, E: ByteOrder> serde::Serializer for &'a mut Serializer<W, E>
} }
} }
pub struct SizeChecker<S: SizeLimit> { pub(crate) struct SizeChecker<O: Options> {
pub size_limit: S, pub options: O,
} }
impl<S: SizeLimit> SizeChecker<S> { impl<O: Options> SizeChecker<O> {
pub fn new(size_limit: S) -> SizeChecker<S> { pub fn new(options: O) -> SizeChecker<O> {
SizeChecker { size_limit: size_limit } SizeChecker { options: options }
} }
fn add_raw(&mut self, size: u64) -> Result<()> { fn add_raw(&mut self, size: u64) -> Result<()> {
self.size_limit.add(size) self.options.limit().add(size)
} }
fn add_value<T>(&mut self, t: T) -> Result<()> { fn add_value<T>(&mut self, t: T) -> Result<()> {
@ -228,16 +228,16 @@ impl<S: SizeLimit> SizeChecker<S> {
} }
} }
impl<'a, S: SizeLimit> serde::Serializer for &'a mut SizeChecker<S> { impl<'a, O: Options> serde::Serializer for &'a mut SizeChecker<O> {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
type SerializeSeq = SizeCompound<'a, S>; type SerializeSeq = SizeCompound<'a, O>;
type SerializeTuple = SizeCompound<'a, S>; type SerializeTuple = SizeCompound<'a, O>;
type SerializeTupleStruct = SizeCompound<'a, S>; type SerializeTupleStruct = SizeCompound<'a, O>;
type SerializeTupleVariant = SizeCompound<'a, S>; type SerializeTupleVariant = SizeCompound<'a, O>;
type SerializeMap = SizeCompound<'a, S>; type SerializeMap = SizeCompound<'a, O>;
type SerializeStruct = SizeCompound<'a, S>; type SerializeStruct = SizeCompound<'a, O>;
type SerializeStructVariant = SizeCompound<'a, S>; type SerializeStructVariant = SizeCompound<'a, O>;
fn serialize_unit(self) -> Result<()> { fn serialize_unit(self) -> Result<()> {
Ok(()) Ok(())
@ -402,15 +402,14 @@ impl<'a, S: SizeLimit> serde::Serializer for &'a mut SizeChecker<S> {
} }
} }
#[doc(hidden)] pub(crate) struct Compound<'a, W: 'a, O: Options + 'a> {
pub struct Compound<'a, W: 'a, E: ByteOrder + 'a> { ser: &'a mut Serializer<W, O>,
ser: &'a mut Serializer<W, E>,
} }
impl<'a, W, E> serde::ser::SerializeSeq for Compound<'a, W, E> impl<'a, W, O> serde::ser::SerializeSeq for Compound<'a, W, O>
where where
W: Write, W: Write,
E: ByteOrder, O: Options,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -429,10 +428,10 @@ where
} }
} }
impl<'a, W, E> serde::ser::SerializeTuple for Compound<'a, W, E> impl<'a, W, O> serde::ser::SerializeTuple for Compound<'a, W, O>
where where
W: Write, W: Write,
E: ByteOrder, O: Options,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -451,10 +450,10 @@ where
} }
} }
impl<'a, W, E> serde::ser::SerializeTupleStruct for Compound<'a, W, E> impl<'a, W, O> serde::ser::SerializeTupleStruct for Compound<'a, W, O>
where where
W: Write, W: Write,
E: ByteOrder, O: Options,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -473,10 +472,10 @@ where
} }
} }
impl<'a, W, E> serde::ser::SerializeTupleVariant for Compound<'a, W, E> impl<'a, W, O> serde::ser::SerializeTupleVariant for Compound<'a, W, O>
where where
W: Write, W: Write,
E: ByteOrder, O: Options,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -495,10 +494,10 @@ where
} }
} }
impl<'a, W, E> serde::ser::SerializeMap for Compound<'a, W, E> impl<'a, W, O> serde::ser::SerializeMap for Compound<'a, W, O>
where where
W: Write, W: Write,
E: ByteOrder, O: Options,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -525,10 +524,10 @@ where
} }
} }
impl<'a, W, E> serde::ser::SerializeStruct for Compound<'a, W, E> impl<'a, W, O> serde::ser::SerializeStruct for Compound<'a, W, O>
where where
W: Write, W: Write,
E: ByteOrder, O: Options,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -547,10 +546,10 @@ where
} }
} }
impl<'a, W, E> serde::ser::SerializeStructVariant for Compound<'a, W, E> impl<'a, W, O> serde::ser::SerializeStructVariant for Compound<'a, W, O>
where where
W: Write, W: Write,
E: ByteOrder, O: Options,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -569,12 +568,11 @@ where
} }
} }
#[doc(hidden)] pub(crate) struct SizeCompound<'a, S: Options + 'a> {
pub struct SizeCompound<'a, S: SizeLimit + 'a> {
ser: &'a mut SizeChecker<S>, ser: &'a mut SizeChecker<S>,
} }
impl<'a, S: SizeLimit> serde::ser::SerializeSeq for SizeCompound<'a, S> { impl<'a, O: Options> serde::ser::SerializeSeq for SizeCompound<'a, O> {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -592,7 +590,7 @@ impl<'a, S: SizeLimit> serde::ser::SerializeSeq for SizeCompound<'a, S> {
} }
} }
impl<'a, S: SizeLimit> serde::ser::SerializeTuple for SizeCompound<'a, S> { impl<'a, O: Options> serde::ser::SerializeTuple for SizeCompound<'a, O> {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -610,7 +608,7 @@ impl<'a, S: SizeLimit> serde::ser::SerializeTuple for SizeCompound<'a, S> {
} }
} }
impl<'a, S: SizeLimit> serde::ser::SerializeTupleStruct for SizeCompound<'a, S> { impl<'a, O: Options> serde::ser::SerializeTupleStruct for SizeCompound<'a, O> {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -628,7 +626,7 @@ impl<'a, S: SizeLimit> serde::ser::SerializeTupleStruct for SizeCompound<'a, S>
} }
} }
impl<'a, S: SizeLimit> serde::ser::SerializeTupleVariant for SizeCompound<'a, S> { impl<'a, O: Options> serde::ser::SerializeTupleVariant for SizeCompound<'a, O> {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -646,7 +644,7 @@ impl<'a, S: SizeLimit> serde::ser::SerializeTupleVariant for SizeCompound<'a, S>
} }
} }
impl<'a, S: SizeLimit + 'a> serde::ser::SerializeMap for SizeCompound<'a, S> { impl<'a, O: Options+ 'a> serde::ser::SerializeMap for SizeCompound<'a, O> {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -672,7 +670,7 @@ impl<'a, S: SizeLimit + 'a> serde::ser::SerializeMap for SizeCompound<'a, S> {
} }
} }
impl<'a, S: SizeLimit> serde::ser::SerializeStruct for SizeCompound<'a, S> { impl<'a, O: Options> serde::ser::SerializeStruct for SizeCompound<'a, O> {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -690,7 +688,7 @@ impl<'a, S: SizeLimit> serde::ser::SerializeStruct for SizeCompound<'a, S> {
} }
} }
impl<'a, S: SizeLimit> serde::ser::SerializeStructVariant for SizeCompound<'a, S> { impl<'a, O: Options> serde::ser::SerializeStructVariant for SizeCompound<'a, O> {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;

View File

@ -2,48 +2,43 @@
extern crate serde_derive; extern crate serde_derive;
extern crate bincode; extern crate bincode;
extern crate byteorder;
extern crate serde; extern crate serde;
extern crate serde_bytes; extern crate serde_bytes;
extern crate byteorder;
use std::fmt::Debug; use std::fmt::Debug;
use std::collections::HashMap; use std::collections::HashMap;
use std::borrow::Cow; use std::borrow::Cow;
use bincode::{Bounded, Infinite}; use bincode::{config, deserialize, deserialize_from, serialize, serialized_size, ErrorKind, Result};
use bincode::{serialized_size, ErrorKind, Result};
use bincode::internal::{deserialize, deserialize_from, serialize};
use bincode::serialize as serialize_little;
use bincode::deserialize as deserialize_little;
use bincode::deserialize_from as deserialize_from_little;
fn the_same<V>(element: V) fn the_same<V>(element: V)
where where
V: serde::Serialize + serde::de::DeserializeOwned + PartialEq + Debug + 'static, V: serde::Serialize + serde::de::DeserializeOwned + PartialEq + Debug + 'static,
{ {
let size = serialized_size(&element); let size = serialized_size(&element).unwrap();
{ {
let encoded = serialize_little(&element, Infinite).unwrap(); let encoded = serialize(&element).unwrap();
let decoded = deserialize_little(&encoded[..]).unwrap(); let decoded = deserialize(&encoded[..]).unwrap();
assert_eq!(element, decoded); assert_eq!(element, decoded);
assert_eq!(size, encoded.len() as u64); assert_eq!(size, encoded.len() as u64);
} }
{ {
let encoded = serialize::<_, _, byteorder::BigEndian>(&element, Infinite).unwrap(); let encoded = config().big_endian().serialize(&element).unwrap();
let decoded = deserialize::<_, byteorder::BigEndian>(&encoded[..]).unwrap(); let decoded = config().big_endian().deserialize(&encoded[..]).unwrap();
let decoded_reader = let decoded_reader = config()
deserialize_from::<_, _, _, byteorder::BigEndian>(&mut &encoded[..], Infinite).unwrap(); .big_endian()
.deserialize_from(&mut &encoded[..])
.unwrap();
assert_eq!(element, decoded); assert_eq!(element, decoded);
assert_eq!(element, decoded_reader); assert_eq!(element, decoded_reader);
assert_eq!(size, encoded.len() as u64); assert_eq!(size, encoded.len() as u64);
} }
} }
#[test] #[test]
fn test_numbers() { fn test_numbers() {
// unsigned positive // unsigned positive
@ -213,12 +208,11 @@ fn test_fixed_size_array() {
#[test] #[test]
fn deserializing_errors() { fn deserializing_errors() {
match *deserialize::<bool>(&vec![0xA][..]).unwrap_err() {
match *deserialize_little::<bool>(&vec![0xA][..]).unwrap_err() {
ErrorKind::InvalidBoolEncoding(0xA) => {} ErrorKind::InvalidBoolEncoding(0xA) => {}
_ => panic!(), _ => panic!(),
} }
match *deserialize_little::<String>(&vec![1, 0, 0, 0, 0, 0, 0, 0, 0xFF][..]).unwrap_err() { match *deserialize::<String>(&vec![1, 0, 0, 0, 0, 0, 0, 0, 0xFF][..]).unwrap_err() {
ErrorKind::InvalidUtf8Encoding(_) => {} ErrorKind::InvalidUtf8Encoding(_) => {}
_ => panic!(), _ => panic!(),
} }
@ -230,12 +224,12 @@ fn deserializing_errors() {
Two, Two,
}; };
match *deserialize_little::<Test>(&vec![0, 0, 0, 5][..]).unwrap_err() { match *deserialize::<Test>(&vec![0, 0, 0, 5][..]).unwrap_err() {
// Error message comes from serde // Error message comes from serde
ErrorKind::Custom(_) => {} ErrorKind::Custom(_) => {}
_ => panic!(), _ => panic!(),
} }
match *deserialize_little::<Option<u8>>(&vec![5, 0][..]).unwrap_err() { match *deserialize::<Option<u8>>(&vec![5, 0][..]).unwrap_err() {
ErrorKind::InvalidTagEncoding(_) => {} ErrorKind::InvalidTagEncoding(_) => {}
_ => panic!(), _ => panic!(),
} }
@ -244,13 +238,11 @@ fn deserializing_errors() {
#[test] #[test]
fn too_big_deserialize() { fn too_big_deserialize() {
let serialized = vec![0, 0, 0, 3]; let serialized = vec![0, 0, 0, 3];
let deserialized: Result<u32> = let deserialized: Result<u32> = config().limit(3).deserialize_from(&mut &serialized[..]);
deserialize_from_little::<_, _, _>(&mut &serialized[..], Bounded(3));
assert!(deserialized.is_err()); assert!(deserialized.is_err());
let serialized = vec![0, 0, 0, 3]; let serialized = vec![0, 0, 0, 3];
let deserialized: Result<u32> = let deserialized: Result<u32> = config().limit(4).deserialize_from(&mut &serialized[..]);
deserialize_from_little::<_, _, _>(&mut &serialized[..], Bounded(4));
assert!(deserialized.is_ok()); assert!(deserialized.is_ok());
} }
@ -258,8 +250,11 @@ fn too_big_deserialize() {
fn char_serialization() { fn char_serialization() {
let chars = "Aa\0☺♪"; let chars = "Aa\0☺♪";
for c in chars.chars() { for c in chars.chars() {
let encoded = serialize_little(&c, Bounded(4)).expect("serializing char failed"); let encoded = config()
let decoded: char = deserialize_little(&encoded).expect("deserializing failed"); .limit(4)
.serialize(&c)
.expect("serializing char failed");
let decoded: char = deserialize(&encoded).expect("deserializing failed");
assert_eq!(decoded, c); assert_eq!(decoded, c);
} }
} }
@ -267,48 +262,60 @@ fn char_serialization() {
#[test] #[test]
fn too_big_char_deserialize() { fn too_big_char_deserialize() {
let serialized = vec![0x41]; let serialized = vec![0x41];
let deserialized: Result<char> = let deserialized: Result<char> = config().limit(1).deserialize_from(&mut &serialized[..]);
deserialize_from_little::<_, _, _>(&mut &serialized[..], Bounded(1));
assert!(deserialized.is_ok()); assert!(deserialized.is_ok());
assert_eq!(deserialized.unwrap(), 'A'); assert_eq!(deserialized.unwrap(), 'A');
} }
#[test] #[test]
fn too_big_serialize() { fn too_big_serialize() {
assert!(serialize_little(&0u32, Bounded(3)).is_err()); assert!(config().limit(3).serialize(&0u32).is_err());
assert!(serialize_little(&0u32, Bounded(4)).is_ok()); assert!(config().limit(4).serialize(&0u32).is_ok());
assert!(serialize_little(&"abcde", Bounded(8 + 4)).is_err());
assert!(serialize_little(&"abcde", Bounded(8 + 5)).is_ok());
}
#[test]
fn test_proxy_encoded_size() {
assert!(serialized_size(&0u8) == 1);
assert!(serialized_size(&0u16) == 2);
assert!(serialized_size(&0u32) == 4);
assert!(serialized_size(&0u64) == 8);
// length isize stored as u64
assert!(serialized_size(&"") == 8);
assert!(serialized_size(&"a") == 8 + 1);
assert!(serialized_size(&vec![0u32, 1u32, 2u32]) == 8 + 3 * (4))
assert!(config().limit(8 + 4).serialize(&"abcde").is_err());
assert!(config().limit(8 + 5).serialize(&"abcde").is_ok());
} }
#[test] #[test]
fn test_serialized_size() { fn test_serialized_size() {
assert!(serialized_size(&0u8) == 1); assert!(serialized_size(&0u8).unwrap() == 1);
assert!(serialized_size(&0u16) == 2); assert!(serialized_size(&0u16).unwrap() == 2);
assert!(serialized_size(&0u32) == 4); assert!(serialized_size(&0u32).unwrap() == 4);
assert!(serialized_size(&0u64) == 8); assert!(serialized_size(&0u64).unwrap() == 8);
// length isize stored as u64 // length isize stored as u64
assert!(serialized_size(&"") == 8); assert!(serialized_size(&"").unwrap() == 8);
assert!(serialized_size(&"a") == 8 + 1); assert!(serialized_size(&"a").unwrap() == 8 + 1);
assert!(serialized_size(&vec![0u32, 1u32, 2u32]) == 8 + 3 * (4)) assert!(serialized_size(&vec![0u32, 1u32, 2u32]).unwrap() == 8 + 3 * (4));
}
#[test]
fn test_serialized_size_bounded() {
// JUST RIGHT
assert!(config().limit(1).serialized_size(&0u8).unwrap() == 1);
assert!(config().limit(2).serialized_size(&0u16).unwrap() == 2);
assert!(config().limit(4).serialized_size(&0u32).unwrap() == 4);
assert!(config().limit(8).serialized_size(&0u64).unwrap() == 8);
assert!(config().limit(8).serialized_size(&"").unwrap() == 8);
assert!(config().limit(8 + 1).serialized_size(&"a").unwrap() == 8 + 1);
assert!(
config()
.limit(8 + 3 * 4)
.serialized_size(&vec![0u32, 1u32, 2u32])
.unwrap() == 8 + 3 * 4
);
// Below
assert!(config().limit(0).serialized_size(&0u8).is_err());
assert!(config().limit(1).serialized_size(&0u16).is_err());
assert!(config().limit(3).serialized_size(&0u32).is_err());
assert!(config().limit(7).serialized_size(&0u64).is_err());
assert!(config().limit(7).serialized_size(&"").is_err());
assert!(config().limit(8 + 0).serialized_size(&"a").is_err());
assert!(
config()
.limit(8 + 3 * 4 - 1)
.serialized_size(&vec![0u32, 1u32, 2u32]).is_err());
} }
#[test] #[test]
@ -331,10 +338,8 @@ fn test_cow_serialize() {
// Test 1 // Test 1
{ {
let serialized = let serialized = serialize(&Message::M1(Cow::Borrowed(&large_object))).unwrap();
serialize_little(&Message::M1(Cow::Borrowed(&large_object)), Infinite).unwrap(); let deserialized: Message<'static> = deserialize_from(&mut &serialized[..]).unwrap();
let deserialized: Message<'static> =
deserialize_from_little(&mut &serialized[..], Infinite).unwrap();
match deserialized { match deserialized {
Message::M1(b) => assert!(&b.into_owned() == &large_object), Message::M1(b) => assert!(&b.into_owned() == &large_object),
@ -344,10 +349,8 @@ fn test_cow_serialize() {
// Test 2 // Test 2
{ {
let serialized = let serialized = serialize(&Message::M2(Cow::Borrowed(&large_map))).unwrap();
serialize_little(&Message::M2(Cow::Borrowed(&large_map)), Infinite).unwrap(); let deserialized: Message<'static> = deserialize_from(&mut &serialized[..]).unwrap();
let deserialized: Message<'static> =
deserialize_from_little(&mut &serialized[..], Infinite).unwrap();
match deserialized { match deserialized {
Message::M2(b) => assert!(&b.into_owned() == &large_map), Message::M2(b) => assert!(&b.into_owned() == &large_map),
@ -359,9 +362,8 @@ fn test_cow_serialize() {
#[test] #[test]
fn test_strbox_serialize() { fn test_strbox_serialize() {
let strx: &'static str = "hello world"; let strx: &'static str = "hello world";
let serialized = serialize_little(&Cow::Borrowed(strx), Infinite).unwrap(); let serialized = serialize(&Cow::Borrowed(strx)).unwrap();
let deserialized: Cow<'static, String> = let deserialized: Cow<'static, String> = deserialize_from(&mut &serialized[..]).unwrap();
deserialize_from_little(&mut &serialized[..], Infinite).unwrap();
let stringx: String = deserialized.into_owned(); let stringx: String = deserialized.into_owned();
assert!(strx == &stringx[..]); assert!(strx == &stringx[..]);
} }
@ -369,10 +371,9 @@ fn test_strbox_serialize() {
#[test] #[test]
fn test_slicebox_serialize() { fn test_slicebox_serialize() {
let slice = [1u32, 2, 3, 4, 5]; let slice = [1u32, 2, 3, 4, 5];
let serialized = serialize_little(&Cow::Borrowed(&slice[..]), Infinite).unwrap(); let serialized = serialize(&Cow::Borrowed(&slice[..])).unwrap();
println!("{:?}", serialized); println!("{:?}", serialized);
let deserialized: Cow<'static, Vec<u32>> = let deserialized: Cow<'static, Vec<u32>> = deserialize_from(&mut &serialized[..]).unwrap();
deserialize_from_little(&mut &serialized[..], Infinite).unwrap();
{ {
let sb: &[u32] = &deserialized; let sb: &[u32] = &deserialized;
assert!(slice == sb); assert!(slice == sb);
@ -383,7 +384,7 @@ fn test_slicebox_serialize() {
#[test] #[test]
fn test_multi_strings_serialize() { fn test_multi_strings_serialize() {
assert!(serialize_little(&("foo", "bar", "baz"), Infinite).is_ok()); assert!(serialize(&("foo", "bar", "baz")).is_ok());
} }
#[test] #[test]
@ -394,14 +395,16 @@ fn test_oom_protection() {
len: u64, len: u64,
byte: u8, byte: u8,
} }
let x = serialize_little( let x = config()
&FakeVec { .limit(10)
.serialize(&FakeVec {
len: 0xffffffffffffffffu64, len: 0xffffffffffffffffu64,
byte: 1, byte: 1,
}, })
Bounded(10), .unwrap();
).unwrap(); let y: Result<Vec<u8>> = config()
let y: Result<Vec<u8>> = deserialize_from_little(&mut Cursor::new(&x[..]), Bounded(10)); .limit(10)
.deserialize_from(&mut Cursor::new(&x[..]));
assert!(y.is_err()); assert!(y.is_err());
} }
@ -409,8 +412,8 @@ fn test_oom_protection() {
fn path_buf() { fn path_buf() {
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
let path = Path::new("foo").to_path_buf(); let path = Path::new("foo").to_path_buf();
let serde_encoded = serialize_little(&path, Infinite).unwrap(); let serde_encoded = serialize(&path).unwrap();
let decoded: PathBuf = deserialize_little(&serde_encoded).unwrap(); let decoded: PathBuf = deserialize(&serde_encoded).unwrap();
assert!(path.to_str() == decoded.to_str()); assert!(path.to_str() == decoded.to_str());
} }
@ -419,8 +422,8 @@ fn bytes() {
use serde_bytes::Bytes; use serde_bytes::Bytes;
let data = b"abc\0123"; let data = b"abc\0123";
let s = serialize_little(&data[..], Infinite).unwrap(); let s = serialize(&data[..]).unwrap();
let s2 = serialize_little(&Bytes::new(data), Infinite).unwrap(); let s2 = serialize(&Bytes::new(data)).unwrap();
assert_eq!(s[..], s2[..]); assert_eq!(s[..], s2[..]);
} }
@ -434,8 +437,8 @@ fn serde_bytes() {
#[test] #[test]
fn endian_difference() { fn endian_difference() {
let x = 10u64; let x = 10u64;
let little = serialize_little(&x, Infinite).unwrap(); let little = serialize(&x).unwrap();
let big = serialize::<_, _, byteorder::BigEndian>(&x, Infinite).unwrap(); let big = config().big_endian().serialize(&x).unwrap();
assert_ne!(little, big); assert_ne!(little, big);
} }
@ -452,8 +455,8 @@ fn test_zero_copy_parse() {
borrowed_bytes: &[0, 1, 2, 3], borrowed_bytes: &[0, 1, 2, 3],
}; };
{ {
let encoded = serialize_little(&f, Infinite).unwrap(); let encoded = serialize(&f).unwrap();
let out: Foo = deserialize_little(&encoded[..]).unwrap(); let out: Foo = deserialize(&encoded[..]).unwrap();
assert_eq!(out, f); assert_eq!(out, f);
} }
} }
@ -463,6 +466,6 @@ fn not_human_readable() {
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
let ip = Ipv4Addr::new(1, 2, 3, 4); let ip = Ipv4Addr::new(1, 2, 3, 4);
the_same(ip); the_same(ip);
assert_eq!(&ip.octets()[..], &serialize_little(&ip, Infinite).unwrap()[..]); assert_eq!(&ip.octets()[..], &serialize(&ip).unwrap()[..]);
assert_eq!(::std::mem::size_of::<Ipv4Addr>() as u64, serialized_size(&ip)); assert_eq!(::std::mem::size_of::<Ipv4Addr>() as u64, serialized_size(&ip).unwrap());
} }