diff --git a/src/config.rs b/src/config.rs index 73a4423..2d08283 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 { _endian: PhantomData, } - impl WithOtherLimit { #[inline(always)] pub(crate) fn new(options: O, limit: L) -> WithOtherLimit { @@ -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(&self, w: W, t: &T) -> Result<()> { + pub fn serialize_into( + &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 { + pub fn deserialize_seed<'a, T: serde::de::DeserializeSeed<'a>>( + &self, + seed: T, + bytes: &'a [u8], + ) -> Result { 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(&self, reader: R) -> Result { + pub fn deserialize_from( + &self, + reader: R, + ) -> Result { 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 { + pub fn deserialize_from_custom<'a, R: BincodeRead<'a>, T: serde::de::DeserializeOwned>( + &self, + reader: R, + ) -> Result { 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(&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); diff --git a/src/de/mod.rs b/src/de/mod.rs index 1b6f125..5dc9d86 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -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 - where R: BincodeRead<'de>, O: Options { + where + R: BincodeRead<'de>, + O: Options, + { type Error = Error; type Variant = Self; fn variant_seed(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(&mut self, seed: T) -> Result> @@ -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(&mut self, seed: K) -> Result> @@ -434,7 +430,10 @@ where } impl<'de, 'a, R, O> serde::de::VariantAccess<'de> for &'a mut Deserializer -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(self, seed: T) -> Result - where T: serde::de::DeserializeSeed<'de>, + where + T: serde::de::DeserializeSeed<'de>, { serde::de::DeserializeSeed::deserialize(seed, self) } - fn tuple_variant(self, - len: usize, - visitor: V) -> Result - where V: serde::de::Visitor<'de>, + fn tuple_variant(self, len: usize, visitor: V) -> Result + where + V: serde::de::Visitor<'de>, { serde::de::Deserializer::deserialize_tuple(self, len, visitor) } - fn struct_variant(self, - fields: &'static [&'static str], - visitor: V) -> Result - where V: serde::de::Visitor<'de>, + fn struct_variant(self, fields: &'static [&'static str], visitor: V) -> Result + 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,0,0,0,0,0,0, // 0xBF -0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2, -2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF -3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF -4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF + 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, 0, 0, 0, 0, 0, + 0, // 0xBF + 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, // 0xDF + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xEF + 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xFF ]; // This function is a copy of core::str::utf8_char_width diff --git a/src/de/read.rs b/src/de/read.rs index 3ddcee5..45aa5d7 100644 --- a/src/de/read.rs +++ b/src/de/read.rs @@ -1,6 +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 io::Read for IoReader { 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, + "", + ))); } } diff --git a/src/error.rs b/src/error.rs index f5cfca3..1f52424 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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), } } diff --git a/src/internal.rs b/src/internal.rs index 4241784..c614781 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -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 where T: serde::de::DeserializeSeed<'a>, @@ -134,7 +133,6 @@ pub(crate) trait SizeLimit: Clone { fn limit(&self) -> Option; } - /// A SizeLimit that restricts serialized or deserialized messages from /// exceeding a certain byte length. #[derive(Copy, Clone)] diff --git a/src/lib.rs b/src/lib.rs index 577c0ba..c1c157d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,13 +19,13 @@ //! assert_eq!(target, decoded); //! } //! ``` -//! +//! //! ### 128bit numbers -//! -//! Support for `i128` and `u128` on Rust toolchains after `1.26.0` is -//! enabled through the `i128` feature. Add the following to your +//! +//! Support for `i128` and `u128` on Rust toolchains after `1.26.0` is +//! enabled through the `i128` feature. Add the following to your //! `Cargo.toml`: -//! +//! //! ```toml,ignore //! [dependencies.bincode] //! features = ["i128"] @@ -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(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) } diff --git a/src/ser/mod.rs b/src/ser/mod.rs index e6d0783..3f9235d 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -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 { } 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<()> { @@ -110,7 +110,7 @@ impl<'a, W: Write, O: Options> serde::Serializer for &'a mut Serializer { #[cfg(not(feature = "i128"))] fn serialize_i128(self, v: i128) -> Result<()> { use serde::ser::Error; - + let _ = v; Err(Error::custom("i128 is not supported. Enable the `i128` feature of `bincode`")) } @@ -130,9 +130,9 @@ impl<'a, W: Write, O: Options> serde::Serializer for &'a mut Serializer { } 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; diff --git a/tests/test.rs b/tests/test.rs index 2df1527..b645d29 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -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>), @@ -421,8 +421,7 @@ fn test_oom_protection() { .serialize(&FakeVec { len: 0xffffffffffffffffu64, byte: 1, - }) - .unwrap(); + }).unwrap(); let y: Result> = 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, + "", + ))); } }