diff --git a/rustfmt.toml b/rustfmt.toml index cfc87c79..ad0225ff 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,2 +1 @@ error_on_line_overflow = false -same_line_attributes = false diff --git a/serde/src/de/from_primitive.rs b/serde/src/de/from_primitive.rs index 819f2801..5230b18b 100644 --- a/serde/src/de/from_primitive.rs +++ b/serde/src/de/from_primitive.rs @@ -9,33 +9,33 @@ use lib::*; macro_rules! int_to_int { - ($dst:ident, $n:ident) => ( + ($dst:ident, $n:ident) => { if $dst::min_value() as i64 <= $n as i64 && $n as i64 <= $dst::max_value() as i64 { Some($n as $dst) } else { None } - ) + }; } macro_rules! int_to_uint { - ($dst:ident, $n:ident) => ( + ($dst:ident, $n:ident) => { if 0 <= $n && $n as u64 <= $dst::max_value() as u64 { Some($n as $dst) } else { None } - ) + }; } macro_rules! uint_to { - ($dst:ident, $n:ident) => ( + ($dst:ident, $n:ident) => { if $n as u64 <= $dst::max_value() as u64 { Some($n as $dst) } else { None } - ) + }; } pub trait FromPrimitive: Sized { @@ -52,54 +52,144 @@ pub trait FromPrimitive: Sized { } macro_rules! impl_from_primitive_for_int { - ($t:ident) => ( + ($t:ident) => { impl FromPrimitive for $t { - #[inline] fn from_isize(n: isize) -> Option { int_to_int!($t, n) } - #[inline] fn from_i8(n: i8) -> Option { int_to_int!($t, n) } - #[inline] fn from_i16(n: i16) -> Option { int_to_int!($t, n) } - #[inline] fn from_i32(n: i32) -> Option { int_to_int!($t, n) } - #[inline] fn from_i64(n: i64) -> Option { int_to_int!($t, n) } - #[inline] fn from_usize(n: usize) -> Option { uint_to!($t, n) } - #[inline] fn from_u8(n: u8) -> Option { uint_to!($t, n) } - #[inline] fn from_u16(n: u16) -> Option { uint_to!($t, n) } - #[inline] fn from_u32(n: u32) -> Option { uint_to!($t, n) } - #[inline] fn from_u64(n: u64) -> Option { uint_to!($t, n) } + #[inline] + fn from_isize(n: isize) -> Option { + int_to_int!($t, n) + } + #[inline] + fn from_i8(n: i8) -> Option { + int_to_int!($t, n) + } + #[inline] + fn from_i16(n: i16) -> Option { + int_to_int!($t, n) + } + #[inline] + fn from_i32(n: i32) -> Option { + int_to_int!($t, n) + } + #[inline] + fn from_i64(n: i64) -> Option { + int_to_int!($t, n) + } + #[inline] + fn from_usize(n: usize) -> Option { + uint_to!($t, n) + } + #[inline] + fn from_u8(n: u8) -> Option { + uint_to!($t, n) + } + #[inline] + fn from_u16(n: u16) -> Option { + uint_to!($t, n) + } + #[inline] + fn from_u32(n: u32) -> Option { + uint_to!($t, n) + } + #[inline] + fn from_u64(n: u64) -> Option { + uint_to!($t, n) + } } - ) + }; } macro_rules! impl_from_primitive_for_uint { - ($t:ident) => ( + ($t:ident) => { impl FromPrimitive for $t { - #[inline] fn from_isize(n: isize) -> Option { int_to_uint!($t, n) } - #[inline] fn from_i8(n: i8) -> Option { int_to_uint!($t, n) } - #[inline] fn from_i16(n: i16) -> Option { int_to_uint!($t, n) } - #[inline] fn from_i32(n: i32) -> Option { int_to_uint!($t, n) } - #[inline] fn from_i64(n: i64) -> Option { int_to_uint!($t, n) } - #[inline] fn from_usize(n: usize) -> Option { uint_to!($t, n) } - #[inline] fn from_u8(n: u8) -> Option { uint_to!($t, n) } - #[inline] fn from_u16(n: u16) -> Option { uint_to!($t, n) } - #[inline] fn from_u32(n: u32) -> Option { uint_to!($t, n) } - #[inline] fn from_u64(n: u64) -> Option { uint_to!($t, n) } + #[inline] + fn from_isize(n: isize) -> Option { + int_to_uint!($t, n) + } + #[inline] + fn from_i8(n: i8) -> Option { + int_to_uint!($t, n) + } + #[inline] + fn from_i16(n: i16) -> Option { + int_to_uint!($t, n) + } + #[inline] + fn from_i32(n: i32) -> Option { + int_to_uint!($t, n) + } + #[inline] + fn from_i64(n: i64) -> Option { + int_to_uint!($t, n) + } + #[inline] + fn from_usize(n: usize) -> Option { + uint_to!($t, n) + } + #[inline] + fn from_u8(n: u8) -> Option { + uint_to!($t, n) + } + #[inline] + fn from_u16(n: u16) -> Option { + uint_to!($t, n) + } + #[inline] + fn from_u32(n: u32) -> Option { + uint_to!($t, n) + } + #[inline] + fn from_u64(n: u64) -> Option { + uint_to!($t, n) + } } - ) + }; } macro_rules! impl_from_primitive_for_float { - ($t:ident) => ( + ($t:ident) => { impl FromPrimitive for $t { - #[inline] fn from_isize(n: isize) -> Option { Some(n as Self) } - #[inline] fn from_i8(n: i8) -> Option { Some(n as Self) } - #[inline] fn from_i16(n: i16) -> Option { Some(n as Self) } - #[inline] fn from_i32(n: i32) -> Option { Some(n as Self) } - #[inline] fn from_i64(n: i64) -> Option { Some(n as Self) } - #[inline] fn from_usize(n: usize) -> Option { Some(n as Self) } - #[inline] fn from_u8(n: u8) -> Option { Some(n as Self) } - #[inline] fn from_u16(n: u16) -> Option { Some(n as Self) } - #[inline] fn from_u32(n: u32) -> Option { Some(n as Self) } - #[inline] fn from_u64(n: u64) -> Option { Some(n as Self) } + #[inline] + fn from_isize(n: isize) -> Option { + Some(n as Self) + } + #[inline] + fn from_i8(n: i8) -> Option { + Some(n as Self) + } + #[inline] + fn from_i16(n: i16) -> Option { + Some(n as Self) + } + #[inline] + fn from_i32(n: i32) -> Option { + Some(n as Self) + } + #[inline] + fn from_i64(n: i64) -> Option { + Some(n as Self) + } + #[inline] + fn from_usize(n: usize) -> Option { + Some(n as Self) + } + #[inline] + fn from_u8(n: u8) -> Option { + Some(n as Self) + } + #[inline] + fn from_u16(n: u16) -> Option { + Some(n as Self) + } + #[inline] + fn from_u32(n: u32) -> Option { + Some(n as Self) + } + #[inline] + fn from_u64(n: u64) -> Option { + Some(n as Self) + } } - ) + }; } impl_from_primitive_for_int!(isize); diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 672a23a5..4fc2361a 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -1077,7 +1077,7 @@ map_impl!( #[cfg(feature = "std")] macro_rules! parse_ip_impl { - ($ty:ty; $size: expr) => { + ($ty:ty; $size:expr) => { impl<'de> Deserialize<'de> for $ty { fn deserialize(deserializer: D) -> Result where @@ -1091,7 +1091,7 @@ macro_rules! parse_ip_impl { } } } - } + }; } #[cfg(feature = "std")] @@ -1234,7 +1234,7 @@ parse_ip_impl!(net::Ipv6Addr; 16); #[cfg(feature = "std")] macro_rules! parse_socket_impl { - ($ty:ty, $new: expr) => { + ($ty:ty, $new:expr) => { impl<'de> Deserialize<'de> for $ty { fn deserialize(deserializer: D) -> Result where @@ -1248,7 +1248,7 @@ macro_rules! parse_socket_impl { } } } - } + }; } #[cfg(feature = "std")] @@ -1494,7 +1494,7 @@ macro_rules! box_forwarded_impl { Box::deserialize(deserializer).map(Into::into) } } - } + }; } #[cfg(all(feature = "unstable", feature = "rc", any(feature = "std", feature = "alloc")))] diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index 6dd1d21f..e477c216 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -37,10 +37,10 @@ use lib::*; +use self::private::{First, Second}; use de::{self, Expected, IntoDeserializer, SeqAccess}; use private::de::size_hint; use ser; -use self::private::{First, Second}; //////////////////////////////////////////////////////////////////////////////// diff --git a/serde/src/lib.rs b/serde/src/lib.rs index bbd2cf81..acf75669 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -135,16 +135,16 @@ extern crate core; /// module. mod lib { mod core { - #[cfg(feature = "std")] - pub use std::*; #[cfg(not(feature = "std"))] pub use core::*; + #[cfg(feature = "std")] + pub use std::*; } pub use self::core::{cmp, iter, mem, ops, slice, str}; + pub use self::core::{f32, f64}; pub use self::core::{isize, i16, i32, i64, i8}; pub use self::core::{usize, u16, u32, u64, u8}; - pub use self::core::{f32, f64}; pub use self::core::cell::{Cell, RefCell}; pub use self::core::clone::{self, Clone}; @@ -155,40 +155,40 @@ mod lib { pub use self::core::option::{self, Option}; pub use self::core::result::{self, Result}; - #[cfg(feature = "std")] - pub use std::borrow::{Cow, ToOwned}; #[cfg(all(feature = "alloc", not(feature = "std")))] pub use alloc::borrow::{Cow, ToOwned}; - #[cfg(feature = "std")] - pub use std::string::String; + pub use std::borrow::{Cow, ToOwned}; + #[cfg(all(feature = "alloc", not(feature = "std")))] pub use alloc::string::{String, ToString}; - #[cfg(feature = "std")] - pub use std::vec::Vec; + pub use std::string::String; + #[cfg(all(feature = "alloc", not(feature = "std")))] pub use alloc::vec::Vec; - #[cfg(feature = "std")] - pub use std::boxed::Box; + pub use std::vec::Vec; + #[cfg(all(feature = "alloc", not(feature = "std")))] pub use alloc::boxed::Box; + #[cfg(feature = "std")] + pub use std::boxed::Box; - #[cfg(all(feature = "rc", feature = "std"))] - pub use std::rc::Rc; #[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))] pub use alloc::rc::Rc; - #[cfg(all(feature = "rc", feature = "std"))] - pub use std::sync::Arc; + pub use std::rc::Rc; + #[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))] pub use alloc::arc::Arc; + #[cfg(all(feature = "rc", feature = "std"))] + pub use std::sync::Arc; - #[cfg(feature = "std")] - pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}; #[cfg(all(feature = "alloc", not(feature = "std")))] pub use alloc::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}; + #[cfg(feature = "std")] + pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}; #[cfg(feature = "std")] pub use std::{error, net}; @@ -206,16 +206,16 @@ mod lib { #[cfg(feature = "std")] pub use std::path::{Path, PathBuf}; #[cfg(feature = "std")] - pub use std::time::{Duration, SystemTime, UNIX_EPOCH}; - #[cfg(feature = "std")] pub use std::sync::{Mutex, RwLock}; + #[cfg(feature = "std")] + pub use std::time::{Duration, SystemTime, UNIX_EPOCH}; #[cfg(feature = "unstable")] #[allow(deprecated)] pub use core::nonzero::{NonZero, Zeroable}; #[cfg(feature = "unstable")] - pub use core::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroUsize}; + pub use core::num::{NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize}; } //////////////////////////////////////////////////////////////////////////////// @@ -223,13 +223,13 @@ mod lib { #[macro_use] mod macros; -pub mod ser; pub mod de; +pub mod ser; -#[doc(inline)] -pub use ser::{Serialize, Serializer}; #[doc(inline)] pub use de::{Deserialize, Deserializer}; +#[doc(inline)] +pub use ser::{Serialize, Serializer}; // Generated code uses these to support no_std. Not public API. #[doc(hidden)] diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 6eae7311..c9c0cba8 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -11,13 +11,13 @@ use lib::*; use de::{Deserialize, DeserializeSeed, Deserializer, Error, IntoDeserializer, Visitor}; #[cfg(any(feature = "std", feature = "alloc"))] -use de::{Unexpected, MapAccess}; +use de::{MapAccess, Unexpected}; #[cfg(any(feature = "std", feature = "alloc"))] -pub use self::content::{Content, ContentDeserializer, ContentRefDeserializer, +pub use self::content::{Content, ContentDeserializer, ContentRefDeserializer, EnumDeserializer, InternallyTaggedUnitVisitor, TagContentOtherField, TagContentOtherFieldVisitor, TagOrContentField, TagOrContentFieldVisitor, - TaggedContentVisitor, UntaggedUnitVisitor, EnumDeserializer}; + TaggedContentVisitor, UntaggedUnitVisitor}; /// If the missing field is of type `Option` then treat is as `None`, /// otherwise it is an error. @@ -228,9 +228,9 @@ mod content { use lib::*; + use super::size_hint; use de::{self, Deserialize, DeserializeSeed, Deserializer, EnumAccess, MapAccess, SeqAccess, Unexpected, Visitor}; - use super::size_hint; /// Used from generated code to buffer the contents of the Deserializer when /// deserializing untagged enums and internally tagged enums. @@ -1184,7 +1184,8 @@ mod content { } impl<'de, E> EnumDeserializer<'de, E> - where E: de::Error + where + E: de::Error, { pub fn new(variant: Content<'de>, value: Option>) -> EnumDeserializer<'de, E> { EnumDeserializer { @@ -2082,12 +2083,13 @@ where #[cfg(any(feature = "std", feature = "alloc"))] pub struct FlatMapDeserializer<'a, 'de: 'a, E>( pub &'a mut Vec, Content<'de>)>>, - pub PhantomData + pub PhantomData, ); #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E> - where E: Error +where + E: Error, { type Error = E; @@ -2102,7 +2104,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E> self, name: &'static str, variants: &'static [&'static str], - visitor: V + visitor: V, ) -> Result where V: Visitor<'de>, @@ -2113,15 +2115,12 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E> // about. let use_item = match *item { None => false, - Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)) + Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)), }; if use_item { let (key, value) = item.take().unwrap(); - return visitor.visit_enum(EnumDeserializer::new( - key, - Some(value) - )); + return visitor.visit_enum(EnumDeserializer::new(key, Some(value))); } } @@ -2142,7 +2141,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E> self, _: &'static str, fields: &'static [&'static str], - visitor: V + visitor: V, ) -> Result where V: Visitor<'de>, @@ -2150,11 +2149,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E> visitor.visit_map(FlatMapAccess::new(self.0.iter_mut(), Some(fields))) } - fn deserialize_newtype_struct( - self, - _name: &str, - visitor: V, - ) -> Result + fn deserialize_newtype_struct(self, _name: &str, visitor: V) -> Result where V: Visitor<'de>, { @@ -2180,7 +2175,7 @@ pub struct FlatMapAccess<'a, 'de: 'a, E> { impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> { fn new( iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>, - fields: Option<&'static [&'static str]> + fields: Option<&'static [&'static str]>, ) -> FlatMapAccess<'a, 'de, E> { FlatMapAccess { iter: iter, @@ -2193,7 +2188,8 @@ impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> { #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E> - where E: Error +where + E: Error, { type Error = E; @@ -2208,13 +2204,12 @@ impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E> let use_item = match *item { None => false, Some((ref c, _)) => { - c.as_str().map_or(self.fields.is_none(), |key| { - match self.fields { + c.as_str() + .map_or(self.fields.is_none(), |key| match self.fields { None => true, Some(fields) if fields.contains(&key) => true, - _ => false - } - }) + _ => false, + }) } }; diff --git a/serde/src/private/mod.rs b/serde/src/private/mod.rs index 98307c97..af88a51a 100644 --- a/serde/src/private/mod.rs +++ b/serde/src/private/mod.rs @@ -8,5 +8,5 @@ mod macros; -pub mod ser; pub mod de; +pub mod ser; diff --git a/serde/src/private/ser.rs b/serde/src/private/ser.rs index 5d743808..c02fdfac 100644 --- a/serde/src/private/ser.rs +++ b/serde/src/private/ser.rs @@ -11,12 +11,8 @@ use lib::*; use ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer}; #[cfg(any(feature = "std", feature = "alloc"))] -use self::content::{ - SerializeStructVariantAsMapValue, - SerializeTupleVariantAsMapValue, - ContentSerializer, - Content, -}; +use self::content::{Content, ContentSerializer, SerializeStructVariantAsMapValue, + SerializeTupleVariantAsMapValue}; /// Used to check that serde(getter) attributes return the expected type. /// Not public API. @@ -1042,16 +1038,20 @@ pub struct FlatMapSerializer<'a, M: 'a>(pub &'a mut M); #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> FlatMapSerializer<'a, M> where - M: SerializeMap + 'a + M: SerializeMap + 'a, { fn bad_type(self, what: Unsupported) -> M::Error { - ser::Error::custom(format_args!("can only flatten structs and maps (got {})", what)) + ser::Error::custom(format_args!( + "can only flatten structs and maps (got {})", + what + )) } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> Serializer for FlatMapSerializer<'a, M> - where M: SerializeMap + 'a +where + M: SerializeMap + 'a, { type Ok = (); type Error = M::Error; @@ -1219,7 +1219,10 @@ impl<'a, M> Serializer for FlatMapSerializer<'a, M> _: usize, ) -> Result { try!(self.0.serialize_key(inner_variant)); - Ok(FlatMapSerializeStructVariantAsMapValue::new(self.0, inner_variant)) + Ok(FlatMapSerializeStructVariantAsMapValue::new( + self.0, + inner_variant, + )) } } @@ -1228,7 +1231,8 @@ pub struct FlatMapSerializeMap<'a, M: 'a>(&'a mut M); #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> ser::SerializeMap for FlatMapSerializeMap<'a, M> - where M: SerializeMap + 'a +where + M: SerializeMap + 'a, { type Ok = (); type Error = M::Error; @@ -1257,12 +1261,17 @@ pub struct FlatMapSerializeStruct<'a, M: 'a>(&'a mut M); #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> ser::SerializeStruct for FlatMapSerializeStruct<'a, M> - where M: SerializeMap + 'a +where + M: SerializeMap + 'a, { type Ok = (); type Error = M::Error; - fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> where T: Serialize, { @@ -1283,7 +1292,8 @@ pub struct FlatMapSerializeStructVariantAsMapValue<'a, M: 'a> { #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> FlatMapSerializeStructVariantAsMapValue<'a, M> - where M: SerializeMap + 'a +where + M: SerializeMap + 'a, { fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> { FlatMapSerializeStructVariantAsMapValue { @@ -1296,12 +1306,17 @@ impl<'a, M> FlatMapSerializeStructVariantAsMapValue<'a, M> #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapValue<'a, M> - where M: SerializeMap + 'a +where + M: SerializeMap + 'a, { type Ok = (); type Error = M::Error; - fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> where T: Serialize, { @@ -1311,7 +1326,10 @@ impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapVa } fn end(self) -> Result<(), Self::Error> { - try!(self.map.serialize_value(&Content::Struct(self.name, self.fields))); + try!( + self.map + .serialize_value(&Content::Struct(self.name, self.fields)) + ); Ok(()) } } diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index 225bdcc4..f2de2822 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -545,11 +545,9 @@ macro_rules! serialize_display_bounded_length { // write! only provides fmt::Formatter to Display implementations, which // has methods write_str and write_char but no method to write arbitrary // bytes. Therefore `written` must be valid UTF-8. - let written_str = unsafe { - str::from_utf8_unchecked(written) - }; + let written_str = unsafe { str::from_utf8_unchecked(written) }; $serializer.serialize_str(written_str) - }} + }}; } #[cfg(feature = "std")]