Format in rfc style
This commit is contained in:
parent
c5bd760133
commit
ea8fb97beb
5
rustfmt.toml
Normal file
5
rustfmt.toml
Normal file
@ -0,0 +1,5 @@
|
||||
fn_args_layout = "Block"
|
||||
array_layout = "Block"
|
||||
where_style = "Rfc"
|
||||
generics_indent = "Block"
|
||||
fn_call_style = "Block"
|
@ -10,7 +10,8 @@ pub struct IgnoredAny;
|
||||
impl<'de> Deserialize<'de> for IgnoredAny {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct IgnoredAnyVisitor;
|
||||
|
||||
@ -43,7 +44,8 @@ impl<'de> Deserialize<'de> for IgnoredAny {
|
||||
|
||||
#[inline]
|
||||
fn visit_str<E>(self, _: &str) -> Result<IgnoredAny, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(IgnoredAny)
|
||||
}
|
||||
@ -55,14 +57,16 @@ impl<'de> Deserialize<'de> for IgnoredAny {
|
||||
|
||||
#[inline]
|
||||
fn visit_some<D>(self, deserializer: D) -> Result<IgnoredAny, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
IgnoredAny::deserialize(deserializer)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<IgnoredAny, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
IgnoredAny::deserialize(deserializer)
|
||||
}
|
||||
@ -74,7 +78,8 @@ impl<'de> Deserialize<'de> for IgnoredAny {
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
|
||||
where V: SeqVisitor<'de>
|
||||
where
|
||||
V: SeqVisitor<'de>,
|
||||
{
|
||||
while let Some(_) = try!(visitor.visit::<IgnoredAny>()) {
|
||||
// Gobble
|
||||
@ -84,7 +89,8 @@ impl<'de> Deserialize<'de> for IgnoredAny {
|
||||
|
||||
#[inline]
|
||||
fn visit_map<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
|
||||
where V: MapVisitor<'de>
|
||||
where
|
||||
V: MapVisitor<'de>,
|
||||
{
|
||||
while let Some((_, _)) = try!(visitor.visit::<IgnoredAny, IgnoredAny>()) {
|
||||
// Gobble
|
||||
@ -94,7 +100,8 @@ impl<'de> Deserialize<'de> for IgnoredAny {
|
||||
|
||||
#[inline]
|
||||
fn visit_bytes<E>(self, _: &[u8]) -> Result<IgnoredAny, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(IgnoredAny)
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use lib::*;
|
||||
|
||||
use de::{Deserialize, Deserializer, EnumVisitor, Error, SeqVisitor, Unexpected,
|
||||
VariantVisitor, Visitor};
|
||||
use de::{Deserialize, Deserializer, EnumVisitor, Error, SeqVisitor, Unexpected, VariantVisitor,
|
||||
Visitor};
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
use de::MapVisitor;
|
||||
@ -20,7 +20,8 @@ impl<'de> Visitor<'de> for UnitVisitor {
|
||||
}
|
||||
|
||||
fn visit_unit<E>(self) -> Result<(), E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(())
|
||||
}
|
||||
@ -28,7 +29,8 @@ impl<'de> Visitor<'de> for UnitVisitor {
|
||||
|
||||
impl<'de> Deserialize<'de> for () {
|
||||
fn deserialize<D>(deserializer: D) -> Result<(), D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_unit(UnitVisitor)
|
||||
}
|
||||
@ -46,7 +48,8 @@ impl<'de> Visitor<'de> for BoolVisitor {
|
||||
}
|
||||
|
||||
fn visit_bool<E>(self, v: bool) -> Result<bool, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(v)
|
||||
}
|
||||
@ -54,7 +57,8 @@ impl<'de> Visitor<'de> for BoolVisitor {
|
||||
|
||||
impl<'de> Deserialize<'de> for bool {
|
||||
fn deserialize<D>(deserializer: D) -> Result<bool, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_bool(BoolVisitor)
|
||||
}
|
||||
@ -155,14 +159,16 @@ impl<'de> Visitor<'de> for CharVisitor {
|
||||
|
||||
#[inline]
|
||||
fn visit_char<E>(self, v: char) -> Result<char, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str<E>(self, v: &str) -> Result<char, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
let mut iter = v.chars();
|
||||
match (iter.next(), iter.next()) {
|
||||
@ -175,7 +181,8 @@ impl<'de> Visitor<'de> for CharVisitor {
|
||||
impl<'de> Deserialize<'de> for char {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: D) -> Result<char, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_char(CharVisitor)
|
||||
}
|
||||
@ -195,19 +202,22 @@ impl<'de> Visitor<'de> for StringVisitor {
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<String, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(v.to_owned())
|
||||
}
|
||||
|
||||
fn visit_string<E>(self, v: String) -> Result<String, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<String, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match str::from_utf8(v) {
|
||||
Ok(s) => Ok(s.to_owned()),
|
||||
@ -216,11 +226,12 @@ impl<'de> Visitor<'de> for StringVisitor {
|
||||
}
|
||||
|
||||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<String, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match String::from_utf8(v) {
|
||||
Ok(s) => Ok(s),
|
||||
Err(e) => Err(Error::invalid_value(Unexpected::Bytes(&e.into_bytes()), &self)),
|
||||
Err(e) => Err(Error::invalid_value(Unexpected::Bytes(&e.into_bytes()), &self),),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -228,7 +239,8 @@ impl<'de> Visitor<'de> for StringVisitor {
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de> Deserialize<'de> for String {
|
||||
fn deserialize<D>(deserializer: D) -> Result<String, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_string(StringVisitor)
|
||||
}
|
||||
@ -246,22 +258,24 @@ impl<'a> Visitor<'a> for StrVisitor {
|
||||
}
|
||||
|
||||
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(v) // so easy
|
||||
}
|
||||
|
||||
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
str::from_utf8(v)
|
||||
.map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
|
||||
str::from_utf8(v).map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de: 'a, 'a> Deserialize<'de> for &'a str {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(StrVisitor)
|
||||
}
|
||||
@ -279,13 +293,15 @@ impl<'a> Visitor<'a> for BytesVisitor {
|
||||
}
|
||||
|
||||
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(v.as_bytes())
|
||||
}
|
||||
@ -293,7 +309,8 @@ impl<'a> Visitor<'a> for BytesVisitor {
|
||||
|
||||
impl<'de: 'a, 'a> Deserialize<'de> for &'a [u8] {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_bytes(BytesVisitor)
|
||||
}
|
||||
@ -313,7 +330,8 @@ impl<'de> Visitor<'de> for CStringVisitor {
|
||||
}
|
||||
|
||||
fn visit_seq<V>(self, mut visitor: V) -> Result<CString, V::Error>
|
||||
where V: SeqVisitor<'de>
|
||||
where
|
||||
V: SeqVisitor<'de>,
|
||||
{
|
||||
let len = cmp::min(visitor.size_hint().0, 4096);
|
||||
let mut values = Vec::with_capacity(len);
|
||||
@ -326,25 +344,29 @@ impl<'de> Visitor<'de> for CStringVisitor {
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<CString, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
CString::new(v).map_err(Error::custom)
|
||||
}
|
||||
|
||||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<CString, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
CString::new(v).map_err(Error::custom)
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<CString, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
CString::new(v).map_err(Error::custom)
|
||||
}
|
||||
|
||||
fn visit_string<E>(self, v: String) -> Result<CString, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
CString::new(v).map_err(Error::custom)
|
||||
}
|
||||
@ -353,7 +375,8 @@ impl<'de> Visitor<'de> for CStringVisitor {
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for CString {
|
||||
fn deserialize<D>(deserializer: D) -> Result<CString, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_byte_buf(CStringVisitor)
|
||||
}
|
||||
@ -362,7 +385,8 @@ impl<'de> Deserialize<'de> for CString {
|
||||
#[cfg(all(feature = "std", feature = "unstable"))]
|
||||
impl<'de> Deserialize<'de> for Box<CStr> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = try!(CString::deserialize(deserializer));
|
||||
Ok(s.into_boxed_c_str())
|
||||
@ -376,7 +400,8 @@ struct OptionVisitor<T> {
|
||||
}
|
||||
|
||||
impl<'de, T> Visitor<'de> for OptionVisitor<T>
|
||||
where T: Deserialize<'de>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
type Value = Option<T>;
|
||||
|
||||
@ -386,31 +411,36 @@ impl<'de, T> Visitor<'de> for OptionVisitor<T>
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(self) -> Result<Option<T>, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_none<E>(self) -> Result<Option<T>, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_some<D>(self, deserializer: D) -> Result<Option<T>, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
Ok(Some(try!(Deserialize::deserialize(deserializer))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T> Deserialize<'de> for Option<T>
|
||||
where T: Deserialize<'de>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Option<T>, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_option(OptionVisitor { marker: PhantomData })
|
||||
}
|
||||
@ -431,7 +461,8 @@ impl<'de, T> Visitor<'de> for PhantomDataVisitor<T> {
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(self) -> Result<PhantomData<T>, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(PhantomData)
|
||||
}
|
||||
@ -439,7 +470,8 @@ impl<'de, T> Visitor<'de> for PhantomDataVisitor<T> {
|
||||
|
||||
impl<'de, T> Deserialize<'de> for PhantomData<T> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<PhantomData<T>, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let visitor = PhantomDataVisitor { marker: PhantomData };
|
||||
deserializer.deserialize_unit_struct("PhantomData", visitor)
|
||||
@ -582,7 +614,8 @@ impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 0]> {
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(self, _: V) -> Result<[T; 0], V::Error>
|
||||
where V: SeqVisitor<'de>
|
||||
where
|
||||
V: SeqVisitor<'de>,
|
||||
{
|
||||
Ok([])
|
||||
}
|
||||
@ -591,7 +624,8 @@ impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 0]> {
|
||||
// Does not require T: Deserialize<'de>.
|
||||
impl<'de, T> Deserialize<'de> for [T; 0] {
|
||||
fn deserialize<D>(deserializer: D) -> Result<[T; 0], D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_seq_fixed_size(0, ArrayVisitor::<[T; 0]>::new())
|
||||
}
|
||||
@ -824,7 +858,8 @@ map_impl!(
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for net::IpAddr {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = try!(String::deserialize(deserializer));
|
||||
match s.parse() {
|
||||
@ -837,7 +872,8 @@ impl<'de> Deserialize<'de> for net::IpAddr {
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for net::Ipv4Addr {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = try!(String::deserialize(deserializer));
|
||||
match s.parse() {
|
||||
@ -850,7 +886,8 @@ impl<'de> Deserialize<'de> for net::Ipv4Addr {
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for net::Ipv6Addr {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = try!(String::deserialize(deserializer));
|
||||
match s.parse() {
|
||||
@ -865,7 +902,8 @@ impl<'de> Deserialize<'de> for net::Ipv6Addr {
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for net::SocketAddr {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = try!(String::deserialize(deserializer));
|
||||
match s.parse() {
|
||||
@ -878,7 +916,8 @@ impl<'de> Deserialize<'de> for net::SocketAddr {
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for net::SocketAddrV4 {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = try!(String::deserialize(deserializer));
|
||||
match s.parse() {
|
||||
@ -891,7 +930,8 @@ impl<'de> Deserialize<'de> for net::SocketAddrV4 {
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for net::SocketAddrV6 {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = try!(String::deserialize(deserializer));
|
||||
match s.parse() {
|
||||
@ -915,13 +955,15 @@ impl<'de> Visitor<'de> for PathBufVisitor {
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<path::PathBuf, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(From::from(v))
|
||||
}
|
||||
|
||||
fn visit_string<E>(self, v: String) -> Result<path::PathBuf, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(From::from(v))
|
||||
}
|
||||
@ -931,7 +973,8 @@ impl<'de> Visitor<'de> for PathBufVisitor {
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for path::PathBuf {
|
||||
fn deserialize<D>(deserializer: D) -> Result<path::PathBuf, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_string(PathBufVisitor)
|
||||
}
|
||||
@ -951,7 +994,8 @@ static OSSTR_VARIANTS: &'static [&'static str] = &["Unix", "Windows"];
|
||||
#[cfg(all(feature = "std", any(unix, windows)))]
|
||||
impl<'de> Deserialize<'de> for OsStringKind {
|
||||
fn deserialize<D>(deserializer: D) -> Result<OsStringKind, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct KindVisitor;
|
||||
|
||||
@ -963,17 +1007,19 @@ impl<'de> Deserialize<'de> for OsStringKind {
|
||||
}
|
||||
|
||||
fn visit_u32<E>(self, value: u32) -> Result<OsStringKind, E>
|
||||
where E: Error,
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
0 => Ok(OsStringKind::Unix),
|
||||
1 => Ok(OsStringKind::Windows),
|
||||
_ => Err(Error::invalid_value(Unexpected::Unsigned(value as u64), &self))
|
||||
_ => Err(Error::invalid_value(Unexpected::Unsigned(value as u64), &self),),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<OsStringKind, E>
|
||||
where E: Error,
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
"Unix" => Ok(OsStringKind::Unix),
|
||||
@ -983,7 +1029,8 @@ impl<'de> Deserialize<'de> for OsStringKind {
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<OsStringKind, E>
|
||||
where E: Error,
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
b"Unix" => Ok(OsStringKind::Unix),
|
||||
@ -991,9 +1038,7 @@ impl<'de> Deserialize<'de> for OsStringKind {
|
||||
_ => {
|
||||
match str::from_utf8(value) {
|
||||
Ok(value) => Err(Error::unknown_variant(value, OSSTR_VARIANTS)),
|
||||
Err(_) => {
|
||||
Err(Error::invalid_value(Unexpected::Bytes(value), &self))
|
||||
}
|
||||
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(value), &self)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1017,33 +1062,31 @@ impl<'de> Visitor<'de> for OsStringVisitor {
|
||||
|
||||
#[cfg(unix)]
|
||||
fn visit_enum<V>(self, visitor: V) -> Result<OsString, V::Error>
|
||||
where V: EnumVisitor<'de>,
|
||||
where
|
||||
V: EnumVisitor<'de>,
|
||||
{
|
||||
use std::os::unix::ffi::OsStringExt;
|
||||
|
||||
match try!(visitor.visit_variant()) {
|
||||
(OsStringKind::Unix, variant) => {
|
||||
variant.visit_newtype().map(OsString::from_vec)
|
||||
}
|
||||
(OsStringKind::Windows, _) => {
|
||||
Err(Error::custom("cannot deserialize Windows OS string on Unix"))
|
||||
}
|
||||
(OsStringKind::Unix, variant) => variant.visit_newtype().map(OsString::from_vec),
|
||||
(OsStringKind::Windows, _) => Err(Error::custom("cannot deserialize Windows OS string on Unix",),),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn visit_enum<V>(self, visitor: V) -> Result<OsString, V::Error>
|
||||
where V: EnumVisitor<'de>,
|
||||
where
|
||||
V: EnumVisitor<'de>,
|
||||
{
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
|
||||
match try!(visitor.visit_variant()) {
|
||||
(OsStringKind::Windows, variant) => {
|
||||
variant.visit_newtype::<Vec<u16>>().map(|vec| OsString::from_wide(&vec))
|
||||
}
|
||||
(OsStringKind::Unix, _) => {
|
||||
Err(Error::custom("cannot deserialize Unix OS string on Windows"))
|
||||
variant
|
||||
.visit_newtype::<Vec<u16>>()
|
||||
.map(|vec| OsString::from_wide(&vec))
|
||||
}
|
||||
(OsStringKind::Unix, _) => Err(Error::custom("cannot deserialize Unix OS string on Windows",),),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1051,7 +1094,8 @@ impl<'de> Visitor<'de> for OsStringVisitor {
|
||||
#[cfg(all(feature = "std", any(unix, windows)))]
|
||||
impl<'de> Deserialize<'de> for OsString {
|
||||
fn deserialize<D>(deserializer: D) -> Result<OsString, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_enum("OsString", OSSTR_VARIANTS, OsStringVisitor)
|
||||
}
|
||||
@ -1062,7 +1106,8 @@ impl<'de> Deserialize<'de> for OsString {
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Box<T> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Box<T>, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Box::new(val))
|
||||
@ -1072,7 +1117,8 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Box<T> {
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Box<[T]> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Box<[T]>, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let v: Vec<T> = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(v.into_boxed_slice())
|
||||
@ -1082,7 +1128,8 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Box<[T]> {
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de> Deserialize<'de> for Box<str> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = try!(String::deserialize(deserializer));
|
||||
Ok(s.into_boxed_str())
|
||||
@ -1092,7 +1139,8 @@ impl<'de> Deserialize<'de> for Box<str> {
|
||||
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
||||
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Arc<T> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Arc<T>, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Arc::new(val))
|
||||
@ -1102,7 +1150,8 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Arc<T> {
|
||||
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
||||
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Rc<T> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Rc<T>, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Rc::new(val))
|
||||
@ -1111,12 +1160,14 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Rc<T> {
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T>
|
||||
where T: ToOwned,
|
||||
T::Owned: Deserialize<'de>
|
||||
where
|
||||
T: ToOwned,
|
||||
T::Owned: Deserialize<'de>,
|
||||
{
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: D) -> Result<Cow<'a, T>, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Cow::Owned(val))
|
||||
@ -1136,7 +1187,8 @@ impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T>
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for Duration {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
enum Field {
|
||||
Secs,
|
||||
@ -1145,7 +1197,8 @@ impl<'de> Deserialize<'de> for Duration {
|
||||
|
||||
impl<'de> Deserialize<'de> for Field {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct FieldVisitor;
|
||||
|
||||
@ -1157,7 +1210,8 @@ impl<'de> Deserialize<'de> for Duration {
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Field, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
"secs" => Ok(Field::Secs),
|
||||
@ -1167,7 +1221,8 @@ impl<'de> Deserialize<'de> for Duration {
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
b"secs" => Ok(Field::Secs),
|
||||
@ -1194,7 +1249,8 @@ impl<'de> Deserialize<'de> for Duration {
|
||||
}
|
||||
|
||||
fn visit_seq<V>(self, mut visitor: V) -> Result<Duration, V::Error>
|
||||
where V: SeqVisitor<'de>
|
||||
where
|
||||
V: SeqVisitor<'de>,
|
||||
{
|
||||
let secs: u64 = match try!(visitor.visit()) {
|
||||
Some(value) => value,
|
||||
@ -1212,7 +1268,8 @@ impl<'de> Deserialize<'de> for Duration {
|
||||
}
|
||||
|
||||
fn visit_map<V>(self, mut visitor: V) -> Result<Duration, V::Error>
|
||||
where V: MapVisitor<'de>
|
||||
where
|
||||
V: MapVisitor<'de>,
|
||||
{
|
||||
let mut secs: Option<u64> = None;
|
||||
let mut nanos: Option<u32> = None;
|
||||
@ -1262,7 +1319,8 @@ impl<'de> Deserialize<'de> for Duration {
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de, Idx: Deserialize<'de>> Deserialize<'de> for ops::Range<Idx> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
enum Field {
|
||||
Start,
|
||||
@ -1271,7 +1329,8 @@ impl<'de, Idx: Deserialize<'de>> Deserialize<'de> for ops::Range<Idx> {
|
||||
|
||||
impl<'de> Deserialize<'de> for Field {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct FieldVisitor;
|
||||
|
||||
@ -1283,7 +1342,8 @@ impl<'de, Idx: Deserialize<'de>> Deserialize<'de> for ops::Range<Idx> {
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Field, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
"start" => Ok(Field::Start),
|
||||
@ -1293,7 +1353,8 @@ impl<'de, Idx: Deserialize<'de>> Deserialize<'de> for ops::Range<Idx> {
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
b"start" => Ok(Field::Start),
|
||||
@ -1322,7 +1383,8 @@ impl<'de, Idx: Deserialize<'de>> Deserialize<'de> for ops::Range<Idx> {
|
||||
}
|
||||
|
||||
fn visit_seq<V>(self, mut visitor: V) -> Result<ops::Range<Idx>, V::Error>
|
||||
where V: SeqVisitor<'de>
|
||||
where
|
||||
V: SeqVisitor<'de>,
|
||||
{
|
||||
let start: Idx = match try!(visitor.visit()) {
|
||||
Some(value) => value,
|
||||
@ -1340,7 +1402,8 @@ impl<'de, Idx: Deserialize<'de>> Deserialize<'de> for ops::Range<Idx> {
|
||||
}
|
||||
|
||||
fn visit_map<V>(self, mut visitor: V) -> Result<ops::Range<Idx>, V::Error>
|
||||
where V: MapVisitor<'de>
|
||||
where
|
||||
V: MapVisitor<'de>,
|
||||
{
|
||||
let mut start: Option<Idx> = None;
|
||||
let mut end: Option<Idx> = None;
|
||||
@ -1385,10 +1448,12 @@ impl<'de, Idx: Deserialize<'de>> Deserialize<'de> for ops::Range<Idx> {
|
||||
#[cfg(feature = "unstable")]
|
||||
#[allow(deprecated)] // num::Zero is deprecated but there is no replacement
|
||||
impl<'de, T> Deserialize<'de> for NonZero<T>
|
||||
where T: Deserialize<'de> + PartialEq + Zeroable + Zero
|
||||
where
|
||||
T: Deserialize<'de> + PartialEq + Zeroable + Zero,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<NonZero<T>, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value = try!(Deserialize::deserialize(deserializer));
|
||||
if value == Zero::zero() {
|
||||
@ -1402,11 +1467,13 @@ impl<'de, T> Deserialize<'de> for NonZero<T>
|
||||
|
||||
|
||||
impl<'de, T, E> Deserialize<'de> for Result<T, E>
|
||||
where T: Deserialize<'de>,
|
||||
E: Deserialize<'de>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
E: Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Result<T, E>, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
enum Field {
|
||||
Ok,
|
||||
@ -1416,7 +1483,8 @@ impl<'de, T, E> Deserialize<'de> for Result<T, E>
|
||||
impl<'de> Deserialize<'de> for Field {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct FieldVisitor;
|
||||
|
||||
@ -1428,19 +1496,21 @@ impl<'de, T, E> Deserialize<'de> for Result<T, E>
|
||||
}
|
||||
|
||||
fn visit_u32<E>(self, value: u32) -> Result<Field, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
0 => Ok(Field::Ok),
|
||||
1 => Ok(Field::Err),
|
||||
_ => {
|
||||
Err(Error::invalid_value(Unexpected::Unsigned(value as u64), &self))
|
||||
Err(Error::invalid_value(Unexpected::Unsigned(value as u64), &self),)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Field, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
"Ok" => Ok(Field::Ok),
|
||||
@ -1450,7 +1520,8 @@ impl<'de, T, E> Deserialize<'de> for Result<T, E>
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
b"Ok" => Ok(Field::Ok),
|
||||
@ -1474,8 +1545,9 @@ impl<'de, T, E> Deserialize<'de> for Result<T, E>
|
||||
struct ResultVisitor<T, E>(PhantomData<Result<T, E>>);
|
||||
|
||||
impl<'de, T, E> Visitor<'de> for ResultVisitor<T, E>
|
||||
where T: Deserialize<'de>,
|
||||
E: Deserialize<'de>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
E: Deserialize<'de>,
|
||||
{
|
||||
type Value = Result<T, E>;
|
||||
|
||||
@ -1484,7 +1556,8 @@ impl<'de, T, E> Deserialize<'de> for Result<T, E>
|
||||
}
|
||||
|
||||
fn visit_enum<V>(self, visitor: V) -> Result<Result<T, E>, V::Error>
|
||||
where V: EnumVisitor<'de>
|
||||
where
|
||||
V: EnumVisitor<'de>,
|
||||
{
|
||||
match try!(visitor.visit_variant()) {
|
||||
(Field::Ok, variant) => variant.visit_newtype().map(Ok),
|
||||
|
@ -421,7 +421,8 @@ pub trait Expected {
|
||||
}
|
||||
|
||||
impl<'de, T> Expected for T
|
||||
where T: Visitor<'de>
|
||||
where
|
||||
T: Visitor<'de>,
|
||||
{
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.expecting(formatter)
|
||||
@ -474,7 +475,8 @@ pub trait Deserialize<'de>: Sized {
|
||||
///
|
||||
/// [impl-deserialize]: https://serde.rs/impl-deserialize.html
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>;
|
||||
where
|
||||
D: Deserializer<'de>;
|
||||
}
|
||||
|
||||
/// A data structure that can be deserialized without borrowing any data from
|
||||
@ -499,7 +501,11 @@ pub trait Deserialize<'de>: Sized {
|
||||
/// # }
|
||||
/// ```
|
||||
pub trait DeserializeOwned: for<'de> Deserialize<'de> {}
|
||||
impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
|
||||
impl<T> DeserializeOwned for T
|
||||
where
|
||||
T: for<'de> Deserialize<'de>,
|
||||
{
|
||||
}
|
||||
|
||||
/// `DeserializeSeed` is the stateful form of the `Deserialize` trait. If you
|
||||
/// ever find yourself looking for a way to pass data into a `Deserialize` impl,
|
||||
@ -651,17 +657,20 @@ pub trait DeserializeSeed<'de>: Sized {
|
||||
/// Equivalent to the more common `Deserialize::deserialize` method, except
|
||||
/// with some initial piece of data (the seed) passed in.
|
||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where D: Deserializer<'de>;
|
||||
where
|
||||
D: Deserializer<'de>;
|
||||
}
|
||||
|
||||
impl<'de, T> DeserializeSeed<'de> for PhantomData<T>
|
||||
where T: Deserialize<'de>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
type Value = T;
|
||||
|
||||
#[inline]
|
||||
fn deserialize<D>(self, deserializer: D) -> Result<T, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
T::deserialize(deserializer)
|
||||
}
|
||||
@ -764,43 +773,69 @@ pub trait Deserializer<'de>: Sized {
|
||||
/// `Deserializer::deserialize` means your data type will be able to
|
||||
/// deserialize from self-describing formats only, ruling out Bincode and
|
||||
/// many others.
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a `bool` value.
|
||||
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a `u8` value.
|
||||
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a `u16` value.
|
||||
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a `u32` value.
|
||||
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a `u64` value.
|
||||
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting an `i8` value.
|
||||
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting an `i16` value.
|
||||
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting an `i32` value.
|
||||
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting an `i64` value.
|
||||
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a `f32` value.
|
||||
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a `f64` value.
|
||||
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a `char` value.
|
||||
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a string value and does
|
||||
/// not benefit from taking ownership of buffered data owned by the
|
||||
@ -809,7 +844,9 @@ pub trait Deserializer<'de>: Sized {
|
||||
/// If the `Visitor` would benefit from taking ownership of `String` data,
|
||||
/// indiciate this to the `Deserializer` by using `deserialize_string`
|
||||
/// instead.
|
||||
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a string value and would
|
||||
/// benefit from taking ownership of buffered data owned by the
|
||||
@ -818,7 +855,9 @@ pub trait Deserializer<'de>: Sized {
|
||||
/// If the `Visitor` would not benefit from taking ownership of `String`
|
||||
/// data, indicate that to the `Deserializer` by using `deserialize_str`
|
||||
/// instead.
|
||||
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a byte array and does not
|
||||
/// benefit from taking ownership of buffered data owned by the
|
||||
@ -827,7 +866,9 @@ pub trait Deserializer<'de>: Sized {
|
||||
/// If the `Visitor` would benefit from taking ownership of `Vec<u8>` data,
|
||||
/// indicate this to the `Deserializer` by using `deserialize_byte_buf`
|
||||
/// instead.
|
||||
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a byte array and would
|
||||
/// benefit from taking ownership of buffered data owned by the
|
||||
@ -836,91 +877,116 @@ pub trait Deserializer<'de>: Sized {
|
||||
/// If the `Visitor` would not benefit from taking ownership of `Vec<u8>`
|
||||
/// data, indicate that to the `Deserializer` by using `deserialize_bytes`
|
||||
/// instead.
|
||||
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting an optional value.
|
||||
///
|
||||
/// This allows deserializers that encode an optional value as a nullable
|
||||
/// value to convert the null value into `None` and a regular value into
|
||||
/// `Some(value)`.
|
||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a unit value.
|
||||
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a unit struct with a
|
||||
/// particular name.
|
||||
fn deserialize_unit_struct<V>(self,
|
||||
name: &'static str,
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
fn deserialize_unit_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a newtype struct with a
|
||||
/// particular name.
|
||||
fn deserialize_newtype_struct<V>(self,
|
||||
name: &'static str,
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
fn deserialize_newtype_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a sequence of values.
|
||||
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a sequence of values and
|
||||
/// knows how many values there are without looking at the serialized data.
|
||||
fn deserialize_seq_fixed_size<V>(self,
|
||||
len: usize,
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
fn deserialize_seq_fixed_size<V>(
|
||||
self,
|
||||
len: usize,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a tuple value with a
|
||||
/// particular number of elements.
|
||||
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a tuple struct with a
|
||||
/// particular name and number of fields.
|
||||
fn deserialize_tuple_struct<V>(self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
fn deserialize_tuple_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a map of key-value pairs.
|
||||
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>;
|
||||
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting a struct with a particular
|
||||
/// name and fields.
|
||||
fn deserialize_struct<V>(self,
|
||||
name: &'static str,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
fn deserialize_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting the name of a struct
|
||||
/// field or the discriminant of an enum variant.
|
||||
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type is expecting an enum value with a
|
||||
/// particular name and possible variants.
|
||||
fn deserialize_enum<V>(self,
|
||||
name: &'static str,
|
||||
variants: &'static [&'static str],
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Hint that the `Deserialize` type needs to deserialize a value whose type
|
||||
/// doesn't matter because it is ignored.
|
||||
///
|
||||
/// Deserializers for non-self-describing formats may not support this mode.
|
||||
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -986,77 +1052,88 @@ pub trait Visitor<'de>: Sized {
|
||||
|
||||
/// Deserialize a `bool` into a `Value`.
|
||||
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Err(Error::invalid_type(Unexpected::Bool(v), &self))
|
||||
}
|
||||
|
||||
/// Deserialize an `i8` into a `Value`.
|
||||
fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// Deserialize an `i16` into a `Value`.
|
||||
fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// Deserialize an `i32` into a `Value`.
|
||||
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// Deserialize an `i64` into a `Value`.
|
||||
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Err(Error::invalid_type(Unexpected::Signed(v), &self))
|
||||
}
|
||||
|
||||
/// Deserialize a `u8` into a `Value`.
|
||||
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// Deserialize a `u16` into a `Value`.
|
||||
fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// Deserialize a `u32` into a `Value`.
|
||||
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// Deserialize a `u64` into a `Value`.
|
||||
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Err(Error::invalid_type(Unexpected::Unsigned(v), &self))
|
||||
}
|
||||
|
||||
/// Deserialize a `f32` into a `Value`.
|
||||
fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_f64(v as f64)
|
||||
}
|
||||
|
||||
/// Deserialize a `f64` into a `Value`.
|
||||
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Err(Error::invalid_type(Unexpected::Float(v), &self))
|
||||
}
|
||||
@ -1064,7 +1141,8 @@ pub trait Visitor<'de>: Sized {
|
||||
/// Deserialize a `char` into a `Value`.
|
||||
#[inline]
|
||||
fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_str(utf8::encode(v).as_str())
|
||||
}
|
||||
@ -1080,7 +1158,8 @@ pub trait Visitor<'de>: Sized {
|
||||
/// It is never correct to implement `visit_string` without implementing
|
||||
/// `visit_str`. Implement neither, both, or just `visit_str`.
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Err(Error::invalid_type(Unexpected::Str(v), &self))
|
||||
}
|
||||
@ -1095,7 +1174,8 @@ pub trait Visitor<'de>: Sized {
|
||||
/// The default implementation forwards to `visit_str`.
|
||||
#[inline]
|
||||
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_str(v)
|
||||
}
|
||||
@ -1117,28 +1197,32 @@ pub trait Visitor<'de>: Sized {
|
||||
#[inline]
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_str(&v)
|
||||
}
|
||||
|
||||
/// Deserialize a `()` into a `Value`.
|
||||
fn visit_unit<E>(self) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Err(Error::invalid_type(Unexpected::Unit, &self))
|
||||
}
|
||||
|
||||
/// Deserialize an absent optional `Value`.
|
||||
fn visit_none<E>(self) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Err(Error::invalid_type(Unexpected::Option, &self))
|
||||
}
|
||||
|
||||
/// Deserialize a present optional `Value`.
|
||||
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let _ = deserializer;
|
||||
Err(Error::invalid_type(Unexpected::Option, &self))
|
||||
@ -1146,7 +1230,8 @@ pub trait Visitor<'de>: Sized {
|
||||
|
||||
/// Deserialize `Value` as a newtype struct.
|
||||
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let _ = deserializer;
|
||||
Err(Error::invalid_type(Unexpected::NewtypeStruct, &self))
|
||||
@ -1154,7 +1239,8 @@ pub trait Visitor<'de>: Sized {
|
||||
|
||||
/// Deserialize `Value` as a sequence of elements.
|
||||
fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: SeqVisitor<'de>
|
||||
where
|
||||
V: SeqVisitor<'de>,
|
||||
{
|
||||
let _ = visitor;
|
||||
Err(Error::invalid_type(Unexpected::Seq, &self))
|
||||
@ -1162,7 +1248,8 @@ pub trait Visitor<'de>: Sized {
|
||||
|
||||
/// Deserialize `Value` as a key-value map.
|
||||
fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: MapVisitor<'de>
|
||||
where
|
||||
V: MapVisitor<'de>,
|
||||
{
|
||||
let _ = visitor;
|
||||
Err(Error::invalid_type(Unexpected::Map, &self))
|
||||
@ -1170,7 +1257,8 @@ pub trait Visitor<'de>: Sized {
|
||||
|
||||
/// Deserialize `Value` as an enum.
|
||||
fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: EnumVisitor<'de>
|
||||
where
|
||||
V: EnumVisitor<'de>,
|
||||
{
|
||||
let _ = visitor;
|
||||
Err(Error::invalid_type(Unexpected::Enum, &self))
|
||||
@ -1187,7 +1275,8 @@ pub trait Visitor<'de>: Sized {
|
||||
/// It is never correct to implement `visit_byte_buf` without implementing
|
||||
/// `visit_bytes`. Implement neither, both, or just `visit_bytes`.
|
||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
let _ = v;
|
||||
Err(Error::invalid_type(Unexpected::Bytes(v), &self))
|
||||
@ -1202,7 +1291,8 @@ pub trait Visitor<'de>: Sized {
|
||||
/// The default implementation forwards to `visit_bytes`.
|
||||
#[inline]
|
||||
fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_bytes(v)
|
||||
}
|
||||
@ -1224,7 +1314,8 @@ pub trait Visitor<'de>: Sized {
|
||||
/// `Vec<u8>`.
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
|
||||
where E: Error
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.visit_bytes(&v)
|
||||
}
|
||||
@ -1247,7 +1338,8 @@ pub trait SeqVisitor<'de> {
|
||||
/// `Deserialize` implementations should typically use `SeqVisitor::visit`
|
||||
/// instead.
|
||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
||||
where T: DeserializeSeed<'de>;
|
||||
where
|
||||
T: DeserializeSeed<'de>;
|
||||
|
||||
/// This returns `Ok(Some(value))` for the next value in the sequence, or
|
||||
/// `Ok(None)` if there are no more remaining items.
|
||||
@ -1256,7 +1348,8 @@ pub trait SeqVisitor<'de> {
|
||||
/// `SeqVisitor` implementations should not override the default behavior.
|
||||
#[inline]
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, Self::Error>
|
||||
where T: Deserialize<'de>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
self.visit_seed(PhantomData)
|
||||
}
|
||||
@ -1269,20 +1362,23 @@ pub trait SeqVisitor<'de> {
|
||||
}
|
||||
|
||||
impl<'de, 'a, V> SeqVisitor<'de> for &'a mut V
|
||||
where V: SeqVisitor<'de>
|
||||
where
|
||||
V: SeqVisitor<'de>,
|
||||
{
|
||||
type Error = V::Error;
|
||||
|
||||
#[inline]
|
||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, V::Error>
|
||||
where T: DeserializeSeed<'de>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
(**self).visit_seed(seed)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, V::Error>
|
||||
where T: Deserialize<'de>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
(**self).visit()
|
||||
}
|
||||
@ -1309,14 +1405,16 @@ pub trait MapVisitor<'de> {
|
||||
/// `Deserialize` implementations should typically use
|
||||
/// `MapVisitor::visit_key` or `MapVisitor::visit` instead.
|
||||
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
|
||||
where K: DeserializeSeed<'de>;
|
||||
where
|
||||
K: DeserializeSeed<'de>;
|
||||
|
||||
/// This returns a `Ok(value)` for the next value in the map.
|
||||
///
|
||||
/// `Deserialize` implementations should typically use
|
||||
/// `MapVisitor::visit_value` instead.
|
||||
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
|
||||
where V: DeserializeSeed<'de>;
|
||||
where
|
||||
V: DeserializeSeed<'de>;
|
||||
|
||||
/// This returns `Ok(Some((key, value)))` for the next (key-value) pair in
|
||||
/// the map, or `Ok(None)` if there are no more remaining items.
|
||||
@ -1327,12 +1425,14 @@ pub trait MapVisitor<'de> {
|
||||
/// `Deserialize` implementations should typically use `MapVisitor::visit`
|
||||
/// instead.
|
||||
#[inline]
|
||||
fn visit_seed<K, V>(&mut self,
|
||||
kseed: K,
|
||||
vseed: V)
|
||||
-> Result<Option<(K::Value, V::Value)>, Self::Error>
|
||||
where K: DeserializeSeed<'de>,
|
||||
V: DeserializeSeed<'de>
|
||||
fn visit_seed<K, V>(
|
||||
&mut self,
|
||||
kseed: K,
|
||||
vseed: V,
|
||||
) -> Result<Option<(K::Value, V::Value)>, Self::Error>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
match try!(self.visit_key_seed(kseed)) {
|
||||
Some(key) => {
|
||||
@ -1350,7 +1450,8 @@ pub trait MapVisitor<'de> {
|
||||
/// `MapVisitor` implementations should not override the default behavior.
|
||||
#[inline]
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>, Self::Error>
|
||||
where K: Deserialize<'de>
|
||||
where
|
||||
K: Deserialize<'de>,
|
||||
{
|
||||
self.visit_key_seed(PhantomData)
|
||||
}
|
||||
@ -1361,7 +1462,8 @@ pub trait MapVisitor<'de> {
|
||||
/// `MapVisitor` implementations should not override the default behavior.
|
||||
#[inline]
|
||||
fn visit_value<V>(&mut self) -> Result<V, Self::Error>
|
||||
where V: Deserialize<'de>
|
||||
where
|
||||
V: Deserialize<'de>,
|
||||
{
|
||||
self.visit_value_seed(PhantomData)
|
||||
}
|
||||
@ -1373,8 +1475,9 @@ pub trait MapVisitor<'de> {
|
||||
/// `MapVisitor` implementations should not override the default behavior.
|
||||
#[inline]
|
||||
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, Self::Error>
|
||||
where K: Deserialize<'de>,
|
||||
V: Deserialize<'de>
|
||||
where
|
||||
K: Deserialize<'de>,
|
||||
V: Deserialize<'de>,
|
||||
{
|
||||
self.visit_seed(PhantomData, PhantomData)
|
||||
}
|
||||
@ -1387,53 +1490,61 @@ pub trait MapVisitor<'de> {
|
||||
}
|
||||
|
||||
impl<'de, 'a, V_> MapVisitor<'de> for &'a mut V_
|
||||
where V_: MapVisitor<'de>
|
||||
where
|
||||
V_: MapVisitor<'de>,
|
||||
{
|
||||
type Error = V_::Error;
|
||||
|
||||
#[inline]
|
||||
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
|
||||
where K: DeserializeSeed<'de>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
{
|
||||
(**self).visit_key_seed(seed)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
|
||||
where V: DeserializeSeed<'de>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
(**self).visit_value_seed(seed)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seed<K, V>(&mut self,
|
||||
kseed: K,
|
||||
vseed: V)
|
||||
-> Result<Option<(K::Value, V::Value)>, Self::Error>
|
||||
where K: DeserializeSeed<'de>,
|
||||
V: DeserializeSeed<'de>
|
||||
fn visit_seed<K, V>(
|
||||
&mut self,
|
||||
kseed: K,
|
||||
vseed: V,
|
||||
) -> Result<Option<(K::Value, V::Value)>, Self::Error>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
(**self).visit_seed(kseed, vseed)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, V_::Error>
|
||||
where K: Deserialize<'de>,
|
||||
V: Deserialize<'de>
|
||||
where
|
||||
K: Deserialize<'de>,
|
||||
V: Deserialize<'de>,
|
||||
{
|
||||
(**self).visit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>, V_::Error>
|
||||
where K: Deserialize<'de>
|
||||
where
|
||||
K: Deserialize<'de>,
|
||||
{
|
||||
(**self).visit_key()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_value<V>(&mut self) -> Result<V, V_::Error>
|
||||
where V: Deserialize<'de>
|
||||
where
|
||||
V: Deserialize<'de>,
|
||||
{
|
||||
(**self).visit_value()
|
||||
}
|
||||
@ -1462,7 +1573,8 @@ pub trait EnumVisitor<'de>: Sized {
|
||||
/// `Deserialize` implementations should typically use
|
||||
/// `EnumVisitor::visit_variant` instead.
|
||||
fn visit_variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
|
||||
where V: DeserializeSeed<'de>;
|
||||
where
|
||||
V: DeserializeSeed<'de>;
|
||||
|
||||
/// `visit_variant` is called to identify which variant to deserialize.
|
||||
///
|
||||
@ -1470,7 +1582,8 @@ pub trait EnumVisitor<'de>: Sized {
|
||||
/// `EnumVisitor` implementations should not override the default behavior.
|
||||
#[inline]
|
||||
fn visit_variant<V>(self) -> Result<(V, Self::Variant), Self::Error>
|
||||
where V: Deserialize<'de>
|
||||
where
|
||||
V: Deserialize<'de>,
|
||||
{
|
||||
self.visit_variant_seed(PhantomData)
|
||||
}
|
||||
@ -1556,7 +1669,8 @@ pub trait VariantVisitor<'de>: Sized {
|
||||
/// # }
|
||||
/// ```
|
||||
fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
|
||||
where T: DeserializeSeed<'de>;
|
||||
where
|
||||
T: DeserializeSeed<'de>;
|
||||
|
||||
/// Called when deserializing a variant with a single value.
|
||||
///
|
||||
@ -1565,7 +1679,8 @@ pub trait VariantVisitor<'de>: Sized {
|
||||
/// behavior.
|
||||
#[inline]
|
||||
fn visit_newtype<T>(self) -> Result<T, Self::Error>
|
||||
where T: Deserialize<'de>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
self.visit_newtype_seed(PhantomData)
|
||||
}
|
||||
@ -1609,7 +1724,8 @@ pub trait VariantVisitor<'de>: Sized {
|
||||
/// # }
|
||||
/// ```
|
||||
fn visit_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
||||
/// Called when deserializing a struct-like variant.
|
||||
///
|
||||
@ -1649,11 +1765,13 @@ pub trait VariantVisitor<'de>: Sized {
|
||||
/// }
|
||||
/// # }
|
||||
/// ```
|
||||
fn visit_struct<V>(self,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: Visitor<'de>;
|
||||
fn visit_struct<V>(
|
||||
self,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -31,10 +31,7 @@ pub fn encode(c: char) -> Encode {
|
||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
||||
0
|
||||
};
|
||||
Encode {
|
||||
buf: buf,
|
||||
pos: pos,
|
||||
}
|
||||
Encode { buf: buf, pos: pos }
|
||||
}
|
||||
|
||||
pub struct Encode {
|
||||
|
@ -51,7 +51,8 @@ impl error::Error for Error {
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<'de, E> IntoDeserializer<'de, E> for ()
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Deserializer = UnitDeserializer<E>;
|
||||
|
||||
@ -67,7 +68,8 @@ pub struct UnitDeserializer<E> {
|
||||
}
|
||||
|
||||
impl<'de, E> de::Deserializer<'de> for UnitDeserializer<E>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
@ -78,13 +80,15 @@ impl<'de, E> de::Deserializer<'de> for UnitDeserializer<E>
|
||||
}
|
||||
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_unit()
|
||||
}
|
||||
|
||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_none()
|
||||
}
|
||||
@ -156,7 +160,8 @@ pub struct U32Deserializer<E> {
|
||||
}
|
||||
|
||||
impl<'de, E> IntoDeserializer<'de, E> for u32
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Deserializer = U32Deserializer<E>;
|
||||
|
||||
@ -169,7 +174,8 @@ impl<'de, E> IntoDeserializer<'de, E> for u32
|
||||
}
|
||||
|
||||
impl<'de, E> de::Deserializer<'de> for U32Deserializer<E>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
@ -180,30 +186,35 @@ impl<'de, E> de::Deserializer<'de> for U32Deserializer<E>
|
||||
}
|
||||
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_u32(self.value)
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_enum(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E> de::EnumVisitor<'de> for U32Deserializer<E>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
type Variant = private::UnitOnly<E>;
|
||||
|
||||
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
||||
where T: de::DeserializeSeed<'de>
|
||||
where
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
seed.deserialize(self).map(private::unit_only)
|
||||
}
|
||||
@ -219,7 +230,8 @@ pub struct StrDeserializer<'a, E> {
|
||||
}
|
||||
|
||||
impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a str
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Deserializer = StrDeserializer<'a, E>;
|
||||
|
||||
@ -232,22 +244,26 @@ impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a str
|
||||
}
|
||||
|
||||
impl<'de, 'a, E> de::Deserializer<'de> for StrDeserializer<'a, E>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_str(self.value)
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_enum(self)
|
||||
}
|
||||
@ -260,13 +276,15 @@ impl<'de, 'a, E> de::Deserializer<'de> for StrDeserializer<'a, E>
|
||||
}
|
||||
|
||||
impl<'de, 'a, E> de::EnumVisitor<'de> for StrDeserializer<'a, E>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
type Variant = private::UnitOnly<E>;
|
||||
|
||||
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
||||
where T: de::DeserializeSeed<'de>
|
||||
where
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
seed.deserialize(self).map(private::unit_only)
|
||||
}
|
||||
@ -284,7 +302,8 @@ pub struct StringDeserializer<E> {
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, E> IntoDeserializer<'de, E> for String
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Deserializer = StringDeserializer<E>;
|
||||
|
||||
@ -298,22 +317,26 @@ impl<'de, E> IntoDeserializer<'de, E> for String
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, E> de::Deserializer<'de> for StringDeserializer<E>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_string(self.value)
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_enum(self)
|
||||
}
|
||||
@ -327,13 +350,15 @@ impl<'de, E> de::Deserializer<'de> for StringDeserializer<E>
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, 'a, E> de::EnumVisitor<'de> for StringDeserializer<E>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
type Variant = private::UnitOnly<E>;
|
||||
|
||||
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
||||
where T: de::DeserializeSeed<'de>
|
||||
where
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
seed.deserialize(self).map(private::unit_only)
|
||||
}
|
||||
@ -351,7 +376,8 @@ pub struct CowStrDeserializer<'a, E> {
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Deserializer = CowStrDeserializer<'a, E>;
|
||||
|
||||
@ -365,12 +391,14 @@ impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str>
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
match self.value {
|
||||
Cow::Borrowed(string) => visitor.visit_str(string),
|
||||
@ -378,12 +406,14 @@ impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E>
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_enum(self)
|
||||
}
|
||||
@ -397,13 +427,15 @@ impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E>
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, 'a, E> de::EnumVisitor<'de> for CowStrDeserializer<'a, E>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
type Variant = private::UnitOnly<E>;
|
||||
|
||||
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
||||
where T: de::DeserializeSeed<'de>
|
||||
where
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
seed.deserialize(self).map(private::unit_only)
|
||||
}
|
||||
@ -420,8 +452,9 @@ pub struct SeqDeserializer<I, E> {
|
||||
}
|
||||
|
||||
impl<I, E> SeqDeserializer<I, E>
|
||||
where I: Iterator,
|
||||
E: de::Error
|
||||
where
|
||||
I: Iterator,
|
||||
E: de::Error,
|
||||
{
|
||||
/// Construct a new `SeqDeserializer<I>`.
|
||||
pub fn new(iter: I) -> Self {
|
||||
@ -444,20 +477,22 @@ impl<I, E> SeqDeserializer<I, E>
|
||||
} else {
|
||||
// First argument is the number of elements in the data, second
|
||||
// argument is the number of elements expected by the Deserialize.
|
||||
Err(de::Error::invalid_length(self.count + remaining, &ExpectedInSeq(self.count)))
|
||||
Err(de::Error::invalid_length(self.count + remaining, &ExpectedInSeq(self.count)),)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, I, T, E> de::Deserializer<'de> for SeqDeserializer<I, E>
|
||||
where I: Iterator<Item = T>,
|
||||
T: IntoDeserializer<'de, E>,
|
||||
E: de::Error
|
||||
where
|
||||
I: Iterator<Item = T>,
|
||||
T: IntoDeserializer<'de, E>,
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn deserialize<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
let v = try!(visitor.visit_seq(&mut self));
|
||||
try!(self.end());
|
||||
@ -472,14 +507,16 @@ impl<'de, I, T, E> de::Deserializer<'de> for SeqDeserializer<I, E>
|
||||
}
|
||||
|
||||
impl<'de, I, T, E> de::SeqVisitor<'de> for SeqDeserializer<I, E>
|
||||
where I: Iterator<Item = T>,
|
||||
T: IntoDeserializer<'de, E>,
|
||||
E: de::Error
|
||||
where
|
||||
I: Iterator<Item = T>,
|
||||
T: IntoDeserializer<'de, E>,
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn visit_seed<V>(&mut self, seed: V) -> Result<Option<V::Value>, Self::Error>
|
||||
where V: de::DeserializeSeed<'de>
|
||||
where
|
||||
V: de::DeserializeSeed<'de>,
|
||||
{
|
||||
match self.iter.next() {
|
||||
Some(value) => {
|
||||
@ -511,8 +548,9 @@ impl Expected for ExpectedInSeq {
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, T, E> IntoDeserializer<'de, E> for Vec<T>
|
||||
where T: IntoDeserializer<'de, E>,
|
||||
E: de::Error
|
||||
where
|
||||
T: IntoDeserializer<'de, E>,
|
||||
E: de::Error,
|
||||
{
|
||||
type Deserializer = SeqDeserializer<<Vec<T> as IntoIterator>::IntoIter, E>;
|
||||
|
||||
@ -523,8 +561,9 @@ impl<'de, T, E> IntoDeserializer<'de, E> for Vec<T>
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet<T>
|
||||
where T: IntoDeserializer<'de, E> + Eq + Ord,
|
||||
E: de::Error
|
||||
where
|
||||
T: IntoDeserializer<'de, E> + Eq + Ord,
|
||||
E: de::Error,
|
||||
{
|
||||
type Deserializer = SeqDeserializer<<BTreeSet<T> as IntoIterator>::IntoIter, E>;
|
||||
|
||||
@ -535,8 +574,9 @@ impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet<T>
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de, T, E> IntoDeserializer<'de, E> for HashSet<T>
|
||||
where T: IntoDeserializer<'de, E> + Eq + Hash,
|
||||
E: de::Error
|
||||
where
|
||||
T: IntoDeserializer<'de, E> + Eq + Hash,
|
||||
E: de::Error,
|
||||
{
|
||||
type Deserializer = SeqDeserializer<<HashSet<T> as IntoIterator>::IntoIter, E>;
|
||||
|
||||
@ -556,19 +596,19 @@ pub struct SeqVisitorDeserializer<V_> {
|
||||
impl<V_> SeqVisitorDeserializer<V_> {
|
||||
/// Construct a new `SeqVisitorDeserializer<V_, E>`.
|
||||
pub fn new(visitor: V_) -> Self {
|
||||
SeqVisitorDeserializer {
|
||||
visitor: visitor,
|
||||
}
|
||||
SeqVisitorDeserializer { visitor: visitor }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, V_> de::Deserializer<'de> for SeqVisitorDeserializer<V_>
|
||||
where V_: de::SeqVisitor<'de>
|
||||
where
|
||||
V_: de::SeqVisitor<'de>,
|
||||
{
|
||||
type Error = V_::Error;
|
||||
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_seq(self.visitor)
|
||||
}
|
||||
@ -584,11 +624,12 @@ impl<'de, V_> de::Deserializer<'de> for SeqVisitorDeserializer<V_>
|
||||
|
||||
/// A helper deserializer that deserializes a map.
|
||||
pub struct MapDeserializer<'de, I, E>
|
||||
where I: Iterator,
|
||||
I::Item: private::Pair,
|
||||
<I::Item as private::Pair>::First: IntoDeserializer<'de, E>,
|
||||
<I::Item as private::Pair>::Second: IntoDeserializer<'de, E>,
|
||||
E: de::Error
|
||||
where
|
||||
I: Iterator,
|
||||
I::Item: private::Pair,
|
||||
<I::Item as private::Pair>::First: IntoDeserializer<'de, E>,
|
||||
<I::Item as private::Pair>::Second: IntoDeserializer<'de, E>,
|
||||
E: de::Error,
|
||||
{
|
||||
iter: iter::Fuse<I>,
|
||||
value: Option<<I::Item as private::Pair>::Second>,
|
||||
@ -598,11 +639,14 @@ pub struct MapDeserializer<'de, I, E>
|
||||
}
|
||||
|
||||
impl<'de, I, E> MapDeserializer<'de, I, E>
|
||||
where I: Iterator,
|
||||
I::Item: private::Pair,
|
||||
<I::Item as private::Pair>::First: IntoDeserializer<'de, E>,
|
||||
<I::Item as private::Pair>::Second: IntoDeserializer<'de, E>,
|
||||
E: de::Error
|
||||
where
|
||||
I: Iterator,
|
||||
I::Item: private::Pair,
|
||||
<I::Item as private::Pair>::First: IntoDeserializer<'de,
|
||||
E>,
|
||||
<I::Item as private::Pair>::Second: IntoDeserializer<'de,
|
||||
E>,
|
||||
E: de::Error,
|
||||
{
|
||||
/// Construct a new `MapDeserializer<I, K, V, E>`.
|
||||
pub fn new(iter: I) -> Self {
|
||||
@ -627,12 +671,12 @@ impl<'de, I, E> MapDeserializer<'de, I, E>
|
||||
} else {
|
||||
// First argument is the number of elements in the data, second
|
||||
// argument is the number of elements expected by the Deserialize.
|
||||
Err(de::Error::invalid_length(self.count + remaining, &ExpectedInMap(self.count)))
|
||||
Err(de::Error::invalid_length(self.count + remaining, &ExpectedInMap(self.count)),)
|
||||
}
|
||||
}
|
||||
|
||||
fn next_pair
|
||||
(&mut self)
|
||||
(&mut self,)
|
||||
-> Option<(<I::Item as private::Pair>::First, <I::Item as private::Pair>::Second)> {
|
||||
match self.iter.next() {
|
||||
Some(kv) => {
|
||||
@ -811,9 +855,10 @@ impl<'de, I, E> Debug for MapDeserializer<'de, I, E>
|
||||
struct PairDeserializer<A, B, E>(A, B, PhantomData<E>);
|
||||
|
||||
impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer<A, B, E>
|
||||
where A: IntoDeserializer<'de, E>,
|
||||
B: IntoDeserializer<'de, E>,
|
||||
E: de::Error
|
||||
where
|
||||
A: IntoDeserializer<'de, E>,
|
||||
B: IntoDeserializer<'de, E>,
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
@ -824,13 +869,15 @@ impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer<A, B, E>
|
||||
}
|
||||
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
self.deserialize_seq(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData);
|
||||
let pair = try!(visitor.visit_seq(&mut pair_visitor));
|
||||
@ -845,7 +892,8 @@ impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer<A, B, E>
|
||||
}
|
||||
|
||||
fn deserialize_seq_fixed_size<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
if len == 2 {
|
||||
self.deserialize_seq(visitor)
|
||||
@ -860,14 +908,16 @@ impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer<A, B, E>
|
||||
struct PairVisitor<A, B, E>(Option<A>, Option<B>, PhantomData<E>);
|
||||
|
||||
impl<'de, A, B, E> de::SeqVisitor<'de> for PairVisitor<A, B, E>
|
||||
where A: IntoDeserializer<'de, E>,
|
||||
B: IntoDeserializer<'de, E>,
|
||||
E: de::Error
|
||||
where
|
||||
A: IntoDeserializer<'de, E>,
|
||||
B: IntoDeserializer<'de, E>,
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
||||
where T: de::DeserializeSeed<'de>
|
||||
where
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
if let Some(k) = self.0.take() {
|
||||
seed.deserialize(k.into_deserializer()).map(Some)
|
||||
@ -906,9 +956,10 @@ impl Expected for ExpectedInMap {
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V>
|
||||
where K: IntoDeserializer<'de, E> + Eq + Ord,
|
||||
V: IntoDeserializer<'de, E>,
|
||||
E: de::Error
|
||||
where
|
||||
K: IntoDeserializer<'de, E> + Eq + Ord,
|
||||
V: IntoDeserializer<'de, E>,
|
||||
E: de::Error,
|
||||
{
|
||||
type Deserializer = MapDeserializer<'de, <BTreeMap<K, V> as IntoIterator>::IntoIter, E>;
|
||||
|
||||
@ -919,9 +970,10 @@ impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V>
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de, K, V, E> IntoDeserializer<'de, E> for HashMap<K, V>
|
||||
where K: IntoDeserializer<'de, E> + Eq + Hash,
|
||||
V: IntoDeserializer<'de, E>,
|
||||
E: de::Error
|
||||
where
|
||||
K: IntoDeserializer<'de, E> + Eq + Hash,
|
||||
V: IntoDeserializer<'de, E>,
|
||||
E: de::Error,
|
||||
{
|
||||
type Deserializer = MapDeserializer<'de, <HashMap<K, V> as IntoIterator>::IntoIter, E>;
|
||||
|
||||
@ -941,19 +993,19 @@ pub struct MapVisitorDeserializer<V_> {
|
||||
impl<V_> MapVisitorDeserializer<V_> {
|
||||
/// Construct a new `MapVisitorDeserializer<V_, E>`.
|
||||
pub fn new(visitor: V_) -> Self {
|
||||
MapVisitorDeserializer {
|
||||
visitor: visitor,
|
||||
}
|
||||
MapVisitorDeserializer { visitor: visitor }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, V_> de::Deserializer<'de> for MapVisitorDeserializer<V_>
|
||||
where V_: de::MapVisitor<'de>
|
||||
where
|
||||
V_: de::MapVisitor<'de>,
|
||||
{
|
||||
type Error = V_::Error;
|
||||
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_map(self.visitor)
|
||||
}
|
||||
@ -982,7 +1034,8 @@ mod private {
|
||||
}
|
||||
|
||||
impl<'de, E> de::VariantVisitor<'de> for UnitOnly<E>
|
||||
where E: de::Error
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
@ -991,24 +1044,28 @@ mod private {
|
||||
}
|
||||
|
||||
fn visit_newtype_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
|
||||
where T: de::DeserializeSeed<'de>
|
||||
where
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"newtype variant"))
|
||||
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"newtype variant"),)
|
||||
}
|
||||
|
||||
fn visit_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"tuple variant"))
|
||||
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"tuple variant"),)
|
||||
}
|
||||
|
||||
fn visit_struct<V>(self,
|
||||
_fields: &'static [&'static str],
|
||||
_visitor: V)
|
||||
-> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
fn visit_struct<V>(
|
||||
self,
|
||||
_fields: &'static [&'static str],
|
||||
_visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"struct variant"))
|
||||
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"struct variant"),)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,7 +135,7 @@ mod lib {
|
||||
pub use std::string::String;
|
||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||
pub use collections::string::{String, ToString};
|
||||
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::vec::Vec;
|
||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -12,23 +12,27 @@ pub fn constrain<T: ?Sized>(t: &T) -> &T {
|
||||
}
|
||||
|
||||
/// Not public API.
|
||||
pub fn serialize_tagged_newtype<S, T>(serializer: S,
|
||||
type_ident: &'static str,
|
||||
variant_ident: &'static str,
|
||||
tag: &'static str,
|
||||
variant_name: &'static str,
|
||||
value: &T)
|
||||
-> Result<S::Ok, S::Error>
|
||||
where S: Serializer,
|
||||
T: Serialize
|
||||
pub fn serialize_tagged_newtype<S, T>(
|
||||
serializer: S,
|
||||
type_ident: &'static str,
|
||||
variant_ident: &'static str,
|
||||
tag: &'static str,
|
||||
variant_name: &'static str,
|
||||
value: &T,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(TaggedSerializer {
|
||||
type_ident: type_ident,
|
||||
variant_ident: variant_ident,
|
||||
tag: tag,
|
||||
variant_name: variant_name,
|
||||
delegate: serializer,
|
||||
})
|
||||
value.serialize(
|
||||
TaggedSerializer {
|
||||
type_ident: type_ident,
|
||||
variant_ident: variant_ident,
|
||||
tag: tag,
|
||||
variant_name: variant_name,
|
||||
delegate: serializer,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
struct TaggedSerializer<S> {
|
||||
@ -78,19 +82,23 @@ impl Display for Unsupported {
|
||||
}
|
||||
|
||||
impl<S> TaggedSerializer<S>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
fn bad_type(self, what: Unsupported) -> S::Error {
|
||||
ser::Error::custom(format_args!(
|
||||
ser::Error::custom(
|
||||
format_args!(
|
||||
"cannot serialize tagged newtype variant {}::{} containing {}",
|
||||
self.type_ident,
|
||||
self.variant_ident,
|
||||
what))
|
||||
what),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Serializer for TaggedSerializer<S>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
@ -172,7 +180,8 @@ impl<S> Serializer for TaggedSerializer<S>
|
||||
}
|
||||
|
||||
fn serialize_some<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
Err(self.bad_type(Unsupported::Optional))
|
||||
}
|
||||
@ -185,33 +194,38 @@ impl<S> Serializer for TaggedSerializer<S>
|
||||
Err(self.bad_type(Unsupported::UnitStruct))
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
inner_variant: &'static str)
|
||||
-> Result<Self::Ok, Self::Error> {
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
inner_variant: &'static str,
|
||||
) -> Result<Self::Ok, Self::Error> {
|
||||
let mut map = try!(self.delegate.serialize_map(Some(2)));
|
||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
||||
try!(map.serialize_entry(inner_variant, &()));
|
||||
map.end()
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T: ?Sized>(self,
|
||||
_: &'static str,
|
||||
value: &T)
|
||||
-> Result<Self::Ok, Self::Error>
|
||||
where T: Serialize
|
||||
fn serialize_newtype_struct<T: ?Sized>(
|
||||
self,
|
||||
_: &'static str,
|
||||
value: &T,
|
||||
) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized>(self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
inner_variant: &'static str,
|
||||
inner_value: &T)
|
||||
-> Result<Self::Ok, Self::Error>
|
||||
where T: Serialize
|
||||
fn serialize_newtype_variant<T: ?Sized>(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
inner_variant: &'static str,
|
||||
inner_value: &T,
|
||||
) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let mut map = try!(self.delegate.serialize_map(Some(2)));
|
||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
||||
@ -231,36 +245,39 @@ impl<S> Serializer for TaggedSerializer<S>
|
||||
Err(self.bad_type(Unsupported::Tuple))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(self,
|
||||
_: &'static str,
|
||||
_: usize)
|
||||
-> Result<Self::SerializeTupleStruct, Self::Error> {
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
||||
Err(self.bad_type(Unsupported::TupleStruct))
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
fn serialize_tuple_variant(self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
_: &'static str,
|
||||
_: usize)
|
||||
-> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||
// Lack of push-based serialization means we need to buffer the content
|
||||
// of the tuple variant, so it requires std.
|
||||
Err(self.bad_type(Unsupported::Enum))
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
fn serialize_tuple_variant(self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
inner_variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
inner_variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||
let mut map = try!(self.delegate.serialize_map(Some(2)));
|
||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
||||
try!(map.serialize_key(inner_variant));
|
||||
Ok(SerializeTupleVariantAsMapValue::new(map, inner_variant, len))
|
||||
Ok(SerializeTupleVariantAsMapValue::new(map, inner_variant, len),)
|
||||
}
|
||||
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
||||
@ -269,43 +286,47 @@ impl<S> Serializer for TaggedSerializer<S>
|
||||
Ok(map)
|
||||
}
|
||||
|
||||
fn serialize_struct(self,
|
||||
name: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeStruct, Self::Error> {
|
||||
fn serialize_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStruct, Self::Error> {
|
||||
let mut state = try!(self.delegate.serialize_struct(name, len + 1));
|
||||
try!(state.serialize_field(self.tag, self.variant_name));
|
||||
Ok(state)
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
fn serialize_struct_variant(self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
_: &'static str,
|
||||
_: usize)
|
||||
-> Result<Self::SerializeStructVariant, Self::Error> {
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
||||
// Lack of push-based serialization means we need to buffer the content
|
||||
// of the struct variant, so it requires std.
|
||||
Err(self.bad_type(Unsupported::Enum))
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
fn serialize_struct_variant(self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
inner_variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeStructVariant, Self::Error> {
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
inner_variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
||||
let mut map = try!(self.delegate.serialize_map(Some(2)));
|
||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
||||
try!(map.serialize_key(inner_variant));
|
||||
Ok(SerializeStructVariantAsMapValue::new(map, inner_variant, len))
|
||||
Ok(SerializeStructVariantAsMapValue::new(map, inner_variant, len),)
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
fn collect_str<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
|
||||
where T: Display
|
||||
where
|
||||
T: Display,
|
||||
{
|
||||
Err(self.bad_type(Unsupported::String))
|
||||
}
|
||||
@ -358,7 +379,8 @@ mod content {
|
||||
}
|
||||
|
||||
impl<M> ser::SerializeTupleVariant for SerializeTupleVariantAsMapValue<M>
|
||||
where M: ser::SerializeMap
|
||||
where
|
||||
M: ser::SerializeMap,
|
||||
{
|
||||
type Ok = M::Ok;
|
||||
type Error = M::Error;
|
||||
@ -392,15 +414,17 @@ mod content {
|
||||
}
|
||||
|
||||
impl<M> ser::SerializeStructVariant for SerializeStructVariantAsMapValue<M>
|
||||
where M: ser::SerializeMap
|
||||
where
|
||||
M: ser::SerializeMap,
|
||||
{
|
||||
type Ok = M::Ok;
|
||||
type Error = M::Error;
|
||||
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
||||
key: &'static str,
|
||||
value: &T)
|
||||
-> Result<(), M::Error> {
|
||||
fn serialize_field<T: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), M::Error> {
|
||||
let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
|
||||
self.fields.push((key, value));
|
||||
Ok(())
|
||||
@ -454,7 +478,8 @@ mod content {
|
||||
|
||||
impl Serialize for Content {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match *self {
|
||||
Content::Bool(b) => serializer.serialize_bool(b),
|
||||
@ -552,7 +577,8 @@ mod content {
|
||||
}
|
||||
|
||||
impl<E> Serializer for ContentSerializer<E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Content;
|
||||
type Error = E;
|
||||
@ -637,111 +663,141 @@ mod content {
|
||||
Ok(Content::UnitStruct(name))
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str)
|
||||
-> Result<Content, E> {
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
) -> Result<Content, E> {
|
||||
Ok(Content::UnitVariant(name, variant_index, variant))
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T: ?Sized + Serialize>(self,
|
||||
name: &'static str,
|
||||
value: &T)
|
||||
-> Result<Content, E> {
|
||||
Ok(Content::NewtypeStruct(name, Box::new(try!(value.serialize(self)))))
|
||||
fn serialize_newtype_struct<T: ?Sized + Serialize>(
|
||||
self,
|
||||
name: &'static str,
|
||||
value: &T,
|
||||
) -> Result<Content, E> {
|
||||
Ok(Content::NewtypeStruct(name, Box::new(try!(value.serialize(self)))),)
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized + Serialize>(self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T)
|
||||
-> Result<Content, E> {
|
||||
Ok(Content::NewtypeVariant(name,
|
||||
variant_index,
|
||||
variant,
|
||||
Box::new(try!(value.serialize(self)))))
|
||||
fn serialize_newtype_variant<T: ?Sized + Serialize>(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T,
|
||||
) -> Result<Content, E> {
|
||||
Ok(
|
||||
Content::NewtypeVariant(
|
||||
name,
|
||||
variant_index,
|
||||
variant,
|
||||
Box::new(try!(value.serialize(self))),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, E> {
|
||||
Ok(SerializeSeq {
|
||||
fixed_size: false,
|
||||
elements: Vec::with_capacity(len.unwrap_or(0)),
|
||||
error: PhantomData,
|
||||
})
|
||||
Ok(
|
||||
SerializeSeq {
|
||||
fixed_size: false,
|
||||
elements: Vec::with_capacity(len.unwrap_or(0)),
|
||||
error: PhantomData,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn serialize_seq_fixed_size(self, size: usize) -> Result<Self::SerializeSeq, E> {
|
||||
Ok(SerializeSeq {
|
||||
fixed_size: true,
|
||||
elements: Vec::with_capacity(size),
|
||||
error: PhantomData,
|
||||
})
|
||||
Ok(
|
||||
SerializeSeq {
|
||||
fixed_size: true,
|
||||
elements: Vec::with_capacity(size),
|
||||
error: PhantomData,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, E> {
|
||||
Ok(SerializeTuple {
|
||||
elements: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
})
|
||||
Ok(
|
||||
SerializeTuple {
|
||||
elements: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(self,
|
||||
name: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeTupleStruct, E> {
|
||||
Ok(SerializeTupleStruct {
|
||||
name: name,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
})
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleStruct, E> {
|
||||
Ok(
|
||||
SerializeTupleStruct {
|
||||
name: name,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeTupleVariant, E> {
|
||||
Ok(SerializeTupleVariant {
|
||||
name: name,
|
||||
variant_index: variant_index,
|
||||
variant: variant,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
})
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleVariant, E> {
|
||||
Ok(
|
||||
SerializeTupleVariant {
|
||||
name: name,
|
||||
variant_index: variant_index,
|
||||
variant: variant,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, E> {
|
||||
Ok(SerializeMap {
|
||||
entries: Vec::with_capacity(len.unwrap_or(0)),
|
||||
key: None,
|
||||
error: PhantomData,
|
||||
})
|
||||
Ok(
|
||||
SerializeMap {
|
||||
entries: Vec::with_capacity(len.unwrap_or(0)),
|
||||
key: None,
|
||||
error: PhantomData,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct, E> {
|
||||
Ok(SerializeStruct {
|
||||
name: name,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
})
|
||||
fn serialize_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStruct, E> {
|
||||
Ok(
|
||||
SerializeStruct {
|
||||
name: name,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeStructVariant, E> {
|
||||
Ok(SerializeStructVariant {
|
||||
name: name,
|
||||
variant_index: variant_index,
|
||||
variant: variant,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
})
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStructVariant, E> {
|
||||
Ok(
|
||||
SerializeStructVariant {
|
||||
name: name,
|
||||
variant_index: variant_index,
|
||||
variant: variant,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -752,7 +808,8 @@ mod content {
|
||||
}
|
||||
|
||||
impl<E> ser::SerializeSeq for SerializeSeq<E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Content;
|
||||
type Error = E;
|
||||
@ -764,11 +821,13 @@ mod content {
|
||||
}
|
||||
|
||||
fn end(self) -> Result<Content, E> {
|
||||
Ok(if self.fixed_size {
|
||||
Content::SeqFixedSize(self.elements)
|
||||
} else {
|
||||
Content::Seq(self.elements)
|
||||
})
|
||||
Ok(
|
||||
if self.fixed_size {
|
||||
Content::SeqFixedSize(self.elements)
|
||||
} else {
|
||||
Content::Seq(self.elements)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -778,7 +837,8 @@ mod content {
|
||||
}
|
||||
|
||||
impl<E> ser::SerializeTuple for SerializeTuple<E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Content;
|
||||
type Error = E;
|
||||
@ -801,7 +861,8 @@ mod content {
|
||||
}
|
||||
|
||||
impl<E> ser::SerializeTupleStruct for SerializeTupleStruct<E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Content;
|
||||
type Error = E;
|
||||
@ -826,7 +887,8 @@ mod content {
|
||||
}
|
||||
|
||||
impl<E> ser::SerializeTupleVariant for SerializeTupleVariant<E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Content;
|
||||
type Error = E;
|
||||
@ -838,7 +900,7 @@ mod content {
|
||||
}
|
||||
|
||||
fn end(self) -> Result<Content, E> {
|
||||
Ok(Content::TupleVariant(self.name, self.variant_index, self.variant, self.fields))
|
||||
Ok(Content::TupleVariant(self.name, self.variant_index, self.variant, self.fields),)
|
||||
}
|
||||
}
|
||||
|
||||
@ -849,7 +911,8 @@ mod content {
|
||||
}
|
||||
|
||||
impl<E> ser::SerializeMap for SerializeMap<E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Content;
|
||||
type Error = E;
|
||||
@ -861,7 +924,9 @@ mod content {
|
||||
}
|
||||
|
||||
fn serialize_value<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), E> {
|
||||
let key = self.key.take().expect("serialize_value called before serialize_key");
|
||||
let key = self.key
|
||||
.take()
|
||||
.expect("serialize_value called before serialize_key");
|
||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
||||
self.entries.push((key, value));
|
||||
Ok(())
|
||||
@ -871,10 +936,11 @@ mod content {
|
||||
Ok(Content::Map(self.entries))
|
||||
}
|
||||
|
||||
fn serialize_entry<K: ?Sized + Serialize, V: ?Sized + Serialize>(&mut self,
|
||||
key: &K,
|
||||
value: &V)
|
||||
-> Result<(), E> {
|
||||
fn serialize_entry<K: ?Sized + Serialize, V: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
key: &K,
|
||||
value: &V,
|
||||
) -> Result<(), E> {
|
||||
let key = try!(key.serialize(ContentSerializer::<E>::new()));
|
||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
||||
self.entries.push((key, value));
|
||||
@ -889,15 +955,17 @@ mod content {
|
||||
}
|
||||
|
||||
impl<E> ser::SerializeStruct for SerializeStruct<E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Content;
|
||||
type Error = E;
|
||||
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
||||
key: &'static str,
|
||||
value: &T)
|
||||
-> Result<(), E> {
|
||||
fn serialize_field<T: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), E> {
|
||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
||||
self.fields.push((key, value));
|
||||
Ok(())
|
||||
@ -917,22 +985,24 @@ mod content {
|
||||
}
|
||||
|
||||
impl<E> ser::SerializeStructVariant for SerializeStructVariant<E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Content;
|
||||
type Error = E;
|
||||
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
||||
key: &'static str,
|
||||
value: &T)
|
||||
-> Result<(), E> {
|
||||
fn serialize_field<T: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), E> {
|
||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
||||
self.fields.push((key, value));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn end(self) -> Result<Content, E> {
|
||||
Ok(Content::StructVariant(self.name, self.variant_index, self.variant, self.fields))
|
||||
Ok(Content::StructVariant(self.name, self.variant_index, self.variant, self.fields),)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,7 +40,8 @@ impl_visit!(char, serialize_char);
|
||||
impl Serialize for str {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self)
|
||||
}
|
||||
@ -50,7 +51,8 @@ impl Serialize for str {
|
||||
impl Serialize for String {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
(&self[..]).serialize(serializer)
|
||||
}
|
||||
@ -62,7 +64,8 @@ impl Serialize for String {
|
||||
impl Serialize for CStr {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_bytes(self.to_bytes())
|
||||
}
|
||||
@ -72,7 +75,8 @@ impl Serialize for CStr {
|
||||
impl Serialize for CString {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_bytes(self.to_bytes())
|
||||
}
|
||||
@ -81,11 +85,13 @@ impl Serialize for CString {
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T> Serialize for Option<T>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match *self {
|
||||
Some(ref value) => serializer.serialize_some(value),
|
||||
@ -99,7 +105,8 @@ impl<T> Serialize for Option<T>
|
||||
impl<T> Serialize for PhantomData<T> {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_unit_struct("PhantomData")
|
||||
}
|
||||
@ -111,7 +118,8 @@ impl<T> Serialize for PhantomData<T> {
|
||||
impl<T> Serialize for [T; 0] {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_seq_fixed_size(0)).end()
|
||||
}
|
||||
@ -181,50 +189,57 @@ macro_rules! serialize_seq {
|
||||
}
|
||||
|
||||
impl<T> Serialize for [T]
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
serialize_seq!();
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<T> Serialize for BinaryHeap<T>
|
||||
where T: Serialize + Ord
|
||||
where
|
||||
T: Serialize + Ord,
|
||||
{
|
||||
serialize_seq!();
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<T> Serialize for BTreeSet<T>
|
||||
where T: Serialize + Ord
|
||||
where
|
||||
T: Serialize + Ord,
|
||||
{
|
||||
serialize_seq!();
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T, H> Serialize for HashSet<T, H>
|
||||
where T: Serialize + Eq + Hash,
|
||||
H: BuildHasher
|
||||
where
|
||||
T: Serialize + Eq + Hash,
|
||||
H: BuildHasher,
|
||||
{
|
||||
serialize_seq!();
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<T> Serialize for LinkedList<T>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
serialize_seq!();
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<T> Serialize for Vec<T>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
serialize_seq!();
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<T> Serialize for VecDeque<T>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
serialize_seq!();
|
||||
}
|
||||
@ -234,7 +249,8 @@ impl<T> Serialize for VecDeque<T>
|
||||
#[cfg(feature = "std")]
|
||||
impl<Idx: Serialize> Serialize for ops::Range<Idx> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
use super::SerializeStruct;
|
||||
let mut state = try!(serializer.serialize_struct("Range", 2));
|
||||
@ -249,7 +265,8 @@ impl<Idx: Serialize> Serialize for ops::Range<Idx> {
|
||||
impl Serialize for () {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_unit()
|
||||
}
|
||||
@ -468,17 +485,19 @@ macro_rules! serialize_map {
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<K, V> Serialize for BTreeMap<K, V>
|
||||
where K: Serialize + Ord,
|
||||
V: Serialize
|
||||
where
|
||||
K: Serialize + Ord,
|
||||
V: Serialize,
|
||||
{
|
||||
serialize_map!();
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<K, V, H> Serialize for HashMap<K, V, H>
|
||||
where K: Serialize + Eq + Hash,
|
||||
V: Serialize,
|
||||
H: BuildHasher
|
||||
where
|
||||
K: Serialize + Eq + Hash,
|
||||
V: Serialize,
|
||||
H: BuildHasher,
|
||||
{
|
||||
serialize_map!();
|
||||
}
|
||||
@ -486,22 +505,26 @@ impl<K, V, H> Serialize for HashMap<K, V, H>
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<'a, T: ?Sized> Serialize for &'a T
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
(**self).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized> Serialize for &'a mut T
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
(**self).serialize(serializer)
|
||||
}
|
||||
@ -509,11 +532,13 @@ impl<'a, T: ?Sized> Serialize for &'a mut T
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<T: ?Sized> Serialize for Box<T>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
(**self).serialize(serializer)
|
||||
}
|
||||
@ -521,11 +546,13 @@ impl<T: ?Sized> Serialize for Box<T>
|
||||
|
||||
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
||||
impl<T> Serialize for Rc<T>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
(**self).serialize(serializer)
|
||||
}
|
||||
@ -533,11 +560,13 @@ impl<T> Serialize for Rc<T>
|
||||
|
||||
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
||||
impl<T> Serialize for Arc<T>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
(**self).serialize(serializer)
|
||||
}
|
||||
@ -545,11 +574,13 @@ impl<T> Serialize for Arc<T>
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
impl<'a, T: ?Sized> Serialize for Cow<'a, T>
|
||||
where T: Serialize + ToOwned
|
||||
where
|
||||
T: Serialize + ToOwned,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
(**self).serialize(serializer)
|
||||
}
|
||||
@ -558,11 +589,13 @@ impl<'a, T: ?Sized> Serialize for Cow<'a, T>
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T, E> Serialize for Result<T, E>
|
||||
where T: Serialize,
|
||||
E: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
E: Serialize,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match *self {
|
||||
Result::Ok(ref value) => serializer.serialize_newtype_variant("Result", 0, "Ok", value),
|
||||
@ -578,7 +611,8 @@ impl<T, E> Serialize for Result<T, E>
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for Duration {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
use super::SerializeStruct;
|
||||
let mut state = try!(serializer.serialize_struct("Duration", 2));
|
||||
@ -623,7 +657,8 @@ macro_rules! serialize_display_bounded_length {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for net::IpAddr {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match *self {
|
||||
net::IpAddr::V4(ref a) => a.serialize(serializer),
|
||||
@ -635,7 +670,8 @@ impl Serialize for net::IpAddr {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for net::Ipv4Addr {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
/// "101.102.103.104".len()
|
||||
const MAX_LEN: usize = 15;
|
||||
@ -646,7 +682,8 @@ impl Serialize for net::Ipv4Addr {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for net::Ipv6Addr {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
/// "1000:1002:1003:1004:1005:1006:1007:1008".len()
|
||||
const MAX_LEN: usize = 39;
|
||||
@ -659,7 +696,8 @@ impl Serialize for net::Ipv6Addr {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for net::SocketAddr {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match *self {
|
||||
net::SocketAddr::V4(ref addr) => addr.serialize(serializer),
|
||||
@ -671,7 +709,8 @@ impl Serialize for net::SocketAddr {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for net::SocketAddrV4 {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
/// "101.102.103.104:65000".len()
|
||||
const MAX_LEN: usize = 21;
|
||||
@ -682,7 +721,8 @@ impl Serialize for net::SocketAddrV4 {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for net::SocketAddrV6 {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
/// "[1000:1002:1003:1004:1005:1006:1007:1008]:65000".len()
|
||||
const MAX_LEN: usize = 47;
|
||||
@ -695,7 +735,8 @@ impl Serialize for net::SocketAddrV6 {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for path::Path {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match self.to_str() {
|
||||
Some(s) => s.serialize(serializer),
|
||||
@ -707,7 +748,8 @@ impl Serialize for path::Path {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for path::PathBuf {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
self.as_path().serialize(serializer)
|
||||
}
|
||||
@ -717,24 +759,20 @@ impl Serialize for path::PathBuf {
|
||||
impl Serialize for OsStr {
|
||||
#[cfg(unix)]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
serializer.serialize_newtype_variant("OsString",
|
||||
0,
|
||||
"Unix",
|
||||
self.as_bytes())
|
||||
serializer.serialize_newtype_variant("OsString", 0, "Unix", self.as_bytes())
|
||||
}
|
||||
#[cfg(windows)]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
let val = self.encode_wide().collect::<Vec<_>>();
|
||||
serializer.serialize_newtype_variant("OsString",
|
||||
1,
|
||||
"Windows",
|
||||
&val)
|
||||
serializer.serialize_newtype_variant("OsString", 1, "Windows", &val)
|
||||
}
|
||||
}
|
||||
|
||||
@ -742,7 +780,8 @@ impl Serialize for OsStr {
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for OsString {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
self.as_os_str().serialize(serializer)
|
||||
}
|
||||
@ -750,10 +789,12 @@ impl Serialize for OsString {
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl<T> Serialize for NonZero<T>
|
||||
where T: Serialize + Zeroable
|
||||
where
|
||||
T: Serialize + Zeroable,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
(**self).serialize(serializer)
|
||||
}
|
||||
|
@ -60,7 +60,8 @@ pub struct Impossible<Ok, E> {
|
||||
enum Void {}
|
||||
|
||||
impl<Ok, E> SerializeSeq for Impossible<Ok, E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Ok;
|
||||
type Error = E;
|
||||
@ -75,7 +76,8 @@ impl<Ok, E> SerializeSeq for Impossible<Ok, E>
|
||||
}
|
||||
|
||||
impl<Ok, E> SerializeTuple for Impossible<Ok, E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Ok;
|
||||
type Error = E;
|
||||
@ -90,7 +92,8 @@ impl<Ok, E> SerializeTuple for Impossible<Ok, E>
|
||||
}
|
||||
|
||||
impl<Ok, E> SerializeTupleStruct for Impossible<Ok, E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Ok;
|
||||
type Error = E;
|
||||
@ -105,7 +108,8 @@ impl<Ok, E> SerializeTupleStruct for Impossible<Ok, E>
|
||||
}
|
||||
|
||||
impl<Ok, E> SerializeTupleVariant for Impossible<Ok, E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Ok;
|
||||
type Error = E;
|
||||
@ -120,7 +124,8 @@ impl<Ok, E> SerializeTupleVariant for Impossible<Ok, E>
|
||||
}
|
||||
|
||||
impl<Ok, E> SerializeMap for Impossible<Ok, E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Ok;
|
||||
type Error = E;
|
||||
@ -139,15 +144,17 @@ impl<Ok, E> SerializeMap for Impossible<Ok, E>
|
||||
}
|
||||
|
||||
impl<Ok, E> SerializeStruct for Impossible<Ok, E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Ok;
|
||||
type Error = E;
|
||||
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
||||
_key: &'static str,
|
||||
_value: &T)
|
||||
-> Result<(), E> {
|
||||
fn serialize_field<T: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
_key: &'static str,
|
||||
_value: &T,
|
||||
) -> Result<(), E> {
|
||||
match self.void {}
|
||||
}
|
||||
|
||||
@ -157,15 +164,17 @@ impl<Ok, E> SerializeStruct for Impossible<Ok, E>
|
||||
}
|
||||
|
||||
impl<Ok, E> SerializeStructVariant for Impossible<Ok, E>
|
||||
where E: ser::Error
|
||||
where
|
||||
E: ser::Error,
|
||||
{
|
||||
type Ok = Ok;
|
||||
type Error = E;
|
||||
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
||||
_key: &'static str,
|
||||
_value: &T)
|
||||
-> Result<(), E> {
|
||||
fn serialize_field<T: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
_key: &'static str,
|
||||
_value: &T,
|
||||
) -> Result<(), E> {
|
||||
match self.void {}
|
||||
}
|
||||
|
||||
|
@ -182,7 +182,9 @@ pub trait Serialize {
|
||||
/// for more information about how to implement this method.
|
||||
///
|
||||
/// [impl-serialize]: https://serde.rs/impl-serialize.html
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer;
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -437,11 +439,12 @@ pub trait Serializer: Sized {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
fn serialize_unit_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str)
|
||||
-> Result<Self::Ok, Self::Error>;
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
) -> Result<Self::Ok, Self::Error>;
|
||||
|
||||
/// Serialize a newtype struct like `struct Millimeters(u8)`.
|
||||
///
|
||||
@ -462,10 +465,11 @@ pub trait Serializer: Sized {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
fn serialize_newtype_struct<T: ?Sized + Serialize>(self,
|
||||
name: &'static str,
|
||||
value: &T)
|
||||
-> Result<Self::Ok, Self::Error>;
|
||||
fn serialize_newtype_struct<T: ?Sized + Serialize>(
|
||||
self,
|
||||
name: &'static str,
|
||||
value: &T,
|
||||
) -> Result<Self::Ok, Self::Error>;
|
||||
|
||||
/// Serialize a newtype variant like `E::N` in `enum E { N(u8) }`.
|
||||
///
|
||||
@ -492,12 +496,13 @@ pub trait Serializer: Sized {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
fn serialize_newtype_variant<T: ?Sized + Serialize>(self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T)
|
||||
-> Result<Self::Ok, Self::Error>;
|
||||
fn serialize_newtype_variant<T: ?Sized + Serialize>(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T,
|
||||
) -> Result<Self::Ok, Self::Error>;
|
||||
|
||||
/// Begin to serialize a dynamically sized sequence. This call must be
|
||||
/// followed by zero or more calls to `serialize_element`, then a call to
|
||||
@ -630,10 +635,11 @@ pub trait Serializer: Sized {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
fn serialize_tuple_struct(self,
|
||||
name: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeTupleStruct, Self::Error>;
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleStruct, Self::Error>;
|
||||
|
||||
/// Begin to serialize a tuple variant like `E::T` in `enum E { T(u8, u8)
|
||||
/// }`. This call must be followed by zero or more calls to
|
||||
@ -674,12 +680,13 @@ pub trait Serializer: Sized {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
fn serialize_tuple_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeTupleVariant, Self::Error>;
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleVariant, Self::Error>;
|
||||
|
||||
/// Begin to serialize a map. This call must be followed by zero or more
|
||||
/// calls to `serialize_key` and `serialize_value`, then a call to `end`.
|
||||
@ -757,10 +764,11 @@ pub trait Serializer: Sized {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
fn serialize_struct(self,
|
||||
name: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeStruct, Self::Error>;
|
||||
fn serialize_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStruct, Self::Error>;
|
||||
|
||||
/// Begin to serialize a struct variant like `E::S` in `enum E { S { r: u8,
|
||||
/// g: u8, b: u8 } }`. This call must be followed by zero or more calls to
|
||||
@ -794,12 +802,13 @@ pub trait Serializer: Sized {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
fn serialize_struct_variant(self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self::SerializeStructVariant, Self::Error>;
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStructVariant, Self::Error>;
|
||||
|
||||
/// Collect an iterator as a sequence.
|
||||
///
|
||||
@ -807,8 +816,9 @@ pub trait Serializer: Sized {
|
||||
/// using `Self::SerializeSeq`. Implementors should not need to override
|
||||
/// this method.
|
||||
fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>
|
||||
where I: IntoIterator,
|
||||
<I as IntoIterator>::Item: Serialize
|
||||
where
|
||||
I: IntoIterator,
|
||||
<I as IntoIterator>::Item: Serialize,
|
||||
{
|
||||
let iter = iter.into_iter();
|
||||
let mut serializer = try!(self.serialize_seq(iter.len_hint()));
|
||||
@ -824,9 +834,10 @@ pub trait Serializer: Sized {
|
||||
/// using `Self::SerializeMap`. Implementors should not need to override
|
||||
/// this method.
|
||||
fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
|
||||
where K: Serialize,
|
||||
V: Serialize,
|
||||
I: IntoIterator<Item = (K, V)>
|
||||
where
|
||||
K: Serialize,
|
||||
V: Serialize,
|
||||
I: IntoIterator<Item = (K, V)>,
|
||||
{
|
||||
let iter = iter.into_iter();
|
||||
let mut serializer = try!(self.serialize_map(iter.len_hint()));
|
||||
@ -864,7 +875,8 @@ pub trait Serializer: Sized {
|
||||
/// ```
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||
where T: Display
|
||||
where
|
||||
T: Display,
|
||||
{
|
||||
use lib::fmt::Write;
|
||||
let mut string = String::new();
|
||||
@ -900,7 +912,8 @@ pub trait Serializer: Sized {
|
||||
/// ```
|
||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||
where T: Display;
|
||||
where
|
||||
T: Display;
|
||||
}
|
||||
|
||||
/// Returned from `Serializer::serialize_seq` and
|
||||
@ -1174,10 +1187,11 @@ pub trait SerializeMap {
|
||||
/// `serialize_value`. This is appropriate for serializers that do not care
|
||||
/// about performance or are not able to optimize `serialize_entry` any
|
||||
/// better than this.
|
||||
fn serialize_entry<K: ?Sized + Serialize, V: ?Sized + Serialize>(&mut self,
|
||||
key: &K,
|
||||
value: &V)
|
||||
-> Result<(), Self::Error> {
|
||||
fn serialize_entry<K: ?Sized + Serialize, V: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
key: &K,
|
||||
value: &V,
|
||||
) -> Result<(), Self::Error> {
|
||||
try!(self.serialize_key(key));
|
||||
self.serialize_value(value)
|
||||
}
|
||||
@ -1218,10 +1232,11 @@ pub trait SerializeStruct {
|
||||
type Error: Error;
|
||||
|
||||
/// Serialize a struct field.
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
||||
key: &'static str,
|
||||
value: &T)
|
||||
-> Result<(), Self::Error>;
|
||||
fn serialize_field<T: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error>;
|
||||
|
||||
/// Finish serializing a struct.
|
||||
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||
@ -1261,10 +1276,11 @@ pub trait SerializeStructVariant {
|
||||
type Error: Error;
|
||||
|
||||
/// Serialize a struct variant field.
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
||||
key: &'static str,
|
||||
value: &T)
|
||||
-> Result<(), Self::Error>;
|
||||
fn serialize_field<T: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error>;
|
||||
|
||||
/// Finish serializing a struct variant.
|
||||
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||
|
@ -16,27 +16,41 @@ macro_rules! path {
|
||||
// allowed here".
|
||||
pub fn without_defaults(generics: &syn::Generics) -> syn::Generics {
|
||||
syn::Generics {
|
||||
ty_params: generics.ty_params
|
||||
ty_params: generics
|
||||
.ty_params
|
||||
.iter()
|
||||
.map(|ty_param| syn::TyParam { default: None, ..ty_param.clone() })
|
||||
.map(
|
||||
|ty_param| {
|
||||
syn::TyParam {
|
||||
default: None,
|
||||
..ty_param.clone()
|
||||
}
|
||||
},
|
||||
)
|
||||
.collect(),
|
||||
..generics.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_where_predicates(generics: &syn::Generics,
|
||||
predicates: &[syn::WherePredicate])
|
||||
-> syn::Generics {
|
||||
pub fn with_where_predicates(
|
||||
generics: &syn::Generics,
|
||||
predicates: &[syn::WherePredicate],
|
||||
) -> syn::Generics {
|
||||
let mut generics = generics.clone();
|
||||
generics.where_clause.predicates.extend_from_slice(predicates);
|
||||
generics
|
||||
.where_clause
|
||||
.predicates
|
||||
.extend_from_slice(predicates);
|
||||
generics
|
||||
}
|
||||
|
||||
pub fn with_where_predicates_from_fields<F>(item: &Item,
|
||||
generics: &syn::Generics,
|
||||
from_field: F)
|
||||
-> syn::Generics
|
||||
where F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>
|
||||
pub fn with_where_predicates_from_fields<F>(
|
||||
item: &Item,
|
||||
generics: &syn::Generics,
|
||||
from_field: F,
|
||||
) -> syn::Generics
|
||||
where
|
||||
F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
|
||||
{
|
||||
let predicates = item.body
|
||||
.all_fields()
|
||||
@ -59,12 +73,14 @@ pub fn with_where_predicates_from_fields<F>(item: &Item,
|
||||
// #[serde(skip_serializing)]
|
||||
// c: C,
|
||||
// }
|
||||
pub fn with_bound<F>(item: &Item,
|
||||
generics: &syn::Generics,
|
||||
filter: F,
|
||||
bound: &syn::Path)
|
||||
-> syn::Generics
|
||||
where F: Fn(&attr::Field) -> bool
|
||||
pub fn with_bound<F>(
|
||||
item: &Item,
|
||||
generics: &syn::Generics,
|
||||
filter: F,
|
||||
bound: &syn::Path,
|
||||
) -> syn::Generics
|
||||
where
|
||||
F: Fn(&attr::Field) -> bool,
|
||||
{
|
||||
struct FindTyParams {
|
||||
// Set of all generic type parameters on the current struct (A, B, C in
|
||||
@ -94,7 +110,8 @@ pub fn with_bound<F>(item: &Item,
|
||||
}
|
||||
}
|
||||
|
||||
let all_ty_params: HashSet<_> = generics.ty_params
|
||||
let all_ty_params: HashSet<_> = generics
|
||||
.ty_params
|
||||
.iter()
|
||||
.map(|ty_param| ty_param.ident.clone())
|
||||
.collect();
|
||||
@ -112,58 +129,66 @@ pub fn with_bound<F>(item: &Item,
|
||||
visit::walk_ty(&mut visitor, ty);
|
||||
}
|
||||
|
||||
let new_predicates = generics.ty_params
|
||||
let new_predicates = generics
|
||||
.ty_params
|
||||
.iter()
|
||||
.map(|ty_param| ty_param.ident.clone())
|
||||
.filter(|id| visitor.relevant_ty_params.contains(id))
|
||||
.map(|id| {
|
||||
syn::WherePredicate::BoundPredicate(syn::WhereBoundPredicate {
|
||||
bound_lifetimes: Vec::new(),
|
||||
// the type parameter that is being bounded e.g. T
|
||||
bounded_ty: syn::Ty::Path(None, id.into()),
|
||||
// the bound e.g. Serialize
|
||||
bounds: vec![syn::TyParamBound::Trait(
|
||||
syn::PolyTraitRef {
|
||||
.map(
|
||||
|id| {
|
||||
syn::WherePredicate::BoundPredicate(
|
||||
syn::WhereBoundPredicate {
|
||||
bound_lifetimes: Vec::new(),
|
||||
trait_ref: bound.clone(),
|
||||
// the type parameter that is being bounded e.g. T
|
||||
bounded_ty: syn::Ty::Path(None, id.into()),
|
||||
// the bound e.g. Serialize
|
||||
bounds: vec![
|
||||
syn::TyParamBound::Trait(
|
||||
syn::PolyTraitRef {
|
||||
bound_lifetimes: Vec::new(),
|
||||
trait_ref: bound.clone(),
|
||||
},
|
||||
syn::TraitBoundModifier::None,
|
||||
),
|
||||
],
|
||||
},
|
||||
syn::TraitBoundModifier::None
|
||||
)],
|
||||
})
|
||||
});
|
||||
)
|
||||
},
|
||||
);
|
||||
|
||||
let mut generics = generics.clone();
|
||||
generics.where_clause.predicates.extend(new_predicates);
|
||||
generics
|
||||
}
|
||||
|
||||
pub fn with_self_bound(item: &Item,
|
||||
generics: &syn::Generics,
|
||||
bound: &syn::Path)
|
||||
-> syn::Generics
|
||||
{
|
||||
pub fn with_self_bound(item: &Item, generics: &syn::Generics, bound: &syn::Path) -> syn::Generics {
|
||||
let mut generics = generics.clone();
|
||||
generics.where_clause.predicates.push(
|
||||
syn::WherePredicate::BoundPredicate(syn::WhereBoundPredicate {
|
||||
bound_lifetimes: Vec::new(),
|
||||
// the type that is being bounded e.g. MyStruct<'a, T>
|
||||
bounded_ty: type_of_item(item),
|
||||
// the bound e.g. Default
|
||||
bounds: vec![syn::TyParamBound::Trait(
|
||||
syn::PolyTraitRef {
|
||||
generics
|
||||
.where_clause
|
||||
.predicates
|
||||
.push(
|
||||
syn::WherePredicate::BoundPredicate(
|
||||
syn::WhereBoundPredicate {
|
||||
bound_lifetimes: Vec::new(),
|
||||
trait_ref: bound.clone(),
|
||||
// the type that is being bounded e.g. MyStruct<'a, T>
|
||||
bounded_ty: type_of_item(item),
|
||||
// the bound e.g. Default
|
||||
bounds: vec![
|
||||
syn::TyParamBound::Trait(
|
||||
syn::PolyTraitRef {
|
||||
bound_lifetimes: Vec::new(),
|
||||
trait_ref: bound.clone(),
|
||||
},
|
||||
syn::TraitBoundModifier::None,
|
||||
),
|
||||
],
|
||||
},
|
||||
syn::TraitBoundModifier::None
|
||||
)],
|
||||
})
|
||||
);
|
||||
),
|
||||
);
|
||||
generics
|
||||
}
|
||||
|
||||
pub fn with_lifetime_bound(generics: &syn::Generics,
|
||||
lifetime: &str)
|
||||
-> syn::Generics {
|
||||
pub fn with_lifetime_bound(generics: &syn::Generics, lifetime: &str) -> syn::Generics {
|
||||
let mut generics = generics.clone();
|
||||
|
||||
for lifetime_def in &mut generics.lifetimes {
|
||||
@ -171,38 +196,49 @@ pub fn with_lifetime_bound(generics: &syn::Generics,
|
||||
}
|
||||
|
||||
for ty_param in &mut generics.ty_params {
|
||||
ty_param.bounds.push(syn::TyParamBound::Region(syn::Lifetime::new(lifetime)));
|
||||
ty_param
|
||||
.bounds
|
||||
.push(syn::TyParamBound::Region(syn::Lifetime::new(lifetime)));
|
||||
}
|
||||
|
||||
generics.lifetimes.push(syn::LifetimeDef {
|
||||
attrs: Vec::new(),
|
||||
lifetime: syn::Lifetime::new(lifetime),
|
||||
bounds: Vec::new(),
|
||||
});
|
||||
generics
|
||||
.lifetimes
|
||||
.push(
|
||||
syn::LifetimeDef {
|
||||
attrs: Vec::new(),
|
||||
lifetime: syn::Lifetime::new(lifetime),
|
||||
bounds: Vec::new(),
|
||||
},
|
||||
);
|
||||
|
||||
generics
|
||||
}
|
||||
|
||||
fn type_of_item(item: &Item) -> syn::Ty {
|
||||
syn::Ty::Path(None, syn::Path {
|
||||
global: false,
|
||||
segments: vec![
|
||||
syn::PathSegment {
|
||||
ident: item.ident.clone(),
|
||||
parameters: syn::PathParameters::AngleBracketed(syn::AngleBracketedParameterData {
|
||||
lifetimes: item.generics
|
||||
.lifetimes
|
||||
.iter()
|
||||
.map(|def| def.lifetime.clone())
|
||||
.collect(),
|
||||
types: item.generics
|
||||
syn::Ty::Path(
|
||||
None,
|
||||
syn::Path {
|
||||
global: false,
|
||||
segments: vec![
|
||||
syn::PathSegment {
|
||||
ident: item.ident.clone(),
|
||||
parameters: syn::PathParameters::AngleBracketed(
|
||||
syn::AngleBracketedParameterData {
|
||||
lifetimes: item.generics
|
||||
.lifetimes
|
||||
.iter()
|
||||
.map(|def| def.lifetime.clone())
|
||||
.collect(),
|
||||
types: item.generics
|
||||
.ty_params
|
||||
.iter()
|
||||
.map(|param| syn::Ty::Path(None, param.ident.clone().into()))
|
||||
.collect(),
|
||||
bindings: Vec::new(),
|
||||
}),
|
||||
}
|
||||
]
|
||||
})
|
||||
bindings: Vec::new(),
|
||||
},
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
}
|
||||
|
@ -44,13 +44,15 @@ pub fn expand_derive_deserialize(item: &syn::DeriveInput) -> Result<Tokens, Stri
|
||||
}
|
||||
};
|
||||
|
||||
Ok(quote! {
|
||||
Ok(
|
||||
quote! {
|
||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||
const #dummy_const: () = {
|
||||
extern crate serde as _serde;
|
||||
#impl_item
|
||||
};
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
struct Parameters {
|
||||
@ -115,8 +117,7 @@ impl Parameters {
|
||||
fn build_generics(item: &Item) -> syn::Generics {
|
||||
let generics = bound::without_defaults(item.generics);
|
||||
|
||||
let generics =
|
||||
bound::with_where_predicates_from_fields(item, &generics, attr::Field::de_bound);
|
||||
let generics = bound::with_where_predicates_from_fields(item, &generics, attr::Field::de_bound);
|
||||
|
||||
match item.attrs.de_bound() {
|
||||
Some(predicates) => bound::with_where_predicates(&generics, predicates),
|
||||
@ -125,19 +126,23 @@ fn build_generics(item: &Item) -> syn::Generics {
|
||||
attr::Default::Default => {
|
||||
bound::with_self_bound(item, &generics, &path!(_serde::export::Default))
|
||||
}
|
||||
attr::Default::None | attr::Default::Path(_) => generics,
|
||||
attr::Default::None |
|
||||
attr::Default::Path(_) => generics,
|
||||
};
|
||||
|
||||
let generics =
|
||||
bound::with_bound(item,
|
||||
&generics,
|
||||
needs_deserialize_bound,
|
||||
&path!(_serde::Deserialize<'de>));
|
||||
let generics = bound::with_bound(
|
||||
item,
|
||||
&generics,
|
||||
needs_deserialize_bound,
|
||||
&path!(_serde::Deserialize<'de>),
|
||||
);
|
||||
|
||||
bound::with_bound(item,
|
||||
&generics,
|
||||
requires_default,
|
||||
&path!(_serde::export::Default))
|
||||
bound::with_bound(
|
||||
item,
|
||||
&generics,
|
||||
requires_default,
|
||||
&path!(_serde::export::Default),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -175,29 +180,19 @@ fn deserialize_body(item: &Item, params: &Parameters) -> Fragment {
|
||||
deserialize_from(from_type)
|
||||
} else {
|
||||
match item.body {
|
||||
Body::Enum(ref variants) => {
|
||||
deserialize_item_enum(params, variants, &item.attrs)
|
||||
}
|
||||
Body::Enum(ref variants) => deserialize_item_enum(params, variants, &item.attrs),
|
||||
Body::Struct(Style::Struct, ref fields) => {
|
||||
if fields.iter().any(|field| field.ident.is_none()) {
|
||||
panic!("struct has unnamed fields");
|
||||
}
|
||||
deserialize_struct(None,
|
||||
params,
|
||||
fields,
|
||||
&item.attrs,
|
||||
None)
|
||||
deserialize_struct(None, params, fields, &item.attrs, None)
|
||||
}
|
||||
Body::Struct(Style::Tuple, ref fields) |
|
||||
Body::Struct(Style::Newtype, ref fields) => {
|
||||
if fields.iter().any(|field| field.ident.is_some()) {
|
||||
panic!("tuple struct has named fields");
|
||||
}
|
||||
deserialize_tuple(None,
|
||||
params,
|
||||
fields,
|
||||
&item.attrs,
|
||||
None)
|
||||
deserialize_tuple(None, params, fields, &item.attrs, None)
|
||||
}
|
||||
Body::Struct(Style::Unit, _) => deserialize_unit_struct(params, &item.attrs),
|
||||
}
|
||||
@ -240,14 +235,15 @@ fn deserialize_unit_struct(params: &Parameters, item_attrs: &attr::Item) -> Frag
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_tuple(variant_ident: Option<&syn::Ident>,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
deserializer: Option<Tokens>)
|
||||
-> Fragment {
|
||||
fn deserialize_tuple(
|
||||
variant_ident: Option<&syn::Ident>,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
deserializer: Option<Tokens>,
|
||||
) -> Fragment {
|
||||
let this = ¶ms.this;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params);
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params,);
|
||||
|
||||
// If there are getters (implying private fields), construct the local type
|
||||
// and use an `Into` conversion to get the remote type. If there are no
|
||||
@ -277,7 +273,7 @@ fn deserialize_tuple(variant_ident: Option<&syn::Ident>,
|
||||
None
|
||||
};
|
||||
|
||||
let visit_seq = Stmts(deserialize_seq(&type_path, params, fields, false, item_attrs));
|
||||
let visit_seq = Stmts(deserialize_seq(&type_path, params, fields, false, item_attrs),);
|
||||
|
||||
let visitor_expr = quote! {
|
||||
__Visitor {
|
||||
@ -297,7 +293,9 @@ fn deserialize_tuple(variant_ident: Option<&syn::Ident>,
|
||||
quote!(_serde::Deserializer::deserialize_tuple_struct(__deserializer, #type_name, #nfields, #visitor_expr))
|
||||
};
|
||||
|
||||
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
||||
let all_skipped = fields
|
||||
.iter()
|
||||
.all(|field| field.attrs.skip_deserializing());
|
||||
let visitor_var = if all_skipped {
|
||||
quote!(_)
|
||||
} else {
|
||||
@ -331,15 +329,17 @@ fn deserialize_tuple(variant_ident: Option<&syn::Ident>,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_seq(type_path: &Tokens,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
is_struct: bool,
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
fn deserialize_seq(
|
||||
type_path: &Tokens,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
is_struct: bool,
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
let vars = (0..fields.len()).map(field_i as fn(_) -> _);
|
||||
|
||||
let deserialized_count = fields.iter()
|
||||
let deserialized_count = fields
|
||||
.iter()
|
||||
.filter(|field| !field.attrs.skip_deserializing())
|
||||
.count();
|
||||
let expecting = format!("tuple of {} elements", deserialized_count);
|
||||
@ -406,10 +406,7 @@ fn deserialize_seq(type_path: &Tokens,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_newtype_struct(type_path: &Tokens,
|
||||
params: &Parameters,
|
||||
field: &Field)
|
||||
-> Tokens {
|
||||
fn deserialize_newtype_struct(type_path: &Tokens, params: &Parameters, field: &Field) -> Tokens {
|
||||
let value = match field.attrs.deserialize_with() {
|
||||
None => {
|
||||
let field_ty = &field.ty;
|
||||
@ -418,8 +415,7 @@ fn deserialize_newtype_struct(type_path: &Tokens,
|
||||
}
|
||||
}
|
||||
Some(path) => {
|
||||
let (wrapper, wrapper_ty) =
|
||||
wrap_deserialize_with(params, field.ty, path);
|
||||
let (wrapper, wrapper_ty) = wrap_deserialize_with(params, field.ty, path);
|
||||
quote!({
|
||||
#wrapper
|
||||
try!(<#wrapper_ty as _serde::Deserialize>::deserialize(__e)).value
|
||||
@ -445,17 +441,18 @@ fn deserialize_newtype_struct(type_path: &Tokens,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_struct(variant_ident: Option<&syn::Ident>,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
deserializer: Option<Tokens>)
|
||||
-> Fragment {
|
||||
fn deserialize_struct(
|
||||
variant_ident: Option<&syn::Ident>,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
deserializer: Option<Tokens>,
|
||||
) -> Fragment {
|
||||
let is_enum = variant_ident.is_some();
|
||||
let is_untagged = deserializer.is_some();
|
||||
|
||||
let this = ¶ms.this;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params);
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params,);
|
||||
|
||||
// If there are getters (implying private fields), construct the local type
|
||||
// and use an `Into` conversion to get the remote type. If there are no
|
||||
@ -476,7 +473,7 @@ fn deserialize_struct(variant_ident: Option<&syn::Ident>,
|
||||
None => format!("struct {}", params.type_name()),
|
||||
};
|
||||
|
||||
let visit_seq = Stmts(deserialize_seq(&type_path, params, fields, true, item_attrs));
|
||||
let visit_seq = Stmts(deserialize_seq(&type_path, params, fields, true, item_attrs),);
|
||||
|
||||
let (field_visitor, fields_stmt, visit_map) =
|
||||
deserialize_struct_visitor(type_path, params, fields, item_attrs);
|
||||
@ -505,7 +502,9 @@ fn deserialize_struct(variant_ident: Option<&syn::Ident>,
|
||||
}
|
||||
};
|
||||
|
||||
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
||||
let all_skipped = fields
|
||||
.iter()
|
||||
.all(|field| field.attrs.skip_deserializing());
|
||||
let visitor_var = if all_skipped {
|
||||
quote!(_)
|
||||
} else {
|
||||
@ -557,48 +556,41 @@ fn deserialize_struct(variant_ident: Option<&syn::Ident>,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_item_enum(params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
fn deserialize_item_enum(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
match *item_attrs.tag() {
|
||||
attr::EnumTag::External => {
|
||||
deserialize_externally_tagged_enum(params, variants, item_attrs)
|
||||
}
|
||||
attr::EnumTag::External => deserialize_externally_tagged_enum(params, variants, item_attrs),
|
||||
attr::EnumTag::Internal { ref tag } => {
|
||||
deserialize_internally_tagged_enum(params,
|
||||
variants,
|
||||
item_attrs,
|
||||
tag)
|
||||
}
|
||||
attr::EnumTag::Adjacent { ref tag, ref content } => {
|
||||
deserialize_adjacently_tagged_enum(params,
|
||||
variants,
|
||||
item_attrs,
|
||||
tag,
|
||||
content)
|
||||
}
|
||||
attr::EnumTag::None => {
|
||||
deserialize_untagged_enum(params, variants, item_attrs)
|
||||
deserialize_internally_tagged_enum(params, variants, item_attrs, tag)
|
||||
}
|
||||
attr::EnumTag::Adjacent {
|
||||
ref tag,
|
||||
ref content,
|
||||
} => deserialize_adjacently_tagged_enum(params, variants, item_attrs, tag, content),
|
||||
attr::EnumTag::None => deserialize_untagged_enum(params, variants, item_attrs),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_externally_tagged_enum(params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
fn deserialize_externally_tagged_enum(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
let this = ¶ms.this;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params);
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params,);
|
||||
|
||||
let type_name = item_attrs.name().deserialize_name();
|
||||
|
||||
let expecting = format!("enum {}", params.type_name());
|
||||
|
||||
let variant_names_idents: Vec<_> = variants.iter()
|
||||
let variant_names_idents: Vec<_> = variants
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||
.map(|(i, variant)| (variant.attrs.name().deserialize_name(), field_i(i)))
|
||||
.map(|(i, variant)| (variant.attrs.name().deserialize_name(), field_i(i)),)
|
||||
.collect();
|
||||
|
||||
let variants_stmt = {
|
||||
@ -608,25 +600,30 @@ fn deserialize_externally_tagged_enum(params: &Parameters,
|
||||
}
|
||||
};
|
||||
|
||||
let variant_visitor = Stmts(deserialize_field_visitor(variant_names_idents, item_attrs, true));
|
||||
let variant_visitor = Stmts(deserialize_field_visitor(variant_names_idents, item_attrs, true),);
|
||||
|
||||
// Match arms to extract a variant from a string
|
||||
let variant_arms = variants.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||
.map(|(i, variant)| {
|
||||
let variant_name = field_i(i);
|
||||
let variant_arms =
|
||||
variants
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||
.map(
|
||||
|(i, variant)| {
|
||||
let variant_name = field_i(i);
|
||||
|
||||
let block = Match(deserialize_externally_tagged_variant(params,
|
||||
variant,
|
||||
item_attrs));
|
||||
let block =
|
||||
Match(deserialize_externally_tagged_variant(params, variant, item_attrs),);
|
||||
|
||||
quote! {
|
||||
quote! {
|
||||
(__Field::#variant_name, __visitor) => #block
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
let all_skipped = variants.iter().all(|variant| variant.attrs.skip_deserializing());
|
||||
let all_skipped = variants
|
||||
.iter()
|
||||
.all(|variant| variant.attrs.skip_deserializing());
|
||||
let match_variant = if all_skipped {
|
||||
// This is an empty enum like `enum Impossible {}` or an enum in which
|
||||
// all variants have `#[serde(skip_deserializing)]`.
|
||||
@ -678,15 +675,17 @@ fn deserialize_externally_tagged_enum(params: &Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_internally_tagged_enum(params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item,
|
||||
tag: &str)
|
||||
-> Fragment {
|
||||
let variant_names_idents: Vec<_> = variants.iter()
|
||||
fn deserialize_internally_tagged_enum(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item,
|
||||
tag: &str,
|
||||
) -> Fragment {
|
||||
let variant_names_idents: Vec<_> = variants
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||
.map(|(i, variant)| (variant.attrs.name().deserialize_name(), field_i(i)))
|
||||
.map(|(i, variant)| (variant.attrs.name().deserialize_name(), field_i(i)),)
|
||||
.collect();
|
||||
|
||||
let variants_stmt = {
|
||||
@ -696,7 +695,7 @@ fn deserialize_internally_tagged_enum(params: &Parameters,
|
||||
}
|
||||
};
|
||||
|
||||
let variant_visitor = Stmts(deserialize_field_visitor(variant_names_idents, item_attrs, true));
|
||||
let variant_visitor = Stmts(deserialize_field_visitor(variant_names_idents, item_attrs, true),);
|
||||
|
||||
// Match arms to extract a variant from a string
|
||||
let variant_arms = variants.iter()
|
||||
@ -732,19 +731,21 @@ fn deserialize_internally_tagged_enum(params: &Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_adjacently_tagged_enum(params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item,
|
||||
tag: &str,
|
||||
content: &str)
|
||||
-> Fragment {
|
||||
fn deserialize_adjacently_tagged_enum(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item,
|
||||
tag: &str,
|
||||
content: &str,
|
||||
) -> Fragment {
|
||||
let this = ¶ms.this;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params);
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params,);
|
||||
|
||||
let variant_names_idents: Vec<_> = variants.iter()
|
||||
let variant_names_idents: Vec<_> = variants
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||
.map(|(i, variant)| (variant.attrs.name().deserialize_name(), field_i(i)))
|
||||
.map(|(i, variant)| (variant.attrs.name().deserialize_name(), field_i(i)),)
|
||||
.collect();
|
||||
|
||||
let variants_stmt = {
|
||||
@ -754,25 +755,30 @@ fn deserialize_adjacently_tagged_enum(params: &Parameters,
|
||||
}
|
||||
};
|
||||
|
||||
let variant_visitor = Stmts(deserialize_field_visitor(variant_names_idents, item_attrs, true));
|
||||
let variant_visitor = Stmts(deserialize_field_visitor(variant_names_idents, item_attrs, true),);
|
||||
|
||||
let ref variant_arms: Vec<_> = variants.iter()
|
||||
let ref variant_arms: Vec<_> = variants
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||
.map(|(i, variant)| {
|
||||
let variant_index = field_i(i);
|
||||
.map(
|
||||
|(i, variant)| {
|
||||
let variant_index = field_i(i);
|
||||
|
||||
let block = Match(deserialize_untagged_variant(
|
||||
params,
|
||||
variant,
|
||||
item_attrs,
|
||||
quote!(__deserializer),
|
||||
));
|
||||
let block = Match(
|
||||
deserialize_untagged_variant(
|
||||
params,
|
||||
variant,
|
||||
item_attrs,
|
||||
quote!(__deserializer),
|
||||
),
|
||||
);
|
||||
|
||||
quote! {
|
||||
quote! {
|
||||
__Field::#variant_index => #block
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
.collect();
|
||||
|
||||
let expecting = format!("adjacently tagged enum {}", params.type_name());
|
||||
@ -799,20 +805,25 @@ fn deserialize_adjacently_tagged_enum(params: &Parameters,
|
||||
let fallthrough = if variants.iter().all(is_unit) {
|
||||
None
|
||||
} else {
|
||||
Some(quote! {
|
||||
_ => #missing_content
|
||||
})
|
||||
};
|
||||
let arms = variants.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_, variant)| !variant.attrs.skip_deserializing() && is_unit(variant))
|
||||
.map(|(i, variant)| {
|
||||
let variant_index = field_i(i);
|
||||
let variant_ident = &variant.ident;
|
||||
Some(
|
||||
quote! {
|
||||
_ => #missing_content
|
||||
},
|
||||
)
|
||||
};
|
||||
let arms = variants
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_, variant)| !variant.attrs.skip_deserializing() && is_unit(variant),)
|
||||
.map(
|
||||
|(i, variant)| {
|
||||
let variant_index = field_i(i);
|
||||
let variant_ident = &variant.ident;
|
||||
quote! {
|
||||
__Field::#variant_index => _serde::export::Ok(#this::#variant_ident),
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
missing_content = quote! {
|
||||
match __field {
|
||||
#(#arms)*
|
||||
@ -970,20 +981,24 @@ fn deserialize_adjacently_tagged_enum(params: &Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_untagged_enum(params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
let attempts = variants.iter()
|
||||
fn deserialize_untagged_enum(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
let attempts = variants
|
||||
.iter()
|
||||
.filter(|variant| !variant.attrs.skip_deserializing())
|
||||
.map(|variant| {
|
||||
Expr(deserialize_untagged_variant(
|
||||
.map(
|
||||
|variant| {
|
||||
Expr(deserialize_untagged_variant(
|
||||
params,
|
||||
variant,
|
||||
item_attrs,
|
||||
quote!(_serde::private::de::ContentRefDeserializer::<__D::Error>::new(&__content)),
|
||||
))
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// TODO this message could be better by saving the errors from the failed
|
||||
// attempts. The heuristic used by TOML was to count the number of fields
|
||||
@ -991,7 +1006,8 @@ fn deserialize_untagged_enum(params: &Parameters,
|
||||
// largest number of fields. I'm not sure I like that. Maybe it would be
|
||||
// better to save all the errors and combine them into one message that
|
||||
// explains why none of the variants matched.
|
||||
let fallthrough_msg = format!("data did not match any variant of untagged enum {}", params.type_name());
|
||||
let fallthrough_msg =
|
||||
format!("data did not match any variant of untagged enum {}", params.type_name());
|
||||
|
||||
quote_block! {
|
||||
let __content = try!(<_serde::private::de::Content as _serde::Deserialize>::deserialize(__deserializer));
|
||||
@ -1006,10 +1022,11 @@ fn deserialize_untagged_enum(params: &Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_externally_tagged_variant(params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
fn deserialize_externally_tagged_variant(
|
||||
params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
let variant_ident = &variant.ident;
|
||||
|
||||
match variant.style {
|
||||
@ -1021,32 +1038,35 @@ fn deserialize_externally_tagged_variant(params: &Parameters,
|
||||
}
|
||||
}
|
||||
Style::Newtype => {
|
||||
deserialize_externally_tagged_newtype_variant(variant_ident,
|
||||
params,
|
||||
&variant.fields[0])
|
||||
deserialize_externally_tagged_newtype_variant(variant_ident, params, &variant.fields[0])
|
||||
}
|
||||
Style::Tuple => {
|
||||
deserialize_tuple(Some(variant_ident),
|
||||
params,
|
||||
&variant.fields,
|
||||
item_attrs,
|
||||
None)
|
||||
deserialize_tuple(
|
||||
Some(variant_ident),
|
||||
params,
|
||||
&variant.fields,
|
||||
item_attrs,
|
||||
None,
|
||||
)
|
||||
}
|
||||
Style::Struct => {
|
||||
deserialize_struct(Some(variant_ident),
|
||||
params,
|
||||
&variant.fields,
|
||||
item_attrs,
|
||||
None)
|
||||
deserialize_struct(
|
||||
Some(variant_ident),
|
||||
params,
|
||||
&variant.fields,
|
||||
item_attrs,
|
||||
None,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_internally_tagged_variant(params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
deserializer: Tokens)
|
||||
-> Fragment {
|
||||
fn deserialize_internally_tagged_variant(
|
||||
params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
deserializer: Tokens,
|
||||
) -> Fragment {
|
||||
let variant_ident = &variant.ident;
|
||||
|
||||
match variant.style {
|
||||
@ -1060,20 +1080,18 @@ fn deserialize_internally_tagged_variant(params: &Parameters,
|
||||
}
|
||||
}
|
||||
Style::Newtype | Style::Struct => {
|
||||
deserialize_untagged_variant(params,
|
||||
variant,
|
||||
item_attrs,
|
||||
deserializer)
|
||||
deserialize_untagged_variant(params, variant, item_attrs, deserializer)
|
||||
}
|
||||
Style::Tuple => unreachable!("checked in serde_codegen_internals"),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_untagged_variant(params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
deserializer: Tokens)
|
||||
-> Fragment {
|
||||
fn deserialize_untagged_variant(
|
||||
params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
deserializer: Tokens,
|
||||
) -> Fragment {
|
||||
let variant_ident = &variant.ident;
|
||||
|
||||
match variant.style {
|
||||
@ -1091,32 +1109,39 @@ fn deserialize_untagged_variant(params: &Parameters,
|
||||
}
|
||||
}
|
||||
Style::Newtype => {
|
||||
deserialize_untagged_newtype_variant(variant_ident,
|
||||
params,
|
||||
&variant.fields[0],
|
||||
deserializer)
|
||||
deserialize_untagged_newtype_variant(
|
||||
variant_ident,
|
||||
params,
|
||||
&variant.fields[0],
|
||||
deserializer,
|
||||
)
|
||||
}
|
||||
Style::Tuple => {
|
||||
deserialize_tuple(Some(variant_ident),
|
||||
params,
|
||||
&variant.fields,
|
||||
item_attrs,
|
||||
Some(deserializer))
|
||||
deserialize_tuple(
|
||||
Some(variant_ident),
|
||||
params,
|
||||
&variant.fields,
|
||||
item_attrs,
|
||||
Some(deserializer),
|
||||
)
|
||||
}
|
||||
Style::Struct => {
|
||||
deserialize_struct(Some(variant_ident),
|
||||
params,
|
||||
&variant.fields,
|
||||
item_attrs,
|
||||
Some(deserializer))
|
||||
deserialize_struct(
|
||||
Some(variant_ident),
|
||||
params,
|
||||
&variant.fields,
|
||||
item_attrs,
|
||||
Some(deserializer),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_externally_tagged_newtype_variant(variant_ident: &syn::Ident,
|
||||
params: &Parameters,
|
||||
field: &Field)
|
||||
-> Fragment {
|
||||
fn deserialize_externally_tagged_newtype_variant(
|
||||
variant_ident: &syn::Ident,
|
||||
params: &Parameters,
|
||||
field: &Field,
|
||||
) -> Fragment {
|
||||
let this = ¶ms.this;
|
||||
match field.attrs.deserialize_with() {
|
||||
None => {
|
||||
@ -1128,8 +1153,7 @@ fn deserialize_externally_tagged_newtype_variant(variant_ident: &syn::Ident,
|
||||
}
|
||||
}
|
||||
Some(path) => {
|
||||
let (wrapper, wrapper_ty) =
|
||||
wrap_deserialize_with(params, field.ty, path);
|
||||
let (wrapper, wrapper_ty) = wrap_deserialize_with(params, field.ty, path);
|
||||
quote_block! {
|
||||
#wrapper
|
||||
_serde::export::Result::map(
|
||||
@ -1140,11 +1164,12 @@ fn deserialize_externally_tagged_newtype_variant(variant_ident: &syn::Ident,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_untagged_newtype_variant(variant_ident: &syn::Ident,
|
||||
params: &Parameters,
|
||||
field: &Field,
|
||||
deserializer: Tokens)
|
||||
-> Fragment {
|
||||
fn deserialize_untagged_newtype_variant(
|
||||
variant_ident: &syn::Ident,
|
||||
params: &Parameters,
|
||||
field: &Field,
|
||||
deserializer: Tokens,
|
||||
) -> Fragment {
|
||||
let this = ¶ms.this;
|
||||
match field.attrs.deserialize_with() {
|
||||
None => {
|
||||
@ -1156,8 +1181,7 @@ fn deserialize_untagged_newtype_variant(variant_ident: &syn::Ident,
|
||||
}
|
||||
}
|
||||
Some(path) => {
|
||||
let (wrapper, wrapper_ty) =
|
||||
wrap_deserialize_with(params, field.ty, path);
|
||||
let (wrapper, wrapper_ty) = wrap_deserialize_with(params, field.ty, path);
|
||||
quote_block! {
|
||||
#wrapper
|
||||
_serde::export::Result::map(
|
||||
@ -1168,10 +1192,11 @@ fn deserialize_untagged_newtype_variant(variant_ident: &syn::Ident,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_field_visitor(fields: Vec<(String, Ident)>,
|
||||
item_attrs: &attr::Item,
|
||||
is_variant: bool)
|
||||
-> Fragment {
|
||||
fn deserialize_field_visitor(
|
||||
fields: Vec<(String, Ident)>,
|
||||
item_attrs: &attr::Item,
|
||||
is_variant: bool,
|
||||
) -> Fragment {
|
||||
let field_strs = fields.iter().map(|&(ref name, _)| name);
|
||||
let field_bytes = fields.iter().map(|&(ref name, _)| quote::ByteStr(name));
|
||||
let field_idents: &Vec<_> = &fields.iter().map(|&(_, ref ident)| ident).collect();
|
||||
@ -1185,7 +1210,8 @@ fn deserialize_field_visitor(fields: Vec<(String, Ident)>,
|
||||
let visit_index = if is_variant {
|
||||
let variant_indices = 0u32..;
|
||||
let fallthrough_msg = format!("variant index 0 <= i < {}", fields.len());
|
||||
Some(quote! {
|
||||
Some(
|
||||
quote! {
|
||||
fn visit_u32<__E>(self, __value: u32) -> _serde::export::Result<__Field, __E>
|
||||
where __E: _serde::de::Error
|
||||
{
|
||||
@ -1198,7 +1224,8 @@ fn deserialize_field_visitor(fields: Vec<(String, Ident)>,
|
||||
&#fallthrough_msg))
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@ -1218,9 +1245,11 @@ fn deserialize_field_visitor(fields: Vec<(String, Ident)>,
|
||||
};
|
||||
|
||||
let bytes_to_str = if is_variant || item_attrs.deny_unknown_fields() {
|
||||
Some(quote! {
|
||||
Some(
|
||||
quote! {
|
||||
let __value = &_serde::export::from_utf8_lossy(__value);
|
||||
})
|
||||
},
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@ -1280,15 +1309,17 @@ fn deserialize_field_visitor(fields: Vec<(String, Ident)>,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_struct_visitor(struct_path: Tokens,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item)
|
||||
-> (Fragment, Fragment, Fragment) {
|
||||
let field_names_idents: Vec<_> = fields.iter()
|
||||
fn deserialize_struct_visitor(
|
||||
struct_path: Tokens,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
) -> (Fragment, Fragment, Fragment) {
|
||||
let field_names_idents: Vec<_> = fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_, field)| !field.attrs.skip_deserializing())
|
||||
.map(|(i, field)| (field.attrs.name().deserialize_name(), field_i(i)))
|
||||
.map(|(i, field)| (field.attrs.name().deserialize_name(), field_i(i)),)
|
||||
.collect();
|
||||
|
||||
let fields_stmt = {
|
||||
@ -1305,26 +1336,31 @@ fn deserialize_struct_visitor(struct_path: Tokens,
|
||||
(field_visitor, fields_stmt, visit_map)
|
||||
}
|
||||
|
||||
fn deserialize_map(struct_path: Tokens,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
fn deserialize_map(
|
||||
struct_path: Tokens,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
// Create the field names for the fields.
|
||||
let fields_names: Vec<_> = fields.iter()
|
||||
let fields_names: Vec<_> = fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, field)| (field, field_i(i)))
|
||||
.collect();
|
||||
|
||||
// Declare each field that will be deserialized.
|
||||
let let_values = fields_names.iter()
|
||||
let let_values = fields_names
|
||||
.iter()
|
||||
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
||||
.map(|&(field, ref name)| {
|
||||
let field_ty = &field.ty;
|
||||
quote! {
|
||||
.map(
|
||||
|&(field, ref name)| {
|
||||
let field_ty = &field.ty;
|
||||
quote! {
|
||||
let mut #name: _serde::export::Option<#field_ty> = _serde::export::None;
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// Match arms to extract a value for a field.
|
||||
let value_arms = fields_names.iter()
|
||||
@ -1367,7 +1403,9 @@ fn deserialize_map(struct_path: Tokens,
|
||||
})
|
||||
};
|
||||
|
||||
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
||||
let all_skipped = fields
|
||||
.iter()
|
||||
.all(|field| field.attrs.skip_deserializing());
|
||||
let match_keys = if item_attrs.deny_unknown_fields() && all_skipped {
|
||||
quote! {
|
||||
// FIXME: Once we drop support for Rust 1.15:
|
||||
@ -1387,40 +1425,53 @@ fn deserialize_map(struct_path: Tokens,
|
||||
}
|
||||
};
|
||||
|
||||
let extract_values = fields_names.iter()
|
||||
let extract_values = fields_names
|
||||
.iter()
|
||||
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
||||
.map(|&(field, ref name)| {
|
||||
let missing_expr = Match(expr_is_missing(&field, item_attrs));
|
||||
.map(
|
||||
|&(field, ref name)| {
|
||||
let missing_expr = Match(expr_is_missing(&field, item_attrs));
|
||||
|
||||
quote! {
|
||||
quote! {
|
||||
let #name = match #name {
|
||||
_serde::export::Some(#name) => #name,
|
||||
_serde::export::None => #missing_expr
|
||||
};
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
let result = fields_names.iter()
|
||||
.map(|&(field, ref name)| {
|
||||
let ident = field.ident.clone().expect("struct contains unnamed fields");
|
||||
if field.attrs.skip_deserializing() {
|
||||
let value = Expr(expr_is_missing(&field, item_attrs));
|
||||
quote!(#ident: #value)
|
||||
} else {
|
||||
quote!(#ident: #name)
|
||||
}
|
||||
});
|
||||
let result = fields_names
|
||||
.iter()
|
||||
.map(
|
||||
|&(field, ref name)| {
|
||||
let ident = field
|
||||
.ident
|
||||
.clone()
|
||||
.expect("struct contains unnamed fields");
|
||||
if field.attrs.skip_deserializing() {
|
||||
let value = Expr(expr_is_missing(&field, item_attrs));
|
||||
quote!(#ident: #value)
|
||||
} else {
|
||||
quote!(#ident: #name)
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
let let_default = match *item_attrs.default() {
|
||||
attr::Default::Default => {
|
||||
Some(quote!(
|
||||
Some(
|
||||
quote!(
|
||||
let __default: Self::Value = _serde::export::Default::default();
|
||||
))
|
||||
),
|
||||
)
|
||||
}
|
||||
attr::Default::Path(ref path) => {
|
||||
Some(quote!(
|
||||
Some(
|
||||
quote!(
|
||||
let __default: Self::Value = #path();
|
||||
))
|
||||
),
|
||||
)
|
||||
}
|
||||
attr::Default::None => {
|
||||
// We don't need the default value, to prevent an unused variable warning
|
||||
@ -1456,12 +1507,13 @@ fn field_i(i: usize) -> Ident {
|
||||
|
||||
/// This function wraps the expression in `#[serde(deserialize_with = "...")]`
|
||||
/// in a trait to prevent it from accessing the internal `Deserialize` state.
|
||||
fn wrap_deserialize_with(params: &Parameters,
|
||||
field_ty: &syn::Ty,
|
||||
deserialize_with: &syn::Path)
|
||||
-> (Tokens, Tokens) {
|
||||
fn wrap_deserialize_with(
|
||||
params: &Parameters,
|
||||
field_ty: &syn::Ty,
|
||||
deserialize_with: &syn::Path,
|
||||
) -> (Tokens, Tokens) {
|
||||
let this = ¶ms.this;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params);
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params,);
|
||||
|
||||
let wrapper = quote! {
|
||||
struct __DeserializeWith #de_impl_generics #where_clause {
|
||||
@ -1500,7 +1552,8 @@ fn expr_is_missing(field: &Field, item_attrs: &attr::Item) -> Fragment {
|
||||
}
|
||||
|
||||
match *item_attrs.default() {
|
||||
attr::Default::Default | attr::Default::Path(_) => {
|
||||
attr::Default::Default |
|
||||
attr::Default::Path(_) => {
|
||||
let ident = &field.ident;
|
||||
return quote_expr!(__default.#ident);
|
||||
}
|
||||
@ -1538,13 +1591,16 @@ struct DeTyGenerics<'a>(&'a syn::Generics);
|
||||
impl<'a> ToTokens for DeTyGenerics<'a> {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
let mut generics = self.0.clone();
|
||||
generics.lifetimes.insert(0, syn::LifetimeDef::new("'de"));
|
||||
generics
|
||||
.lifetimes
|
||||
.insert(0, syn::LifetimeDef::new("'de"));
|
||||
let (_, ty_generics, _) = generics.split_for_impl();
|
||||
ty_generics.to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
|
||||
fn split_with_de_lifetime(params: &Parameters) -> (DeImplGenerics, DeTyGenerics, syn::TyGenerics, &syn::WhereClause) {
|
||||
fn split_with_de_lifetime(params: &Parameters,)
|
||||
-> (DeImplGenerics, DeTyGenerics, syn::TyGenerics, &syn::WhereClause) {
|
||||
let de_impl_generics = DeImplGenerics(¶ms);
|
||||
let de_ty_generics = DeTyGenerics(¶ms.generics);
|
||||
let (_, ty_generics, where_clause) = params.generics.split_for_impl();
|
||||
|
@ -42,13 +42,15 @@ pub fn expand_derive_serialize(item: &syn::DeriveInput) -> Result<Tokens, String
|
||||
}
|
||||
};
|
||||
|
||||
Ok(quote! {
|
||||
Ok(
|
||||
quote! {
|
||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||
const #dummy_const: () = {
|
||||
extern crate serde as _serde;
|
||||
#impl_item
|
||||
};
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
struct Parameters {
|
||||
@ -110,10 +112,12 @@ fn build_generics(item: &Item) -> syn::Generics {
|
||||
match item.attrs.ser_bound() {
|
||||
Some(predicates) => bound::with_where_predicates(&generics, predicates),
|
||||
None => {
|
||||
bound::with_bound(item,
|
||||
&generics,
|
||||
needs_serialize_bound,
|
||||
&path!(_serde::Serialize))
|
||||
bound::with_bound(
|
||||
item,
|
||||
&generics,
|
||||
needs_serialize_bound,
|
||||
&path!(_serde::Serialize),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -131,9 +135,7 @@ fn serialize_body(item: &Item, params: &Parameters) -> Fragment {
|
||||
serialize_into(params, into_type)
|
||||
} else {
|
||||
match item.body {
|
||||
Body::Enum(ref variants) => {
|
||||
serialize_item_enum(params, variants, &item.attrs)
|
||||
}
|
||||
Body::Enum(ref variants) => serialize_item_enum(params, variants, &item.attrs),
|
||||
Body::Struct(Style::Struct, ref fields) => {
|
||||
if fields.iter().any(|field| field.ident.is_none()) {
|
||||
panic!("struct has unnamed fields");
|
||||
@ -171,10 +173,11 @@ fn serialize_unit_struct(item_attrs: &attr::Item) -> Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct(params: &Parameters,
|
||||
field: &Field,
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
fn serialize_newtype_struct(
|
||||
params: &Parameters,
|
||||
field: &Field,
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
let type_name = item_attrs.name().serialize_name();
|
||||
|
||||
let mut field_expr = get_field(params, field, 0);
|
||||
@ -187,15 +190,17 @@ fn serialize_newtype_struct(params: &Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
let serialize_stmts =
|
||||
serialize_tuple_struct_visitor(fields,
|
||||
params,
|
||||
false,
|
||||
quote!(_serde::ser::SerializeTupleStruct::serialize_field));
|
||||
fn serialize_tuple_struct(
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
let serialize_stmts = serialize_tuple_struct_visitor(
|
||||
fields,
|
||||
params,
|
||||
false,
|
||||
quote!(_serde::ser::SerializeTupleStruct::serialize_field),
|
||||
);
|
||||
|
||||
let type_name = item_attrs.name().serialize_name();
|
||||
let len = serialize_stmts.len();
|
||||
@ -208,36 +213,36 @@ fn serialize_tuple_struct(params: &Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_struct(params: &Parameters,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
fn serialize_struct(params: &Parameters, fields: &[Field], item_attrs: &attr::Item) -> Fragment {
|
||||
assert!(fields.len() as u64 <= u32::MAX as u64);
|
||||
|
||||
let serialize_fields =
|
||||
serialize_struct_visitor(fields,
|
||||
params,
|
||||
false,
|
||||
quote!(_serde::ser::SerializeStruct::serialize_field));
|
||||
let serialize_fields = serialize_struct_visitor(
|
||||
fields,
|
||||
params,
|
||||
false,
|
||||
quote!(_serde::ser::SerializeStruct::serialize_field),
|
||||
);
|
||||
|
||||
let type_name = item_attrs.name().serialize_name();
|
||||
|
||||
let mut serialized_fields = fields.iter()
|
||||
let mut serialized_fields = fields
|
||||
.iter()
|
||||
.filter(|&field| !field.attrs.skip_serializing())
|
||||
.peekable();
|
||||
|
||||
let let_mut = mut_if(serialized_fields.peek().is_some());
|
||||
|
||||
let len = serialized_fields.map(|field| {
|
||||
match field.attrs.skip_serializing_if() {
|
||||
let len = serialized_fields
|
||||
.map(
|
||||
|field| match field.attrs.skip_serializing_if() {
|
||||
None => quote!(1),
|
||||
Some(path) => {
|
||||
let ident = field.ident.clone().expect("struct has unnamed fields");
|
||||
let field_expr = get_field(params, field, ident);
|
||||
quote!(if #path(#field_expr) { 0 } else { 1 })
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
||||
|
||||
quote_block! {
|
||||
@ -247,22 +252,23 @@ fn serialize_struct(params: &Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_item_enum(params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
fn serialize_item_enum(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
assert!(variants.len() as u64 <= u32::MAX as u64);
|
||||
|
||||
let self_var = ¶ms.self_var;
|
||||
|
||||
let arms: Vec<_> = variants.iter()
|
||||
let arms: Vec<_> = variants
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(variant_index, variant)| {
|
||||
serialize_variant(params,
|
||||
variant,
|
||||
variant_index as u32,
|
||||
item_attrs)
|
||||
})
|
||||
.map(
|
||||
|(variant_index, variant)| {
|
||||
serialize_variant(params, variant, variant_index as u32, item_attrs)
|
||||
},
|
||||
)
|
||||
.collect();
|
||||
|
||||
quote_expr! {
|
||||
@ -272,24 +278,34 @@ fn serialize_item_enum(params: &Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_variant(params: &Parameters,
|
||||
variant: &Variant,
|
||||
variant_index: u32,
|
||||
item_attrs: &attr::Item)
|
||||
-> Tokens {
|
||||
fn serialize_variant(
|
||||
params: &Parameters,
|
||||
variant: &Variant,
|
||||
variant_index: u32,
|
||||
item_attrs: &attr::Item,
|
||||
) -> Tokens {
|
||||
let this = ¶ms.this;
|
||||
let variant_ident = variant.ident.clone();
|
||||
|
||||
if variant.attrs.skip_serializing() {
|
||||
let skipped_msg = format!("the enum variant {}::{} cannot be serialized",
|
||||
params.type_name(), variant_ident);
|
||||
let skipped_msg = format!(
|
||||
"the enum variant {}::{} cannot be serialized",
|
||||
params.type_name(),
|
||||
variant_ident
|
||||
);
|
||||
let skipped_err = quote! {
|
||||
_serde::export::Err(_serde::ser::Error::custom(#skipped_msg))
|
||||
};
|
||||
let fields_pat = match variant.style {
|
||||
Style::Unit => quote!(),
|
||||
Style::Newtype | Style::Tuple => quote!( (..) ),
|
||||
Style::Struct => quote!( {..} ),
|
||||
Style::Newtype | Style::Tuple => quote!((..)),
|
||||
Style::Struct => {
|
||||
quote!(
|
||||
{
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
};
|
||||
quote! {
|
||||
#this::#variant_ident #fields_pat => #skipped_err,
|
||||
@ -308,44 +324,44 @@ fn serialize_variant(params: &Parameters,
|
||||
}
|
||||
}
|
||||
Style::Tuple => {
|
||||
let field_names = (0..variant.fields.len())
|
||||
.map(|i| Ident::new(format!("__field{}", i)));
|
||||
let field_names =
|
||||
(0..variant.fields.len()).map(|i| Ident::new(format!("__field{}", i)));
|
||||
quote! {
|
||||
#this::#variant_ident(#(ref #field_names),*)
|
||||
}
|
||||
}
|
||||
Style::Struct => {
|
||||
let fields = variant.fields
|
||||
let fields = variant
|
||||
.fields
|
||||
.iter()
|
||||
.map(|f| f.ident.clone().expect("struct variant has unnamed fields"));
|
||||
.map(
|
||||
|f| {
|
||||
f.ident
|
||||
.clone()
|
||||
.expect("struct variant has unnamed fields")
|
||||
},
|
||||
);
|
||||
quote! {
|
||||
#this::#variant_ident { #(ref #fields),* }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let body = Match(match *item_attrs.tag() {
|
||||
attr::EnumTag::External => {
|
||||
serialize_externally_tagged_variant(params,
|
||||
variant,
|
||||
variant_index,
|
||||
item_attrs)
|
||||
}
|
||||
attr::EnumTag::Internal { ref tag } => {
|
||||
serialize_internally_tagged_variant(params,
|
||||
variant,
|
||||
item_attrs,
|
||||
tag)
|
||||
}
|
||||
attr::EnumTag::Adjacent { ref tag, ref content } => {
|
||||
serialize_adjacently_tagged_variant(params,
|
||||
variant,
|
||||
item_attrs,
|
||||
tag,
|
||||
content)
|
||||
}
|
||||
attr::EnumTag::None => serialize_untagged_variant(params, variant, item_attrs),
|
||||
});
|
||||
let body = Match(
|
||||
match *item_attrs.tag() {
|
||||
attr::EnumTag::External => {
|
||||
serialize_externally_tagged_variant(params, variant, variant_index, item_attrs)
|
||||
}
|
||||
attr::EnumTag::Internal { ref tag } => {
|
||||
serialize_internally_tagged_variant(params, variant, item_attrs, tag)
|
||||
}
|
||||
attr::EnumTag::Adjacent {
|
||||
ref tag,
|
||||
ref content,
|
||||
} => serialize_adjacently_tagged_variant(params, variant, item_attrs, tag, content),
|
||||
attr::EnumTag::None => serialize_untagged_variant(params, variant, item_attrs),
|
||||
},
|
||||
);
|
||||
|
||||
quote! {
|
||||
#case => #body
|
||||
@ -353,11 +369,12 @@ fn serialize_variant(params: &Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_externally_tagged_variant(params: &Parameters,
|
||||
variant: &Variant,
|
||||
variant_index: u32,
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
fn serialize_externally_tagged_variant(
|
||||
params: &Parameters,
|
||||
variant: &Variant,
|
||||
variant_index: u32,
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
let type_name = item_attrs.name().serialize_name();
|
||||
let variant_name = variant.attrs.name().serialize_name();
|
||||
|
||||
@ -390,31 +407,36 @@ fn serialize_externally_tagged_variant(params: &Parameters,
|
||||
}
|
||||
}
|
||||
Style::Tuple => {
|
||||
serialize_tuple_variant(TupleVariant::ExternallyTagged {
|
||||
type_name: type_name,
|
||||
variant_index: variant_index,
|
||||
variant_name: variant_name,
|
||||
},
|
||||
params,
|
||||
&variant.fields)
|
||||
serialize_tuple_variant(
|
||||
TupleVariant::ExternallyTagged {
|
||||
type_name: type_name,
|
||||
variant_index: variant_index,
|
||||
variant_name: variant_name,
|
||||
},
|
||||
params,
|
||||
&variant.fields,
|
||||
)
|
||||
}
|
||||
Style::Struct => {
|
||||
serialize_struct_variant(StructVariant::ExternallyTagged {
|
||||
variant_index: variant_index,
|
||||
variant_name: variant_name,
|
||||
},
|
||||
params,
|
||||
&variant.fields,
|
||||
&type_name)
|
||||
serialize_struct_variant(
|
||||
StructVariant::ExternallyTagged {
|
||||
variant_index: variant_index,
|
||||
variant_name: variant_name,
|
||||
},
|
||||
params,
|
||||
&variant.fields,
|
||||
&type_name,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_internally_tagged_variant(params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
tag: &str)
|
||||
-> Fragment {
|
||||
fn serialize_internally_tagged_variant(
|
||||
params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
tag: &str,
|
||||
) -> Fragment {
|
||||
let type_name = item_attrs.name().serialize_name();
|
||||
let variant_name = variant.attrs.name().serialize_name();
|
||||
|
||||
@ -450,61 +472,66 @@ fn serialize_internally_tagged_variant(params: &Parameters,
|
||||
}
|
||||
}
|
||||
Style::Struct => {
|
||||
serialize_struct_variant(StructVariant::InternallyTagged {
|
||||
tag: tag,
|
||||
variant_name: variant_name,
|
||||
},
|
||||
params,
|
||||
&variant.fields,
|
||||
&type_name)
|
||||
serialize_struct_variant(
|
||||
StructVariant::InternallyTagged {
|
||||
tag: tag,
|
||||
variant_name: variant_name,
|
||||
},
|
||||
params,
|
||||
&variant.fields,
|
||||
&type_name,
|
||||
)
|
||||
}
|
||||
Style::Tuple => unreachable!("checked in serde_codegen_internals"),
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_adjacently_tagged_variant(params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
tag: &str,
|
||||
content: &str)
|
||||
-> Fragment {
|
||||
fn serialize_adjacently_tagged_variant(
|
||||
params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
tag: &str,
|
||||
content: &str,
|
||||
) -> Fragment {
|
||||
let this = ¶ms.this;
|
||||
let type_name = item_attrs.name().serialize_name();
|
||||
let variant_name = variant.attrs.name().serialize_name();
|
||||
|
||||
let inner = Stmts(match variant.style {
|
||||
Style::Unit => {
|
||||
return quote_block! {
|
||||
let inner = Stmts(
|
||||
match variant.style {
|
||||
Style::Unit => {
|
||||
return quote_block! {
|
||||
let mut __struct = try!(_serde::Serializer::serialize_struct(
|
||||
__serializer, #type_name, 1));
|
||||
try!(_serde::ser::SerializeStruct::serialize_field(
|
||||
&mut __struct, #tag, #variant_name));
|
||||
_serde::ser::SerializeStruct::end(__struct)
|
||||
};
|
||||
}
|
||||
Style::Newtype => {
|
||||
let field = &variant.fields[0];
|
||||
let mut field_expr = quote!(__field0);
|
||||
if let Some(path) = field.attrs.serialize_with() {
|
||||
field_expr = wrap_serialize_with(params, field.ty, path, field_expr);
|
||||
}
|
||||
Style::Newtype => {
|
||||
let field = &variant.fields[0];
|
||||
let mut field_expr = quote!(__field0);
|
||||
if let Some(path) = field.attrs.serialize_with() {
|
||||
field_expr = wrap_serialize_with(params, field.ty, path, field_expr);
|
||||
}
|
||||
|
||||
quote_expr! {
|
||||
quote_expr! {
|
||||
_serde::Serialize::serialize(#field_expr, __serializer)
|
||||
}
|
||||
}
|
||||
Style::Tuple => {
|
||||
serialize_tuple_variant(TupleVariant::Untagged,
|
||||
params,
|
||||
&variant.fields)
|
||||
}
|
||||
Style::Struct => {
|
||||
serialize_struct_variant(StructVariant::Untagged,
|
||||
params,
|
||||
&variant.fields,
|
||||
&variant_name)
|
||||
}
|
||||
});
|
||||
}
|
||||
Style::Tuple => {
|
||||
serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields)
|
||||
}
|
||||
Style::Struct => {
|
||||
serialize_struct_variant(
|
||||
StructVariant::Untagged,
|
||||
params,
|
||||
&variant.fields,
|
||||
&variant_name,
|
||||
)
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
let fields_ty = variant.fields.iter().map(|f| &f.ty);
|
||||
let ref fields_ident: Vec<_> = match variant.style {
|
||||
@ -516,9 +543,16 @@ fn serialize_adjacently_tagged_variant(params: &Parameters,
|
||||
.collect()
|
||||
}
|
||||
Style::Struct => {
|
||||
variant.fields
|
||||
variant
|
||||
.fields
|
||||
.iter()
|
||||
.map(|f| f.ident.clone().expect("struct variant has unnamed fields"))
|
||||
.map(
|
||||
|f| {
|
||||
f.ident
|
||||
.clone()
|
||||
.expect("struct variant has unnamed fields")
|
||||
},
|
||||
)
|
||||
.collect()
|
||||
}
|
||||
};
|
||||
@ -556,10 +590,11 @@ fn serialize_adjacently_tagged_variant(params: &Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_untagged_variant(params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item)
|
||||
-> Fragment {
|
||||
fn serialize_untagged_variant(
|
||||
params: &Parameters,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
) -> Fragment {
|
||||
match variant.style {
|
||||
Style::Unit => {
|
||||
quote_expr! {
|
||||
@ -577,15 +612,10 @@ fn serialize_untagged_variant(params: &Parameters,
|
||||
_serde::Serialize::serialize(#field_expr, __serializer)
|
||||
}
|
||||
}
|
||||
Style::Tuple => {
|
||||
serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields)
|
||||
}
|
||||
Style::Tuple => serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields),
|
||||
Style::Struct => {
|
||||
let type_name = item_attrs.name().serialize_name();
|
||||
serialize_struct_variant(StructVariant::Untagged,
|
||||
params,
|
||||
&variant.fields,
|
||||
&type_name)
|
||||
serialize_struct_variant(StructVariant::Untagged, params, &variant.fields, &type_name)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -599,10 +629,11 @@ enum TupleVariant {
|
||||
Untagged,
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(context: TupleVariant,
|
||||
params: &Parameters,
|
||||
fields: &[Field])
|
||||
-> Fragment {
|
||||
fn serialize_tuple_variant(
|
||||
context: TupleVariant,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
) -> Fragment {
|
||||
let method = match context {
|
||||
TupleVariant::ExternallyTagged { .. } => {
|
||||
quote!(_serde::ser::SerializeTupleVariant::serialize_field)
|
||||
@ -610,14 +641,17 @@ fn serialize_tuple_variant(context: TupleVariant,
|
||||
TupleVariant::Untagged => quote!(_serde::ser::SerializeTuple::serialize_element),
|
||||
};
|
||||
|
||||
let serialize_stmts =
|
||||
serialize_tuple_struct_visitor(fields, params, true, method);
|
||||
let serialize_stmts = serialize_tuple_struct_visitor(fields, params, true, method);
|
||||
|
||||
let len = serialize_stmts.len();
|
||||
let let_mut = mut_if(len > 0);
|
||||
|
||||
match context {
|
||||
TupleVariant::ExternallyTagged { type_name, variant_index, variant_name } => {
|
||||
TupleVariant::ExternallyTagged {
|
||||
type_name,
|
||||
variant_index,
|
||||
variant_name,
|
||||
} => {
|
||||
quote_block! {
|
||||
let #let_mut __serde_state = try!(_serde::Serializer::serialize_tuple_variant(
|
||||
__serializer,
|
||||
@ -650,11 +684,12 @@ enum StructVariant<'a> {
|
||||
Untagged,
|
||||
}
|
||||
|
||||
fn serialize_struct_variant<'a>(context: StructVariant<'a>,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
name: &str)
|
||||
-> Fragment {
|
||||
fn serialize_struct_variant<'a>(
|
||||
context: StructVariant<'a>,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
name: &str,
|
||||
) -> Fragment {
|
||||
let method = match context {
|
||||
StructVariant::ExternallyTagged { .. } => {
|
||||
quote!(_serde::ser::SerializeStructVariant::serialize_field)
|
||||
@ -665,24 +700,31 @@ fn serialize_struct_variant<'a>(context: StructVariant<'a>,
|
||||
|
||||
let serialize_fields = serialize_struct_visitor(fields, params, true, method);
|
||||
|
||||
let mut serialized_fields = fields.iter()
|
||||
let mut serialized_fields = fields
|
||||
.iter()
|
||||
.filter(|&field| !field.attrs.skip_serializing())
|
||||
.peekable();
|
||||
|
||||
let let_mut = mut_if(serialized_fields.peek().is_some());
|
||||
|
||||
let len = serialized_fields.map(|field| {
|
||||
let ident = field.ident.clone().expect("struct has unnamed fields");
|
||||
let len = serialized_fields
|
||||
.map(
|
||||
|field| {
|
||||
let ident = field.ident.clone().expect("struct has unnamed fields");
|
||||
|
||||
match field.attrs.skip_serializing_if() {
|
||||
Some(path) => quote!(if #path(#ident) { 0 } else { 1 }),
|
||||
None => quote!(1),
|
||||
}
|
||||
})
|
||||
match field.attrs.skip_serializing_if() {
|
||||
Some(path) => quote!(if #path(#ident) { 0 } else { 1 }),
|
||||
None => quote!(1),
|
||||
}
|
||||
},
|
||||
)
|
||||
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
||||
|
||||
match context {
|
||||
StructVariant::ExternallyTagged { variant_index, variant_name } => {
|
||||
StructVariant::ExternallyTagged {
|
||||
variant_index,
|
||||
variant_name,
|
||||
} => {
|
||||
quote_block! {
|
||||
let #let_mut __serde_state = try!(_serde::Serializer::serialize_struct_variant(
|
||||
__serializer,
|
||||
@ -725,85 +767,94 @@ fn serialize_struct_variant<'a>(context: StructVariant<'a>,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct_visitor(fields: &[Field],
|
||||
params: &Parameters,
|
||||
is_enum: bool,
|
||||
func: Tokens)
|
||||
-> Vec<Tokens> {
|
||||
fields.iter()
|
||||
fn serialize_tuple_struct_visitor(
|
||||
fields: &[Field],
|
||||
params: &Parameters,
|
||||
is_enum: bool,
|
||||
func: Tokens,
|
||||
) -> Vec<Tokens> {
|
||||
fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, field)| {
|
||||
let mut field_expr = if is_enum {
|
||||
let id = Ident::new(format!("__field{}", i));
|
||||
quote!(#id)
|
||||
} else {
|
||||
get_field(params, field, i)
|
||||
};
|
||||
.map(
|
||||
|(i, field)| {
|
||||
let mut field_expr = if is_enum {
|
||||
let id = Ident::new(format!("__field{}", i));
|
||||
quote!(#id)
|
||||
} else {
|
||||
get_field(params, field, i)
|
||||
};
|
||||
|
||||
let skip = field.attrs
|
||||
.skip_serializing_if()
|
||||
.map(|path| quote!(#path(#field_expr)));
|
||||
let skip = field
|
||||
.attrs
|
||||
.skip_serializing_if()
|
||||
.map(|path| quote!(#path(#field_expr)));
|
||||
|
||||
if let Some(path) = field.attrs.serialize_with() {
|
||||
field_expr =
|
||||
wrap_serialize_with(params, field.ty, path, field_expr);
|
||||
}
|
||||
if let Some(path) = field.attrs.serialize_with() {
|
||||
field_expr = wrap_serialize_with(params, field.ty, path, field_expr);
|
||||
}
|
||||
|
||||
let ser = quote! {
|
||||
let ser = quote! {
|
||||
try!(#func(&mut __serde_state, #field_expr));
|
||||
};
|
||||
|
||||
match skip {
|
||||
None => ser,
|
||||
Some(skip) => quote!(if !#skip { #ser }),
|
||||
}
|
||||
})
|
||||
match skip {
|
||||
None => ser,
|
||||
Some(skip) => quote!(if !#skip { #ser }),
|
||||
}
|
||||
},
|
||||
)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn serialize_struct_visitor(fields: &[Field],
|
||||
params: &Parameters,
|
||||
is_enum: bool,
|
||||
func: Tokens)
|
||||
-> Vec<Tokens> {
|
||||
fields.iter()
|
||||
fn serialize_struct_visitor(
|
||||
fields: &[Field],
|
||||
params: &Parameters,
|
||||
is_enum: bool,
|
||||
func: Tokens,
|
||||
) -> Vec<Tokens> {
|
||||
fields
|
||||
.iter()
|
||||
.filter(|&field| !field.attrs.skip_serializing())
|
||||
.map(|field| {
|
||||
let field_ident = field.ident.clone().expect("struct has unnamed field");
|
||||
let mut field_expr = if is_enum {
|
||||
quote!(#field_ident)
|
||||
} else {
|
||||
get_field(params, field, field_ident)
|
||||
};
|
||||
.map(
|
||||
|field| {
|
||||
let field_ident = field.ident.clone().expect("struct has unnamed field");
|
||||
let mut field_expr = if is_enum {
|
||||
quote!(#field_ident)
|
||||
} else {
|
||||
get_field(params, field, field_ident)
|
||||
};
|
||||
|
||||
let key_expr = field.attrs.name().serialize_name();
|
||||
let key_expr = field.attrs.name().serialize_name();
|
||||
|
||||
let skip = field.attrs
|
||||
.skip_serializing_if()
|
||||
.map(|path| quote!(#path(#field_expr)));
|
||||
let skip = field
|
||||
.attrs
|
||||
.skip_serializing_if()
|
||||
.map(|path| quote!(#path(#field_expr)));
|
||||
|
||||
if let Some(path) = field.attrs.serialize_with() {
|
||||
field_expr =
|
||||
wrap_serialize_with(params, field.ty, path, field_expr)
|
||||
}
|
||||
if let Some(path) = field.attrs.serialize_with() {
|
||||
field_expr = wrap_serialize_with(params, field.ty, path, field_expr)
|
||||
}
|
||||
|
||||
let ser = quote! {
|
||||
let ser = quote! {
|
||||
try!(#func(&mut __serde_state, #key_expr, #field_expr));
|
||||
};
|
||||
|
||||
match skip {
|
||||
None => ser,
|
||||
Some(skip) => quote!(if !#skip { #ser }),
|
||||
}
|
||||
})
|
||||
match skip {
|
||||
None => ser,
|
||||
Some(skip) => quote!(if !#skip { #ser }),
|
||||
}
|
||||
},
|
||||
)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn wrap_serialize_with(params: &Parameters,
|
||||
field_ty: &syn::Ty,
|
||||
serialize_with: &syn::Path,
|
||||
value: Tokens)
|
||||
-> Tokens {
|
||||
fn wrap_serialize_with(
|
||||
params: &Parameters,
|
||||
field_ty: &syn::Ty,
|
||||
serialize_with: &syn::Path,
|
||||
value: Tokens,
|
||||
) -> Tokens {
|
||||
let this = ¶ms.this;
|
||||
let (_, ty_generics, where_clause) = params.generics.split_for_impl();
|
||||
|
||||
@ -842,7 +893,8 @@ fn mut_if(is_mut: bool) -> Option<Tokens> {
|
||||
}
|
||||
|
||||
fn get_field<I>(params: &Parameters, field: &Field, ident: I) -> Tokens
|
||||
where I: Into<Ident>
|
||||
where
|
||||
I: Into<Ident>,
|
||||
{
|
||||
let self_var = ¶ms.self_var;
|
||||
match (params.is_remote, field.attrs.getter()) {
|
||||
|
@ -9,7 +9,8 @@ use std::fmt::Debug;
|
||||
|
||||
/// Runs both `assert_ser_tokens` and `assert_de_tokens`.
|
||||
pub fn assert_tokens<'de, T>(value: &T, tokens: &'de [Token])
|
||||
where T: Serialize + Deserialize<'de> + PartialEq + Debug
|
||||
where
|
||||
T: Serialize + Deserialize<'de> + PartialEq + Debug,
|
||||
{
|
||||
assert_ser_tokens(value, tokens);
|
||||
assert_de_tokens(value, tokens);
|
||||
@ -17,7 +18,8 @@ pub fn assert_tokens<'de, T>(value: &T, tokens: &'de [Token])
|
||||
|
||||
/// Asserts that `value` serializes to the given `tokens`.
|
||||
pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let mut ser = Serializer::new(tokens);
|
||||
assert_eq!(Serialize::serialize(value, &mut ser), Ok(()));
|
||||
@ -26,7 +28,8 @@ pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
||||
|
||||
/// Asserts that `value` serializes to the given `tokens`, and then yields `error`.
|
||||
pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: Error)
|
||||
where T: Serialize + PartialEq + Debug
|
||||
where
|
||||
T: Serialize + PartialEq + Debug,
|
||||
{
|
||||
let mut ser = Serializer::new(tokens);
|
||||
let v: Result<(), Error> = Serialize::serialize(value, &mut ser);
|
||||
@ -36,7 +39,8 @@ pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: Error)
|
||||
|
||||
/// Asserts that the given `tokens` deserialize into `value`.
|
||||
pub fn assert_de_tokens<'de, T>(value: &T, tokens: &'de [Token])
|
||||
where T: Deserialize<'de> + PartialEq + Debug
|
||||
where
|
||||
T: Deserialize<'de> + PartialEq + Debug,
|
||||
{
|
||||
let mut de = Deserializer::new(tokens);
|
||||
let v: Result<T, Error> = Deserialize::deserialize(&mut de);
|
||||
@ -46,7 +50,8 @@ pub fn assert_de_tokens<'de, T>(value: &T, tokens: &'de [Token])
|
||||
|
||||
/// Asserts that the given `tokens` yield `error` when deserializing.
|
||||
pub fn assert_de_tokens_error<'de, T>(tokens: &'de [Token], error: Error)
|
||||
where T: Deserialize<'de> + PartialEq + Debug
|
||||
where
|
||||
T: Deserialize<'de> + PartialEq + Debug,
|
||||
{
|
||||
let mut de = Deserializer::new(tokens);
|
||||
let v: Result<T, Error> = Deserialize::deserialize(&mut de);
|
||||
|
@ -1,5 +1,5 @@
|
||||
use serde::de::{self, Deserialize, DeserializeSeed, EnumVisitor, IntoDeserializer,
|
||||
MapVisitor, SeqVisitor, VariantVisitor, Visitor};
|
||||
use serde::de::{self, Deserialize, DeserializeSeed, EnumVisitor, IntoDeserializer, MapVisitor,
|
||||
SeqVisitor, VariantVisitor, Visitor};
|
||||
use serde::de::value::{MapVisitorDeserializer, SeqVisitorDeserializer};
|
||||
|
||||
use error::Error;
|
||||
@ -41,34 +41,46 @@ impl<'de> Deserializer<'de> {
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self,
|
||||
len: Option<usize>,
|
||||
end: Token,
|
||||
visitor: V)
|
||||
-> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
fn visit_seq<V>(
|
||||
&mut self,
|
||||
len: Option<usize>,
|
||||
end: Token,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
let value = try!(visitor.visit_seq(DeserializerSeqVisitor {
|
||||
de: self,
|
||||
len: len,
|
||||
end: end.clone(),
|
||||
}));
|
||||
let value = try!(
|
||||
visitor.visit_seq(
|
||||
DeserializerSeqVisitor {
|
||||
de: self,
|
||||
len: len,
|
||||
end: end.clone(),
|
||||
},
|
||||
)
|
||||
);
|
||||
try!(self.expect_token(end));
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self,
|
||||
len: Option<usize>,
|
||||
end: Token,
|
||||
visitor: V)
|
||||
-> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
fn visit_map<V>(
|
||||
&mut self,
|
||||
len: Option<usize>,
|
||||
end: Token,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
let value = try!(visitor.visit_map(DeserializerMapVisitor {
|
||||
de: self,
|
||||
len: len,
|
||||
end: end.clone(),
|
||||
}));
|
||||
let value = try!(
|
||||
visitor.visit_map(
|
||||
DeserializerMapVisitor {
|
||||
de: self,
|
||||
len: len,
|
||||
end: end.clone(),
|
||||
},
|
||||
)
|
||||
);
|
||||
try!(self.expect_token(end));
|
||||
Ok(value)
|
||||
}
|
||||
@ -83,7 +95,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
}
|
||||
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
let token = self.next_token().ok_or(Error::EndOfTokens)?;
|
||||
match token {
|
||||
@ -110,26 +123,12 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
Token::Unit => visitor.visit_unit(),
|
||||
Token::UnitStruct(_name) => visitor.visit_unit(),
|
||||
Token::NewtypeStruct(_name) => visitor.visit_newtype_struct(self),
|
||||
Token::Seq(len) => {
|
||||
self.visit_seq(len, Token::SeqEnd, visitor)
|
||||
}
|
||||
Token::SeqFixedSize(len) => {
|
||||
self.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||
}
|
||||
Token::Tuple(len) => {
|
||||
self.visit_seq(Some(len), Token::TupleEnd, visitor)
|
||||
}
|
||||
Token::TupleStruct(_, len) => {
|
||||
self.visit_seq(Some(len),
|
||||
Token::TupleStructEnd,
|
||||
visitor)
|
||||
}
|
||||
Token::Map(len) => {
|
||||
self.visit_map(len, Token::MapEnd, visitor)
|
||||
}
|
||||
Token::Struct(_, len) => {
|
||||
self.visit_map(Some(len), Token::StructEnd, visitor)
|
||||
}
|
||||
Token::Seq(len) => self.visit_seq(len, Token::SeqEnd, visitor),
|
||||
Token::SeqFixedSize(len) => self.visit_seq(Some(len), Token::SeqEnd, visitor),
|
||||
Token::Tuple(len) => self.visit_seq(Some(len), Token::TupleEnd, visitor),
|
||||
Token::TupleStruct(_, len) => self.visit_seq(Some(len), Token::TupleStructEnd, visitor),
|
||||
Token::Map(len) => self.visit_map(len, Token::MapEnd, visitor),
|
||||
Token::Struct(_, len) => self.visit_map(Some(len), Token::StructEnd, visitor),
|
||||
Token::Enum(_) => {
|
||||
let variant = self.next_token().ok_or(Error::EndOfTokens)?;
|
||||
let next = *self.tokens.first().ok_or(Error::EndOfTokens)?;
|
||||
@ -146,9 +145,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
self.next_token();
|
||||
visitor.visit_u32(variant)
|
||||
}
|
||||
(variant, Token::Unit) => {
|
||||
Err(Error::UnexpectedToken(variant))
|
||||
}
|
||||
(variant, Token::Unit) => Err(Error::UnexpectedToken(variant)),
|
||||
(variant, _) => {
|
||||
visitor.visit_map(EnumMapVisitor::new(self, variant, EnumFormat::Any))
|
||||
}
|
||||
@ -156,28 +153,26 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
}
|
||||
Token::UnitVariant(_, variant) => visitor.visit_str(variant),
|
||||
Token::NewtypeVariant(_, variant) => {
|
||||
visitor.visit_map(EnumMapVisitor::new(self, Token::Str(variant), EnumFormat::Any))
|
||||
visitor.visit_map(EnumMapVisitor::new(self, Token::Str(variant), EnumFormat::Any),)
|
||||
}
|
||||
Token::TupleVariant(_, variant, _) => {
|
||||
visitor.visit_map(EnumMapVisitor::new(self, Token::Str(variant), EnumFormat::Seq))
|
||||
visitor.visit_map(EnumMapVisitor::new(self, Token::Str(variant), EnumFormat::Seq),)
|
||||
}
|
||||
Token::StructVariant(_, variant, _) => {
|
||||
visitor.visit_map(EnumMapVisitor::new(self, Token::Str(variant), EnumFormat::Map))
|
||||
visitor.visit_map(EnumMapVisitor::new(self, Token::Str(variant), EnumFormat::Map),)
|
||||
}
|
||||
Token::SeqEnd | Token::TupleEnd | Token::TupleStructEnd | Token::MapEnd |
|
||||
Token::StructEnd | Token::TupleVariantEnd | Token::StructVariantEnd => {
|
||||
Err(Error::UnexpectedToken(token))
|
||||
}
|
||||
Token::SeqEnd |
|
||||
Token::TupleEnd |
|
||||
Token::TupleStructEnd |
|
||||
Token::MapEnd |
|
||||
Token::StructEnd |
|
||||
Token::TupleVariantEnd |
|
||||
Token::StructVariantEnd => Err(Error::UnexpectedToken(token)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Hook into `Option` deserializing so we can treat `Unit` as a
|
||||
/// `None`, or a regular value as `Some(value)`.
|
||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.tokens.first() {
|
||||
Some(&Token::Unit) |
|
||||
@ -194,12 +189,14 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(self,
|
||||
name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V)
|
||||
-> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.tokens.first() {
|
||||
Some(&Token::Enum(n)) if name == n => {
|
||||
@ -222,7 +219,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
}
|
||||
|
||||
fn deserialize_unit_struct<V>(self, name: &str, visitor: V) -> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.tokens.first() {
|
||||
Some(&Token::UnitStruct(n)) => {
|
||||
@ -239,7 +237,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
}
|
||||
|
||||
fn deserialize_newtype_struct<V>(self, name: &str, visitor: V) -> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.tokens.first() {
|
||||
Some(&Token::NewtypeStruct(n)) => {
|
||||
@ -256,7 +255,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
}
|
||||
|
||||
fn deserialize_seq_fixed_size<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.tokens.first() {
|
||||
Some(&Token::SeqFixedSize(_)) => {
|
||||
@ -269,7 +269,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
}
|
||||
|
||||
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.tokens.first() {
|
||||
Some(&Token::Unit) |
|
||||
@ -291,21 +292,21 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
}
|
||||
Some(&Token::TupleStruct(_, _)) => {
|
||||
self.next_token();
|
||||
self.visit_seq(Some(len),
|
||||
Token::TupleStructEnd,
|
||||
visitor)
|
||||
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
||||
}
|
||||
Some(_) => self.deserialize(visitor),
|
||||
None => Err(Error::EndOfTokens),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_tuple_struct<V>(self,
|
||||
name: &str,
|
||||
len: usize,
|
||||
visitor: V)
|
||||
-> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
fn deserialize_tuple_struct<V>(
|
||||
self,
|
||||
name: &str,
|
||||
len: usize,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.tokens.first() {
|
||||
Some(&Token::Unit) => {
|
||||
@ -335,9 +336,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
Some(&Token::TupleStruct(n, _)) => {
|
||||
self.next_token();
|
||||
if name == n {
|
||||
self.visit_seq(Some(len),
|
||||
Token::TupleStructEnd,
|
||||
visitor)
|
||||
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
||||
} else {
|
||||
Err(Error::InvalidName(n))
|
||||
}
|
||||
@ -347,20 +346,20 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_struct<V>(self,
|
||||
name: &str,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V)
|
||||
-> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
fn deserialize_struct<V>(
|
||||
self,
|
||||
name: &str,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.tokens.first() {
|
||||
Some(&Token::Struct(n, _)) => {
|
||||
self.next_token();
|
||||
if name == n {
|
||||
self.visit_map(Some(fields.len()),
|
||||
Token::StructEnd,
|
||||
visitor)
|
||||
self.visit_map(Some(fields.len()), Token::StructEnd, visitor)
|
||||
} else {
|
||||
Err(Error::InvalidName(n))
|
||||
}
|
||||
@ -387,7 +386,8 @@ impl<'de, 'a> SeqVisitor<'de> for DeserializerSeqVisitor<'a, 'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
|
||||
where T: DeserializeSeed<'de>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
if self.de.tokens.first() == Some(&self.end) {
|
||||
return Ok(None);
|
||||
@ -414,7 +414,8 @@ impl<'de, 'a> MapVisitor<'de> for DeserializerMapVisitor<'a, 'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
||||
where K: DeserializeSeed<'de>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
{
|
||||
if self.de.tokens.first() == Some(&self.end) {
|
||||
return Ok(None);
|
||||
@ -424,7 +425,8 @@ impl<'de, 'a> MapVisitor<'de> for DeserializerMapVisitor<'a, 'de> {
|
||||
}
|
||||
|
||||
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
|
||||
where V: DeserializeSeed<'de>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
seed.deserialize(&mut *self.de)
|
||||
}
|
||||
@ -446,7 +448,8 @@ impl<'de, 'a> EnumVisitor<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
type Variant = Self;
|
||||
|
||||
fn visit_variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Error>
|
||||
where V: DeserializeSeed<'de>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.de.tokens.first() {
|
||||
Some(&Token::UnitVariant(_, v)) |
|
||||
@ -481,7 +484,8 @@ impl<'de, 'a> VariantVisitor<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
}
|
||||
|
||||
fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
|
||||
where T: DeserializeSeed<'de>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.de.tokens.first() {
|
||||
Some(&Token::NewtypeVariant(_, _)) => {
|
||||
@ -494,14 +498,16 @@ impl<'de, 'a> VariantVisitor<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
}
|
||||
|
||||
fn visit_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.de.tokens.first() {
|
||||
Some(&Token::TupleVariant(_, _, enum_len)) => {
|
||||
let token = self.de.next_token().unwrap();
|
||||
|
||||
if len == enum_len {
|
||||
self.de.visit_seq(Some(len), Token::TupleVariantEnd, visitor)
|
||||
self.de
|
||||
.visit_seq(Some(len), Token::TupleVariantEnd, visitor)
|
||||
} else {
|
||||
Err(Error::UnexpectedToken(token))
|
||||
}
|
||||
@ -521,16 +527,16 @@ impl<'de, 'a> VariantVisitor<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
}
|
||||
|
||||
fn visit_struct<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value, Error>
|
||||
where V: Visitor<'de>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.de.tokens.first() {
|
||||
Some(&Token::StructVariant(_, _, enum_len)) => {
|
||||
let token = self.de.next_token().unwrap();
|
||||
|
||||
if fields.len() == enum_len {
|
||||
self.de.visit_map(Some(fields.len()),
|
||||
Token::StructVariantEnd,
|
||||
visitor)
|
||||
self.de
|
||||
.visit_map(Some(fields.len()), Token::StructVariantEnd, visitor)
|
||||
} else {
|
||||
Err(Error::UnexpectedToken(token))
|
||||
}
|
||||
@ -539,7 +545,8 @@ impl<'de, 'a> VariantVisitor<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
let token = self.de.next_token().unwrap();
|
||||
|
||||
if fields.len() == enum_len {
|
||||
self.de.visit_map(Some(fields.len()), Token::MapEnd, visitor)
|
||||
self.de
|
||||
.visit_map(Some(fields.len()), Token::MapEnd, visitor)
|
||||
} else {
|
||||
Err(Error::UnexpectedToken(token))
|
||||
}
|
||||
@ -578,27 +585,24 @@ impl<'de, 'a> MapVisitor<'de> for EnumMapVisitor<'a, 'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
||||
where K: DeserializeSeed<'de>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.variant.take() {
|
||||
Some(Token::Str(variant)) => {
|
||||
seed.deserialize(variant.into_deserializer()).map(Some)
|
||||
}
|
||||
Some(Token::Str(variant)) => seed.deserialize(variant.into_deserializer()).map(Some),
|
||||
Some(Token::Bytes(variant)) => {
|
||||
seed.deserialize(BytesDeserializer { value: variant }).map(Some)
|
||||
}
|
||||
Some(Token::U32(variant)) => {
|
||||
seed.deserialize(variant.into_deserializer()).map(Some)
|
||||
}
|
||||
Some(other) => {
|
||||
Err(Error::UnexpectedToken(other))
|
||||
seed.deserialize(BytesDeserializer { value: variant })
|
||||
.map(Some)
|
||||
}
|
||||
Some(Token::U32(variant)) => seed.deserialize(variant.into_deserializer()).map(Some),
|
||||
Some(other) => Err(Error::UnexpectedToken(other)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
|
||||
where V: DeserializeSeed<'de>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.format {
|
||||
EnumFormat::Seq => {
|
||||
@ -638,7 +642,8 @@ impl<'de> de::Deserializer<'de> for BytesDeserializer {
|
||||
type Error = Error;
|
||||
|
||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor<'de>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_bytes(self.value)
|
||||
}
|
||||
|
@ -12,9 +12,7 @@ pub struct Serializer<'a> {
|
||||
impl<'a> Serializer<'a> {
|
||||
/// Creates the serializer.
|
||||
pub fn new(tokens: &'a [Token]) -> Self {
|
||||
Serializer {
|
||||
tokens: tokens,
|
||||
}
|
||||
Serializer { tokens: tokens }
|
||||
}
|
||||
|
||||
/// Pulls the next token off of the serializer, ignoring it.
|
||||
@ -145,11 +143,12 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str)
|
||||
-> Result<(), Error> {
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
) -> Result<(), Error> {
|
||||
if self.tokens.first() == Some(&Token::Enum(name)) {
|
||||
self.next_token();
|
||||
assert_next_token!(self, Str(variant));
|
||||
@ -161,19 +160,22 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T: ?Sized>(self, name: &'static str, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
assert_next_token!(self, NewtypeStruct(name));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized>(self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T)
|
||||
-> Result<(), Error>
|
||||
where T: Serialize
|
||||
fn serialize_newtype_variant<T: ?Sized>(
|
||||
self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
if self.tokens.first() == Some(&Token::Enum(name)) {
|
||||
self.next_token();
|
||||
@ -190,7 +192,8 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
}
|
||||
|
||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
assert_next_token!(self, Some);
|
||||
value.serialize(self)
|
||||
@ -216,12 +219,13 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self, Error> {
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self, Error> {
|
||||
assert_next_token!(self, TupleVariant(name, variant, len));
|
||||
Ok(self)
|
||||
}
|
||||
@ -236,12 +240,13 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self, Error> {
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self, Error> {
|
||||
assert_next_token!(self, StructVariant(name, variant, len));
|
||||
Ok(self)
|
||||
}
|
||||
@ -252,7 +257,8 @@ impl<'s, 'a> ser::SerializeSeq for &'s mut Serializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
@ -268,7 +274,8 @@ impl<'s, 'a> ser::SerializeTuple for &'s mut Serializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
@ -284,7 +291,8 @@ impl<'s, 'a> ser::SerializeTupleStruct for &'s mut Serializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
@ -300,7 +308,8 @@ impl<'s, 'a> ser::SerializeTupleVariant for &'s mut Serializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
@ -316,13 +325,15 @@ impl<'s, 'a> ser::SerializeMap for &'s mut Serializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
key.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
@ -337,11 +348,13 @@ impl<'s, 'a> ser::SerializeStruct for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self,
|
||||
key: &'static str,
|
||||
value: &T)
|
||||
-> Result<(), Self::Error>
|
||||
where T: Serialize
|
||||
fn serialize_field<T: ?Sized>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
try!(key.serialize(&mut **self));
|
||||
value.serialize(&mut **self)
|
||||
@ -357,11 +370,13 @@ impl<'s, 'a> ser::SerializeStructVariant for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self,
|
||||
key: &'static str,
|
||||
value: &T)
|
||||
-> Result<(), Self::Error>
|
||||
where T: Serialize
|
||||
fn serialize_field<T: ?Sized>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
try!(key.serialize(&mut **self));
|
||||
value.serialize(&mut **self)
|
||||
|
@ -5,14 +5,8 @@ extern crate serde;
|
||||
use self::serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||
|
||||
extern crate serde_test;
|
||||
use self::serde_test::{
|
||||
Error,
|
||||
Token,
|
||||
assert_tokens,
|
||||
assert_ser_tokens,
|
||||
assert_de_tokens,
|
||||
assert_de_tokens_error
|
||||
};
|
||||
use self::serde_test::{Error, Token, assert_tokens, assert_ser_tokens, assert_de_tokens,
|
||||
assert_de_tokens_error};
|
||||
|
||||
trait MyDefault: Sized {
|
||||
fn my_default() -> Self;
|
||||
@ -24,25 +18,32 @@ trait ShouldSkip: Sized {
|
||||
|
||||
trait SerializeWith: Sized {
|
||||
fn serialize_with<S>(&self, ser: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer;
|
||||
where
|
||||
S: Serializer;
|
||||
}
|
||||
|
||||
trait DeserializeWith: Sized {
|
||||
fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>;
|
||||
where
|
||||
D: Deserializer<'de>;
|
||||
}
|
||||
|
||||
impl MyDefault for i32 {
|
||||
fn my_default() -> Self { 123 }
|
||||
fn my_default() -> Self {
|
||||
123
|
||||
}
|
||||
}
|
||||
|
||||
impl ShouldSkip for i32 {
|
||||
fn should_skip(&self) -> bool { *self == 123 }
|
||||
fn should_skip(&self) -> bool {
|
||||
*self == 123
|
||||
}
|
||||
}
|
||||
|
||||
impl SerializeWith for i32 {
|
||||
fn serialize_with<S>(&self, ser: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
if *self == 123 {
|
||||
true.serialize(ser)
|
||||
@ -54,7 +55,8 @@ impl SerializeWith for i32 {
|
||||
|
||||
impl DeserializeWith for i32 {
|
||||
fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
if try!(Deserialize::deserialize(de)) {
|
||||
Ok(123)
|
||||
@ -66,8 +68,9 @@ impl DeserializeWith for i32 {
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct DefaultStruct<A, B, C, D, E>
|
||||
where C: MyDefault,
|
||||
E: MyDefault,
|
||||
where
|
||||
C: MyDefault,
|
||||
E: MyDefault,
|
||||
{
|
||||
a1: A,
|
||||
#[serde(default)]
|
||||
@ -83,7 +86,13 @@ struct DefaultStruct<A, B, C, D, E>
|
||||
#[test]
|
||||
fn test_default_struct() {
|
||||
assert_de_tokens(
|
||||
&DefaultStruct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 },
|
||||
&DefaultStruct {
|
||||
a1: 1,
|
||||
a2: 2,
|
||||
a3: 3,
|
||||
a4: 0,
|
||||
a5: 123,
|
||||
},
|
||||
&[
|
||||
Token::Struct("DefaultStruct", 3),
|
||||
|
||||
@ -103,11 +112,17 @@ fn test_default_struct() {
|
||||
Token::I32(5),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&DefaultStruct { a1: 1, a2: 0, a3: 123, a4: 0, a5: 123 },
|
||||
&DefaultStruct {
|
||||
a1: 1,
|
||||
a2: 0,
|
||||
a3: 123,
|
||||
a4: 0,
|
||||
a5: 123,
|
||||
},
|
||||
&[
|
||||
Token::Struct("DefaultStruct", 1),
|
||||
|
||||
@ -115,14 +130,15 @@ fn test_default_struct() {
|
||||
Token::I32(1),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
enum DefaultEnum<A, B, C, D, E>
|
||||
where C: MyDefault,
|
||||
E: MyDefault
|
||||
where
|
||||
C: MyDefault,
|
||||
E: MyDefault,
|
||||
{
|
||||
Struct {
|
||||
a1: A,
|
||||
@ -134,13 +150,19 @@ enum DefaultEnum<A, B, C, D, E>
|
||||
a4: D,
|
||||
#[serde(skip_deserializing, default="MyDefault::my_default")]
|
||||
a5: E,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_enum() {
|
||||
assert_de_tokens(
|
||||
&DefaultEnum::Struct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 },
|
||||
&DefaultEnum::Struct {
|
||||
a1: 1,
|
||||
a2: 2,
|
||||
a3: 3,
|
||||
a4: 0,
|
||||
a5: 123,
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("DefaultEnum", "Struct", 3),
|
||||
|
||||
@ -160,11 +182,17 @@ fn test_default_enum() {
|
||||
Token::I32(5),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&DefaultEnum::Struct { a1: 1, a2: 0, a3: 123, a4: 0, a5: 123 },
|
||||
&DefaultEnum::Struct {
|
||||
a1: 1,
|
||||
a2: 0,
|
||||
a3: 123,
|
||||
a4: 0,
|
||||
a5: 123,
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("DefaultEnum", "Struct", 3),
|
||||
|
||||
@ -172,7 +200,7 @@ fn test_default_enum() {
|
||||
Token::I32(1),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -198,10 +226,7 @@ struct ContainsNoStdDefault<A: MyDefault> {
|
||||
fn test_no_std_default() {
|
||||
assert_de_tokens(
|
||||
&ContainsNoStdDefault { a: NoStdDefault(123) },
|
||||
&[
|
||||
Token::Struct("ContainsNoStdDefault", 1),
|
||||
Token::StructEnd,
|
||||
]
|
||||
&[Token::Struct("ContainsNoStdDefault", 1), Token::StructEnd],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
@ -214,7 +239,7 @@ fn test_no_std_default() {
|
||||
Token::I8(8),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -230,7 +255,8 @@ impl Default for NotDeserializeStruct {
|
||||
|
||||
impl DeserializeWith for NotDeserializeStruct {
|
||||
fn deserialize_with<'de, D>(_: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
panic!()
|
||||
}
|
||||
@ -238,7 +264,9 @@ impl DeserializeWith for NotDeserializeStruct {
|
||||
|
||||
// Does not implement Deserialize.
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum NotDeserializeEnum { Trouble }
|
||||
enum NotDeserializeEnum {
|
||||
Trouble,
|
||||
}
|
||||
|
||||
impl MyDefault for NotDeserializeEnum {
|
||||
fn my_default() -> Self {
|
||||
@ -265,15 +293,12 @@ struct ContainsNotDeserialize<A, B, C: DeserializeWith, E: MyDefault> {
|
||||
fn test_elt_not_deserialize() {
|
||||
assert_de_tokens(
|
||||
&ContainsNotDeserialize {
|
||||
a: NotDeserializeStruct(123),
|
||||
b: NotDeserializeStruct(123),
|
||||
c: NotDeserializeStruct(123),
|
||||
e: NotDeserializeEnum::Trouble,
|
||||
},
|
||||
&[
|
||||
Token::Struct("ContainsNotDeserialize", 3),
|
||||
Token::StructEnd,
|
||||
]
|
||||
a: NotDeserializeStruct(123),
|
||||
b: NotDeserializeStruct(123),
|
||||
c: NotDeserializeStruct(123),
|
||||
e: NotDeserializeEnum::Trouble,
|
||||
},
|
||||
&[Token::Struct("ContainsNotDeserialize", 3), Token::StructEnd],
|
||||
);
|
||||
}
|
||||
|
||||
@ -287,7 +312,13 @@ struct DenyUnknown {
|
||||
fn test_ignore_unknown() {
|
||||
// 'Default' allows unknown. Basic smoke test of ignore...
|
||||
assert_de_tokens(
|
||||
&DefaultStruct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 },
|
||||
&DefaultStruct {
|
||||
a1: 1,
|
||||
a2: 2,
|
||||
a3: 3,
|
||||
a4: 0,
|
||||
a5: 123,
|
||||
},
|
||||
&[
|
||||
Token::Struct("DefaultStruct", 5),
|
||||
|
||||
@ -312,7 +343,7 @@ fn test_ignore_unknown() {
|
||||
Token::I32(3),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<DenyUnknown>(
|
||||
@ -324,7 +355,7 @@ fn test_ignore_unknown() {
|
||||
|
||||
Token::Str("whoops"),
|
||||
],
|
||||
Error::Message("unknown field `whoops`, expected `a1`".to_owned())
|
||||
Error::Message("unknown field `whoops`, expected `a1`".to_owned()),
|
||||
);
|
||||
}
|
||||
|
||||
@ -358,7 +389,7 @@ fn test_rename_struct() {
|
||||
Token::I32(2),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
@ -373,7 +404,7 @@ fn test_rename_struct() {
|
||||
Token::I32(2),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
@ -388,7 +419,7 @@ fn test_rename_struct() {
|
||||
Token::I32(2),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -424,9 +455,7 @@ enum RenameEnumSerializeDeserialize<A> {
|
||||
fn test_rename_enum() {
|
||||
assert_tokens(
|
||||
&RenameEnum::Batman,
|
||||
&[
|
||||
Token::UnitVariant("Superhero", "bruce_wayne"),
|
||||
]
|
||||
&[Token::UnitVariant("Superhero", "bruce_wayne")],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -434,7 +463,7 @@ fn test_rename_enum() {
|
||||
&[
|
||||
Token::NewtypeVariant("Superhero", "clark_kent"),
|
||||
Token::I8(0),
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -444,7 +473,7 @@ fn test_rename_enum() {
|
||||
Token::I8(0),
|
||||
Token::I8(1),
|
||||
Token::TupleVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -456,14 +485,14 @@ fn test_rename_enum() {
|
||||
Token::I32(1),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
&RenameEnumSerializeDeserialize::Robin {
|
||||
a: 0,
|
||||
b: String::new(),
|
||||
},
|
||||
a: 0,
|
||||
b: String::new(),
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("SuperheroSer", "dick_grayson", 2),
|
||||
|
||||
@ -474,14 +503,14 @@ fn test_rename_enum() {
|
||||
Token::Str(""),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&RenameEnumSerializeDeserialize::Robin {
|
||||
a: 0,
|
||||
b: String::new(),
|
||||
},
|
||||
a: 0,
|
||||
b: String::new(),
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("SuperheroDe", "jason_todd", 2),
|
||||
|
||||
@ -492,12 +521,15 @@ fn test_rename_enum() {
|
||||
Token::Str(""),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
struct SkipSerializingStruct<'a, B, C> where C: ShouldSkip {
|
||||
struct SkipSerializingStruct<'a, B, C>
|
||||
where
|
||||
C: ShouldSkip,
|
||||
{
|
||||
a: &'a i8,
|
||||
#[serde(skip_serializing)]
|
||||
b: B,
|
||||
@ -509,11 +541,7 @@ struct SkipSerializingStruct<'a, B, C> where C: ShouldSkip {
|
||||
fn test_skip_serializing_struct() {
|
||||
let a = 1;
|
||||
assert_ser_tokens(
|
||||
&SkipSerializingStruct {
|
||||
a: &a,
|
||||
b: 2,
|
||||
c: 3,
|
||||
},
|
||||
&SkipSerializingStruct { a: &a, b: 2, c: 3 },
|
||||
&[
|
||||
Token::Struct("SkipSerializingStruct", 2),
|
||||
|
||||
@ -524,15 +552,15 @@ fn test_skip_serializing_struct() {
|
||||
Token::I32(3),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
&SkipSerializingStruct {
|
||||
a: &a,
|
||||
b: 2,
|
||||
c: 123,
|
||||
},
|
||||
a: &a,
|
||||
b: 2,
|
||||
c: 123,
|
||||
},
|
||||
&[
|
||||
Token::Struct("SkipSerializingStruct", 1),
|
||||
|
||||
@ -540,30 +568,29 @@ fn test_skip_serializing_struct() {
|
||||
Token::I8(1),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
enum SkipSerializingEnum<'a, B, C> where C: ShouldSkip {
|
||||
enum SkipSerializingEnum<'a, B, C>
|
||||
where
|
||||
C: ShouldSkip,
|
||||
{
|
||||
Struct {
|
||||
a: &'a i8,
|
||||
#[serde(skip_serializing)]
|
||||
_b: B,
|
||||
#[serde(skip_serializing_if="ShouldSkip::should_skip")]
|
||||
c: C,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_skip_serializing_enum() {
|
||||
let a = 1;
|
||||
assert_ser_tokens(
|
||||
&SkipSerializingEnum::Struct {
|
||||
a: &a,
|
||||
_b: 2,
|
||||
c: 3,
|
||||
},
|
||||
&SkipSerializingEnum::Struct { a: &a, _b: 2, c: 3 },
|
||||
&[
|
||||
Token::StructVariant("SkipSerializingEnum", "Struct", 2),
|
||||
|
||||
@ -574,15 +601,15 @@ fn test_skip_serializing_enum() {
|
||||
Token::I32(3),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
&SkipSerializingEnum::Struct {
|
||||
a: &a,
|
||||
_b: 2,
|
||||
c: 123,
|
||||
},
|
||||
a: &a,
|
||||
_b: 2,
|
||||
c: 123,
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("SkipSerializingEnum", "Struct", 1),
|
||||
|
||||
@ -590,7 +617,7 @@ fn test_skip_serializing_enum() {
|
||||
Token::I8(1),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -598,18 +625,25 @@ fn test_skip_serializing_enum() {
|
||||
struct NotSerializeStruct(i8);
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum NotSerializeEnum { Trouble }
|
||||
enum NotSerializeEnum {
|
||||
Trouble,
|
||||
}
|
||||
|
||||
impl SerializeWith for NotSerializeEnum {
|
||||
fn serialize_with<S>(&self, ser: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
"trouble".serialize(ser)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
struct ContainsNotSerialize<'a, B, C, D> where B: 'a, D: SerializeWith {
|
||||
struct ContainsNotSerialize<'a, B, C, D>
|
||||
where
|
||||
B: 'a,
|
||||
D: SerializeWith,
|
||||
{
|
||||
a: &'a Option<i8>,
|
||||
#[serde(skip_serializing)]
|
||||
b: &'a B,
|
||||
@ -624,11 +658,11 @@ fn test_elt_not_serialize() {
|
||||
let a = 1;
|
||||
assert_ser_tokens(
|
||||
&ContainsNotSerialize {
|
||||
a: &Some(a),
|
||||
b: &NotSerializeStruct(2),
|
||||
c: Some(NotSerializeEnum::Trouble),
|
||||
d: NotSerializeEnum::Trouble,
|
||||
},
|
||||
a: &Some(a),
|
||||
b: &NotSerializeStruct(2),
|
||||
c: Some(NotSerializeEnum::Trouble),
|
||||
d: NotSerializeEnum::Trouble,
|
||||
},
|
||||
&[
|
||||
Token::Struct("ContainsNotSerialize", 2),
|
||||
|
||||
@ -640,12 +674,15 @@ fn test_elt_not_serialize() {
|
||||
Token::Str("trouble"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
struct SerializeWithStruct<'a, B> where B: SerializeWith {
|
||||
struct SerializeWithStruct<'a, B>
|
||||
where
|
||||
B: SerializeWith,
|
||||
{
|
||||
a: &'a i8,
|
||||
#[serde(serialize_with="SerializeWith::serialize_with")]
|
||||
b: B,
|
||||
@ -655,10 +692,7 @@ struct SerializeWithStruct<'a, B> where B: SerializeWith {
|
||||
fn test_serialize_with_struct() {
|
||||
let a = 1;
|
||||
assert_ser_tokens(
|
||||
&SerializeWithStruct {
|
||||
a: &a,
|
||||
b: 2,
|
||||
},
|
||||
&SerializeWithStruct { a: &a, b: 2 },
|
||||
&[
|
||||
Token::Struct("SerializeWithStruct", 2),
|
||||
|
||||
@ -669,14 +703,11 @@ fn test_serialize_with_struct() {
|
||||
Token::Bool(false),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
&SerializeWithStruct {
|
||||
a: &a,
|
||||
b: 123,
|
||||
},
|
||||
&SerializeWithStruct { a: &a, b: 123 },
|
||||
&[
|
||||
Token::Struct("SerializeWithStruct", 2),
|
||||
|
||||
@ -687,27 +718,27 @@ fn test_serialize_with_struct() {
|
||||
Token::Bool(true),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
enum SerializeWithEnum<'a, B> where B: SerializeWith {
|
||||
enum SerializeWithEnum<'a, B>
|
||||
where
|
||||
B: SerializeWith,
|
||||
{
|
||||
Struct {
|
||||
a: &'a i8,
|
||||
#[serde(serialize_with="SerializeWith::serialize_with")]
|
||||
b: B,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_with_enum() {
|
||||
let a = 1;
|
||||
assert_ser_tokens(
|
||||
&SerializeWithEnum::Struct {
|
||||
a: &a,
|
||||
b: 2,
|
||||
},
|
||||
&SerializeWithEnum::Struct { a: &a, b: 2 },
|
||||
&[
|
||||
Token::StructVariant("SerializeWithEnum", "Struct", 2),
|
||||
|
||||
@ -718,14 +749,11 @@ fn test_serialize_with_enum() {
|
||||
Token::Bool(false),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
&SerializeWithEnum::Struct {
|
||||
a: &a,
|
||||
b: 123,
|
||||
},
|
||||
&SerializeWithEnum::Struct { a: &a, b: 123 },
|
||||
&[
|
||||
Token::StructVariant("SerializeWithEnum", "Struct", 2),
|
||||
|
||||
@ -736,12 +764,15 @@ fn test_serialize_with_enum() {
|
||||
Token::Bool(true),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct DeserializeWithStruct<B> where B: DeserializeWith {
|
||||
struct DeserializeWithStruct<B>
|
||||
where
|
||||
B: DeserializeWith,
|
||||
{
|
||||
a: i8,
|
||||
#[serde(deserialize_with="DeserializeWith::deserialize_with")]
|
||||
b: B,
|
||||
@ -750,10 +781,7 @@ struct DeserializeWithStruct<B> where B: DeserializeWith {
|
||||
#[test]
|
||||
fn test_deserialize_with_struct() {
|
||||
assert_de_tokens(
|
||||
&DeserializeWithStruct {
|
||||
a: 1,
|
||||
b: 2,
|
||||
},
|
||||
&DeserializeWithStruct { a: 1, b: 2 },
|
||||
&[
|
||||
Token::Struct("DeserializeWithStruct", 2),
|
||||
|
||||
@ -764,14 +792,11 @@ fn test_deserialize_with_struct() {
|
||||
Token::Bool(false),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&DeserializeWithStruct {
|
||||
a: 1,
|
||||
b: 123,
|
||||
},
|
||||
&DeserializeWithStruct { a: 1, b: 123 },
|
||||
&[
|
||||
Token::Struct("DeserializeWithStruct", 2),
|
||||
|
||||
@ -782,26 +807,26 @@ fn test_deserialize_with_struct() {
|
||||
Token::Bool(true),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
enum DeserializeWithEnum<B> where B: DeserializeWith {
|
||||
enum DeserializeWithEnum<B>
|
||||
where
|
||||
B: DeserializeWith,
|
||||
{
|
||||
Struct {
|
||||
a: i8,
|
||||
#[serde(deserialize_with="DeserializeWith::deserialize_with")]
|
||||
b: B,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_with_enum() {
|
||||
assert_de_tokens(
|
||||
&DeserializeWithEnum::Struct {
|
||||
a: 1,
|
||||
b: 2,
|
||||
},
|
||||
&DeserializeWithEnum::Struct { a: 1, b: 2 },
|
||||
&[
|
||||
Token::StructVariant("DeserializeWithEnum", "Struct", 2),
|
||||
|
||||
@ -812,14 +837,11 @@ fn test_deserialize_with_enum() {
|
||||
Token::Bool(false),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&DeserializeWithEnum::Struct {
|
||||
a: 1,
|
||||
b: 123,
|
||||
},
|
||||
&DeserializeWithEnum::Struct { a: 1, b: 123 },
|
||||
&[
|
||||
Token::StructVariant("DeserializeWithEnum", "Struct", 2),
|
||||
|
||||
@ -830,7 +852,7 @@ fn test_deserialize_with_enum() {
|
||||
Token::Bool(true),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -888,7 +910,12 @@ fn test_missing_renamed_field_enum() {
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
enum InvalidLengthEnum {
|
||||
A(i32, i32, i32),
|
||||
B(#[serde(skip_deserializing)] i32, i32, i32),
|
||||
B(
|
||||
#[serde(skip_deserializing)]
|
||||
i32,
|
||||
i32,
|
||||
i32
|
||||
),
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -918,7 +945,7 @@ struct StructFromEnum(Option<u32>);
|
||||
impl Into<EnumToU32> for StructFromEnum {
|
||||
fn into(self) -> EnumToU32 {
|
||||
match self {
|
||||
StructFromEnum(v) => v.into()
|
||||
StructFromEnum(v) => v.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -936,7 +963,7 @@ enum EnumToU32 {
|
||||
Two,
|
||||
Three,
|
||||
Four,
|
||||
Nothing
|
||||
Nothing,
|
||||
}
|
||||
|
||||
impl Into<Option<u32>> for EnumToU32 {
|
||||
@ -946,7 +973,7 @@ impl Into<Option<u32>> for EnumToU32 {
|
||||
EnumToU32::Two => Some(2),
|
||||
EnumToU32::Three => Some(3),
|
||||
EnumToU32::Four => Some(4),
|
||||
EnumToU32::Nothing => None
|
||||
EnumToU32::Nothing => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -958,35 +985,17 @@ impl From<Option<u32>> for EnumToU32 {
|
||||
Some(2) => EnumToU32::Two,
|
||||
Some(3) => EnumToU32::Three,
|
||||
Some(4) => EnumToU32::Four,
|
||||
_ => EnumToU32::Nothing
|
||||
_ => EnumToU32::Nothing,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_into_traits() {
|
||||
assert_ser_tokens::<EnumToU32>(&EnumToU32::One,
|
||||
&[Token::Some,
|
||||
Token::U32(1)
|
||||
]
|
||||
);
|
||||
assert_ser_tokens::<EnumToU32>(&EnumToU32::Nothing,
|
||||
&[Token::None]
|
||||
);
|
||||
assert_de_tokens::<EnumToU32>(&EnumToU32::Two,
|
||||
&[Token::Some,
|
||||
Token::U32(2)
|
||||
]
|
||||
);
|
||||
assert_ser_tokens::<StructFromEnum>(&StructFromEnum(Some(5)),
|
||||
&[Token::None]
|
||||
);
|
||||
assert_ser_tokens::<StructFromEnum>(&StructFromEnum(None),
|
||||
&[Token::None]
|
||||
);
|
||||
assert_de_tokens::<StructFromEnum>(&StructFromEnum(Some(2)),
|
||||
&[Token::Some,
|
||||
Token::U32(2)
|
||||
]
|
||||
);
|
||||
assert_ser_tokens::<EnumToU32>(&EnumToU32::One, &[Token::Some, Token::U32(1)]);
|
||||
assert_ser_tokens::<EnumToU32>(&EnumToU32::Nothing, &[Token::None]);
|
||||
assert_de_tokens::<EnumToU32>(&EnumToU32::Two, &[Token::Some, Token::U32(2)]);
|
||||
assert_ser_tokens::<StructFromEnum>(&StructFromEnum(Some(5)), &[Token::None]);
|
||||
assert_ser_tokens::<StructFromEnum>(&StructFromEnum(None), &[Token::None]);
|
||||
assert_de_tokens::<StructFromEnum>(&StructFromEnum(Some(2)), &[Token::Some, Token::U32(2)]);
|
||||
}
|
||||
|
@ -5,82 +5,54 @@ extern crate serde;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
||||
extern crate serde_test;
|
||||
use serde_test::{
|
||||
Error,
|
||||
Token,
|
||||
assert_de_tokens,
|
||||
assert_de_tokens_error,
|
||||
};
|
||||
use serde_test::{Error, Token, assert_de_tokens, assert_de_tokens_error};
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[test]
|
||||
fn test_borrowed_str() {
|
||||
assert_de_tokens(
|
||||
&"borrowed",
|
||||
&[
|
||||
Token::BorrowedStr("borrowed"),
|
||||
]
|
||||
);
|
||||
assert_de_tokens(&"borrowed", &[Token::BorrowedStr("borrowed")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_borrowed_str_from_string() {
|
||||
assert_de_tokens_error::<&str>(
|
||||
&[
|
||||
Token::String("borrowed"),
|
||||
],
|
||||
Error::Message("invalid type: string \"borrowed\", expected a borrowed string".to_owned()),
|
||||
&[Token::String("borrowed")],
|
||||
Error::Message("invalid type: string \"borrowed\", expected a borrowed string".to_owned(),),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_borrowed_str_from_str() {
|
||||
assert_de_tokens_error::<&str>(
|
||||
&[
|
||||
Token::Str("borrowed"),
|
||||
],
|
||||
Error::Message("invalid type: string \"borrowed\", expected a borrowed string".to_owned()),
|
||||
&[Token::Str("borrowed")],
|
||||
Error::Message("invalid type: string \"borrowed\", expected a borrowed string".to_owned(),),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_string_from_borrowed_str() {
|
||||
assert_de_tokens(
|
||||
&"owned".to_owned(),
|
||||
&[
|
||||
Token::BorrowedStr("owned"),
|
||||
]
|
||||
);
|
||||
assert_de_tokens(&"owned".to_owned(), &[Token::BorrowedStr("owned")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_borrowed_bytes() {
|
||||
assert_de_tokens(
|
||||
&&b"borrowed"[..],
|
||||
&[
|
||||
Token::BorrowedBytes(b"borrowed"),
|
||||
]
|
||||
);
|
||||
assert_de_tokens(&&b"borrowed"[..], &[Token::BorrowedBytes(b"borrowed")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_borrowed_bytes_from_bytebuf() {
|
||||
assert_de_tokens_error::<&[u8]>(
|
||||
&[
|
||||
Token::ByteBuf(b"borrowed"),
|
||||
],
|
||||
Error::Message("invalid type: byte array, expected a borrowed byte array".to_owned()),
|
||||
&[Token::ByteBuf(b"borrowed")],
|
||||
Error::Message("invalid type: byte array, expected a borrowed byte array".to_owned(),),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_borrowed_bytes_from_bytes() {
|
||||
assert_de_tokens_error::<&[u8]>(
|
||||
&[
|
||||
Token::Bytes(b"borrowed"),
|
||||
],
|
||||
Error::Message("invalid type: byte array, expected a borrowed byte array".to_owned()),
|
||||
&[Token::Bytes(b"borrowed")],
|
||||
Error::Message("invalid type: byte array, expected a borrowed byte array".to_owned(),),
|
||||
);
|
||||
}
|
||||
|
||||
@ -93,7 +65,7 @@ fn test_tuple() {
|
||||
Token::BorrowedStr("str"),
|
||||
Token::BorrowedBytes(b"bytes"),
|
||||
Token::TupleEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -106,7 +78,10 @@ fn test_struct() {
|
||||
}
|
||||
|
||||
assert_de_tokens(
|
||||
&Borrowing { bs: "str", bb: b"bytes" },
|
||||
&Borrowing {
|
||||
bs: "str",
|
||||
bb: b"bytes",
|
||||
},
|
||||
&[
|
||||
Token::Struct("Borrowing", 2),
|
||||
|
||||
@ -117,7 +92,7 @@ fn test_struct() {
|
||||
Token::BorrowedBytes(b"bytes"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -170,7 +145,8 @@ fn test_lifetimes() {
|
||||
|
||||
// Tests that `'de: 'a` is not required by the Deserialize impl.
|
||||
fn _cows_lifetimes<'de: 'b, 'a, 'b, D>(deserializer: D) -> Cows<'a, 'b>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
Deserialize::deserialize(deserializer).unwrap()
|
||||
}
|
||||
@ -183,7 +159,8 @@ fn test_lifetimes() {
|
||||
|
||||
// Tests that `'de: 'a` is not required by the Deserialize impl.
|
||||
fn _wrap_lifetimes<'de: 'b, 'a, 'b, D>(deserializer: D) -> Wrap<'a, 'b>
|
||||
where D: Deserializer<'de>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
Deserialize::deserialize(deserializer).unwrap()
|
||||
}
|
||||
|
@ -20,12 +20,7 @@ extern crate fnv;
|
||||
use self::fnv::FnvHasher;
|
||||
|
||||
extern crate serde_test;
|
||||
use self::serde_test::{
|
||||
Error,
|
||||
Token,
|
||||
assert_de_tokens,
|
||||
assert_de_tokens_error,
|
||||
};
|
||||
use self::serde_test::{Error, Token, assert_de_tokens, assert_de_tokens_error};
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
@ -146,23 +141,21 @@ fn assert_de_tokens_ignore(ignorable_tokens: &[Token]) {
|
||||
a: i32,
|
||||
}
|
||||
|
||||
let expected = IgnoreBase{a: 1};
|
||||
let expected = IgnoreBase { a: 1 };
|
||||
|
||||
// Embed the tokens to be ignored in the normal token
|
||||
// stream for an IgnoreBase type
|
||||
let concated_tokens : Vec<Token> = vec![
|
||||
Token::Map(Some(2)),
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
let concated_tokens: Vec<Token> = vec![
|
||||
Token::Map(Some(2)),
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::Str("ignored")
|
||||
]
|
||||
.into_iter()
|
||||
.chain(ignorable_tokens.to_vec().into_iter())
|
||||
.chain(vec![
|
||||
Token::MapEnd,
|
||||
].into_iter())
|
||||
.collect();
|
||||
Token::Str("ignored"),
|
||||
]
|
||||
.into_iter()
|
||||
.chain(ignorable_tokens.to_vec().into_iter())
|
||||
.chain(vec![Token::MapEnd].into_iter())
|
||||
.collect();
|
||||
|
||||
let mut de = serde_test::Deserializer::new(&concated_tokens);
|
||||
let v: Result<IgnoreBase, Error> = Deserialize::deserialize(&mut de);
|
||||
@ -735,9 +728,9 @@ fn test_osstring() {
|
||||
Token::Enum("OsString"),
|
||||
Token::Str("Unix"),
|
||||
Token::Seq(Some(2)),
|
||||
Token::U8(1),
|
||||
Token::U8(2),
|
||||
Token::U8(3),
|
||||
Token::U8(1),
|
||||
Token::U8(2),
|
||||
Token::U8(3),
|
||||
Token::SeqEnd,
|
||||
];
|
||||
|
||||
@ -755,9 +748,9 @@ fn test_osstring() {
|
||||
Token::Enum("OsString"),
|
||||
Token::Str("Windows"),
|
||||
Token::Seq(Some(2)),
|
||||
Token::U16(1),
|
||||
Token::U16(2),
|
||||
Token::U16(3),
|
||||
Token::U16(1),
|
||||
Token::U16(2),
|
||||
Token::U16(3),
|
||||
Token::SeqEnd,
|
||||
];
|
||||
|
||||
@ -768,8 +761,10 @@ fn test_osstring() {
|
||||
#[cfg(feature = "unstable")]
|
||||
#[test]
|
||||
fn test_cstr() {
|
||||
assert_de_tokens::<Box<CStr>>(&CString::new("abc").unwrap().into_boxed_c_str(),
|
||||
&[Token::Bytes(b"abc")]);
|
||||
assert_de_tokens::<Box<CStr>>(
|
||||
&CString::new("abc").unwrap().into_boxed_c_str(),
|
||||
&[Token::Bytes(b"abc")],
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
@ -785,10 +780,8 @@ fn test_net_ipaddr() {
|
||||
#[test]
|
||||
fn test_cstr_internal_null() {
|
||||
assert_de_tokens_error::<Box<CStr>>(
|
||||
&[
|
||||
Token::Bytes(b"a\0c"),
|
||||
],
|
||||
Error::Message("nul byte found in provided data at position: 1".into())
|
||||
&[Token::Bytes(b"a\0c")],
|
||||
Error::Message("nul byte found in provided data at position: 1".into()),
|
||||
);
|
||||
}
|
||||
|
||||
@ -796,10 +789,8 @@ fn test_cstr_internal_null() {
|
||||
#[test]
|
||||
fn test_cstr_internal_null_end() {
|
||||
assert_de_tokens_error::<Box<CStr>>(
|
||||
&[
|
||||
Token::Bytes(b"ac\0"),
|
||||
],
|
||||
Error::Message("nul byte found in provided data at position: 2".into())
|
||||
&[Token::Bytes(b"ac\0")],
|
||||
Error::Message("nul byte found in provided data at position: 2".into()),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -82,20 +82,28 @@ fn test_gen() {
|
||||
Unit,
|
||||
Newtype(
|
||||
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||
X),
|
||||
X
|
||||
),
|
||||
Tuple(
|
||||
T,
|
||||
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||
X),
|
||||
X
|
||||
),
|
||||
Struct {
|
||||
t: T,
|
||||
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||
x: X },
|
||||
x: X,
|
||||
},
|
||||
}
|
||||
assert::<EnumWith<i32>>();
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct MultipleRef<'a, 'b, 'c, T> where T: 'c, 'c: 'b, 'b: 'a {
|
||||
struct MultipleRef<'a, 'b, 'c, T>
|
||||
where
|
||||
T: 'c,
|
||||
'c: 'b,
|
||||
'b: 'a,
|
||||
{
|
||||
t: T,
|
||||
rrrt: &'a &'b &'c T,
|
||||
}
|
||||
@ -112,7 +120,7 @@ fn test_gen() {
|
||||
struct Tuple<T>(
|
||||
T,
|
||||
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||
X,
|
||||
X
|
||||
);
|
||||
assert::<Tuple<i32>>();
|
||||
|
||||
@ -122,9 +130,7 @@ fn test_gen() {
|
||||
left: Box<TreeNode<D>>,
|
||||
right: Box<TreeNode<D>>,
|
||||
},
|
||||
Leaf {
|
||||
data: D,
|
||||
},
|
||||
Leaf { data: D },
|
||||
}
|
||||
assert::<TreeNode<i32>>();
|
||||
|
||||
@ -218,7 +224,7 @@ fn test_gen() {
|
||||
#[cfg(feature = "unstable")]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct NonAsciiIdents {
|
||||
σ: f64
|
||||
σ: f64,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@ -251,11 +257,17 @@ fn test_gen() {
|
||||
struct EmptyTupleDenyUnknown();
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct TupleSkipAll(#[serde(skip_deserializing)] u8);
|
||||
struct TupleSkipAll(
|
||||
#[serde(skip_deserializing)]
|
||||
u8
|
||||
);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct TupleSkipAllDenyUnknown(#[serde(skip_deserializing)] u8);
|
||||
struct TupleSkipAllDenyUnknown(
|
||||
#[serde(skip_deserializing)]
|
||||
u8
|
||||
);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
enum EmptyEnum {}
|
||||
@ -280,7 +292,10 @@ fn test_gen() {
|
||||
#[serde(skip_deserializing)]
|
||||
f: u8,
|
||||
},
|
||||
TupleSkip(#[serde(skip_deserializing)] u8),
|
||||
TupleSkip(
|
||||
#[serde(skip_deserializing)]
|
||||
u8
|
||||
),
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
@ -293,7 +308,10 @@ fn test_gen() {
|
||||
#[serde(skip_deserializing)]
|
||||
f: u8,
|
||||
},
|
||||
TupleSkip(#[serde(skip_deserializing)] u8),
|
||||
TupleSkip(
|
||||
#[serde(skip_deserializing)]
|
||||
u8
|
||||
),
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
@ -2,14 +2,8 @@
|
||||
extern crate serde_derive;
|
||||
|
||||
extern crate serde_test;
|
||||
use self::serde_test::{
|
||||
Error,
|
||||
Token,
|
||||
assert_tokens,
|
||||
assert_ser_tokens,
|
||||
assert_de_tokens,
|
||||
assert_de_tokens_error,
|
||||
};
|
||||
use self::serde_test::{Error, Token, assert_tokens, assert_ser_tokens, assert_de_tokens,
|
||||
assert_de_tokens_error};
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::marker::PhantomData;
|
||||
@ -45,67 +39,30 @@ struct DeNamedMap<A, B, C> {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
enum SerEnum<'a, B: 'a, C: 'a, D> where D: 'a {
|
||||
enum SerEnum<'a, B: 'a, C: 'a, D>
|
||||
where
|
||||
D: 'a,
|
||||
{
|
||||
Unit,
|
||||
Seq(
|
||||
i8,
|
||||
B,
|
||||
&'a C,
|
||||
&'a mut D,
|
||||
),
|
||||
Map {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: &'a C,
|
||||
d: &'a mut D,
|
||||
},
|
||||
Seq(i8, B, &'a C, &'a mut D),
|
||||
Map { a: i8, b: B, c: &'a C, d: &'a mut D },
|
||||
|
||||
// Make sure we can support more than one variant.
|
||||
_Unit2,
|
||||
_Seq2(
|
||||
i8,
|
||||
B,
|
||||
&'a C,
|
||||
&'a mut D,
|
||||
),
|
||||
_Map2 {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: &'a C,
|
||||
d: &'a mut D,
|
||||
},
|
||||
_Seq2(i8, B, &'a C, &'a mut D),
|
||||
_Map2 { a: i8, b: B, c: &'a C, d: &'a mut D },
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
enum DeEnum<B, C, D> {
|
||||
Unit,
|
||||
Seq(
|
||||
i8,
|
||||
B,
|
||||
C,
|
||||
D,
|
||||
),
|
||||
Map {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: C,
|
||||
d: D,
|
||||
},
|
||||
Seq(i8, B, C, D),
|
||||
Map { a: i8, b: B, c: C, d: D },
|
||||
|
||||
// Make sure we can support more than one variant.
|
||||
_Unit2,
|
||||
_Seq2(
|
||||
i8,
|
||||
B,
|
||||
C,
|
||||
D,
|
||||
),
|
||||
_Map2 {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: C,
|
||||
d: D,
|
||||
},
|
||||
_Seq2(i8, B, C, D),
|
||||
_Map2 { a: i8, b: B, c: C, d: D },
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@ -144,16 +101,13 @@ impl AssociatedType for i32 {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct DefaultTyParam<T: AssociatedType<X=i32> = i32> {
|
||||
phantom: PhantomData<T>
|
||||
struct DefaultTyParam<T: AssociatedType<X = i32> = i32> {
|
||||
phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_named_unit() {
|
||||
assert_tokens(
|
||||
&NamedUnit,
|
||||
&[Token::UnitStruct("NamedUnit")]
|
||||
);
|
||||
assert_tokens(&NamedUnit, &[Token::UnitStruct("NamedUnit")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -183,7 +137,7 @@ fn test_de_named_tuple() {
|
||||
Token::I32(6),
|
||||
Token::I32(7),
|
||||
Token::SeqEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
@ -194,7 +148,7 @@ fn test_de_named_tuple() {
|
||||
Token::I32(6),
|
||||
Token::I32(7),
|
||||
Token::TupleStructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -206,10 +160,10 @@ fn test_ser_named_map() {
|
||||
|
||||
assert_ser_tokens(
|
||||
&SerNamedMap {
|
||||
a: &a,
|
||||
b: &mut b,
|
||||
c: c,
|
||||
},
|
||||
a: &a,
|
||||
b: &mut b,
|
||||
c: c,
|
||||
},
|
||||
&[
|
||||
Token::Struct("SerNamedMap", 3),
|
||||
|
||||
@ -223,18 +177,14 @@ fn test_ser_named_map() {
|
||||
Token::I32(7),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_named_map() {
|
||||
assert_de_tokens(
|
||||
&DeNamedMap {
|
||||
a: 5,
|
||||
b: 6,
|
||||
c: 7,
|
||||
},
|
||||
&DeNamedMap { a: 5, b: 6, c: 7 },
|
||||
&[
|
||||
Token::Struct("DeNamedMap", 3),
|
||||
|
||||
@ -248,7 +198,7 @@ fn test_de_named_map() {
|
||||
Token::I32(7),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -256,9 +206,7 @@ fn test_de_named_map() {
|
||||
fn test_ser_enum_unit() {
|
||||
assert_ser_tokens(
|
||||
&SerEnum::Unit::<u32, u32, u32>,
|
||||
&[
|
||||
Token::UnitVariant("SerEnum", "Unit"),
|
||||
]
|
||||
&[Token::UnitVariant("SerEnum", "Unit")],
|
||||
);
|
||||
}
|
||||
|
||||
@ -270,12 +218,7 @@ fn test_ser_enum_seq() {
|
||||
let mut d = 4;
|
||||
|
||||
assert_ser_tokens(
|
||||
&SerEnum::Seq(
|
||||
a,
|
||||
b,
|
||||
&c,
|
||||
&mut d,
|
||||
),
|
||||
&SerEnum::Seq(a, b, &c, &mut d),
|
||||
&[
|
||||
Token::TupleVariant("SerEnum", "Seq", 4),
|
||||
Token::I8(1),
|
||||
@ -296,11 +239,11 @@ fn test_ser_enum_map() {
|
||||
|
||||
assert_ser_tokens(
|
||||
&SerEnum::Map {
|
||||
a: a,
|
||||
b: b,
|
||||
c: &c,
|
||||
d: &mut d,
|
||||
},
|
||||
a: a,
|
||||
b: b,
|
||||
c: &c,
|
||||
d: &mut d,
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("SerEnum", "Map", 4),
|
||||
|
||||
@ -325,9 +268,7 @@ fn test_ser_enum_map() {
|
||||
fn test_de_enum_unit() {
|
||||
assert_tokens(
|
||||
&DeEnum::Unit::<u32, u32, u32>,
|
||||
&[
|
||||
Token::UnitVariant("DeEnum", "Unit"),
|
||||
],
|
||||
&[Token::UnitVariant("DeEnum", "Unit")],
|
||||
);
|
||||
}
|
||||
|
||||
@ -339,12 +280,7 @@ fn test_de_enum_seq() {
|
||||
let d = 4;
|
||||
|
||||
assert_tokens(
|
||||
&DeEnum::Seq(
|
||||
a,
|
||||
b,
|
||||
c,
|
||||
d,
|
||||
),
|
||||
&DeEnum::Seq(a, b, c, d),
|
||||
&[
|
||||
Token::TupleVariant("DeEnum", "Seq", 4),
|
||||
Token::I8(1),
|
||||
@ -365,11 +301,11 @@ fn test_de_enum_map() {
|
||||
|
||||
assert_tokens(
|
||||
&DeEnum::Map {
|
||||
a: a,
|
||||
b: b,
|
||||
c: c,
|
||||
d: d,
|
||||
},
|
||||
a: a,
|
||||
b: b,
|
||||
c: c,
|
||||
d: d,
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("DeEnum", "Map", 4),
|
||||
|
||||
@ -399,7 +335,7 @@ fn test_lifetimes() {
|
||||
&[
|
||||
Token::NewtypeVariant("Lifetimes", "LifetimeSeq"),
|
||||
Token::I32(5),
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
@ -407,7 +343,7 @@ fn test_lifetimes() {
|
||||
&[
|
||||
Token::NewtypeVariant("Lifetimes", "NoLifetimeSeq"),
|
||||
Token::I32(5),
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
@ -419,7 +355,7 @@ fn test_lifetimes() {
|
||||
Token::I32(5),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
@ -431,7 +367,7 @@ fn test_lifetimes() {
|
||||
Token::I32(5),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -446,7 +382,7 @@ fn test_generic_struct() {
|
||||
Token::U32(5),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -454,10 +390,7 @@ fn test_generic_struct() {
|
||||
fn test_generic_newtype_struct() {
|
||||
assert_tokens(
|
||||
&GenericNewTypeStruct(5u32),
|
||||
&[
|
||||
Token::NewtypeStruct("GenericNewTypeStruct"),
|
||||
Token::U32(5),
|
||||
]
|
||||
&[Token::NewtypeStruct("GenericNewTypeStruct"), Token::U32(5)],
|
||||
);
|
||||
}
|
||||
|
||||
@ -470,7 +403,7 @@ fn test_generic_tuple_struct() {
|
||||
Token::U32(5),
|
||||
Token::U32(6),
|
||||
Token::TupleStructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -478,9 +411,7 @@ fn test_generic_tuple_struct() {
|
||||
fn test_generic_enum_unit() {
|
||||
assert_tokens(
|
||||
&GenericEnum::Unit::<u32, u32>,
|
||||
&[
|
||||
Token::UnitVariant("GenericEnum", "Unit"),
|
||||
]
|
||||
&[Token::UnitVariant("GenericEnum", "Unit")],
|
||||
);
|
||||
}
|
||||
|
||||
@ -491,7 +422,7 @@ fn test_generic_enum_newtype() {
|
||||
&[
|
||||
Token::NewtypeVariant("GenericEnum", "NewType"),
|
||||
Token::U32(5),
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -504,7 +435,7 @@ fn test_generic_enum_seq() {
|
||||
Token::U32(5),
|
||||
Token::U32(6),
|
||||
Token::TupleVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -522,7 +453,7 @@ fn test_generic_enum_map() {
|
||||
Token::U32(6),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -537,7 +468,7 @@ fn test_default_ty_param() {
|
||||
Token::UnitStruct("PhantomData"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -549,7 +480,10 @@ fn test_enum_state_field() {
|
||||
}
|
||||
|
||||
assert_tokens(
|
||||
&SomeEnum::Key { key: 'a', state: true },
|
||||
&SomeEnum::Key {
|
||||
key: 'a',
|
||||
state: true,
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("SomeEnum", "Key", 2),
|
||||
|
||||
@ -560,7 +494,7 @@ fn test_enum_state_field() {
|
||||
Token::Bool(true),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -569,12 +503,8 @@ fn test_untagged_enum() {
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum Untagged {
|
||||
A {
|
||||
a: u8,
|
||||
},
|
||||
B {
|
||||
b: u8,
|
||||
},
|
||||
A { a: u8 },
|
||||
B { b: u8 },
|
||||
C,
|
||||
D(u8),
|
||||
E(String),
|
||||
@ -590,7 +520,7 @@ fn test_untagged_enum() {
|
||||
Token::U8(1),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -602,53 +532,27 @@ fn test_untagged_enum() {
|
||||
Token::U8(2),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
&Untagged::C,
|
||||
&[
|
||||
Token::Unit,
|
||||
]
|
||||
);
|
||||
assert_tokens(&Untagged::C, &[Token::Unit]);
|
||||
|
||||
assert_tokens(
|
||||
&Untagged::D(4),
|
||||
&[
|
||||
Token::U8(4),
|
||||
]
|
||||
);
|
||||
assert_tokens(
|
||||
&Untagged::E("e".to_owned()),
|
||||
&[
|
||||
Token::Str("e"),
|
||||
]
|
||||
);
|
||||
assert_tokens(&Untagged::D(4), &[Token::U8(4)]);
|
||||
assert_tokens(&Untagged::E("e".to_owned()), &[Token::Str("e")]);
|
||||
|
||||
assert_tokens(
|
||||
&Untagged::F(1, 2),
|
||||
&[
|
||||
Token::Tuple(2),
|
||||
Token::U8(1),
|
||||
Token::U8(2),
|
||||
Token::TupleEnd,
|
||||
]
|
||||
&[Token::Tuple(2), Token::U8(1), Token::U8(2), Token::TupleEnd],
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<Untagged>(
|
||||
&[
|
||||
Token::None,
|
||||
],
|
||||
Error::Message("data did not match any variant of untagged enum Untagged".to_owned()),
|
||||
&[Token::None],
|
||||
Error::Message("data did not match any variant of untagged enum Untagged".to_owned(),),
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<Untagged>(
|
||||
&[
|
||||
Token::Tuple(1),
|
||||
Token::U8(1),
|
||||
Token::TupleEnd,
|
||||
],
|
||||
Error::Message("data did not match any variant of untagged enum Untagged".to_owned()),
|
||||
&[Token::Tuple(1), Token::U8(1), Token::TupleEnd],
|
||||
Error::Message("data did not match any variant of untagged enum Untagged".to_owned(),),
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<Untagged>(
|
||||
@ -659,7 +563,7 @@ fn test_untagged_enum() {
|
||||
Token::U8(3),
|
||||
Token::TupleEnd,
|
||||
],
|
||||
Error::Message("data did not match any variant of untagged enum Untagged".to_owned()),
|
||||
Error::Message("data did not match any variant of untagged enum Untagged".to_owned(),),
|
||||
);
|
||||
}
|
||||
|
||||
@ -676,12 +580,8 @@ fn test_internally_tagged_enum() {
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
enum InternallyTagged {
|
||||
A {
|
||||
a: u8,
|
||||
},
|
||||
B {
|
||||
b: u8,
|
||||
},
|
||||
A { a: u8 },
|
||||
B { b: u8 },
|
||||
C,
|
||||
D(BTreeMap<String, String>),
|
||||
E(Newtype),
|
||||
@ -700,7 +600,7 @@ fn test_internally_tagged_enum() {
|
||||
Token::U8(1),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -715,7 +615,7 @@ fn test_internally_tagged_enum() {
|
||||
Token::U8(2),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -727,7 +627,7 @@ fn test_internally_tagged_enum() {
|
||||
Token::Str("C"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -739,7 +639,7 @@ fn test_internally_tagged_enum() {
|
||||
Token::Str("D"),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -751,7 +651,7 @@ fn test_internally_tagged_enum() {
|
||||
Token::Str("E"),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -766,14 +666,11 @@ fn test_internally_tagged_enum() {
|
||||
Token::U8(6),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<InternallyTagged>(
|
||||
&[
|
||||
Token::Map(Some(0)),
|
||||
Token::MapEnd,
|
||||
],
|
||||
&[Token::Map(Some(0)), Token::MapEnd],
|
||||
Error::Message("missing field `type`".to_owned()),
|
||||
);
|
||||
|
||||
@ -786,7 +683,7 @@ fn test_internally_tagged_enum() {
|
||||
|
||||
Token::MapEnd,
|
||||
],
|
||||
Error::Message("unknown variant `Z`, expected one of `A`, `B`, `C`, `D`, `E`, `F`".to_owned()),
|
||||
Error::Message("unknown variant `Z`, expected one of `A`, `B`, `C`, `D`, `E`, `F`".to_owned(),),
|
||||
);
|
||||
}
|
||||
|
||||
@ -811,7 +708,7 @@ fn test_adjacently_tagged_enum() {
|
||||
Token::Str("Unit"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
// unit with tag first
|
||||
@ -827,7 +724,7 @@ fn test_adjacently_tagged_enum() {
|
||||
Token::Unit,
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
// unit with content first
|
||||
@ -843,7 +740,7 @@ fn test_adjacently_tagged_enum() {
|
||||
Token::Str("Unit"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
// newtype with tag first
|
||||
@ -859,7 +756,7 @@ fn test_adjacently_tagged_enum() {
|
||||
Token::U8(1),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
// newtype with content first
|
||||
@ -875,7 +772,7 @@ fn test_adjacently_tagged_enum() {
|
||||
Token::Str("Newtype"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
// tuple with tag first
|
||||
@ -894,7 +791,7 @@ fn test_adjacently_tagged_enum() {
|
||||
Token::TupleEnd,
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
// tuple with content first
|
||||
@ -913,7 +810,7 @@ fn test_adjacently_tagged_enum() {
|
||||
Token::Str("Tuple"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
// struct with tag first
|
||||
@ -932,7 +829,7 @@ fn test_adjacently_tagged_enum() {
|
||||
Token::StructEnd,
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
// struct with content first
|
||||
@ -951,7 +848,7 @@ fn test_adjacently_tagged_enum() {
|
||||
Token::Str("Struct"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -983,7 +880,7 @@ fn test_enum_in_internally_tagged_enum() {
|
||||
Token::Unit,
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -998,7 +895,7 @@ fn test_enum_in_internally_tagged_enum() {
|
||||
Token::U8(1),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -1016,7 +913,7 @@ fn test_enum_in_internally_tagged_enum() {
|
||||
Token::TupleStructEnd,
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -1034,7 +931,7 @@ fn test_enum_in_internally_tagged_enum() {
|
||||
Token::StructEnd,
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -1056,17 +953,12 @@ fn test_enum_in_untagged_enum() {
|
||||
|
||||
assert_tokens(
|
||||
&Outer::Inner(Inner::Unit),
|
||||
&[
|
||||
Token::UnitVariant("Inner", "Unit"),
|
||||
]
|
||||
&[Token::UnitVariant("Inner", "Unit")],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
&Outer::Inner(Inner::Newtype(1)),
|
||||
&[
|
||||
Token::NewtypeVariant("Inner", "Newtype"),
|
||||
Token::U8(1),
|
||||
]
|
||||
&[Token::NewtypeVariant("Inner", "Newtype"), Token::U8(1)],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -1076,7 +968,7 @@ fn test_enum_in_untagged_enum() {
|
||||
Token::U8(1),
|
||||
Token::U8(1),
|
||||
Token::TupleVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
@ -1088,7 +980,7 @@ fn test_enum_in_untagged_enum() {
|
||||
Token::U8(1),
|
||||
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -1122,7 +1014,10 @@ fn test_rename_all() {
|
||||
}
|
||||
|
||||
assert_tokens(
|
||||
&E::Serialize { serialize: true, serialize_seq: true },
|
||||
&E::Serialize {
|
||||
serialize: true,
|
||||
serialize_seq: true,
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("E", "serialize", 2),
|
||||
Token::Str("serialize"),
|
||||
@ -1130,11 +1025,14 @@ fn test_rename_all() {
|
||||
Token::Str("serializeSeq"),
|
||||
Token::Bool(true),
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
&E::SerializeSeq { serialize: true, serialize_seq: true },
|
||||
&E::SerializeSeq {
|
||||
serialize: true,
|
||||
serialize_seq: true,
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("E", "serialize_seq", 2),
|
||||
Token::Str("serialize"),
|
||||
@ -1142,11 +1040,14 @@ fn test_rename_all() {
|
||||
Token::Str("serialize-seq"),
|
||||
Token::Bool(true),
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
&E::SerializeMap { serialize: true, serialize_seq: true },
|
||||
&E::SerializeMap {
|
||||
serialize: true,
|
||||
serialize_seq: true,
|
||||
},
|
||||
&[
|
||||
Token::StructVariant("E", "serialize_map", 2),
|
||||
Token::Str("SERIALIZE"),
|
||||
@ -1154,11 +1055,14 @@ fn test_rename_all() {
|
||||
Token::Str("SERIALIZE_SEQ"),
|
||||
Token::Bool(true),
|
||||
Token::StructVariantEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens(
|
||||
&S { serialize: true, serialize_seq: true },
|
||||
&S {
|
||||
serialize: true,
|
||||
serialize_seq: true,
|
||||
},
|
||||
&[
|
||||
Token::Struct("S", 2),
|
||||
Token::Str("Serialize"),
|
||||
@ -1166,6 +1070,6 @@ fn test_rename_all() {
|
||||
Token::Str("SerializeSeq"),
|
||||
Token::Bool(true),
|
||||
Token::StructEnd,
|
||||
]
|
||||
],
|
||||
);
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ mod remote {
|
||||
b: Unit,
|
||||
}
|
||||
|
||||
pub struct StructPub {
|
||||
pub struct StructPub {
|
||||
pub a: u8,
|
||||
pub b: Unit,
|
||||
}
|
||||
@ -113,7 +113,10 @@ struct UnitDef;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(remote = "remote::PrimitivePriv")]
|
||||
struct PrimitivePrivDef(#[serde(getter = "remote::PrimitivePriv::get")] u8);
|
||||
struct PrimitivePrivDef(
|
||||
#[serde(getter = "remote::PrimitivePriv::get")]
|
||||
u8
|
||||
);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(remote = "remote::PrimitivePub")]
|
||||
@ -121,21 +124,34 @@ struct PrimitivePubDef(u8);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(remote = "remote::NewtypePriv")]
|
||||
struct NewtypePrivDef(#[serde(getter = "remote::NewtypePriv::get", with = "UnitDef")] remote::Unit);
|
||||
struct NewtypePrivDef(
|
||||
#[serde(getter = "remote::NewtypePriv::get", with = "UnitDef")]
|
||||
remote::Unit
|
||||
);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(remote = "remote::NewtypePub")]
|
||||
struct NewtypePubDef(#[serde(with = "UnitDef")] remote::Unit);
|
||||
struct NewtypePubDef(
|
||||
#[serde(with = "UnitDef")]
|
||||
remote::Unit
|
||||
);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(remote = "remote::TuplePriv")]
|
||||
struct TuplePrivDef(
|
||||
#[serde(getter = "remote::TuplePriv::first")] u8,
|
||||
#[serde(getter = "remote::TuplePriv::second", with = "UnitDef")] remote::Unit);
|
||||
#[serde(getter = "remote::TuplePriv::first")]
|
||||
u8,
|
||||
#[serde(getter = "remote::TuplePriv::second", with = "UnitDef")]
|
||||
remote::Unit
|
||||
);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(remote = "remote::TuplePub")]
|
||||
struct TuplePubDef(u8, #[serde(with = "UnitDef")] remote::Unit);
|
||||
struct TuplePubDef(
|
||||
u8,
|
||||
#[serde(with = "UnitDef")]
|
||||
remote::Unit
|
||||
);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(remote = "remote::StructPriv")]
|
||||
|
@ -13,12 +13,7 @@ use std::str;
|
||||
extern crate serde;
|
||||
|
||||
extern crate serde_test;
|
||||
use self::serde_test::{
|
||||
Error,
|
||||
Token,
|
||||
assert_ser_tokens,
|
||||
assert_ser_tokens_error,
|
||||
};
|
||||
use self::serde_test::{Error, Token, assert_ser_tokens, assert_ser_tokens_error};
|
||||
|
||||
extern crate fnv;
|
||||
use self::fnv::FnvHasher;
|
||||
@ -372,13 +367,12 @@ fn test_net_ipaddr() {
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_cannot_serialize_paths() {
|
||||
let path = unsafe {
|
||||
str::from_utf8_unchecked(b"Hello \xF0\x90\x80World")
|
||||
};
|
||||
let path = unsafe { str::from_utf8_unchecked(b"Hello \xF0\x90\x80World") };
|
||||
assert_ser_tokens_error(
|
||||
&Path::new(path),
|
||||
&[],
|
||||
Error::Message("path contains invalid UTF-8 characters".to_owned()));
|
||||
Error::Message("path contains invalid UTF-8 characters".to_owned()),
|
||||
);
|
||||
|
||||
let mut path_buf = PathBuf::new();
|
||||
path_buf.push(path);
|
||||
@ -386,7 +380,8 @@ fn test_cannot_serialize_paths() {
|
||||
assert_ser_tokens_error(
|
||||
&path_buf,
|
||||
&[],
|
||||
Error::Message("path contains invalid UTF-8 characters".to_owned()));
|
||||
Error::Message("path contains invalid UTF-8 characters".to_owned()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -394,17 +389,21 @@ fn test_enum_skipped() {
|
||||
assert_ser_tokens_error(
|
||||
&Enum::SkippedUnit,
|
||||
&[],
|
||||
Error::Message("the enum variant Enum::SkippedUnit cannot be serialized".to_owned()));
|
||||
Error::Message("the enum variant Enum::SkippedUnit cannot be serialized".to_owned(),),
|
||||
);
|
||||
assert_ser_tokens_error(
|
||||
&Enum::SkippedOne(42),
|
||||
&[],
|
||||
Error::Message("the enum variant Enum::SkippedOne cannot be serialized".to_owned()));
|
||||
Error::Message("the enum variant Enum::SkippedOne cannot be serialized".to_owned(),),
|
||||
);
|
||||
assert_ser_tokens_error(
|
||||
&Enum::SkippedSeq(1, 2),
|
||||
&[],
|
||||
Error::Message("the enum variant Enum::SkippedSeq cannot be serialized".to_owned()));
|
||||
Error::Message("the enum variant Enum::SkippedSeq cannot be serialized".to_owned(),),
|
||||
);
|
||||
assert_ser_tokens_error(
|
||||
&Enum::SkippedMap { _a: 1, _b: 2 },
|
||||
&[],
|
||||
Error::Message("the enum variant Enum::SkippedMap cannot be serialized".to_owned()));
|
||||
Error::Message("the enum variant Enum::SkippedMap cannot be serialized".to_owned(),),
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user