Format with rustfmt 0.4.1

This commit is contained in:
David Tolnay 2018-04-01 00:04:54 +02:00
parent 21698f264a
commit 9c659d9d86
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
9 changed files with 220 additions and 120 deletions

View File

@ -1,2 +1 @@
error_on_line_overflow = false error_on_line_overflow = false
same_line_attributes = false

View File

@ -9,33 +9,33 @@
use lib::*; use lib::*;
macro_rules! int_to_int { 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 { if $dst::min_value() as i64 <= $n as i64 && $n as i64 <= $dst::max_value() as i64 {
Some($n as $dst) Some($n as $dst)
} else { } else {
None None
} }
) };
} }
macro_rules! int_to_uint { macro_rules! int_to_uint {
($dst:ident, $n:ident) => ( ($dst:ident, $n:ident) => {
if 0 <= $n && $n as u64 <= $dst::max_value() as u64 { if 0 <= $n && $n as u64 <= $dst::max_value() as u64 {
Some($n as $dst) Some($n as $dst)
} else { } else {
None None
} }
) };
} }
macro_rules! uint_to { macro_rules! uint_to {
($dst:ident, $n:ident) => ( ($dst:ident, $n:ident) => {
if $n as u64 <= $dst::max_value() as u64 { if $n as u64 <= $dst::max_value() as u64 {
Some($n as $dst) Some($n as $dst)
} else { } else {
None None
} }
) };
} }
pub trait FromPrimitive: Sized { pub trait FromPrimitive: Sized {
@ -52,54 +52,144 @@ pub trait FromPrimitive: Sized {
} }
macro_rules! impl_from_primitive_for_int { macro_rules! impl_from_primitive_for_int {
($t:ident) => ( ($t:ident) => {
impl FromPrimitive for $t { impl FromPrimitive for $t {
#[inline] fn from_isize(n: isize) -> Option<Self> { int_to_int!($t, n) } #[inline]
#[inline] fn from_i8(n: i8) -> Option<Self> { int_to_int!($t, n) } fn from_isize(n: isize) -> Option<Self> {
#[inline] fn from_i16(n: i16) -> Option<Self> { int_to_int!($t, n) } int_to_int!($t, n)
#[inline] fn from_i32(n: i32) -> Option<Self> { int_to_int!($t, n) } }
#[inline] fn from_i64(n: i64) -> Option<Self> { int_to_int!($t, n) } #[inline]
#[inline] fn from_usize(n: usize) -> Option<Self> { uint_to!($t, n) } fn from_i8(n: i8) -> Option<Self> {
#[inline] fn from_u8(n: u8) -> Option<Self> { uint_to!($t, n) } int_to_int!($t, n)
#[inline] fn from_u16(n: u16) -> Option<Self> { uint_to!($t, n) } }
#[inline] fn from_u32(n: u32) -> Option<Self> { uint_to!($t, n) } #[inline]
#[inline] fn from_u64(n: u64) -> Option<Self> { uint_to!($t, n) } fn from_i16(n: i16) -> Option<Self> {
int_to_int!($t, n)
}
#[inline]
fn from_i32(n: i32) -> Option<Self> {
int_to_int!($t, n)
}
#[inline]
fn from_i64(n: i64) -> Option<Self> {
int_to_int!($t, n)
}
#[inline]
fn from_usize(n: usize) -> Option<Self> {
uint_to!($t, n)
}
#[inline]
fn from_u8(n: u8) -> Option<Self> {
uint_to!($t, n)
}
#[inline]
fn from_u16(n: u16) -> Option<Self> {
uint_to!($t, n)
}
#[inline]
fn from_u32(n: u32) -> Option<Self> {
uint_to!($t, n)
}
#[inline]
fn from_u64(n: u64) -> Option<Self> {
uint_to!($t, n)
}
} }
) };
} }
macro_rules! impl_from_primitive_for_uint { macro_rules! impl_from_primitive_for_uint {
($t:ident) => ( ($t:ident) => {
impl FromPrimitive for $t { impl FromPrimitive for $t {
#[inline] fn from_isize(n: isize) -> Option<Self> { int_to_uint!($t, n) } #[inline]
#[inline] fn from_i8(n: i8) -> Option<Self> { int_to_uint!($t, n) } fn from_isize(n: isize) -> Option<Self> {
#[inline] fn from_i16(n: i16) -> Option<Self> { int_to_uint!($t, n) } int_to_uint!($t, n)
#[inline] fn from_i32(n: i32) -> Option<Self> { int_to_uint!($t, n) } }
#[inline] fn from_i64(n: i64) -> Option<Self> { int_to_uint!($t, n) } #[inline]
#[inline] fn from_usize(n: usize) -> Option<Self> { uint_to!($t, n) } fn from_i8(n: i8) -> Option<Self> {
#[inline] fn from_u8(n: u8) -> Option<Self> { uint_to!($t, n) } int_to_uint!($t, n)
#[inline] fn from_u16(n: u16) -> Option<Self> { uint_to!($t, n) } }
#[inline] fn from_u32(n: u32) -> Option<Self> { uint_to!($t, n) } #[inline]
#[inline] fn from_u64(n: u64) -> Option<Self> { uint_to!($t, n) } fn from_i16(n: i16) -> Option<Self> {
int_to_uint!($t, n)
}
#[inline]
fn from_i32(n: i32) -> Option<Self> {
int_to_uint!($t, n)
}
#[inline]
fn from_i64(n: i64) -> Option<Self> {
int_to_uint!($t, n)
}
#[inline]
fn from_usize(n: usize) -> Option<Self> {
uint_to!($t, n)
}
#[inline]
fn from_u8(n: u8) -> Option<Self> {
uint_to!($t, n)
}
#[inline]
fn from_u16(n: u16) -> Option<Self> {
uint_to!($t, n)
}
#[inline]
fn from_u32(n: u32) -> Option<Self> {
uint_to!($t, n)
}
#[inline]
fn from_u64(n: u64) -> Option<Self> {
uint_to!($t, n)
}
} }
) };
} }
macro_rules! impl_from_primitive_for_float { macro_rules! impl_from_primitive_for_float {
($t:ident) => ( ($t:ident) => {
impl FromPrimitive for $t { impl FromPrimitive for $t {
#[inline] fn from_isize(n: isize) -> Option<Self> { Some(n as Self) } #[inline]
#[inline] fn from_i8(n: i8) -> Option<Self> { Some(n as Self) } fn from_isize(n: isize) -> Option<Self> {
#[inline] fn from_i16(n: i16) -> Option<Self> { Some(n as Self) } Some(n as Self)
#[inline] fn from_i32(n: i32) -> Option<Self> { Some(n as Self) } }
#[inline] fn from_i64(n: i64) -> Option<Self> { Some(n as Self) } #[inline]
#[inline] fn from_usize(n: usize) -> Option<Self> { Some(n as Self) } fn from_i8(n: i8) -> Option<Self> {
#[inline] fn from_u8(n: u8) -> Option<Self> { Some(n as Self) } Some(n as Self)
#[inline] fn from_u16(n: u16) -> Option<Self> { Some(n as Self) } }
#[inline] fn from_u32(n: u32) -> Option<Self> { Some(n as Self) } #[inline]
#[inline] fn from_u64(n: u64) -> Option<Self> { Some(n as Self) } fn from_i16(n: i16) -> Option<Self> {
Some(n as Self)
}
#[inline]
fn from_i32(n: i32) -> Option<Self> {
Some(n as Self)
}
#[inline]
fn from_i64(n: i64) -> Option<Self> {
Some(n as Self)
}
#[inline]
fn from_usize(n: usize) -> Option<Self> {
Some(n as Self)
}
#[inline]
fn from_u8(n: u8) -> Option<Self> {
Some(n as Self)
}
#[inline]
fn from_u16(n: u16) -> Option<Self> {
Some(n as Self)
}
#[inline]
fn from_u32(n: u32) -> Option<Self> {
Some(n as Self)
}
#[inline]
fn from_u64(n: u64) -> Option<Self> {
Some(n as Self)
}
} }
) };
} }
impl_from_primitive_for_int!(isize); impl_from_primitive_for_int!(isize);

