This commit is contained in:
Ty Overby 2018-09-04 16:25:10 -07:00
parent 0e7b438bb4
commit d8704644d7
8 changed files with 132 additions and 123 deletions

View File

@ -1,14 +1,14 @@
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 error::Result;
use serde;
use std::io::{Read, Write};
use std::marker::PhantomData;
use {DeserializerAcceptor, SerializerAcceptor};
use self::LimitOption::*;
use self::EndianOption::*;
use self::LimitOption::*;
struct DefaultOptions(Infinite);
@ -110,7 +110,6 @@ pub(crate) struct WithOtherEndian<O: Options, E: ByteOrder> {
_endian: PhantomData<E>,
}
impl<O: Options, L: SizeLimit> WithOtherLimit<O, L> {
#[inline(always)]
pub(crate) fn new(options: O, limit: L) -> WithOtherLimit<O, L> {
@ -179,7 +178,7 @@ macro_rules! config_map {
$call
}
}
}
};
}
impl Config {
@ -217,7 +216,7 @@ impl Config {
/// Sets the endianness to big-endian
#[inline(always)]
pub fn big_endian(&mut self) -> &mut Self {
self.endian= EndianOption::Big;
self.endian = EndianOption::Big;
self
}
@ -245,7 +244,11 @@ impl Config {
/// 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<()> {
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))
}
@ -258,17 +261,21 @@ impl Config {
/// TODO: document
#[doc(hidden)]
#[inline(always)]
pub fn deserialize_in_place<'a, R, T: >(&self, reader: R, place: &mut T) -> Result<()>
pub fn deserialize_in_place<'a, R, T>(&self, reader: R, place: &mut T) -> Result<()>
where
R: BincodeRead<'a>,
T: serde::de::Deserialize<'a>
T: serde::de::Deserialize<'a>,
{
config_map!(self, opts => ::internal::deserialize_in_place(reader, opts, place))
}
/// Deserializes a slice of bytes with state `seed` using this configuration.
#[inline(always)]
pub fn deserialize_seed<'a, T: serde::de::DeserializeSeed<'a>>(&self, seed: T, bytes: &'a [u8]) -> Result<T::Value> {
pub fn deserialize_seed<'a, T: serde::de::DeserializeSeed<'a>>(
&self,
seed: T,
bytes: &'a [u8],
) -> Result<T::Value> {
config_map!(self, opts => ::internal::deserialize_seed(seed, bytes, opts))
}
@ -276,7 +283,10 @@ impl Config {
///
/// 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> {
pub fn deserialize_from<R: Read, T: serde::de::DeserializeOwned>(
&self,
reader: R,
) -> Result<T> {
config_map!(self, opts => ::internal::deserialize_from(reader, opts))
}
@ -286,16 +296,20 @@ impl Config {
///
/// 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> {
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.
/// NOT A PART OF THE STABLE PUBLIC API
#[doc(hidden)]
pub fn with_deserializer<'a, A, R>(&self, reader: R, acceptor: A) -> A::Output
where A: DeserializerAcceptor<'a>,
R: BincodeRead<'a>
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);
@ -307,8 +321,9 @@ impl Config {
/// NOT A PART OF THE STABLE PUBLIC API
#[doc(hidden)]
pub fn with_serializer<A, W>(&self, writer: W, acceptor: A) -> A::Output
where A: SerializerAcceptor,
W: Write
where
A: SerializerAcceptor,
W: Write,
{
config_map!(self, opts => {
let mut serializer = ::ser::Serializer::new(writer, opts);

View File

@ -1,13 +1,13 @@
use config::Options;
use std::io::Read;
use ::config::Options;
use serde;
use byteorder::ReadBytesExt;
use serde::de::IntoDeserializer;
use serde::de::Error as DeError;
use ::{Error, ErrorKind, Result};
use ::internal::SizeLimit;
use self::read::BincodeRead;
use byteorder::ReadBytesExt;
use internal::SizeLimit;
use serde;
use serde::de::Error as DeError;
use serde::de::IntoDeserializer;
use {Error, ErrorKind, Result};
pub mod read;
@ -234,12 +234,16 @@ where
V: serde::de::Visitor<'de>,
{
impl<'de, 'a, R: 'a, O> serde::de::EnumAccess<'de> for &'a mut Deserializer<R, O>
where R: BincodeRead<'de>, O: Options {
where
R: BincodeRead<'de>,
O: Options,
{
type Error = Error;
type Variant = Self;
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant)>
where V: serde::de::DeserializeSeed<'de>,
where
V: serde::de::DeserializeSeed<'de>,
{
let idx: u32 = try!(serde::de::Deserialize::deserialize(&mut *self));
let val: Result<_> = seed.deserialize(idx.into_deserializer());
@ -259,13 +263,9 @@ where
len: usize,
}
impl<
'de,
'a,
'b: 'a,
R: BincodeRead<'de> + 'b,
O: Options,
> serde::de::SeqAccess<'de> for Access<'a, R, O> {
impl<'de, 'a, 'b: 'a, R: BincodeRead<'de> + 'b, O: Options> serde::de::SeqAccess<'de>
for Access<'a, R, O>
{
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
@ -325,13 +325,9 @@ where
len: usize,
}
impl<
'de,
'a,
'b: 'a,
R: BincodeRead<'de> + 'b,
O: Options,
> serde::de::MapAccess<'de> for Access<'a, R, O> {
impl<'de, 'a, 'b: 'a, R: BincodeRead<'de> + 'b, O: Options> serde::de::MapAccess<'de>
for Access<'a, R, O>
{
type Error = Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
@ -434,7 +430,10 @@ where
}
impl<'de, 'a, R, O> serde::de::VariantAccess<'de> for &'a mut Deserializer<R, O>
where R: BincodeRead<'de>, O: Options{
where
R: BincodeRead<'de>,
O: Options,
{
type Error = Error;
fn unit_variant(self) -> Result<()> {
@ -442,44 +441,43 @@ where R: BincodeRead<'de>, O: Options{
}
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
where T: serde::de::DeserializeSeed<'de>,
where
T: serde::de::DeserializeSeed<'de>,
{
serde::de::DeserializeSeed::deserialize(seed, self)
}
fn tuple_variant<V>(self,
len: usize,
visitor: V) -> Result<V::Value>
where V: serde::de::Visitor<'de>,
fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value>
where
V: serde::de::Visitor<'de>,
{
serde::de::Deserializer::deserialize_tuple(self, len, visitor)
}
fn struct_variant<V>(self,
fields: &'static [&'static str],
visitor: V) -> Result<V::Value>
where V: serde::de::Visitor<'de>,
fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value>
where
V: serde::de::Visitor<'de>,
{
serde::de::Deserializer::deserialize_tuple(self, fields.len(), visitor)
}
}
static UTF8_CHAR_WIDTH: [u8; 256] = [
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0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+1,6 @@
use std::io;
use error::Result;
use serde;
use std::io;
/// An optional Read trait for advanced Bincode usage.
///
@ -78,9 +78,10 @@ impl<R: io::Read> io::Read for IoReader<R> {
impl<'storage> SliceReader<'storage> {
#[inline(always)]
fn unexpected_eof() -> Box<::ErrorKind> {
return Box::new(::ErrorKind::Io(
io::Error::new(io::ErrorKind::UnexpectedEof, ""),
));
return Box::new(::ErrorKind::Io(io::Error::new(
io::ErrorKind::UnexpectedEof,
"",
)));
}
}

View File

@ -1,7 +1,7 @@
use std::io;
use std::{error, fmt};
use std::str::Utf8Error;
use std::error::Error as StdError;
use std::io;
use std::str::Utf8Error;
use std::{error, fmt};
use serde;
@ -47,14 +47,14 @@ impl StdError for ErrorKind {
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::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,
}
}
@ -91,16 +91,12 @@ impl fmt::Display for ErrorKind {
ErrorKind::InvalidTagEncoding(tag) => {
write!(fmt, "{}, found {}", self.description(), tag)
}
ErrorKind::SequenceMustHaveLength => {
write!(fmt, "{}", self.description())
}
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::DeserializeAnyNotSupported => write!(
fmt,
"Bincode does not support the serde::Deserializer::deserialize_any method"
),
ErrorKind::Custom(ref s) => s.fmt(fmt),
}
}

View File

@ -1,5 +1,5 @@
use std::io::{Read, Write};
use serde;
use std::io::{Read, Write};
use config::{Options, OptionsExt};
use de::read::BincodeRead;
@ -114,7 +114,6 @@ where
serde::Deserialize::deserialize(&mut deserializer)
}
pub(crate) fn deserialize_seed<'a, T, O>(seed: T, bytes: &'a [u8], options: O) -> Result<T::Value>
where
T: serde::de::DeserializeSeed<'a>,
@ -134,7 +133,6 @@ pub(crate) trait SizeLimit: Clone {
fn limit(&self) -> Option<u64>;
}
/// A SizeLimit that restricts serialized or deserialized messages from
/// exceeding a certain byte length.
#[derive(Copy, Clone)]

View File

@ -40,14 +40,14 @@ extern crate byteorder;
extern crate serde;
mod config;
mod ser;
mod error;
mod de;
mod error;
mod internal;
mod ser;
pub use error::{Error, ErrorKind, Result};
pub use config::Config;
pub use de::read::{BincodeRead, IoReader, SliceReader};
pub use error::{Error, ErrorKind, Result};
/// An object that implements this trait can be passed a
/// serde::Deserializer without knowing its concrete type.
@ -135,7 +135,7 @@ where
pub fn deserialize_in_place<'a, R, T>(reader: R, place: &mut T) -> Result<()>
where
T: serde::de::Deserialize<'a>,
R: BincodeRead<'a>
R: BincodeRead<'a>,
{
config().deserialize_in_place(reader, place)
}
@ -159,9 +159,10 @@ where
/// Executes the acceptor with a serde::Deserializer instance.
/// NOT A PART OF THE STABLE PUBLIC API
#[doc(hidden)]
pub fn with_deserializer<'a, A, R>(reader: R, acceptor: A) -> A::Output
where A: DeserializerAcceptor<'a>,
R: BincodeRead<'a>
pub fn with_deserializer<'a, A, R>(reader: R, acceptor: A) -> A::Output
where
A: DeserializerAcceptor<'a>,
R: BincodeRead<'a>,
{
config().with_deserializer(reader, acceptor)
}
@ -170,8 +171,9 @@ where A: DeserializerAcceptor<'a>,
/// NOT A PART OF THE STABLE PUBLIC API
#[doc(hidden)]
pub fn with_serializer<A, W>(writer: W, acceptor: A) -> A::Output
where A: SerializerAcceptor,
W: std::io::Write
where
A: SerializerAcceptor,
W: std::io::Write,
{
config().with_serializer(writer, acceptor)
}

View File

@ -5,9 +5,9 @@ use serde;
use byteorder::WriteBytesExt;
use super::{Result, Error, ErrorKind};
use ::config::Options;
use super::internal::SizeLimit;
use super::{Error, ErrorKind, Result};
use config::Options;
/// An Serializer that encodes values directly into a Writer.
///
@ -51,9 +51,9 @@ impl<'a, W: Write, O: Options> serde::Serializer for &'a mut Serializer<W, O> {
}
fn serialize_bool(self, v: bool) -> Result<()> {
self.writer.write_u8(if v { 1 } else { 0 }).map_err(
Into::into,
)
self.writer
.write_u8(if v { 1 } else { 0 })
.map_err(Into::into)
}
fn serialize_u8(self, v: u8) -> Result<()> {
@ -130,9 +130,9 @@ impl<'a, W: Write, O: Options> serde::Serializer for &'a mut Serializer<W, O> {
}
fn serialize_char(self, c: char) -> Result<()> {
self.writer.write_all(encode_utf8(c).as_slice()).map_err(
Into::into,
)
self.writer
.write_all(encode_utf8(c).as_slice())
.map_err(Into::into)
}
fn serialize_bytes(self, v: &[u8]) -> Result<()> {
@ -682,7 +682,7 @@ impl<'a, O: Options> serde::ser::SerializeTupleVariant for SizeCompound<'a, O> {
}
}
impl<'a, O: Options+ 'a> serde::ser::SerializeMap for SizeCompound<'a, O> {
impl<'a, O: Options + 'a> serde::ser::SerializeMap for SizeCompound<'a, O> {
type Ok = ();
type Error = Error;

View File

@ -322,7 +322,8 @@ fn test_serialized_size_bounded() {
config()
.limit(8 + 3 * 4)
.serialized_size(&vec![0u32, 1u32, 2u32])
.unwrap() == 8 + 3 * 4
.unwrap()
== 8 + 3 * 4
);
// Below
assert!(config().limit(0).serialized_size(&0u8).is_err());
@ -350,7 +351,6 @@ fn test_cow_serialize() {
let mut large_map = HashMap::new();
large_map.insert(1, 2);
#[derive(Serialize, Deserialize, Debug)]
enum Message<'a> {
M1(Cow<'a, Vec<u32>>),
@ -421,8 +421,7 @@ fn test_oom_protection() {
.serialize(&FakeVec {
len: 0xffffffffffffffffu64,
byte: 1,
})
.unwrap();
}).unwrap();
let y: Result<Vec<u8>> = config()
.limit(10)
.deserialize_from(&mut Cursor::new(&x[..]));
@ -454,7 +453,6 @@ fn serde_bytes() {
the_same(ByteBuf::from(vec![1, 2, 3, 4, 5]));
}
#[test]
fn endian_difference() {
let x = 10u64;
@ -495,9 +493,10 @@ fn test_zero_copy_parse_deserialize_into() {
impl<'storage> SliceReader<'storage> {
#[inline(always)]
fn unexpected_eof() -> Box<::ErrorKind> {
return Box::new(::ErrorKind::Io(
io::Error::new(io::ErrorKind::UnexpectedEof, ""),
));
return Box::new(::ErrorKind::Io(io::Error::new(
io::ErrorKind::UnexpectedEof,
"",
)));
}
}