View File

@ -1077,7 +1077,7 @@ map_impl!(
#[cfg(feature = "std")] #[cfg(feature = "std")]
macro_rules! parse_ip_impl { macro_rules! parse_ip_impl {
($ty:ty; $size: expr) => { ($ty:ty; $size:expr) => {
impl<'de> Deserialize<'de> for $ty { impl<'de> Deserialize<'de> for $ty {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
@ -1091,7 +1091,7 @@ macro_rules! parse_ip_impl {
} }
} }
} }
} };
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -1234,7 +1234,7 @@ parse_ip_impl!(net::Ipv6Addr; 16);
#[cfg(feature = "std")] #[cfg(feature = "std")]
macro_rules! parse_socket_impl { macro_rules! parse_socket_impl {
($ty:ty, $new: expr) => { ($ty:ty, $new:expr) => {
impl<'de> Deserialize<'de> for $ty { impl<'de> Deserialize<'de> for $ty {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
@ -1248,7 +1248,7 @@ macro_rules! parse_socket_impl {
} }
} }
} }
} };
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -1494,7 +1494,7 @@ macro_rules! box_forwarded_impl {
Box::deserialize(deserializer).map(Into::into) Box::deserialize(deserializer).map(Into::into)
} }
} }
} };
} }
#[cfg(all(feature = "unstable", feature = "rc", any(feature = "std", feature = "alloc")))] #[cfg(all(feature = "unstable", feature = "rc", any(feature = "std", feature = "alloc")))]

View File

@ -37,10 +37,10 @@
use lib::*; use lib::*;
use self::private::{First, Second};
use de::{self, Expected, IntoDeserializer, SeqAccess}; use de::{self, Expected, IntoDeserializer, SeqAccess};
use private::de::size_hint; use private::de::size_hint;
use ser; use ser;
use self::private::{First, Second};
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -135,16 +135,16 @@ extern crate core;
/// module. /// module.
mod lib { mod lib {
mod core { mod core {
#[cfg(feature = "std")]
pub use std::*;
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
pub use core::*; pub use core::*;
#[cfg(feature = "std")]
pub use std::*;
} }
pub use self::core::{cmp, iter, mem, ops, slice, str}; 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::{isize, i16, i32, i64, i8};
pub use self::core::{usize, u16, u32, u64, u8}; 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::cell::{Cell, RefCell};
pub use self::core::clone::{self, Clone}; pub use self::core::clone::{self, Clone};
@ -155,40 +155,40 @@ mod lib {
pub use self::core::option::{self, Option}; pub use self::core::option::{self, Option};
pub use self::core::result::{self, Result}; pub use self::core::result::{self, Result};
#[cfg(feature = "std")]
pub use std::borrow::{Cow, ToOwned};
#[cfg(all(feature = "alloc", not(feature = "std")))] #[cfg(all(feature = "alloc", not(feature = "std")))]
pub use alloc::borrow::{Cow, ToOwned}; pub use alloc::borrow::{Cow, ToOwned};
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::string::String; pub use std::borrow::{Cow, ToOwned};
#[cfg(all(feature = "alloc", not(feature = "std")))] #[cfg(all(feature = "alloc", not(feature = "std")))]
pub use alloc::string::{String, ToString}; pub use alloc::string::{String, ToString};
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::vec::Vec; pub use std::string::String;
#[cfg(all(feature = "alloc", not(feature = "std")))] #[cfg(all(feature = "alloc", not(feature = "std")))]
pub use alloc::vec::Vec; pub use alloc::vec::Vec;
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::boxed::Box; pub use std::vec::Vec;
#[cfg(all(feature = "alloc", not(feature = "std")))] #[cfg(all(feature = "alloc", not(feature = "std")))]
pub use alloc::boxed::Box; 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")))] #[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
pub use alloc::rc::Rc; pub use alloc::rc::Rc;
#[cfg(all(feature = "rc", feature = "std"))] #[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")))] #[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
pub use alloc::arc::Arc; 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")))] #[cfg(all(feature = "alloc", not(feature = "std")))]
pub use alloc::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}; pub use alloc::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
#[cfg(feature = "std")]
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::{error, net}; pub use std::{error, net};
@ -206,16 +206,16 @@ mod lib {
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::path::{Path, PathBuf}; pub use std::path::{Path, PathBuf};
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::time::{Duration, SystemTime, UNIX_EPOCH};
#[cfg(feature = "std")]
pub use std::sync::{Mutex, RwLock}; pub use std::sync::{Mutex, RwLock};
#[cfg(feature = "std")]
pub use std::time::{Duration, SystemTime, UNIX_EPOCH};
#[cfg(feature = "unstable")] #[cfg(feature = "unstable")]
#[allow(deprecated)] #[allow(deprecated)]
pub use core::nonzero::{NonZero, Zeroable}; pub use core::nonzero::{NonZero, Zeroable};
#[cfg(feature = "unstable")] #[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] #[macro_use]
mod macros; mod macros;
pub mod ser;
pub mod de; pub mod de;
pub mod ser;
#[doc(inline)]
pub use ser::{Serialize, Serializer};
#[doc(inline)] #[doc(inline)]
pub use de::{Deserialize, Deserializer}; pub use de::{Deserialize, Deserializer};
#[doc(inline)]
pub use ser::{Serialize, Serializer};
// Generated code uses these to support no_std. Not public API. // Generated code uses these to support no_std. Not public API.
#[doc(hidden)] #[doc(hidden)]

View File

@ -11,13 +11,13 @@ use lib::*;
use de::{Deserialize, DeserializeSeed, Deserializer, Error, IntoDeserializer, Visitor}; use de::{Deserialize, DeserializeSeed, Deserializer, Error, IntoDeserializer, Visitor};
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
use de::{Unexpected, MapAccess}; use de::{MapAccess, Unexpected};
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
pub use self::content::{Content, ContentDeserializer, ContentRefDeserializer, pub use self::content::{Content, ContentDeserializer, ContentRefDeserializer, EnumDeserializer,
InternallyTaggedUnitVisitor, TagContentOtherField, InternallyTaggedUnitVisitor, TagContentOtherField,
TagContentOtherFieldVisitor, TagOrContentField, TagOrContentFieldVisitor, TagContentOtherFieldVisitor, TagOrContentField, TagOrContentFieldVisitor,
TaggedContentVisitor, UntaggedUnitVisitor, EnumDeserializer}; TaggedContentVisitor, UntaggedUnitVisitor};
/// If the missing field is of type `Option<T>` then treat is as `None`, /// If the missing field is of type `Option<T>` then treat is as `None`,
/// otherwise it is an error. /// otherwise it is an error.
@ -228,9 +228,9 @@ mod content {
use lib::*; use lib::*;
use super::size_hint;
use de::{self, Deserialize, DeserializeSeed, Deserializer, EnumAccess, MapAccess, SeqAccess, use de::{self, Deserialize, DeserializeSeed, Deserializer, EnumAccess, MapAccess, SeqAccess,
Unexpected, Visitor}; Unexpected, Visitor};
use super::size_hint;
/// Used from generated code to buffer the contents of the Deserializer when /// Used from generated code to buffer the contents of the Deserializer when
/// deserializing untagged enums and internally tagged enums. /// deserializing untagged enums and internally tagged enums.
@ -1184,7 +1184,8 @@ mod content {
} }
impl<'de, E> EnumDeserializer<'de, E> impl<'de, E> EnumDeserializer<'de, E>
where E: de::Error where
E: de::Error,
{ {
pub fn new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E> { pub fn new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E> {
EnumDeserializer { EnumDeserializer {
@ -2082,12 +2083,13 @@ where
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
pub struct FlatMapDeserializer<'a, 'de: 'a, E>( pub struct FlatMapDeserializer<'a, 'de: 'a, E>(
pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>, pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>,
pub PhantomData<E> pub PhantomData<E>,
); );
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E> impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
where E: Error where
E: Error,
{ {
type Error = E; type Error = E;
@ -2102,7 +2104,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
self, self,
name: &'static str, name: &'static str,
variants: &'static [&'static str], variants: &'static [&'static str],
visitor: V visitor: V,
) -> Result<V::Value, Self::Error> ) -> Result<V::Value, Self::Error>
where where
V: Visitor<'de>, V: Visitor<'de>,
@ -2113,15 +2115,12 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
// about. // about.
let use_item = match *item { let use_item = match *item {
None => false, 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 { if use_item {
let (key, value) = item.take().unwrap(); let (key, value) = item.take().unwrap();
return visitor.visit_enum(EnumDeserializer::new( return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
key,
Some(value)
));
} }
} }
@ -2142,7 +2141,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
self, self,
_: &'static str, _: &'static str,
fields: &'static [&'static str], fields: &'static [&'static str],
visitor: V visitor: V,
) -> Result<V::Value, Self::Error> ) -> Result<V::Value, Self::Error>
where where
V: Visitor<'de>, 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))) visitor.visit_map(FlatMapAccess::new(self.0.iter_mut(), Some(fields)))
} }
fn deserialize_newtype_struct<V>( fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
self,
_name: &str,
visitor: V,
) -> Result<V::Value, Self::Error>
where where
V: Visitor<'de>, V: Visitor<'de>,
{ {
@ -2180,7 +2175,7 @@ pub struct FlatMapAccess<'a, 'de: 'a, E> {
impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> { impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> {
fn new( fn new(
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>, iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
fields: Option<&'static [&'static str]> fields: Option<&'static [&'static str]>,
) -> FlatMapAccess<'a, 'de, E> { ) -> FlatMapAccess<'a, 'de, E> {
FlatMapAccess { FlatMapAccess {
iter: iter, iter: iter,
@ -2193,7 +2188,8 @@ impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> {
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E> impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E>
where E: Error where
E: Error,
{ {
type Error = E; type Error = E;
@ -2208,13 +2204,12 @@ impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E>
let use_item = match *item { let use_item = match *item {
None => false, None => false,
Some((ref c, _)) => { Some((ref c, _)) => {
c.as_str().map_or(self.fields.is_none(), |key| { c.as_str()
match self.fields { .map_or(self.fields.is_none(), |key| match self.fields {
None => true, None => true,
Some(fields) if fields.contains(&key) => true, Some(fields) if fields.contains(&key) => true,
_ => false _ => false,
} })
})
} }
}; };

View File

@ -8,5 +8,5 @@
mod macros; mod macros;
pub mod ser;
pub mod de; pub mod de;
pub mod ser;

View File

@ -11,12 +11,8 @@ use lib::*;
use ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer}; use ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer};
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
use self::content::{ use self::content::{Content, ContentSerializer, SerializeStructVariantAsMapValue,
SerializeStructVariantAsMapValue, SerializeTupleVariantAsMapValue};
SerializeTupleVariantAsMapValue,
ContentSerializer,
Content,
};
/// Used to check that serde(getter) attributes return the expected type. /// Used to check that serde(getter) attributes return the expected type.
/// Not public API. /// Not public API.
@ -1042,16 +1038,20 @@ pub struct FlatMapSerializer<'a, M: 'a>(pub &'a mut M);
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, M> FlatMapSerializer<'a, M> impl<'a, M> FlatMapSerializer<'a, M>
where where
M: SerializeMap + 'a M: SerializeMap + 'a,
{ {
fn bad_type(self, what: Unsupported) -> M::Error { 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"))] #[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, M> Serializer for FlatMapSerializer<'a, M> impl<'a, M> Serializer for FlatMapSerializer<'a, M>
where M: SerializeMap + 'a where
M: SerializeMap + 'a,
{ {
type Ok = (); type Ok = ();
type Error = M::Error; type Error = M::Error;
@ -1219,7 +1219,10 @@ impl<'a, M> Serializer for FlatMapSerializer<'a, M>
_: usize, _: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> { ) -> Result<Self::SerializeStructVariant, Self::Error> {
try!(self.0.serialize_key(inner_variant)); 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"))] #[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, M> ser::SerializeMap for FlatMapSerializeMap<'a, M> impl<'a, M> ser::SerializeMap for FlatMapSerializeMap<'a, M>
where M: SerializeMap + 'a where
M: SerializeMap + 'a,
{ {
type Ok = (); type Ok = ();
type Error = M::Error; type Error = M::Error;
@ -1257,12 +1261,17 @@ pub struct FlatMapSerializeStruct<'a, M: 'a>(&'a mut M);
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, M> ser::SerializeStruct for FlatMapSerializeStruct<'a, M> impl<'a, M> ser::SerializeStruct for FlatMapSerializeStruct<'a, M>
where M: SerializeMap + 'a where
M: SerializeMap + 'a,
{ {
type Ok = (); type Ok = ();
type Error = M::Error; type Error = M::Error;
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> fn serialize_field<T: ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error>
where where
T: Serialize, T: Serialize,
{ {
@ -1283,7 +1292,8 @@ pub struct FlatMapSerializeStructVariantAsMapValue<'a, M: 'a> {
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, M> FlatMapSerializeStructVariantAsMapValue<'a, M> 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> { fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> {
FlatMapSerializeStructVariantAsMapValue { FlatMapSerializeStructVariantAsMapValue {
@ -1296,12 +1306,17 @@ impl<'a, M> FlatMapSerializeStructVariantAsMapValue<'a, M>
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapValue<'a, M> impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapValue<'a, M>
where M: SerializeMap + 'a where
M: SerializeMap + 'a,
{ {
type Ok = (); type Ok = ();
type Error = M::Error; type Error = M::Error;
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> fn serialize_field<T: ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error>
where where
T: Serialize, T: Serialize,
{ {
@ -1311,7 +1326,10 @@ impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapVa
} }
fn end(self) -> Result<(), Self::Error> { 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(()) Ok(())
} }
} }

View File

@ -545,11 +545,9 @@ macro_rules! serialize_display_bounded_length {
// write! only provides fmt::Formatter to Display implementations, which // write! only provides fmt::Formatter to Display implementations, which
// has methods write_str and write_char but no method to write arbitrary // has methods write_str and write_char but no method to write arbitrary
// bytes. Therefore `written` must be valid UTF-8. // bytes. Therefore `written` must be valid UTF-8.
let written_str = unsafe { let written_str = unsafe { str::from_utf8_unchecked(written) };
str::from_utf8_unchecked(written)
};
$serializer.serialize_str(written_str) $serializer.serialize_str(written_str)
}} }};
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]