mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-24 14:37:59 +00:00
+20
-16
@@ -60,9 +60,7 @@ pub struct Bytes<'a> {
|
|||||||
impl<'a> Bytes<'a> {
|
impl<'a> Bytes<'a> {
|
||||||
/// Wrap an existing `&[u8]`.
|
/// Wrap an existing `&[u8]`.
|
||||||
pub fn new(bytes: &'a [u8]) -> Self {
|
pub fn new(bytes: &'a [u8]) -> Self {
|
||||||
Bytes {
|
Bytes { bytes: bytes }
|
||||||
bytes: bytes,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +96,9 @@ impl<'a> Into<&'a [u8]> for Bytes<'a> {
|
|||||||
impl<'a> ops::Deref for Bytes<'a> {
|
impl<'a> ops::Deref for Bytes<'a> {
|
||||||
type Target = [u8];
|
type Target = [u8];
|
||||||
|
|
||||||
fn deref(&self) -> &[u8] { self.bytes }
|
fn deref(&self) -> &[u8] {
|
||||||
|
self.bytes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ser::Serialize for Bytes<'a> {
|
impl<'a> ser::Serialize for Bytes<'a> {
|
||||||
@@ -161,9 +161,7 @@ mod bytebuf {
|
|||||||
|
|
||||||
/// Wrap existing bytes in a `ByteBuf`.
|
/// Wrap existing bytes in a `ByteBuf`.
|
||||||
pub fn from<T: Into<Vec<u8>>>(bytes: T) -> Self {
|
pub fn from<T: Into<Vec<u8>>>(bytes: T) -> Self {
|
||||||
ByteBuf {
|
ByteBuf { bytes: bytes.into() }
|
||||||
bytes: bytes.into(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,11 +214,15 @@ mod bytebuf {
|
|||||||
impl ops::Deref for ByteBuf {
|
impl ops::Deref for ByteBuf {
|
||||||
type Target = [u8];
|
type Target = [u8];
|
||||||
|
|
||||||
fn deref(&self) -> &[u8] { &self.bytes[..] }
|
fn deref(&self) -> &[u8] {
|
||||||
|
&self.bytes[..]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ops::DerefMut for ByteBuf {
|
impl ops::DerefMut for ByteBuf {
|
||||||
fn deref_mut(&mut self) -> &mut [u8] { &mut self.bytes[..] }
|
fn deref_mut(&mut self) -> &mut [u8] {
|
||||||
|
&mut self.bytes[..]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ser::Serialize for ByteBuf {
|
impl ser::Serialize for ByteBuf {
|
||||||
@@ -243,14 +245,14 @@ mod bytebuf {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_unit<E>(self) -> Result<ByteBuf, E>
|
fn visit_unit<E>(self) -> Result<ByteBuf, E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
Ok(ByteBuf::new())
|
Ok(ByteBuf::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_seq<V>(self, mut visitor: V) -> Result<ByteBuf, V::Error>
|
fn visit_seq<V>(self, mut visitor: V) -> Result<ByteBuf, V::Error>
|
||||||
where V: de::SeqVisitor,
|
where V: de::SeqVisitor
|
||||||
{
|
{
|
||||||
let (len, _) = visitor.size_hint();
|
let (len, _) = visitor.size_hint();
|
||||||
let mut values = Vec::with_capacity(len);
|
let mut values = Vec::with_capacity(len);
|
||||||
@@ -264,26 +266,26 @@ mod bytebuf {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<ByteBuf, E>
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<ByteBuf, E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
Ok(ByteBuf::from(v))
|
Ok(ByteBuf::from(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<ByteBuf, E>
|
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<ByteBuf, E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
Ok(ByteBuf::from(v))
|
Ok(ByteBuf::from(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<ByteBuf, E>
|
fn visit_str<E>(self, v: &str) -> Result<ByteBuf, E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
Ok(ByteBuf::from(v))
|
Ok(ByteBuf::from(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_string<E>(self, v: String) -> Result<ByteBuf, E>
|
fn visit_string<E>(self, v: String) -> Result<ByteBuf, E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
Ok(ByteBuf::from(v))
|
Ok(ByteBuf::from(v))
|
||||||
}
|
}
|
||||||
@@ -302,7 +304,9 @@ mod bytebuf {
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn escape_bytestring<'a>(bytes: &'a [u8]) -> iter::FlatMap<slice::Iter<'a, u8>, char::EscapeDefault, fn(&u8) -> char::EscapeDefault> {
|
fn escape_bytestring<'a>
|
||||||
|
(bytes: &'a [u8])
|
||||||
|
-> iter::FlatMap<slice::Iter<'a, u8>, char::EscapeDefault, fn(&u8) -> char::EscapeDefault> {
|
||||||
fn f(b: &u8) -> char::EscapeDefault {
|
fn f(b: &u8) -> char::EscapeDefault {
|
||||||
char::from_u32(*b as u32).unwrap().escape_default()
|
char::from_u32(*b as u32).unwrap().escape_default()
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-24
@@ -19,16 +19,8 @@ use collections::{String, Vec};
|
|||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
use alloc::boxed::Box;
|
use alloc::boxed::Box;
|
||||||
|
|
||||||
use de::{
|
use de::{self, Deserialize, DeserializeSeed, Deserializer, Visitor, SeqVisitor, MapVisitor,
|
||||||
self,
|
EnumVisitor};
|
||||||
Deserialize,
|
|
||||||
DeserializeSeed,
|
|
||||||
Deserializer,
|
|
||||||
Visitor,
|
|
||||||
SeqVisitor,
|
|
||||||
MapVisitor,
|
|
||||||
EnumVisitor,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Used from generated code to buffer the contents of the Deserializer when
|
/// Used from generated code to buffer the contents of the Deserializer when
|
||||||
/// deserializing untagged enums and internally tagged enums.
|
/// deserializing untagged enums and internally tagged enums.
|
||||||
@@ -243,9 +235,7 @@ struct TagOrContentVisitor {
|
|||||||
|
|
||||||
impl TagOrContentVisitor {
|
impl TagOrContentVisitor {
|
||||||
fn new(name: &'static str) -> Self {
|
fn new(name: &'static str) -> Self {
|
||||||
TagOrContentVisitor {
|
TagOrContentVisitor { name: name }
|
||||||
name: name,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -491,9 +481,7 @@ impl<T> Visitor for TaggedContentVisitor<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
match tag {
|
match tag {
|
||||||
None => {
|
None => Err(de::Error::missing_field(self.tag_name)),
|
||||||
Err(de::Error::missing_field(self.tag_name))
|
|
||||||
}
|
|
||||||
Some(tag) => {
|
Some(tag) => {
|
||||||
Ok(TaggedContent {
|
Ok(TaggedContent {
|
||||||
tag: tag,
|
tag: tag,
|
||||||
@@ -544,14 +532,15 @@ impl<E> Deserializer for ContentDeserializer<E>
|
|||||||
let value = try!(visitor.visit_seq(&mut seq_visitor));
|
let value = try!(visitor.visit_seq(&mut seq_visitor));
|
||||||
try!(seq_visitor.end());
|
try!(seq_visitor.end());
|
||||||
Ok(value)
|
Ok(value)
|
||||||
},
|
}
|
||||||
Content::Map(v) => {
|
Content::Map(v) => {
|
||||||
let map = v.into_iter().map(|(k, v)| (ContentDeserializer::new(k), ContentDeserializer::new(v)));
|
let map = v.into_iter()
|
||||||
|
.map(|(k, v)| (ContentDeserializer::new(k), ContentDeserializer::new(v)));
|
||||||
let mut map_visitor = de::value::MapDeserializer::new(map);
|
let mut map_visitor = de::value::MapDeserializer::new(map);
|
||||||
let value = try!(visitor.visit_map(&mut map_visitor));
|
let value = try!(visitor.visit_map(&mut map_visitor));
|
||||||
try!(map_visitor.end());
|
try!(map_visitor.end());
|
||||||
Ok(value)
|
Ok(value)
|
||||||
},
|
}
|
||||||
Content::Bytes(v) => visitor.visit_byte_buf(v),
|
Content::Bytes(v) => visitor.visit_byte_buf(v),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -563,7 +552,7 @@ impl<E> Deserializer for ContentDeserializer<E>
|
|||||||
Content::None => visitor.visit_none(),
|
Content::None => visitor.visit_none(),
|
||||||
Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
|
Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
|
||||||
Content::Unit => visitor.visit_unit(),
|
Content::Unit => visitor.visit_unit(),
|
||||||
_ => visitor.visit_some(self)
|
_ => visitor.visit_some(self),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -630,14 +619,16 @@ impl<'a, E> Deserializer for ContentRefDeserializer<'a, E>
|
|||||||
let value = try!(visitor.visit_seq(&mut seq_visitor));
|
let value = try!(visitor.visit_seq(&mut seq_visitor));
|
||||||
try!(seq_visitor.end());
|
try!(seq_visitor.end());
|
||||||
Ok(value)
|
Ok(value)
|
||||||
},
|
}
|
||||||
Content::Map(ref v) => {
|
Content::Map(ref v) => {
|
||||||
let map = v.into_iter().map(|&(ref k, ref v)| (ContentRefDeserializer::new(k), ContentRefDeserializer::new(v)));
|
let map = v.into_iter().map(|&(ref k, ref v)| {
|
||||||
|
(ContentRefDeserializer::new(k), ContentRefDeserializer::new(v))
|
||||||
|
});
|
||||||
let mut map_visitor = de::value::MapDeserializer::new(map);
|
let mut map_visitor = de::value::MapDeserializer::new(map);
|
||||||
let value = try!(visitor.visit_map(&mut map_visitor));
|
let value = try!(visitor.visit_map(&mut map_visitor));
|
||||||
try!(map_visitor.end());
|
try!(map_visitor.end());
|
||||||
Ok(value)
|
Ok(value)
|
||||||
},
|
}
|
||||||
Content::Bytes(ref v) => visitor.visit_bytes(v),
|
Content::Bytes(ref v) => visitor.visit_bytes(v),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -649,7 +640,7 @@ impl<'a, E> Deserializer for ContentRefDeserializer<'a, E>
|
|||||||
Content::None => visitor.visit_none(),
|
Content::None => visitor.visit_none(),
|
||||||
Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
|
Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
|
||||||
Content::Unit => visitor.visit_unit(),
|
Content::Unit => visitor.visit_unit(),
|
||||||
_ => visitor.visit_some(self)
|
_ => visitor.visit_some(self),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+100
-103
@@ -6,26 +6,10 @@ use std::borrow::Cow;
|
|||||||
use collections::borrow::Cow;
|
use collections::borrow::Cow;
|
||||||
|
|
||||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
use collections::{
|
use collections::{BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque, Vec, String};
|
||||||
BinaryHeap,
|
|
||||||
BTreeMap,
|
|
||||||
BTreeSet,
|
|
||||||
LinkedList,
|
|
||||||
VecDeque,
|
|
||||||
Vec,
|
|
||||||
String,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::collections::{
|
use std::collections::{HashMap, HashSet, BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque};
|
||||||
HashMap,
|
|
||||||
HashSet,
|
|
||||||
BinaryHeap,
|
|
||||||
BTreeMap,
|
|
||||||
BTreeSet,
|
|
||||||
LinkedList,
|
|
||||||
VecDeque,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(feature = "collections")]
|
#[cfg(feature = "collections")]
|
||||||
use collections::borrow::ToOwned;
|
use collections::borrow::ToOwned;
|
||||||
@@ -63,17 +47,8 @@ use core::nonzero::{NonZero, Zeroable};
|
|||||||
#[allow(deprecated)] // required for impl Deserialize for NonZero<T>
|
#[allow(deprecated)] // required for impl Deserialize for NonZero<T>
|
||||||
use core::num::Zero;
|
use core::num::Zero;
|
||||||
|
|
||||||
use de::{
|
use de::{Deserialize, Deserializer, EnumVisitor, Error, MapVisitor, SeqVisitor, Unexpected,
|
||||||
Deserialize,
|
VariantVisitor, Visitor};
|
||||||
Deserializer,
|
|
||||||
EnumVisitor,
|
|
||||||
Error,
|
|
||||||
MapVisitor,
|
|
||||||
SeqVisitor,
|
|
||||||
Unexpected,
|
|
||||||
VariantVisitor,
|
|
||||||
Visitor,
|
|
||||||
};
|
|
||||||
use de::from_primitive::FromPrimitive;
|
use de::from_primitive::FromPrimitive;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -89,13 +64,13 @@ impl Visitor for UnitVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_unit<E>(self) -> Result<(), E>
|
fn visit_unit<E>(self) -> Result<(), E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_seq<V>(self, _: V) -> Result<(), V::Error>
|
fn visit_seq<V>(self, _: V) -> Result<(), V::Error>
|
||||||
where V: SeqVisitor,
|
where V: SeqVisitor
|
||||||
{
|
{
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -103,7 +78,7 @@ impl Visitor for UnitVisitor {
|
|||||||
|
|
||||||
impl Deserialize for () {
|
impl Deserialize for () {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<(), D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<(), D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
deserializer.deserialize_unit(UnitVisitor)
|
deserializer.deserialize_unit(UnitVisitor)
|
||||||
}
|
}
|
||||||
@@ -122,13 +97,13 @@ impl Visitor for BoolVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_bool<E>(self, v: bool) -> Result<bool, E>
|
fn visit_bool<E>(self, v: bool) -> Result<bool, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(v)
|
Ok(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, s: &str) -> Result<bool, E>
|
fn visit_str<E>(self, s: &str) -> Result<bool, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
match s.trim_matches(::utils::Pattern_White_Space) {
|
match s.trim_matches(::utils::Pattern_White_Space) {
|
||||||
"true" => Ok(true),
|
"true" => Ok(true),
|
||||||
@@ -140,7 +115,7 @@ impl Visitor for BoolVisitor {
|
|||||||
|
|
||||||
impl Deserialize for bool {
|
impl Deserialize for bool {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<bool, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<bool, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
deserializer.deserialize_bool(BoolVisitor)
|
deserializer.deserialize_bool(BoolVisitor)
|
||||||
}
|
}
|
||||||
@@ -231,14 +206,14 @@ impl Visitor for CharVisitor {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_char<E>(self, v: char) -> Result<char, E>
|
fn visit_char<E>(self, v: char) -> Result<char, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(v)
|
Ok(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_str<E>(self, v: &str) -> Result<char, E>
|
fn visit_str<E>(self, v: &str) -> Result<char, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
let mut iter = v.chars();
|
let mut iter = v.chars();
|
||||||
match (iter.next(), iter.next()) {
|
match (iter.next(), iter.next()) {
|
||||||
@@ -251,7 +226,7 @@ impl Visitor for CharVisitor {
|
|||||||
impl Deserialize for char {
|
impl Deserialize for char {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize<D>(deserializer: D) -> Result<char, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<char, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
deserializer.deserialize_char(CharVisitor)
|
deserializer.deserialize_char(CharVisitor)
|
||||||
}
|
}
|
||||||
@@ -271,25 +246,25 @@ impl Visitor for StringVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<String, E>
|
fn visit_str<E>(self, v: &str) -> Result<String, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(v.to_owned())
|
Ok(v.to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_string<E>(self, v: String) -> Result<String, E>
|
fn visit_string<E>(self, v: String) -> Result<String, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(v)
|
Ok(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_unit<E>(self) -> Result<String, E>
|
fn visit_unit<E>(self) -> Result<String, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(String::new())
|
Ok(String::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<String, E>
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<String, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
match str::from_utf8(v) {
|
match str::from_utf8(v) {
|
||||||
Ok(s) => Ok(s.to_owned()),
|
Ok(s) => Ok(s.to_owned()),
|
||||||
@@ -298,7 +273,7 @@ impl Visitor for StringVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<String, E>
|
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<String, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
match String::from_utf8(v) {
|
match String::from_utf8(v) {
|
||||||
Ok(s) => Ok(s),
|
Ok(s) => Ok(s),
|
||||||
@@ -310,7 +285,7 @@ impl Visitor for StringVisitor {
|
|||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl Deserialize for String {
|
impl Deserialize for String {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<String, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<String, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
deserializer.deserialize_string(StringVisitor)
|
deserializer.deserialize_string(StringVisitor)
|
||||||
}
|
}
|
||||||
@@ -322,9 +297,7 @@ struct OptionVisitor<T> {
|
|||||||
marker: PhantomData<T>,
|
marker: PhantomData<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<
|
impl<T: Deserialize> Visitor for OptionVisitor<T> {
|
||||||
T: Deserialize,
|
|
||||||
> Visitor for OptionVisitor<T> {
|
|
||||||
type Value = Option<T>;
|
type Value = Option<T>;
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
@@ -333,29 +306,31 @@ impl<
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_unit<E>(self) -> Result<Option<T>, E>
|
fn visit_unit<E>(self) -> Result<Option<T>, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_none<E>(self) -> Result<Option<T>, E>
|
fn visit_none<E>(self) -> Result<Option<T>, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_some<D>(self, deserializer: D) -> Result<Option<T>, D::Error>
|
fn visit_some<D>(self, deserializer: D) -> Result<Option<T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
Ok(Some(try!(Deserialize::deserialize(deserializer))))
|
Ok(Some(try!(Deserialize::deserialize(deserializer))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Deserialize for Option<T> where T: Deserialize {
|
impl<T> Deserialize for Option<T>
|
||||||
|
where T: Deserialize
|
||||||
|
{
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Option<T>, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Option<T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
deserializer.deserialize_option(OptionVisitor { marker: PhantomData })
|
deserializer.deserialize_option(OptionVisitor { marker: PhantomData })
|
||||||
}
|
}
|
||||||
@@ -377,7 +352,7 @@ impl<T> Visitor for PhantomDataVisitor<T> {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_unit<E>(self) -> Result<PhantomData<T>, E>
|
fn visit_unit<E>(self) -> Result<PhantomData<T>, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(PhantomData)
|
Ok(PhantomData)
|
||||||
}
|
}
|
||||||
@@ -385,7 +360,7 @@ impl<T> Visitor for PhantomDataVisitor<T> {
|
|||||||
|
|
||||||
impl<T> Deserialize for PhantomData<T> {
|
impl<T> Deserialize for PhantomData<T> {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<PhantomData<T>, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<PhantomData<T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let visitor = PhantomDataVisitor { marker: PhantomData };
|
let visitor = PhantomDataVisitor { marker: PhantomData };
|
||||||
deserializer.deserialize_unit_struct("PhantomData", visitor)
|
deserializer.deserialize_unit_struct("PhantomData", visitor)
|
||||||
@@ -524,13 +499,13 @@ struct ArrayVisitor<A> {
|
|||||||
|
|
||||||
impl<A> ArrayVisitor<A> {
|
impl<A> ArrayVisitor<A> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
ArrayVisitor {
|
ArrayVisitor { marker: PhantomData }
|
||||||
marker: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Visitor for ArrayVisitor<[T; 0]> where T: Deserialize {
|
impl<T> Visitor for ArrayVisitor<[T; 0]>
|
||||||
|
where T: Deserialize
|
||||||
|
{
|
||||||
type Value = [T; 0];
|
type Value = [T; 0];
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
@@ -539,14 +514,14 @@ impl<T> Visitor for ArrayVisitor<[T; 0]> where T: Deserialize {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_unit<E>(self) -> Result<[T; 0], E>
|
fn visit_unit<E>(self) -> Result<[T; 0], E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok([])
|
Ok([])
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_seq<V>(self, _: V) -> Result<[T; 0], V::Error>
|
fn visit_seq<V>(self, _: V) -> Result<[T; 0], V::Error>
|
||||||
where V: SeqVisitor,
|
where V: SeqVisitor
|
||||||
{
|
{
|
||||||
Ok([])
|
Ok([])
|
||||||
}
|
}
|
||||||
@@ -556,7 +531,7 @@ impl<T> Deserialize for [T; 0]
|
|||||||
where T: Deserialize
|
where T: Deserialize
|
||||||
{
|
{
|
||||||
fn deserialize<D>(deserializer: D) -> Result<[T; 0], D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<[T; 0], D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
deserializer.deserialize_seq_fixed_size(0, ArrayVisitor::<[T; 0]>::new())
|
deserializer.deserialize_seq_fixed_size(0, ArrayVisitor::<[T; 0]>::new())
|
||||||
}
|
}
|
||||||
@@ -798,7 +773,7 @@ map_impl!(
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::IpAddr {
|
impl Deserialize for net::IpAddr {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let s = try!(String::deserialize(deserializer));
|
let s = try!(String::deserialize(deserializer));
|
||||||
match s.parse() {
|
match s.parse() {
|
||||||
@@ -811,7 +786,7 @@ impl Deserialize for net::IpAddr {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::Ipv4Addr {
|
impl Deserialize for net::Ipv4Addr {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let s = try!(String::deserialize(deserializer));
|
let s = try!(String::deserialize(deserializer));
|
||||||
match s.parse() {
|
match s.parse() {
|
||||||
@@ -824,7 +799,7 @@ impl Deserialize for net::Ipv4Addr {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::Ipv6Addr {
|
impl Deserialize for net::Ipv6Addr {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let s = try!(String::deserialize(deserializer));
|
let s = try!(String::deserialize(deserializer));
|
||||||
match s.parse() {
|
match s.parse() {
|
||||||
@@ -839,7 +814,7 @@ impl Deserialize for net::Ipv6Addr {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::SocketAddr {
|
impl Deserialize for net::SocketAddr {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let s = try!(String::deserialize(deserializer));
|
let s = try!(String::deserialize(deserializer));
|
||||||
match s.parse() {
|
match s.parse() {
|
||||||
@@ -852,7 +827,7 @@ impl Deserialize for net::SocketAddr {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::SocketAddrV4 {
|
impl Deserialize for net::SocketAddrV4 {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let s = try!(String::deserialize(deserializer));
|
let s = try!(String::deserialize(deserializer));
|
||||||
match s.parse() {
|
match s.parse() {
|
||||||
@@ -865,7 +840,7 @@ impl Deserialize for net::SocketAddrV4 {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::SocketAddrV6 {
|
impl Deserialize for net::SocketAddrV6 {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let s = try!(String::deserialize(deserializer));
|
let s = try!(String::deserialize(deserializer));
|
||||||
match s.parse() {
|
match s.parse() {
|
||||||
@@ -889,13 +864,13 @@ impl Visitor for PathBufVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<path::PathBuf, E>
|
fn visit_str<E>(self, v: &str) -> Result<path::PathBuf, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(From::from(v))
|
Ok(From::from(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_string<E>(self, v: String) -> Result<path::PathBuf, E>
|
fn visit_string<E>(self, v: String) -> Result<path::PathBuf, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(From::from(v))
|
Ok(From::from(v))
|
||||||
}
|
}
|
||||||
@@ -904,7 +879,7 @@ impl Visitor for PathBufVisitor {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for path::PathBuf {
|
impl Deserialize for path::PathBuf {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<path::PathBuf, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<path::PathBuf, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
deserializer.deserialize_string(PathBufVisitor)
|
deserializer.deserialize_string(PathBufVisitor)
|
||||||
}
|
}
|
||||||
@@ -915,7 +890,7 @@ impl Deserialize for path::PathBuf {
|
|||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T: Deserialize> Deserialize for Box<T> {
|
impl<T: Deserialize> Deserialize for Box<T> {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Box<T>, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Box<T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let val = try!(Deserialize::deserialize(deserializer));
|
let val = try!(Deserialize::deserialize(deserializer));
|
||||||
Ok(Box::new(val))
|
Ok(Box::new(val))
|
||||||
@@ -925,7 +900,7 @@ impl<T: Deserialize> Deserialize for Box<T> {
|
|||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T: Deserialize> Deserialize for Box<[T]> {
|
impl<T: Deserialize> Deserialize for Box<[T]> {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Box<[T]>, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Box<[T]>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let v: Vec<T> = try!(Deserialize::deserialize(deserializer));
|
let v: Vec<T> = try!(Deserialize::deserialize(deserializer));
|
||||||
Ok(v.into_boxed_slice())
|
Ok(v.into_boxed_slice())
|
||||||
@@ -945,7 +920,7 @@ impl Deserialize for Box<str> {
|
|||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T: Deserialize> Deserialize for Arc<T> {
|
impl<T: Deserialize> Deserialize for Arc<T> {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Arc<T>, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Arc<T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let val = try!(Deserialize::deserialize(deserializer));
|
let val = try!(Deserialize::deserialize(deserializer));
|
||||||
Ok(Arc::new(val))
|
Ok(Arc::new(val))
|
||||||
@@ -955,7 +930,7 @@ impl<T: Deserialize> Deserialize for Arc<T> {
|
|||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T: Deserialize> Deserialize for Rc<T> {
|
impl<T: Deserialize> Deserialize for Rc<T> {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Rc<T>, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Rc<T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let val = try!(Deserialize::deserialize(deserializer));
|
let val = try!(Deserialize::deserialize(deserializer));
|
||||||
Ok(Rc::new(val))
|
Ok(Rc::new(val))
|
||||||
@@ -963,10 +938,13 @@ impl<T: Deserialize> Deserialize for Rc<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deserialize, {
|
impl<'a, T: ?Sized> Deserialize for Cow<'a, T>
|
||||||
|
where T: ToOwned,
|
||||||
|
T::Owned: Deserialize
|
||||||
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Cow<'a, T>, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Cow<'a, T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let val = try!(Deserialize::deserialize(deserializer));
|
let val = try!(Deserialize::deserialize(deserializer));
|
||||||
Ok(Cow::Owned(val))
|
Ok(Cow::Owned(val))
|
||||||
@@ -986,13 +964,16 @@ impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deser
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for Duration {
|
impl Deserialize for Duration {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
enum Field { Secs, Nanos };
|
enum Field {
|
||||||
|
Secs,
|
||||||
|
Nanos,
|
||||||
|
};
|
||||||
|
|
||||||
impl Deserialize for Field {
|
impl Deserialize for Field {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
struct FieldVisitor;
|
struct FieldVisitor;
|
||||||
|
|
||||||
@@ -1004,7 +985,7 @@ impl Deserialize for Duration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, value: &str) -> Result<Field, E>
|
fn visit_str<E>(self, value: &str) -> Result<Field, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
match value {
|
match value {
|
||||||
"secs" => Ok(Field::Secs),
|
"secs" => Ok(Field::Secs),
|
||||||
@@ -1014,7 +995,7 @@ impl Deserialize for Duration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E>
|
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
match value {
|
match value {
|
||||||
b"secs" => Ok(Field::Secs),
|
b"secs" => Ok(Field::Secs),
|
||||||
@@ -1041,7 +1022,7 @@ impl Deserialize for Duration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_seq<V>(self, mut visitor: V) -> Result<Duration, V::Error>
|
fn visit_seq<V>(self, mut visitor: V) -> Result<Duration, V::Error>
|
||||||
where V: SeqVisitor,
|
where V: SeqVisitor
|
||||||
{
|
{
|
||||||
let secs: u64 = match try!(visitor.visit()) {
|
let secs: u64 = match try!(visitor.visit()) {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
@@ -1059,7 +1040,7 @@ impl Deserialize for Duration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_map<V>(self, mut visitor: V) -> Result<Duration, V::Error>
|
fn visit_map<V>(self, mut visitor: V) -> Result<Duration, V::Error>
|
||||||
where V: MapVisitor,
|
where V: MapVisitor
|
||||||
{
|
{
|
||||||
let mut secs: Option<u64> = None;
|
let mut secs: Option<u64> = None;
|
||||||
let mut nanos: Option<u32> = None;
|
let mut nanos: Option<u32> = None;
|
||||||
@@ -1100,24 +1081,30 @@ impl Deserialize for Duration {
|
|||||||
|
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
#[allow(deprecated)] // num::Zero is deprecated but there is no replacement
|
#[allow(deprecated)] // num::Zero is deprecated but there is no replacement
|
||||||
impl<T> Deserialize for NonZero<T> where T: Deserialize + PartialEq + Zeroable + Zero {
|
impl<T> Deserialize for NonZero<T>
|
||||||
fn deserialize<D>(deserializer: D) -> Result<NonZero<T>, D::Error> where D: Deserializer {
|
where T: Deserialize + PartialEq + Zeroable + Zero
|
||||||
|
{
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<NonZero<T>, D::Error>
|
||||||
|
where D: Deserializer
|
||||||
|
{
|
||||||
let value = try!(Deserialize::deserialize(deserializer));
|
let value = try!(Deserialize::deserialize(deserializer));
|
||||||
if value == Zero::zero() {
|
if value == Zero::zero() {
|
||||||
return Err(Error::custom("expected a non-zero value"))
|
return Err(Error::custom("expected a non-zero value"));
|
||||||
}
|
|
||||||
unsafe {
|
|
||||||
Ok(NonZero::new(value))
|
|
||||||
}
|
}
|
||||||
|
unsafe { Ok(NonZero::new(value)) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
|
impl<T, E> Deserialize for Result<T, E>
|
||||||
|
where T: Deserialize,
|
||||||
|
E: Deserialize
|
||||||
|
{
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Result<T, E>, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Result<T, E>, D::Error>
|
||||||
where D: Deserializer {
|
where D: Deserializer
|
||||||
|
{
|
||||||
enum Field {
|
enum Field {
|
||||||
Ok,
|
Ok,
|
||||||
Err,
|
Err,
|
||||||
@@ -1137,15 +1124,21 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
|
|||||||
formatter.write_str("`Ok` or `Err`")
|
formatter.write_str("`Ok` or `Err`")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_u32<E>(self, value: u32) -> Result<Field, E> where E: Error {
|
fn visit_u32<E>(self, value: u32) -> Result<Field, E>
|
||||||
|
where E: Error
|
||||||
|
{
|
||||||
match value {
|
match value {
|
||||||
0 => Ok(Field::Ok),
|
0 => Ok(Field::Ok),
|
||||||
1 => Ok(Field::Err),
|
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 {
|
fn visit_str<E>(self, value: &str) -> Result<Field, E>
|
||||||
|
where E: Error
|
||||||
|
{
|
||||||
match value {
|
match value {
|
||||||
"Ok" => Ok(Field::Ok),
|
"Ok" => Ok(Field::Ok),
|
||||||
"Err" => Ok(Field::Err),
|
"Err" => Ok(Field::Err),
|
||||||
@@ -1153,14 +1146,18 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E> where E: Error {
|
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E>
|
||||||
|
where E: Error
|
||||||
|
{
|
||||||
match value {
|
match value {
|
||||||
b"Ok" => Ok(Field::Ok),
|
b"Ok" => Ok(Field::Ok),
|
||||||
b"Err" => Ok(Field::Err),
|
b"Err" => Ok(Field::Err),
|
||||||
_ => {
|
_ => {
|
||||||
match str::from_utf8(value) {
|
match str::from_utf8(value) {
|
||||||
Ok(value) => Err(Error::unknown_variant(value, VARIANTS)),
|
Ok(value) => Err(Error::unknown_variant(value, VARIANTS)),
|
||||||
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(value), &self)),
|
Err(_) => {
|
||||||
|
Err(Error::invalid_value(Unexpected::Bytes(value), &self))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1208,7 +1205,7 @@ pub struct IgnoredAny;
|
|||||||
impl Deserialize for IgnoredAny {
|
impl Deserialize for IgnoredAny {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
struct IgnoredAnyVisitor;
|
struct IgnoredAnyVisitor;
|
||||||
|
|
||||||
@@ -1241,7 +1238,7 @@ impl Deserialize for IgnoredAny {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_str<E>(self, _: &str) -> Result<IgnoredAny, E>
|
fn visit_str<E>(self, _: &str) -> Result<IgnoredAny, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(IgnoredAny)
|
Ok(IgnoredAny)
|
||||||
}
|
}
|
||||||
@@ -1253,14 +1250,14 @@ impl Deserialize for IgnoredAny {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_some<D>(self, _: D) -> Result<IgnoredAny, D::Error>
|
fn visit_some<D>(self, _: D) -> Result<IgnoredAny, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
Ok(IgnoredAny)
|
Ok(IgnoredAny)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_newtype_struct<D>(self, _: D) -> Result<IgnoredAny, D::Error>
|
fn visit_newtype_struct<D>(self, _: D) -> Result<IgnoredAny, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
Ok(IgnoredAny)
|
Ok(IgnoredAny)
|
||||||
}
|
}
|
||||||
@@ -1272,7 +1269,7 @@ impl Deserialize for IgnoredAny {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_seq<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
|
fn visit_seq<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
|
||||||
where V: SeqVisitor,
|
where V: SeqVisitor
|
||||||
{
|
{
|
||||||
while let Some(_) = try!(visitor.visit::<IgnoredAny>()) {
|
while let Some(_) = try!(visitor.visit::<IgnoredAny>()) {
|
||||||
// Gobble
|
// Gobble
|
||||||
@@ -1282,7 +1279,7 @@ impl Deserialize for IgnoredAny {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_map<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
|
fn visit_map<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
|
||||||
where V: MapVisitor,
|
where V: MapVisitor
|
||||||
{
|
{
|
||||||
while let Some((_, _)) = try!(visitor.visit::<IgnoredAny, IgnoredAny>()) {
|
while let Some((_, _)) = try!(visitor.visit::<IgnoredAny, IgnoredAny>()) {
|
||||||
// Gobble
|
// Gobble
|
||||||
@@ -1292,7 +1289,7 @@ impl Deserialize for IgnoredAny {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_bytes<E>(self, _: &[u8]) -> Result<IgnoredAny, E>
|
fn visit_bytes<E>(self, _: &[u8]) -> Result<IgnoredAny, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Ok(IgnoredAny)
|
Ok(IgnoredAny)
|
||||||
}
|
}
|
||||||
|
|||||||
+102
-93
@@ -181,7 +181,10 @@ pub trait Error: Sized + error::Error {
|
|||||||
write!(formatter, "invalid type: {}, expected {}", self.unexp, self.exp)
|
write!(formatter, "invalid type: {}, expected {}", self.unexp, self.exp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Error::custom(InvalidType { unexp: unexp, exp: exp })
|
Error::custom(InvalidType {
|
||||||
|
unexp: unexp,
|
||||||
|
exp: exp,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Raised when a `Deserialize` receives a value of the right type but that
|
/// Raised when a `Deserialize` receives a value of the right type but that
|
||||||
@@ -207,7 +210,10 @@ pub trait Error: Sized + error::Error {
|
|||||||
write!(formatter, "invalid value: {}, expected {}", self.unexp, self.exp)
|
write!(formatter, "invalid value: {}, expected {}", self.unexp, self.exp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Error::custom(InvalidValue { unexp: unexp, exp: exp })
|
Error::custom(InvalidValue {
|
||||||
|
unexp: unexp,
|
||||||
|
exp: exp,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Raised when deserializing a sequence or map and the input data contains
|
/// Raised when deserializing a sequence or map and the input data contains
|
||||||
@@ -229,7 +235,10 @@ pub trait Error: Sized + error::Error {
|
|||||||
write!(formatter, "invalid length {}, expected {}", self.len, self.exp)
|
write!(formatter, "invalid length {}, expected {}", self.len, self.exp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Error::custom(InvalidLength { len: len, exp: exp })
|
Error::custom(InvalidLength {
|
||||||
|
len: len,
|
||||||
|
exp: exp,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Raised when a `Deserialize` enum type received a variant with an
|
/// Raised when a `Deserialize` enum type received a variant with an
|
||||||
@@ -253,7 +262,10 @@ pub trait Error: Sized + error::Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Error::custom(UnknownVariant { variant: variant, expected: expected })
|
Error::custom(UnknownVariant {
|
||||||
|
variant: variant,
|
||||||
|
expected: expected,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Raised when a `Deserialize` struct type received a field with an
|
/// Raised when a `Deserialize` struct type received a field with an
|
||||||
@@ -277,7 +289,10 @@ pub trait Error: Sized + error::Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Error::custom(UnknownField { field: field, expected: expected })
|
Error::custom(UnknownField {
|
||||||
|
field: field,
|
||||||
|
expected: expected,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Raised when a `Deserialize` struct type expected to receive a required
|
/// Raised when a `Deserialize` struct type expected to receive a required
|
||||||
@@ -470,7 +485,9 @@ pub trait Expected {
|
|||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result;
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Expected for T where T: Visitor {
|
impl<T> Expected for T
|
||||||
|
where T: Visitor
|
||||||
|
{
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.expecting(formatter)
|
self.expecting(formatter)
|
||||||
}
|
}
|
||||||
@@ -521,8 +538,7 @@ pub trait Deserialize: Sized {
|
|||||||
/// manual for more information about how to implement this method.
|
/// manual for more information about how to implement this method.
|
||||||
///
|
///
|
||||||
/// [impl-deserialize]: https://serde.rs/impl-deserialize.html
|
/// [impl-deserialize]: https://serde.rs/impl-deserialize.html
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer;
|
||||||
where D: Deserializer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `DeserializeSeed` is the stateful form of the `Deserialize` trait. If you
|
/// `DeserializeSeed` is the stateful form of the `Deserialize` trait. If you
|
||||||
@@ -670,8 +686,7 @@ pub trait DeserializeSeed: Sized {
|
|||||||
|
|
||||||
/// Equivalent to the more common `Deserialize::deserialize` method, except
|
/// Equivalent to the more common `Deserialize::deserialize` method, except
|
||||||
/// with some initial piece of data (the seed) passed in.
|
/// with some initial piece of data (the seed) passed in.
|
||||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer;
|
||||||
where D: Deserializer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> DeserializeSeed for PhantomData<T>
|
impl<T> DeserializeSeed for PhantomData<T>
|
||||||
@@ -784,56 +799,43 @@ pub trait Deserializer: Sized {
|
|||||||
/// `Deserializer::deserialize` means your data type will be able to
|
/// `Deserializer::deserialize` means your data type will be able to
|
||||||
/// deserialize from self-describing formats only, ruling out Bincode and
|
/// deserialize from self-describing formats only, ruling out Bincode and
|
||||||
/// many others.
|
/// many others.
|
||||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a `bool` value.
|
/// Hint that the `Deserialize` type is expecting a `bool` value.
|
||||||
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a `u8` value.
|
/// Hint that the `Deserialize` type is expecting a `u8` value.
|
||||||
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a `u16` value.
|
/// Hint that the `Deserialize` type is expecting a `u16` value.
|
||||||
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a `u32` value.
|
/// Hint that the `Deserialize` type is expecting a `u32` value.
|
||||||
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a `u64` value.
|
/// Hint that the `Deserialize` type is expecting a `u64` value.
|
||||||
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting an `i8` value.
|
/// Hint that the `Deserialize` type is expecting an `i8` value.
|
||||||
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting an `i16` value.
|
/// Hint that the `Deserialize` type is expecting an `i16` value.
|
||||||
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting an `i32` value.
|
/// Hint that the `Deserialize` type is expecting an `i32` value.
|
||||||
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting an `i64` value.
|
/// Hint that the `Deserialize` type is expecting an `i64` value.
|
||||||
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a `f32` value.
|
/// Hint that the `Deserialize` type is expecting a `f32` value.
|
||||||
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a `f64` value.
|
/// Hint that the `Deserialize` type is expecting a `f64` value.
|
||||||
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a `char` value.
|
/// Hint that the `Deserialize` type is expecting a `char` value.
|
||||||
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a string value and does
|
/// Hint that the `Deserialize` type is expecting a string value and does
|
||||||
/// not benefit from taking ownership of buffered data owned by the
|
/// not benefit from taking ownership of buffered data owned by the
|
||||||
@@ -842,8 +844,7 @@ pub trait Deserializer: Sized {
|
|||||||
/// If the `Visitor` would benefit from taking ownership of `String` data,
|
/// If the `Visitor` would benefit from taking ownership of `String` data,
|
||||||
/// indiciate this to the `Deserializer` by using `deserialize_string`
|
/// indiciate this to the `Deserializer` by using `deserialize_string`
|
||||||
/// instead.
|
/// instead.
|
||||||
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a string value and would
|
/// Hint that the `Deserialize` type is expecting a string value and would
|
||||||
/// benefit from taking ownership of buffered data owned by the
|
/// benefit from taking ownership of buffered data owned by the
|
||||||
@@ -852,8 +853,7 @@ pub trait Deserializer: Sized {
|
|||||||
/// If the `Visitor` would not benefit from taking ownership of `String`
|
/// If the `Visitor` would not benefit from taking ownership of `String`
|
||||||
/// data, indicate that to the `Deserializer` by using `deserialize_str`
|
/// data, indicate that to the `Deserializer` by using `deserialize_str`
|
||||||
/// instead.
|
/// instead.
|
||||||
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a byte array and does not
|
/// Hint that the `Deserialize` type is expecting a byte array and does not
|
||||||
/// benefit from taking ownership of buffered data owned by the
|
/// benefit from taking ownership of buffered data owned by the
|
||||||
@@ -862,8 +862,7 @@ pub trait Deserializer: Sized {
|
|||||||
/// If the `Visitor` would benefit from taking ownership of `Vec<u8>` data,
|
/// If the `Visitor` would benefit from taking ownership of `Vec<u8>` data,
|
||||||
/// indicate this to the `Deserializer` by using `deserialize_byte_buf`
|
/// indicate this to the `Deserializer` by using `deserialize_byte_buf`
|
||||||
/// instead.
|
/// instead.
|
||||||
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a byte array and would
|
/// Hint that the `Deserialize` type is expecting a byte array and would
|
||||||
/// benefit from taking ownership of buffered data owned by the
|
/// benefit from taking ownership of buffered data owned by the
|
||||||
@@ -872,44 +871,43 @@ pub trait Deserializer: Sized {
|
|||||||
/// If the `Visitor` would not benefit from taking ownership of `Vec<u8>`
|
/// If the `Visitor` would not benefit from taking ownership of `Vec<u8>`
|
||||||
/// data, indicate that to the `Deserializer` by using `deserialize_bytes`
|
/// data, indicate that to the `Deserializer` by using `deserialize_bytes`
|
||||||
/// instead.
|
/// instead.
|
||||||
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting an optional value.
|
/// Hint that the `Deserialize` type is expecting an optional value.
|
||||||
///
|
///
|
||||||
/// This allows deserializers that encode an optional value as a nullable
|
/// This allows deserializers that encode an optional value as a nullable
|
||||||
/// value to convert the null value into `None` and a regular value into
|
/// value to convert the null value into `None` and a regular value into
|
||||||
/// `Some(value)`.
|
/// `Some(value)`.
|
||||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a unit value.
|
/// Hint that the `Deserialize` type is expecting a unit value.
|
||||||
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a unit struct with a
|
/// Hint that the `Deserialize` type is expecting a unit struct with a
|
||||||
/// particular name.
|
/// particular name.
|
||||||
fn deserialize_unit_struct<V>(self,
|
fn deserialize_unit_struct<V>(self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V)
|
||||||
|
-> Result<V::Value, Self::Error>
|
||||||
where V: Visitor;
|
where V: Visitor;
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a newtype struct with a
|
/// Hint that the `Deserialize` type is expecting a newtype struct with a
|
||||||
/// particular name.
|
/// particular name.
|
||||||
fn deserialize_newtype_struct<V>(self,
|
fn deserialize_newtype_struct<V>(self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V)
|
||||||
|
-> Result<V::Value, Self::Error>
|
||||||
where V: Visitor;
|
where V: Visitor;
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a sequence of values.
|
/// Hint that the `Deserialize` type is expecting a sequence of values.
|
||||||
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a sequence of values and
|
/// Hint that the `Deserialize` type is expecting a sequence of values and
|
||||||
/// knows how many values there are without looking at the serialized data.
|
/// knows how many values there are without looking at the serialized data.
|
||||||
fn deserialize_seq_fixed_size<V>(self,
|
fn deserialize_seq_fixed_size<V>(self,
|
||||||
len: usize,
|
len: usize,
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V)
|
||||||
|
-> Result<V::Value, Self::Error>
|
||||||
where V: Visitor;
|
where V: Visitor;
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a tuple value with a
|
/// Hint that the `Deserialize` type is expecting a tuple value with a
|
||||||
@@ -922,19 +920,20 @@ pub trait Deserializer: Sized {
|
|||||||
fn deserialize_tuple_struct<V>(self,
|
fn deserialize_tuple_struct<V>(self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V)
|
||||||
|
-> Result<V::Value, Self::Error>
|
||||||
where V: Visitor;
|
where V: Visitor;
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a map of key-value pairs.
|
/// 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>
|
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
|
||||||
where V: Visitor;
|
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a struct with a particular
|
/// Hint that the `Deserialize` type is expecting a struct with a particular
|
||||||
/// name and fields.
|
/// name and fields.
|
||||||
fn deserialize_struct<V>(self,
|
fn deserialize_struct<V>(self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
fields: &'static [&'static str],
|
fields: &'static [&'static str],
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V)
|
||||||
|
-> Result<V::Value, Self::Error>
|
||||||
where V: Visitor;
|
where V: Visitor;
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting the name of a struct
|
/// Hint that the `Deserialize` type is expecting the name of a struct
|
||||||
@@ -947,7 +946,8 @@ pub trait Deserializer: Sized {
|
|||||||
fn deserialize_enum<V>(self,
|
fn deserialize_enum<V>(self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
variants: &'static [&'static str],
|
variants: &'static [&'static str],
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V)
|
||||||
|
-> Result<V::Value, Self::Error>
|
||||||
where V: Visitor;
|
where V: Visitor;
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type needs to deserialize a value whose type
|
/// Hint that the `Deserialize` type needs to deserialize a value whose type
|
||||||
@@ -1016,77 +1016,77 @@ pub trait Visitor: Sized {
|
|||||||
|
|
||||||
/// Deserialize a `bool` into a `Value`.
|
/// Deserialize a `bool` into a `Value`.
|
||||||
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
|
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))
|
Err(Error::invalid_type(Unexpected::Bool(v), &self))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize an `i8` into a `Value`.
|
/// Deserialize an `i8` into a `Value`.
|
||||||
fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
|
fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
self.visit_i64(v as i64)
|
self.visit_i64(v as i64)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize an `i16` into a `Value`.
|
/// Deserialize an `i16` into a `Value`.
|
||||||
fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
|
fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
self.visit_i64(v as i64)
|
self.visit_i64(v as i64)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize an `i32` into a `Value`.
|
/// Deserialize an `i32` into a `Value`.
|
||||||
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
|
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
self.visit_i64(v as i64)
|
self.visit_i64(v as i64)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize an `i64` into a `Value`.
|
/// Deserialize an `i64` into a `Value`.
|
||||||
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
|
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))
|
Err(Error::invalid_type(Unexpected::Signed(v), &self))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a `u8` into a `Value`.
|
/// Deserialize a `u8` into a `Value`.
|
||||||
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
|
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
self.visit_u64(v as u64)
|
self.visit_u64(v as u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a `u16` into a `Value`.
|
/// Deserialize a `u16` into a `Value`.
|
||||||
fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
|
fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
self.visit_u64(v as u64)
|
self.visit_u64(v as u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a `u32` into a `Value`.
|
/// Deserialize a `u32` into a `Value`.
|
||||||
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
|
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
self.visit_u64(v as u64)
|
self.visit_u64(v as u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a `u64` into a `Value`.
|
/// Deserialize a `u64` into a `Value`.
|
||||||
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
|
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))
|
Err(Error::invalid_type(Unexpected::Unsigned(v), &self))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a `f32` into a `Value`.
|
/// Deserialize a `f32` into a `Value`.
|
||||||
fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
|
fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
self.visit_f64(v as f64)
|
self.visit_f64(v as f64)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a `f64` into a `Value`.
|
/// Deserialize a `f64` into a `Value`.
|
||||||
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
|
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))
|
Err(Error::invalid_type(Unexpected::Float(v), &self))
|
||||||
}
|
}
|
||||||
@@ -1094,7 +1094,7 @@ pub trait Visitor: Sized {
|
|||||||
/// Deserialize a `char` into a `Value`.
|
/// Deserialize a `char` into a `Value`.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
|
fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
self.visit_str(::utils::encode_utf8(v).as_str())
|
self.visit_str(::utils::encode_utf8(v).as_str())
|
||||||
}
|
}
|
||||||
@@ -1110,7 +1110,7 @@ pub trait Visitor: Sized {
|
|||||||
/// It is never correct to implement `visit_string` without implementing
|
/// It is never correct to implement `visit_string` without implementing
|
||||||
/// `visit_str`. Implement neither, both, or just `visit_str`.
|
/// `visit_str`. Implement neither, both, or just `visit_str`.
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
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))
|
Err(Error::invalid_type(Unexpected::Str(v), &self))
|
||||||
}
|
}
|
||||||
@@ -1132,28 +1132,28 @@ pub trait Visitor: Sized {
|
|||||||
#[inline]
|
#[inline]
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
self.visit_str(&v)
|
self.visit_str(&v)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a `()` into a `Value`.
|
/// Deserialize a `()` into a `Value`.
|
||||||
fn visit_unit<E>(self) -> Result<Self::Value, E>
|
fn visit_unit<E>(self) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Err(Error::invalid_type(Unexpected::Unit, &self))
|
Err(Error::invalid_type(Unexpected::Unit, &self))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize an absent optional `Value`.
|
/// Deserialize an absent optional `Value`.
|
||||||
fn visit_none<E>(self) -> Result<Self::Value, E>
|
fn visit_none<E>(self) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
Err(Error::invalid_type(Unexpected::Option, &self))
|
Err(Error::invalid_type(Unexpected::Option, &self))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a present optional `Value`.
|
/// Deserialize a present optional `Value`.
|
||||||
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let _ = deserializer;
|
let _ = deserializer;
|
||||||
Err(Error::invalid_type(Unexpected::Option, &self))
|
Err(Error::invalid_type(Unexpected::Option, &self))
|
||||||
@@ -1161,7 +1161,7 @@ pub trait Visitor: Sized {
|
|||||||
|
|
||||||
/// Deserialize `Value` as a newtype struct.
|
/// Deserialize `Value` as a newtype struct.
|
||||||
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
let _ = deserializer;
|
let _ = deserializer;
|
||||||
Err(Error::invalid_type(Unexpected::NewtypeStruct, &self))
|
Err(Error::invalid_type(Unexpected::NewtypeStruct, &self))
|
||||||
@@ -1169,7 +1169,7 @@ pub trait Visitor: Sized {
|
|||||||
|
|
||||||
/// Deserialize `Value` as a sequence of elements.
|
/// Deserialize `Value` as a sequence of elements.
|
||||||
fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
||||||
where V: SeqVisitor,
|
where V: SeqVisitor
|
||||||
{
|
{
|
||||||
let _ = visitor;
|
let _ = visitor;
|
||||||
Err(Error::invalid_type(Unexpected::Seq, &self))
|
Err(Error::invalid_type(Unexpected::Seq, &self))
|
||||||
@@ -1177,7 +1177,7 @@ pub trait Visitor: Sized {
|
|||||||
|
|
||||||
/// Deserialize `Value` as a key-value map.
|
/// Deserialize `Value` as a key-value map.
|
||||||
fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
||||||
where V: MapVisitor,
|
where V: MapVisitor
|
||||||
{
|
{
|
||||||
let _ = visitor;
|
let _ = visitor;
|
||||||
Err(Error::invalid_type(Unexpected::Map, &self))
|
Err(Error::invalid_type(Unexpected::Map, &self))
|
||||||
@@ -1185,7 +1185,7 @@ pub trait Visitor: Sized {
|
|||||||
|
|
||||||
/// Deserialize `Value` as an enum.
|
/// Deserialize `Value` as an enum.
|
||||||
fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
||||||
where V: EnumVisitor,
|
where V: EnumVisitor
|
||||||
{
|
{
|
||||||
let _ = visitor;
|
let _ = visitor;
|
||||||
Err(Error::invalid_type(Unexpected::Enum, &self))
|
Err(Error::invalid_type(Unexpected::Enum, &self))
|
||||||
@@ -1202,7 +1202,7 @@ pub trait Visitor: Sized {
|
|||||||
/// It is never correct to implement `visit_byte_buf` without implementing
|
/// It is never correct to implement `visit_byte_buf` without implementing
|
||||||
/// `visit_bytes`. Implement neither, both, or just `visit_bytes`.
|
/// `visit_bytes`. Implement neither, both, or just `visit_bytes`.
|
||||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
let _ = v;
|
let _ = v;
|
||||||
Err(Error::invalid_type(Unexpected::Bytes(v), &self))
|
Err(Error::invalid_type(Unexpected::Bytes(v), &self))
|
||||||
@@ -1225,7 +1225,7 @@ pub trait Visitor: Sized {
|
|||||||
/// `Vec<u8>`.
|
/// `Vec<u8>`.
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
|
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error
|
||||||
{
|
{
|
||||||
self.visit_bytes(&v)
|
self.visit_bytes(&v)
|
||||||
}
|
}
|
||||||
@@ -1269,7 +1269,9 @@ pub trait SeqVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, V> SeqVisitor for &'a mut V where V: SeqVisitor {
|
impl<'a, V> SeqVisitor for &'a mut V
|
||||||
|
where V: SeqVisitor
|
||||||
|
{
|
||||||
type Error = V::Error;
|
type Error = V::Error;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -1326,7 +1328,10 @@ pub trait MapVisitor {
|
|||||||
/// `Deserialize` implementations should typically use `MapVisitor::visit`
|
/// `Deserialize` implementations should typically use `MapVisitor::visit`
|
||||||
/// instead.
|
/// instead.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_seed<K, V>(&mut self, kseed: K, vseed: V) -> Result<Option<(K::Value, V::Value)>, Self::Error>
|
fn visit_seed<K, V>(&mut self,
|
||||||
|
kseed: K,
|
||||||
|
vseed: V)
|
||||||
|
-> Result<Option<(K::Value, V::Value)>, Self::Error>
|
||||||
where K: DeserializeSeed,
|
where K: DeserializeSeed,
|
||||||
V: DeserializeSeed
|
V: DeserializeSeed
|
||||||
{
|
{
|
||||||
@@ -1335,7 +1340,7 @@ pub trait MapVisitor {
|
|||||||
let value = try!(self.visit_value_seed(vseed));
|
let value = try!(self.visit_value_seed(vseed));
|
||||||
Ok(Some((key, value)))
|
Ok(Some((key, value)))
|
||||||
}
|
}
|
||||||
None => Ok(None)
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1370,7 +1375,7 @@ pub trait MapVisitor {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, Self::Error>
|
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, Self::Error>
|
||||||
where K: Deserialize,
|
where K: Deserialize,
|
||||||
V: Deserialize,
|
V: Deserialize
|
||||||
{
|
{
|
||||||
self.visit_seed(PhantomData, PhantomData)
|
self.visit_seed(PhantomData, PhantomData)
|
||||||
}
|
}
|
||||||
@@ -1382,7 +1387,9 @@ pub trait MapVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
|
impl<'a, V_> MapVisitor for &'a mut V_
|
||||||
|
where V_: MapVisitor
|
||||||
|
{
|
||||||
type Error = V_::Error;
|
type Error = V_::Error;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -1400,7 +1407,10 @@ impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_seed<K, V>(&mut self, kseed: K, vseed: V) -> Result<Option<(K::Value, V::Value)>, Self::Error>
|
fn visit_seed<K, V>(&mut self,
|
||||||
|
kseed: K,
|
||||||
|
vseed: V)
|
||||||
|
-> Result<Option<(K::Value, V::Value)>, Self::Error>
|
||||||
where K: DeserializeSeed,
|
where K: DeserializeSeed,
|
||||||
V: DeserializeSeed
|
V: DeserializeSeed
|
||||||
{
|
{
|
||||||
@@ -1410,7 +1420,7 @@ impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, V_::Error>
|
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, V_::Error>
|
||||||
where K: Deserialize,
|
where K: Deserialize,
|
||||||
V: Deserialize,
|
V: Deserialize
|
||||||
{
|
{
|
||||||
(**self).visit()
|
(**self).visit()
|
||||||
}
|
}
|
||||||
@@ -1446,7 +1456,7 @@ pub trait EnumVisitor: Sized {
|
|||||||
type Error: Error;
|
type Error: Error;
|
||||||
/// The `Visitor` that will be used to deserialize the content of the enum
|
/// The `Visitor` that will be used to deserialize the content of the enum
|
||||||
/// variant.
|
/// variant.
|
||||||
type Variant: VariantVisitor<Error=Self::Error>;
|
type Variant: VariantVisitor<Error = Self::Error>;
|
||||||
|
|
||||||
/// `visit_variant` is called to identify which variant to deserialize.
|
/// `visit_variant` is called to identify which variant to deserialize.
|
||||||
///
|
///
|
||||||
@@ -1539,9 +1549,7 @@ pub trait VariantVisitor: Sized {
|
|||||||
/// Err(Error::invalid_type(unexp, &"tuple variant"))
|
/// Err(Error::invalid_type(unexp, &"tuple variant"))
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
fn visit_tuple<V>(self,
|
fn visit_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
len: usize,
|
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
|
||||||
where V: Visitor;
|
where V: Visitor;
|
||||||
|
|
||||||
/// Called when deserializing a struct-like variant.
|
/// Called when deserializing a struct-like variant.
|
||||||
@@ -1564,7 +1572,8 @@ pub trait VariantVisitor: Sized {
|
|||||||
/// ```
|
/// ```
|
||||||
fn visit_struct<V>(self,
|
fn visit_struct<V>(self,
|
||||||
fields: &'static [&'static str],
|
fields: &'static [&'static str],
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V)
|
||||||
|
-> Result<V::Value, Self::Error>
|
||||||
where V: Visitor;
|
where V: Visitor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,14 +3,8 @@ use core::marker::PhantomData;
|
|||||||
use de::{Deserialize, Deserializer, Error, Visitor};
|
use de::{Deserialize, Deserializer, Error, Visitor};
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
pub use de::content::{
|
pub use de::content::{Content, ContentRefDeserializer, ContentDeserializer, TaggedContentVisitor,
|
||||||
Content,
|
InternallyTaggedUnitVisitor, UntaggedUnitVisitor};
|
||||||
ContentRefDeserializer,
|
|
||||||
ContentDeserializer,
|
|
||||||
TaggedContentVisitor,
|
|
||||||
InternallyTaggedUnitVisitor,
|
|
||||||
UntaggedUnitVisitor,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// If the missing field is of type `Option<T>` then treat is as `None`,
|
/// If the missing field is of type `Option<T>` then treat is as `None`,
|
||||||
/// otherwise it is an error.
|
/// otherwise it is an error.
|
||||||
|
|||||||
+100
-110
@@ -1,31 +1,15 @@
|
|||||||
//! This module supports deserializing from primitives with the `ValueDeserializer` trait.
|
//! This module supports deserializing from primitives with the `ValueDeserializer` trait.
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::collections::{
|
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, btree_map, btree_set, hash_map,
|
||||||
BTreeMap,
|
hash_set};
|
||||||
BTreeSet,
|
|
||||||
HashMap,
|
|
||||||
HashSet,
|
|
||||||
btree_map,
|
|
||||||
btree_set,
|
|
||||||
hash_map,
|
|
||||||
hash_set,
|
|
||||||
};
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::vec;
|
use std::vec;
|
||||||
|
|
||||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
use collections::{
|
use collections::{BTreeMap, BTreeSet, Vec, String, btree_map, btree_set, vec};
|
||||||
BTreeMap,
|
|
||||||
BTreeSet,
|
|
||||||
Vec,
|
|
||||||
String,
|
|
||||||
btree_map,
|
|
||||||
btree_set,
|
|
||||||
vec,
|
|
||||||
};
|
|
||||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
use collections::borrow::Cow;
|
use collections::borrow::Cow;
|
||||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
@@ -41,7 +25,7 @@ use std::error;
|
|||||||
use error;
|
use error;
|
||||||
|
|
||||||
use core::fmt::{self, Display};
|
use core::fmt::{self, Display};
|
||||||
use core::iter::{self, Iterator};
|
use core::iter::{self, Iterator};
|
||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
use de::{self, Expected, SeqVisitor};
|
use de::{self, Expected, SeqVisitor};
|
||||||
@@ -63,16 +47,12 @@ type ErrorImpl = ();
|
|||||||
impl de::Error for Error {
|
impl de::Error for Error {
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn custom<T: Display>(msg: T) -> Self {
|
fn custom<T: Display>(msg: T) -> Self {
|
||||||
Error {
|
Error { err: msg.to_string().into_boxed_str() }
|
||||||
err: msg.to_string().into_boxed_str(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||||
fn custom<T: Display>(_msg: T) -> Self {
|
fn custom<T: Display>(_msg: T) -> Self {
|
||||||
Error {
|
Error { err: () }
|
||||||
err: (),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +85,7 @@ impl error::Error for Error {
|
|||||||
/// This trait converts primitive types into a deserializer.
|
/// This trait converts primitive types into a deserializer.
|
||||||
pub trait ValueDeserializer<E: de::Error = Error> {
|
pub trait ValueDeserializer<E: de::Error = Error> {
|
||||||
/// The actual deserializer type.
|
/// The actual deserializer type.
|
||||||
type Deserializer: de::Deserializer<Error=E>;
|
type Deserializer: de::Deserializer<Error = E>;
|
||||||
|
|
||||||
/// Convert this value into a deserializer.
|
/// Convert this value into a deserializer.
|
||||||
fn into_deserializer(self) -> Self::Deserializer;
|
fn into_deserializer(self) -> Self::Deserializer;
|
||||||
@@ -114,14 +94,12 @@ pub trait ValueDeserializer<E: de::Error = Error> {
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
impl<E> ValueDeserializer<E> for ()
|
impl<E> ValueDeserializer<E> for ()
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = UnitDeserializer<E>;
|
type Deserializer = UnitDeserializer<E>;
|
||||||
|
|
||||||
fn into_deserializer(self) -> UnitDeserializer<E> {
|
fn into_deserializer(self) -> UnitDeserializer<E> {
|
||||||
UnitDeserializer {
|
UnitDeserializer { marker: PhantomData }
|
||||||
marker: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,13 +120,13 @@ impl<E> de::Deserializer for UnitDeserializer<E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
visitor.visit_unit()
|
visitor.visit_unit()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
visitor.visit_none()
|
visitor.visit_none()
|
||||||
}
|
}
|
||||||
@@ -221,7 +199,7 @@ pub struct StrDeserializer<'a, E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E> ValueDeserializer<E> for &'a str
|
impl<'a, E> ValueDeserializer<E> for &'a str
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = StrDeserializer<'a, E>;
|
type Deserializer = StrDeserializer<'a, E>;
|
||||||
|
|
||||||
@@ -234,21 +212,22 @@ impl<'a, E> ValueDeserializer<E> for &'a str
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E> de::Deserializer for StrDeserializer<'a, E>
|
impl<'a, E> de::Deserializer for StrDeserializer<'a, E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
visitor.visit_str(self.value)
|
visitor.visit_str(self.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_enum<V>(self,
|
fn deserialize_enum<V>(self,
|
||||||
_name: &str,
|
_name: &str,
|
||||||
_variants: &'static [&'static str],
|
_variants: &'static [&'static str],
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V)
|
||||||
where V: de::Visitor,
|
-> Result<V::Value, Self::Error>
|
||||||
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
visitor.visit_enum(self)
|
visitor.visit_enum(self)
|
||||||
}
|
}
|
||||||
@@ -261,13 +240,13 @@ impl<'a, E> de::Deserializer for StrDeserializer<'a, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E> de::EnumVisitor for StrDeserializer<'a, E>
|
impl<'a, E> de::EnumVisitor for StrDeserializer<'a, E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Variant = private::UnitOnly<E>;
|
type Variant = private::UnitOnly<E>;
|
||||||
|
|
||||||
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
||||||
where T: de::DeserializeSeed,
|
where T: de::DeserializeSeed
|
||||||
{
|
{
|
||||||
seed.deserialize(self).map(private::unit_only)
|
seed.deserialize(self).map(private::unit_only)
|
||||||
}
|
}
|
||||||
@@ -284,7 +263,7 @@ pub struct StringDeserializer<E> {
|
|||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<E> ValueDeserializer<E> for String
|
impl<E> ValueDeserializer<E> for String
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = StringDeserializer<E>;
|
type Deserializer = StringDeserializer<E>;
|
||||||
|
|
||||||
@@ -298,21 +277,22 @@ impl<E> ValueDeserializer<E> for String
|
|||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<E> de::Deserializer for StringDeserializer<E>
|
impl<E> de::Deserializer for StringDeserializer<E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
visitor.visit_string(self.value)
|
visitor.visit_string(self.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_enum<V>(self,
|
fn deserialize_enum<V>(self,
|
||||||
_name: &str,
|
_name: &str,
|
||||||
_variants: &'static [&'static str],
|
_variants: &'static [&'static str],
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V)
|
||||||
where V: de::Visitor,
|
-> Result<V::Value, Self::Error>
|
||||||
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
visitor.visit_enum(self)
|
visitor.visit_enum(self)
|
||||||
}
|
}
|
||||||
@@ -326,13 +306,13 @@ impl<E> de::Deserializer for StringDeserializer<E>
|
|||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<'a, E> de::EnumVisitor for StringDeserializer<E>
|
impl<'a, E> de::EnumVisitor for StringDeserializer<E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Variant = private::UnitOnly<E>;
|
type Variant = private::UnitOnly<E>;
|
||||||
|
|
||||||
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
||||||
where T: de::DeserializeSeed,
|
where T: de::DeserializeSeed
|
||||||
{
|
{
|
||||||
seed.deserialize(self).map(private::unit_only)
|
seed.deserialize(self).map(private::unit_only)
|
||||||
}
|
}
|
||||||
@@ -349,7 +329,7 @@ pub struct CowStrDeserializer<'a, E> {
|
|||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<'a, E> ValueDeserializer<E> for Cow<'a, str>
|
impl<'a, E> ValueDeserializer<E> for Cow<'a, str>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = CowStrDeserializer<'a, E>;
|
type Deserializer = CowStrDeserializer<'a, E>;
|
||||||
|
|
||||||
@@ -363,12 +343,12 @@ impl<'a, E> ValueDeserializer<E> for Cow<'a, str>
|
|||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E>
|
impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
match self.value {
|
match self.value {
|
||||||
Cow::Borrowed(string) => visitor.visit_str(string),
|
Cow::Borrowed(string) => visitor.visit_str(string),
|
||||||
@@ -377,10 +357,11 @@ impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_enum<V>(self,
|
fn deserialize_enum<V>(self,
|
||||||
_name: &str,
|
_name: &str,
|
||||||
_variants: &'static [&'static str],
|
_variants: &'static [&'static str],
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V)
|
||||||
where V: de::Visitor,
|
-> Result<V::Value, Self::Error>
|
||||||
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
visitor.visit_enum(self)
|
visitor.visit_enum(self)
|
||||||
}
|
}
|
||||||
@@ -394,13 +375,13 @@ impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E>
|
|||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<'a, E> de::EnumVisitor for CowStrDeserializer<'a, E>
|
impl<'a, E> de::EnumVisitor for CowStrDeserializer<'a, E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Variant = private::UnitOnly<E>;
|
type Variant = private::UnitOnly<E>;
|
||||||
|
|
||||||
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
|
||||||
where T: de::DeserializeSeed,
|
where T: de::DeserializeSeed
|
||||||
{
|
{
|
||||||
seed.deserialize(self).map(private::unit_only)
|
seed.deserialize(self).map(private::unit_only)
|
||||||
}
|
}
|
||||||
@@ -417,7 +398,7 @@ pub struct SeqDeserializer<I, E> {
|
|||||||
|
|
||||||
impl<I, E> SeqDeserializer<I, E>
|
impl<I, E> SeqDeserializer<I, E>
|
||||||
where I: Iterator,
|
where I: Iterator,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
/// Construct a new `SeqDeserializer<I>`.
|
/// Construct a new `SeqDeserializer<I>`.
|
||||||
pub fn new(iter: I) -> Self {
|
pub fn new(iter: I) -> Self {
|
||||||
@@ -446,14 +427,14 @@ impl<I, E> SeqDeserializer<I, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<I, T, E> de::Deserializer for SeqDeserializer<I, E>
|
impl<I, T, E> de::Deserializer for SeqDeserializer<I, E>
|
||||||
where I: Iterator<Item=T>,
|
where I: Iterator<Item = T>,
|
||||||
T: ValueDeserializer<E>,
|
T: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn deserialize<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
let v = try!(visitor.visit_seq(&mut self));
|
let v = try!(visitor.visit_seq(&mut self));
|
||||||
try!(self.end());
|
try!(self.end());
|
||||||
@@ -468,9 +449,9 @@ impl<I, T, E> de::Deserializer for SeqDeserializer<I, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<I, T, E> de::SeqVisitor for SeqDeserializer<I, E>
|
impl<I, T, E> de::SeqVisitor for SeqDeserializer<I, E>
|
||||||
where I: Iterator<Item=T>,
|
where I: Iterator<Item = T>,
|
||||||
T: ValueDeserializer<E>,
|
T: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
@@ -508,7 +489,7 @@ impl Expected for ExpectedInSeq {
|
|||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T, E> ValueDeserializer<E> for Vec<T>
|
impl<T, E> ValueDeserializer<E> for Vec<T>
|
||||||
where T: ValueDeserializer<E>,
|
where T: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = SeqDeserializer<vec::IntoIter<T>, E>;
|
type Deserializer = SeqDeserializer<vec::IntoIter<T>, E>;
|
||||||
|
|
||||||
@@ -520,7 +501,7 @@ impl<T, E> ValueDeserializer<E> for Vec<T>
|
|||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T, E> ValueDeserializer<E> for BTreeSet<T>
|
impl<T, E> ValueDeserializer<E> for BTreeSet<T>
|
||||||
where T: ValueDeserializer<E> + Eq + Ord,
|
where T: ValueDeserializer<E> + Eq + Ord,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = SeqDeserializer<btree_set::IntoIter<T>, E>;
|
type Deserializer = SeqDeserializer<btree_set::IntoIter<T>, E>;
|
||||||
|
|
||||||
@@ -532,7 +513,7 @@ impl<T, E> ValueDeserializer<E> for BTreeSet<T>
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl<T, E> ValueDeserializer<E> for HashSet<T>
|
impl<T, E> ValueDeserializer<E> for HashSet<T>
|
||||||
where T: ValueDeserializer<E> + Eq + Hash,
|
where T: ValueDeserializer<E> + Eq + Hash,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = SeqDeserializer<hash_set::IntoIter<T>, E>;
|
type Deserializer = SeqDeserializer<hash_set::IntoIter<T>, E>;
|
||||||
|
|
||||||
@@ -551,20 +532,20 @@ pub struct SeqVisitorDeserializer<V_, E> {
|
|||||||
|
|
||||||
impl<V_, E> SeqVisitorDeserializer<V_, E>
|
impl<V_, E> SeqVisitorDeserializer<V_, E>
|
||||||
where V_: de::SeqVisitor<Error = E>,
|
where V_: de::SeqVisitor<Error = E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
/// Construct a new `SeqVisitorDeserializer<V_, E>`.
|
/// Construct a new `SeqVisitorDeserializer<V_, E>`.
|
||||||
pub fn new(visitor: V_) -> Self {
|
pub fn new(visitor: V_) -> Self {
|
||||||
SeqVisitorDeserializer{
|
SeqVisitorDeserializer {
|
||||||
visitor: visitor,
|
visitor: visitor,
|
||||||
marker: PhantomData
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V_, E> de::Deserializer for SeqVisitorDeserializer<V_, E>
|
impl<V_, E> de::Deserializer for SeqVisitorDeserializer<V_, E>
|
||||||
where V_: de::SeqVisitor<Error = E>,
|
where V_: de::SeqVisitor<Error = E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
@@ -587,7 +568,7 @@ pub struct MapDeserializer<I, E>
|
|||||||
I::Item: private::Pair,
|
I::Item: private::Pair,
|
||||||
<I::Item as private::Pair>::First: ValueDeserializer<E>,
|
<I::Item as private::Pair>::First: ValueDeserializer<E>,
|
||||||
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
|
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
iter: iter::Fuse<I>,
|
iter: iter::Fuse<I>,
|
||||||
value: Option<<I::Item as private::Pair>::Second>,
|
value: Option<<I::Item as private::Pair>::Second>,
|
||||||
@@ -600,7 +581,7 @@ impl<I, E> MapDeserializer<I, E>
|
|||||||
I::Item: private::Pair,
|
I::Item: private::Pair,
|
||||||
<I::Item as private::Pair>::First: ValueDeserializer<E>,
|
<I::Item as private::Pair>::First: ValueDeserializer<E>,
|
||||||
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
|
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
/// Construct a new `MapDeserializer<I, K, V, E>`.
|
/// Construct a new `MapDeserializer<I, K, V, E>`.
|
||||||
pub fn new(iter: I) -> Self {
|
pub fn new(iter: I) -> Self {
|
||||||
@@ -628,7 +609,9 @@ impl<I, E> MapDeserializer<I, E>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_pair(&mut self) -> Option<(<I::Item as private::Pair>::First, <I::Item as private::Pair>::Second)> {
|
fn next_pair
|
||||||
|
(&mut self)
|
||||||
|
-> Option<(<I::Item as private::Pair>::First, <I::Item as private::Pair>::Second)> {
|
||||||
match self.iter.next() {
|
match self.iter.next() {
|
||||||
Some(kv) => {
|
Some(kv) => {
|
||||||
self.count += 1;
|
self.count += 1;
|
||||||
@@ -644,12 +627,12 @@ impl<I, E> de::Deserializer for MapDeserializer<I, E>
|
|||||||
I::Item: private::Pair,
|
I::Item: private::Pair,
|
||||||
<I::Item as private::Pair>::First: ValueDeserializer<E>,
|
<I::Item as private::Pair>::First: ValueDeserializer<E>,
|
||||||
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
|
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn deserialize<V_>(mut self, visitor: V_) -> Result<V_::Value, Self::Error>
|
fn deserialize<V_>(mut self, visitor: V_) -> Result<V_::Value, Self::Error>
|
||||||
where V_: de::Visitor,
|
where V_: de::Visitor
|
||||||
{
|
{
|
||||||
let value = try!(visitor.visit_map(&mut self));
|
let value = try!(visitor.visit_map(&mut self));
|
||||||
try!(self.end());
|
try!(self.end());
|
||||||
@@ -657,15 +640,18 @@ impl<I, E> de::Deserializer for MapDeserializer<I, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_seq<V_>(mut self, visitor: V_) -> Result<V_::Value, Self::Error>
|
fn deserialize_seq<V_>(mut self, visitor: V_) -> Result<V_::Value, Self::Error>
|
||||||
where V_: de::Visitor,
|
where V_: de::Visitor
|
||||||
{
|
{
|
||||||
let value = try!(visitor.visit_seq(&mut self));
|
let value = try!(visitor.visit_seq(&mut self));
|
||||||
try!(self.end());
|
try!(self.end());
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_seq_fixed_size<V_>(self, _len: usize, visitor: V_) -> Result<V_::Value, Self::Error>
|
fn deserialize_seq_fixed_size<V_>(self,
|
||||||
where V_: de::Visitor,
|
_len: usize,
|
||||||
|
visitor: V_)
|
||||||
|
-> Result<V_::Value, Self::Error>
|
||||||
|
where V_: de::Visitor
|
||||||
{
|
{
|
||||||
self.deserialize_seq(visitor)
|
self.deserialize_seq(visitor)
|
||||||
}
|
}
|
||||||
@@ -682,12 +668,12 @@ impl<I, E> de::MapVisitor for MapDeserializer<I, E>
|
|||||||
I::Item: private::Pair,
|
I::Item: private::Pair,
|
||||||
<I::Item as private::Pair>::First: ValueDeserializer<E>,
|
<I::Item as private::Pair>::First: ValueDeserializer<E>,
|
||||||
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
|
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn visit_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
fn visit_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
||||||
where T: de::DeserializeSeed,
|
where T: de::DeserializeSeed
|
||||||
{
|
{
|
||||||
match self.next_pair() {
|
match self.next_pair() {
|
||||||
Some((key, value)) => {
|
Some((key, value)) => {
|
||||||
@@ -699,7 +685,7 @@ impl<I, E> de::MapVisitor for MapDeserializer<I, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
|
fn visit_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
|
||||||
where T: de::DeserializeSeed,
|
where T: de::DeserializeSeed
|
||||||
{
|
{
|
||||||
let value = self.value.take();
|
let value = self.value.take();
|
||||||
// Panic because this indicates a bug in the program rather than an
|
// Panic because this indicates a bug in the program rather than an
|
||||||
@@ -708,7 +694,10 @@ impl<I, E> de::MapVisitor for MapDeserializer<I, E>
|
|||||||
seed.deserialize(value.into_deserializer())
|
seed.deserialize(value.into_deserializer())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_seed<TK, TV>(&mut self, kseed: TK, vseed: TV) -> Result<Option<(TK::Value, TV::Value)>, Self::Error>
|
fn visit_seed<TK, TV>(&mut self,
|
||||||
|
kseed: TK,
|
||||||
|
vseed: TV)
|
||||||
|
-> Result<Option<(TK::Value, TV::Value)>, Self::Error>
|
||||||
where TK: de::DeserializeSeed,
|
where TK: de::DeserializeSeed,
|
||||||
TV: de::DeserializeSeed
|
TV: de::DeserializeSeed
|
||||||
{
|
{
|
||||||
@@ -718,7 +707,7 @@ impl<I, E> de::MapVisitor for MapDeserializer<I, E>
|
|||||||
let value = try!(vseed.deserialize(value.into_deserializer()));
|
let value = try!(vseed.deserialize(value.into_deserializer()));
|
||||||
Ok(Some((key, value)))
|
Ok(Some((key, value)))
|
||||||
}
|
}
|
||||||
None => Ok(None)
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -732,12 +721,12 @@ impl<I, E> de::SeqVisitor for MapDeserializer<I, E>
|
|||||||
I::Item: private::Pair,
|
I::Item: private::Pair,
|
||||||
<I::Item as private::Pair>::First: ValueDeserializer<E>,
|
<I::Item as private::Pair>::First: ValueDeserializer<E>,
|
||||||
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
|
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
||||||
where T: de::DeserializeSeed,
|
where T: de::DeserializeSeed
|
||||||
{
|
{
|
||||||
match self.next_pair() {
|
match self.next_pair() {
|
||||||
Some((k, v)) => {
|
Some((k, v)) => {
|
||||||
@@ -771,13 +760,13 @@ impl<A, B, E> de::Deserializer for PairDeserializer<A, B, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
self.deserialize_seq(visitor)
|
self.deserialize_seq(visitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData);
|
let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData);
|
||||||
let pair = try!(visitor.visit_seq(&mut pair_visitor));
|
let pair = try!(visitor.visit_seq(&mut pair_visitor));
|
||||||
@@ -792,7 +781,7 @@ impl<A, B, E> de::Deserializer for PairDeserializer<A, B, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_seq_fixed_size<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_seq_fixed_size<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
if len == 2 {
|
if len == 2 {
|
||||||
self.deserialize_seq(visitor)
|
self.deserialize_seq(visitor)
|
||||||
@@ -809,12 +798,12 @@ struct PairVisitor<A, B, E>(Option<A>, Option<B>, PhantomData<E>);
|
|||||||
impl<A, B, E> de::SeqVisitor for PairVisitor<A, B, E>
|
impl<A, B, E> de::SeqVisitor for PairVisitor<A, B, E>
|
||||||
where A: ValueDeserializer<E>,
|
where A: ValueDeserializer<E>,
|
||||||
B: ValueDeserializer<E>,
|
B: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
||||||
where T: de::DeserializeSeed,
|
where T: de::DeserializeSeed
|
||||||
{
|
{
|
||||||
if let Some(k) = self.0.take() {
|
if let Some(k) = self.0.take() {
|
||||||
seed.deserialize(k.into_deserializer()).map(Some)
|
seed.deserialize(k.into_deserializer()).map(Some)
|
||||||
@@ -855,7 +844,7 @@ impl Expected for ExpectedInMap {
|
|||||||
impl<K, V, E> ValueDeserializer<E> for BTreeMap<K, V>
|
impl<K, V, E> ValueDeserializer<E> for BTreeMap<K, V>
|
||||||
where K: ValueDeserializer<E> + Eq + Ord,
|
where K: ValueDeserializer<E> + Eq + Ord,
|
||||||
V: ValueDeserializer<E>,
|
V: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = MapDeserializer<btree_map::IntoIter<K, V>, E>;
|
type Deserializer = MapDeserializer<btree_map::IntoIter<K, V>, E>;
|
||||||
|
|
||||||
@@ -868,7 +857,7 @@ impl<K, V, E> ValueDeserializer<E> for BTreeMap<K, V>
|
|||||||
impl<K, V, E> ValueDeserializer<E> for HashMap<K, V>
|
impl<K, V, E> ValueDeserializer<E> for HashMap<K, V>
|
||||||
where K: ValueDeserializer<E> + Eq + Hash,
|
where K: ValueDeserializer<E> + Eq + Hash,
|
||||||
V: ValueDeserializer<E>,
|
V: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = MapDeserializer<hash_map::IntoIter<K, V>, E>;
|
type Deserializer = MapDeserializer<hash_map::IntoIter<K, V>, E>;
|
||||||
|
|
||||||
@@ -887,20 +876,20 @@ pub struct MapVisitorDeserializer<V_, E> {
|
|||||||
|
|
||||||
impl<V_, E> MapVisitorDeserializer<V_, E>
|
impl<V_, E> MapVisitorDeserializer<V_, E>
|
||||||
where V_: de::MapVisitor<Error = E>,
|
where V_: de::MapVisitor<Error = E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
/// Construct a new `MapVisitorDeserializer<V_, E>`.
|
/// Construct a new `MapVisitorDeserializer<V_, E>`.
|
||||||
pub fn new(visitor: V_) -> Self {
|
pub fn new(visitor: V_) -> Self {
|
||||||
MapVisitorDeserializer{
|
MapVisitorDeserializer {
|
||||||
visitor: visitor,
|
visitor: visitor,
|
||||||
marker: PhantomData
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V_, E> de::Deserializer for MapVisitorDeserializer<V_, E>
|
impl<V_, E> de::Deserializer for MapVisitorDeserializer<V_, E>
|
||||||
where V_: de::MapVisitor<Error = E>,
|
where V_: de::MapVisitor<Error = E>,
|
||||||
E: de::Error,
|
E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
@@ -918,7 +907,7 @@ impl<V_, E> de::Deserializer for MapVisitorDeserializer<V_, E>
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
impl<'a, E> ValueDeserializer<E> for bytes::Bytes<'a>
|
impl<'a, E> ValueDeserializer<E> for bytes::Bytes<'a>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = BytesDeserializer<'a, E>;
|
type Deserializer = BytesDeserializer<'a, E>;
|
||||||
|
|
||||||
@@ -942,7 +931,7 @@ impl<'a, E> de::Deserializer for BytesDeserializer<'a, E>
|
|||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
visitor.visit_bytes(self.value)
|
visitor.visit_bytes(self.value)
|
||||||
}
|
}
|
||||||
@@ -958,7 +947,7 @@ impl<'a, E> de::Deserializer for BytesDeserializer<'a, E>
|
|||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<E> ValueDeserializer<E> for bytes::ByteBuf
|
impl<E> ValueDeserializer<E> for bytes::ByteBuf
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = ByteBufDeserializer<E>;
|
type Deserializer = ByteBufDeserializer<E>;
|
||||||
|
|
||||||
@@ -979,12 +968,12 @@ pub struct ByteBufDeserializer<E> {
|
|||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<E> de::Deserializer for ByteBufDeserializer<E>
|
impl<E> de::Deserializer for ByteBufDeserializer<E>
|
||||||
where E: de::Error,
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
visitor.visit_byte_buf(self.value)
|
visitor.visit_byte_buf(self.value)
|
||||||
}
|
}
|
||||||
@@ -1020,14 +1009,12 @@ mod private {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_newtype_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
|
fn visit_newtype_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
|
||||||
where T: de::DeserializeSeed,
|
where T: de::DeserializeSeed
|
||||||
{
|
{
|
||||||
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"newtype variant"))
|
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"newtype variant"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_tuple<V>(self,
|
fn visit_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
|
||||||
_len: usize,
|
|
||||||
_visitor: V) -> Result<V::Value, Self::Error>
|
|
||||||
where V: de::Visitor
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"tuple variant"))
|
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"tuple variant"))
|
||||||
@@ -1035,7 +1022,8 @@ mod private {
|
|||||||
|
|
||||||
fn visit_struct<V>(self,
|
fn visit_struct<V>(self,
|
||||||
_fields: &'static [&'static str],
|
_fields: &'static [&'static str],
|
||||||
_visitor: V) -> Result<V::Value, Self::Error>
|
_visitor: V)
|
||||||
|
-> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor
|
where V: de::Visitor
|
||||||
{
|
{
|
||||||
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"struct variant"))
|
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"struct variant"))
|
||||||
@@ -1053,6 +1041,8 @@ mod private {
|
|||||||
impl<A, B> Pair for (A, B) {
|
impl<A, B> Pair for (A, B) {
|
||||||
type First = A;
|
type First = A;
|
||||||
type Second = B;
|
type Second = B;
|
||||||
fn split(self) -> (A, B) { self }
|
fn split(self) -> (A, B) {
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -11,5 +11,7 @@ pub trait Error: Debug + Display {
|
|||||||
fn description(&self) -> &str;
|
fn description(&self) -> &str;
|
||||||
|
|
||||||
/// The lower-level cause of this error, if any.
|
/// The lower-level cause of this error, if any.
|
||||||
fn cause(&self) -> Option<&Error> { None }
|
fn cause(&self) -> Option<&Error> {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-12
@@ -4,13 +4,13 @@ use std::io;
|
|||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
|
||||||
/// Iterator over a byte stream that tracks the current position's line and column.
|
/// Iterator over a byte stream that tracks the current position's line and column.
|
||||||
pub struct LineColIterator<Iter: Iterator<Item=io::Result<u8>>> {
|
pub struct LineColIterator<Iter: Iterator<Item = io::Result<u8>>> {
|
||||||
iter: Iter,
|
iter: Iter,
|
||||||
line: usize,
|
line: usize,
|
||||||
col: usize,
|
col: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
|
impl<Iter: Iterator<Item = io::Result<u8>>> LineColIterator<Iter> {
|
||||||
/// Construct a new `LineColIterator<Iter>`.
|
/// Construct a new `LineColIterator<Iter>`.
|
||||||
pub fn new(iter: Iter) -> LineColIterator<Iter> {
|
pub fn new(iter: Iter) -> LineColIterator<Iter> {
|
||||||
LineColIterator {
|
LineColIterator {
|
||||||
@@ -21,27 +21,39 @@ impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Report the current line inside the iterator.
|
/// Report the current line inside the iterator.
|
||||||
pub fn line(&self) -> usize { self.line }
|
pub fn line(&self) -> usize {
|
||||||
|
self.line
|
||||||
|
}
|
||||||
|
|
||||||
/// Report the current column inside the iterator.
|
/// Report the current column inside the iterator.
|
||||||
pub fn col(&self) -> usize { self.col }
|
pub fn col(&self) -> usize {
|
||||||
|
self.col
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets a reference to the underlying iterator.
|
/// Gets a reference to the underlying iterator.
|
||||||
pub fn get_ref(&self) -> &Iter { &self.iter }
|
pub fn get_ref(&self) -> &Iter {
|
||||||
|
&self.iter
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets a mutable reference to the underlying iterator.
|
/// Gets a mutable reference to the underlying iterator.
|
||||||
pub fn get_mut(&mut self) -> &mut Iter { &mut self.iter }
|
pub fn get_mut(&mut self) -> &mut Iter {
|
||||||
|
&mut self.iter
|
||||||
|
}
|
||||||
|
|
||||||
/// Unwraps this `LineColIterator`, returning the underlying iterator.
|
/// Unwraps this `LineColIterator`, returning the underlying iterator.
|
||||||
pub fn into_inner(self) -> Iter { self.iter }
|
pub fn into_inner(self) -> Iter {
|
||||||
|
self.iter
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Peekable<Iter>> {
|
impl<Iter: Iterator<Item = io::Result<u8>>> LineColIterator<Peekable<Iter>> {
|
||||||
/// peeks at the next value
|
/// peeks at the next value
|
||||||
pub fn peek(&mut self) -> Option<&io::Result<u8>> { self.iter.peek() }
|
pub fn peek(&mut self) -> Option<&io::Result<u8>> {
|
||||||
|
self.iter.peek()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Iter: Iterator<Item=io::Result<u8>>> Iterator for LineColIterator<Iter> {
|
impl<Iter: Iterator<Item = io::Result<u8>>> Iterator for LineColIterator<Iter> {
|
||||||
type Item = io::Result<u8>;
|
type Item = io::Result<u8>;
|
||||||
fn next(&mut self) -> Option<io::Result<u8>> {
|
fn next(&mut self) -> Option<io::Result<u8>> {
|
||||||
match self.iter.next() {
|
match self.iter.next() {
|
||||||
@@ -50,11 +62,11 @@ impl<Iter: Iterator<Item=io::Result<u8>>> Iterator for LineColIterator<Iter> {
|
|||||||
self.line += 1;
|
self.line += 1;
|
||||||
self.col = 0;
|
self.col = 0;
|
||||||
Some(Ok(b'\n'))
|
Some(Ok(b'\n'))
|
||||||
},
|
}
|
||||||
Some(Ok(c)) => {
|
Some(Ok(c)) => {
|
||||||
self.col += 1;
|
self.col += 1;
|
||||||
Some(Ok(c))
|
Some(Ok(c))
|
||||||
},
|
}
|
||||||
Some(Err(e)) => Some(Err(e)),
|
Some(Err(e)) => Some(Err(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -79,7 +79,7 @@ extern crate core as actual_core;
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
mod core {
|
mod core {
|
||||||
pub use std::{ops, hash, fmt, cmp, marker, mem, i8, i16, i32, i64, u8, u16, u32, u64, isize,
|
pub use std::{ops, hash, fmt, cmp, marker, mem, i8, i16, i32, i64, u8, u16, u32, u64, isize,
|
||||||
usize, f32, f64, char, str, num, slice, iter, cell, default, result, option};
|
usize, f32, f64, char, str, num, slice, iter, cell, default, result, option};
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
pub use actual_core::nonzero;
|
pub use actual_core::nonzero;
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-59
@@ -4,25 +4,9 @@ use std::borrow::Cow;
|
|||||||
use collections::borrow::Cow;
|
use collections::borrow::Cow;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::collections::{
|
use std::collections::{BinaryHeap, BTreeMap, BTreeSet, LinkedList, HashMap, HashSet, VecDeque};
|
||||||
BinaryHeap,
|
|
||||||
BTreeMap,
|
|
||||||
BTreeSet,
|
|
||||||
LinkedList,
|
|
||||||
HashMap,
|
|
||||||
HashSet,
|
|
||||||
VecDeque,
|
|
||||||
};
|
|
||||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
use collections::{
|
use collections::{BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque, String, Vec};
|
||||||
BinaryHeap,
|
|
||||||
BTreeMap,
|
|
||||||
BTreeSet,
|
|
||||||
LinkedList,
|
|
||||||
VecDeque,
|
|
||||||
String,
|
|
||||||
Vec,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(feature = "collections")]
|
#[cfg(feature = "collections")]
|
||||||
use collections::borrow::ToOwned;
|
use collections::borrow::ToOwned;
|
||||||
@@ -57,12 +41,7 @@ use core::marker::PhantomData;
|
|||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
use core::nonzero::{NonZero, Zeroable};
|
use core::nonzero::{NonZero, Zeroable};
|
||||||
|
|
||||||
use super::{
|
use super::{Serialize, SerializeSeq, SerializeTuple, Serializer};
|
||||||
Serialize,
|
|
||||||
SerializeSeq,
|
|
||||||
SerializeTuple,
|
|
||||||
Serializer,
|
|
||||||
};
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use super::Error;
|
use super::Error;
|
||||||
|
|
||||||
@@ -101,7 +80,7 @@ impl_visit!(char, serialize_char);
|
|||||||
impl Serialize for str {
|
impl Serialize for str {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
serializer.serialize_str(self)
|
serializer.serialize_str(self)
|
||||||
}
|
}
|
||||||
@@ -111,7 +90,7 @@ impl Serialize for str {
|
|||||||
impl Serialize for String {
|
impl Serialize for String {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
(&self[..]).serialize(serializer)
|
(&self[..]).serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -124,7 +103,7 @@ impl<T> Serialize for Option<T>
|
|||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
match *self {
|
match *self {
|
||||||
Some(ref value) => serializer.serialize_some(value),
|
Some(ref value) => serializer.serialize_some(value),
|
||||||
@@ -138,7 +117,7 @@ impl<T> Serialize for Option<T>
|
|||||||
impl<T> Serialize for PhantomData<T> {
|
impl<T> Serialize for PhantomData<T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
serializer.serialize_unit_struct("PhantomData")
|
serializer.serialize_unit_struct("PhantomData")
|
||||||
}
|
}
|
||||||
@@ -211,7 +190,7 @@ macro_rules! serialize_seq {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Serialize for [T]
|
impl<T> Serialize for [T]
|
||||||
where T: Serialize,
|
where T: Serialize
|
||||||
{
|
{
|
||||||
serialize_seq!();
|
serialize_seq!();
|
||||||
}
|
}
|
||||||
@@ -225,7 +204,7 @@ impl<T> Serialize for BinaryHeap<T>
|
|||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T> Serialize for BTreeSet<T>
|
impl<T> Serialize for BTreeSet<T>
|
||||||
where T: Serialize + Ord,
|
where T: Serialize + Ord
|
||||||
{
|
{
|
||||||
serialize_seq!();
|
serialize_seq!();
|
||||||
}
|
}
|
||||||
@@ -233,14 +212,14 @@ impl<T> Serialize for BTreeSet<T>
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl<T, H> Serialize for HashSet<T, H>
|
impl<T, H> Serialize for HashSet<T, H>
|
||||||
where T: Serialize + Eq + Hash,
|
where T: Serialize + Eq + Hash,
|
||||||
H: BuildHasher,
|
H: BuildHasher
|
||||||
{
|
{
|
||||||
serialize_seq!();
|
serialize_seq!();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T> Serialize for LinkedList<T>
|
impl<T> Serialize for LinkedList<T>
|
||||||
where T: Serialize,
|
where T: Serialize
|
||||||
{
|
{
|
||||||
serialize_seq!();
|
serialize_seq!();
|
||||||
}
|
}
|
||||||
@@ -262,11 +241,11 @@ impl<T> Serialize for VecDeque<T>
|
|||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
impl<A> Serialize for ops::Range<A>
|
impl<A> Serialize for ops::Range<A>
|
||||||
where ops::Range<A>: ExactSizeIterator + iter::Iterator<Item = A> + Clone,
|
where ops::Range<A>: ExactSizeIterator + iter::Iterator<Item = A> + Clone,
|
||||||
A: Serialize,
|
A: Serialize
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
let mut seq = try!(serializer.serialize_seq(Some(self.len())));
|
let mut seq = try!(serializer.serialize_seq(Some(self.len())));
|
||||||
for e in self.clone() {
|
for e in self.clone() {
|
||||||
@@ -279,11 +258,11 @@ impl<A> Serialize for ops::Range<A>
|
|||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
impl<A> Serialize for ops::RangeInclusive<A>
|
impl<A> Serialize for ops::RangeInclusive<A>
|
||||||
where ops::RangeInclusive<A>: ExactSizeIterator + iter::Iterator<Item = A> + Clone,
|
where ops::RangeInclusive<A>: ExactSizeIterator + iter::Iterator<Item = A> + Clone,
|
||||||
A: Serialize,
|
A: Serialize
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
let mut seq = try!(serializer.serialize_seq(Some(self.len())));
|
let mut seq = try!(serializer.serialize_seq(Some(self.len())));
|
||||||
for e in self.clone() {
|
for e in self.clone() {
|
||||||
@@ -298,7 +277,7 @@ impl<A> Serialize for ops::RangeInclusive<A>
|
|||||||
impl Serialize for () {
|
impl Serialize for () {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
serializer.serialize_unit()
|
serializer.serialize_unit()
|
||||||
}
|
}
|
||||||
@@ -518,7 +497,7 @@ macro_rules! serialize_map {
|
|||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<K, V> Serialize for BTreeMap<K, V>
|
impl<K, V> Serialize for BTreeMap<K, V>
|
||||||
where K: Serialize + Ord,
|
where K: Serialize + Ord,
|
||||||
V: Serialize,
|
V: Serialize
|
||||||
{
|
{
|
||||||
serialize_map!();
|
serialize_map!();
|
||||||
}
|
}
|
||||||
@@ -527,26 +506,30 @@ impl<K, V> Serialize for BTreeMap<K, V>
|
|||||||
impl<K, V, H> Serialize for HashMap<K, V, H>
|
impl<K, V, H> Serialize for HashMap<K, V, H>
|
||||||
where K: Serialize + Eq + Hash,
|
where K: Serialize + Eq + Hash,
|
||||||
V: Serialize,
|
V: Serialize,
|
||||||
H: BuildHasher,
|
H: BuildHasher
|
||||||
{
|
{
|
||||||
serialize_map!();
|
serialize_map!();
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
impl<'a, T: ?Sized> Serialize for &'a T where T: Serialize {
|
impl<'a, T: ?Sized> Serialize for &'a T
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize {
|
impl<'a, T: ?Sized> Serialize for &'a mut T
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -558,7 +541,7 @@ impl<T: ?Sized> Serialize for Box<T>
|
|||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -570,7 +553,7 @@ impl<T> Serialize for Rc<T>
|
|||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -582,7 +565,7 @@ impl<T> Serialize for Arc<T>
|
|||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -594,7 +577,7 @@ impl<'a, T: ?Sized> Serialize for Cow<'a, T>
|
|||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -610,9 +593,7 @@ impl<T, E> Serialize for Result<T, E>
|
|||||||
where S: Serializer
|
where S: Serializer
|
||||||
{
|
{
|
||||||
match *self {
|
match *self {
|
||||||
Result::Ok(ref value) => {
|
Result::Ok(ref value) => serializer.serialize_newtype_variant("Result", 0, "Ok", value),
|
||||||
serializer.serialize_newtype_variant("Result", 0, "Ok", value)
|
|
||||||
}
|
|
||||||
Result::Err(ref value) => {
|
Result::Err(ref value) => {
|
||||||
serializer.serialize_newtype_variant("Result", 1, "Err", value)
|
serializer.serialize_newtype_variant("Result", 1, "Err", value)
|
||||||
}
|
}
|
||||||
@@ -625,7 +606,7 @@ impl<T, E> Serialize for Result<T, E>
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for Duration {
|
impl Serialize for Duration {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
use super::SerializeStruct;
|
use super::SerializeStruct;
|
||||||
let mut state = try!(serializer.serialize_struct("Duration", 2));
|
let mut state = try!(serializer.serialize_struct("Duration", 2));
|
||||||
@@ -640,7 +621,7 @@ impl Serialize for Duration {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::IpAddr {
|
impl Serialize for net::IpAddr {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
self.to_string().serialize(serializer)
|
self.to_string().serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -649,7 +630,7 @@ impl Serialize for net::IpAddr {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::Ipv4Addr {
|
impl Serialize for net::Ipv4Addr {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
self.to_string().serialize(serializer)
|
self.to_string().serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -658,7 +639,7 @@ impl Serialize for net::Ipv4Addr {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::Ipv6Addr {
|
impl Serialize for net::Ipv6Addr {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
self.to_string().serialize(serializer)
|
self.to_string().serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -669,7 +650,7 @@ impl Serialize for net::Ipv6Addr {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::SocketAddr {
|
impl Serialize for net::SocketAddr {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
match *self {
|
match *self {
|
||||||
net::SocketAddr::V4(ref addr) => addr.serialize(serializer),
|
net::SocketAddr::V4(ref addr) => addr.serialize(serializer),
|
||||||
@@ -681,7 +662,7 @@ impl Serialize for net::SocketAddr {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::SocketAddrV4 {
|
impl Serialize for net::SocketAddrV4 {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
self.to_string().serialize(serializer)
|
self.to_string().serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -690,7 +671,7 @@ impl Serialize for net::SocketAddrV4 {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::SocketAddrV6 {
|
impl Serialize for net::SocketAddrV6 {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
self.to_string().serialize(serializer)
|
self.to_string().serialize(serializer)
|
||||||
}
|
}
|
||||||
@@ -701,7 +682,7 @@ impl Serialize for net::SocketAddrV6 {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for path::Path {
|
impl Serialize for path::Path {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
match self.to_str() {
|
match self.to_str() {
|
||||||
Some(s) => s.serialize(serializer),
|
Some(s) => s.serialize(serializer),
|
||||||
@@ -713,7 +694,7 @@ impl Serialize for path::Path {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for path::PathBuf {
|
impl Serialize for path::PathBuf {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer
|
||||||
{
|
{
|
||||||
self.as_path().serialize(serializer)
|
self.as_path().serialize(serializer)
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-38
@@ -2,17 +2,8 @@
|
|||||||
|
|
||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
use ser::{
|
use ser::{self, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct,
|
||||||
self,
|
SerializeTupleVariant, SerializeMap, SerializeStruct, SerializeStructVariant};
|
||||||
Serialize,
|
|
||||||
SerializeSeq,
|
|
||||||
SerializeTuple,
|
|
||||||
SerializeTupleStruct,
|
|
||||||
SerializeTupleVariant,
|
|
||||||
SerializeMap,
|
|
||||||
SerializeStruct,
|
|
||||||
SerializeStructVariant,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Helper type for implementing a `Serializer` that does not support
|
/// Helper type for implementing a `Serializer` that does not support
|
||||||
/// serializing one of the compound types.
|
/// serializing one of the compound types.
|
||||||
@@ -50,14 +41,12 @@ pub struct Impossible<Ok, E> {
|
|||||||
enum Void {}
|
enum Void {}
|
||||||
|
|
||||||
impl<Ok, E> SerializeSeq for Impossible<Ok, E>
|
impl<Ok, E> SerializeSeq for Impossible<Ok, E>
|
||||||
where E: ser::Error,
|
where E: ser::Error
|
||||||
{
|
{
|
||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_element<T: ?Sized + Serialize>(&mut self,
|
fn serialize_element<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
|
||||||
_value: &T)
|
|
||||||
-> Result<(), E> {
|
|
||||||
match self.void {}
|
match self.void {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,14 +56,12 @@ impl<Ok, E> SerializeSeq for Impossible<Ok, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<Ok, E> SerializeTuple for Impossible<Ok, E>
|
impl<Ok, E> SerializeTuple for Impossible<Ok, E>
|
||||||
where E: ser::Error,
|
where E: ser::Error
|
||||||
{
|
{
|
||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_element<T: ?Sized + Serialize>(&mut self,
|
fn serialize_element<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
|
||||||
_value: &T)
|
|
||||||
-> Result<(), E> {
|
|
||||||
match self.void {}
|
match self.void {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,14 +71,12 @@ impl<Ok, E> SerializeTuple for Impossible<Ok, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<Ok, E> SerializeTupleStruct for Impossible<Ok, E>
|
impl<Ok, E> SerializeTupleStruct for Impossible<Ok, E>
|
||||||
where E: ser::Error,
|
where E: ser::Error
|
||||||
{
|
{
|
||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
fn serialize_field<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
|
||||||
_value: &T)
|
|
||||||
-> Result<(), E> {
|
|
||||||
match self.void {}
|
match self.void {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,14 +86,12 @@ impl<Ok, E> SerializeTupleStruct for Impossible<Ok, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<Ok, E> SerializeTupleVariant for Impossible<Ok, E>
|
impl<Ok, E> SerializeTupleVariant for Impossible<Ok, E>
|
||||||
where E: ser::Error,
|
where E: ser::Error
|
||||||
{
|
{
|
||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
fn serialize_field<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
|
||||||
_value: &T)
|
|
||||||
-> Result<(), E> {
|
|
||||||
match self.void {}
|
match self.void {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,20 +101,16 @@ impl<Ok, E> SerializeTupleVariant for Impossible<Ok, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<Ok, E> SerializeMap for Impossible<Ok, E>
|
impl<Ok, E> SerializeMap for Impossible<Ok, E>
|
||||||
where E: ser::Error,
|
where E: ser::Error
|
||||||
{
|
{
|
||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_key<T: ?Sized + Serialize>(&mut self,
|
fn serialize_key<T: ?Sized + Serialize>(&mut self, _key: &T) -> Result<(), E> {
|
||||||
_key: &T)
|
|
||||||
-> Result<(), E> {
|
|
||||||
match self.void {}
|
match self.void {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_value<T: ?Sized + Serialize>(&mut self,
|
fn serialize_value<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
|
||||||
_value: &T)
|
|
||||||
-> Result<(), E> {
|
|
||||||
match self.void {}
|
match self.void {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,13 +120,13 @@ impl<Ok, E> SerializeMap for Impossible<Ok, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<Ok, E> SerializeStruct for Impossible<Ok, E>
|
impl<Ok, E> SerializeStruct for Impossible<Ok, E>
|
||||||
where E: ser::Error,
|
where E: ser::Error
|
||||||
{
|
{
|
||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
||||||
_key: &'static str,
|
_key: &'static str,
|
||||||
_value: &T)
|
_value: &T)
|
||||||
-> Result<(), E> {
|
-> Result<(), E> {
|
||||||
match self.void {}
|
match self.void {}
|
||||||
@@ -159,13 +138,13 @@ impl<Ok, E> SerializeStruct for Impossible<Ok, E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<Ok, E> SerializeStructVariant for Impossible<Ok, E>
|
impl<Ok, E> SerializeStructVariant for Impossible<Ok, E>
|
||||||
where E: ser::Error,
|
where E: ser::Error
|
||||||
{
|
{
|
||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
fn serialize_field<T: ?Sized + Serialize>(&mut self,
|
||||||
_key: &'static str,
|
_key: &'static str,
|
||||||
_value: &T)
|
_value: &T)
|
||||||
-> Result<(), E> {
|
-> Result<(), E> {
|
||||||
match self.void {}
|
match self.void {}
|
||||||
|
|||||||
+63
-84
@@ -174,8 +174,7 @@ pub trait Serialize {
|
|||||||
/// for more information about how to implement this method.
|
/// for more information about how to implement this method.
|
||||||
///
|
///
|
||||||
/// [impl-serialize]: https://serde.rs/impl-serialize.html
|
/// [impl-serialize]: https://serde.rs/impl-serialize.html
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer;
|
||||||
where S: Serializer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -254,31 +253,31 @@ pub trait Serializer: Sized {
|
|||||||
|
|
||||||
/// Type returned from `serialize_seq` and `serialize_seq_fixed_size` for
|
/// Type returned from `serialize_seq` and `serialize_seq_fixed_size` for
|
||||||
/// serializing the content of the sequence.
|
/// serializing the content of the sequence.
|
||||||
type SerializeSeq: SerializeSeq<Ok=Self::Ok, Error=Self::Error>;
|
type SerializeSeq: SerializeSeq<Ok = Self::Ok, Error = Self::Error>;
|
||||||
|
|
||||||
/// Type returned from `serialize_tuple` for serializing the content of the
|
/// Type returned from `serialize_tuple` for serializing the content of the
|
||||||
/// tuple.
|
/// tuple.
|
||||||
type SerializeTuple: SerializeTuple<Ok=Self::Ok, Error=Self::Error>;
|
type SerializeTuple: SerializeTuple<Ok = Self::Ok, Error = Self::Error>;
|
||||||
|
|
||||||
/// Type returned from `serialize_tuple_struct` for serializing the content
|
/// Type returned from `serialize_tuple_struct` for serializing the content
|
||||||
/// of the tuple struct.
|
/// of the tuple struct.
|
||||||
type SerializeTupleStruct: SerializeTupleStruct<Ok=Self::Ok, Error=Self::Error>;
|
type SerializeTupleStruct: SerializeTupleStruct<Ok = Self::Ok, Error = Self::Error>;
|
||||||
|
|
||||||
/// Type returned from `serialize_tuple_variant` for serializing the content
|
/// Type returned from `serialize_tuple_variant` for serializing the content
|
||||||
/// of the tuple variant.
|
/// of the tuple variant.
|
||||||
type SerializeTupleVariant: SerializeTupleVariant<Ok=Self::Ok, Error=Self::Error>;
|
type SerializeTupleVariant: SerializeTupleVariant<Ok = Self::Ok, Error = Self::Error>;
|
||||||
|
|
||||||
/// Type returned from `serialize_map` for serializing the content of the
|
/// Type returned from `serialize_map` for serializing the content of the
|
||||||
/// map.
|
/// map.
|
||||||
type SerializeMap: SerializeMap<Ok=Self::Ok, Error=Self::Error>;
|
type SerializeMap: SerializeMap<Ok = Self::Ok, Error = Self::Error>;
|
||||||
|
|
||||||
/// Type returned from `serialize_struct` for serializing the content of the
|
/// Type returned from `serialize_struct` for serializing the content of the
|
||||||
/// struct.
|
/// struct.
|
||||||
type SerializeStruct: SerializeStruct<Ok=Self::Ok, Error=Self::Error>;
|
type SerializeStruct: SerializeStruct<Ok = Self::Ok, Error = Self::Error>;
|
||||||
|
|
||||||
/// Type returned from `serialize_struct_variant` for serializing the
|
/// Type returned from `serialize_struct_variant` for serializing the
|
||||||
/// content of the struct variant.
|
/// content of the struct variant.
|
||||||
type SerializeStructVariant: SerializeStructVariant<Ok=Self::Ok, Error=Self::Error>;
|
type SerializeStructVariant: SerializeStructVariant<Ok = Self::Ok, Error = Self::Error>;
|
||||||
|
|
||||||
/// Serialize a `bool` value.
|
/// Serialize a `bool` value.
|
||||||
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error>;
|
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error>;
|
||||||
@@ -371,10 +370,7 @@ pub trait Serializer: Sized {
|
|||||||
fn serialize_none(self) -> Result<Self::Ok, Self::Error>;
|
fn serialize_none(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serialize a `Some(T)` value.
|
/// Serialize a `Some(T)` value.
|
||||||
fn serialize_some<T: ?Sized + Serialize>(
|
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Self::Ok, Self::Error>;
|
||||||
self,
|
|
||||||
value: &T,
|
|
||||||
) -> Result<Self::Ok, Self::Error>;
|
|
||||||
|
|
||||||
/// Serialize a `()` value.
|
/// Serialize a `()` value.
|
||||||
fn serialize_unit(self) -> Result<Self::Ok, Self::Error>;
|
fn serialize_unit(self) -> Result<Self::Ok, Self::Error>;
|
||||||
@@ -382,10 +378,7 @@ pub trait Serializer: Sized {
|
|||||||
/// Serialize a unit struct like `struct Unit` or `PhantomData<T>`.
|
/// Serialize a unit struct like `struct Unit` or `PhantomData<T>`.
|
||||||
///
|
///
|
||||||
/// A reasonable implementation would be to forward to `serialize_unit`.
|
/// A reasonable implementation would be to forward to `serialize_unit`.
|
||||||
fn serialize_unit_struct(
|
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error>;
|
||||||
self,
|
|
||||||
name: &'static str,
|
|
||||||
) -> Result<Self::Ok, Self::Error>;
|
|
||||||
|
|
||||||
/// Serialize a unit variant like `E::A` in `enum E { A, B }`.
|
/// Serialize a unit variant like `E::A` in `enum E { A, B }`.
|
||||||
///
|
///
|
||||||
@@ -401,12 +394,11 @@ pub trait Serializer: Sized {
|
|||||||
/// E::B => serializer.serialize_unit_variant("E", 1, "B"),
|
/// E::B => serializer.serialize_unit_variant("E", 1, "B"),
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_unit_variant(
|
fn serialize_unit_variant(self,
|
||||||
self,
|
name: &'static str,
|
||||||
name: &'static str,
|
variant_index: usize,
|
||||||
variant_index: usize,
|
variant: &'static str)
|
||||||
variant: &'static str,
|
-> Result<Self::Ok, Self::Error>;
|
||||||
) -> Result<Self::Ok, Self::Error>;
|
|
||||||
|
|
||||||
/// Serialize a newtype struct like `struct Millimeters(u8)`.
|
/// Serialize a newtype struct like `struct Millimeters(u8)`.
|
||||||
///
|
///
|
||||||
@@ -417,11 +409,10 @@ pub trait Serializer: Sized {
|
|||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
/// serializer.serialize_newtype_struct("Millimeters", &self.0)
|
/// serializer.serialize_newtype_struct("Millimeters", &self.0)
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_newtype_struct<T: ?Sized + Serialize>(
|
fn serialize_newtype_struct<T: ?Sized + Serialize>(self,
|
||||||
self,
|
name: &'static str,
|
||||||
name: &'static str,
|
value: &T)
|
||||||
value: &T,
|
-> Result<Self::Ok, Self::Error>;
|
||||||
) -> Result<Self::Ok, Self::Error>;
|
|
||||||
|
|
||||||
/// Serialize a newtype variant like `E::N` in `enum E { N(u8) }`.
|
/// Serialize a newtype variant like `E::N` in `enum E { N(u8) }`.
|
||||||
///
|
///
|
||||||
@@ -434,13 +425,12 @@ pub trait Serializer: Sized {
|
|||||||
/// E::N(ref n) => serializer.serialize_newtype_variant("E", 0, "N", n),
|
/// E::N(ref n) => serializer.serialize_newtype_variant("E", 0, "N", n),
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_newtype_variant<T: ?Sized + Serialize>(
|
fn serialize_newtype_variant<T: ?Sized + Serialize>(self,
|
||||||
self,
|
name: &'static str,
|
||||||
name: &'static str,
|
variant_index: usize,
|
||||||
variant_index: usize,
|
variant: &'static str,
|
||||||
variant: &'static str,
|
value: &T)
|
||||||
value: &T,
|
-> Result<Self::Ok, Self::Error>;
|
||||||
) -> Result<Self::Ok, Self::Error>;
|
|
||||||
|
|
||||||
/// Begin to serialize a dynamically sized sequence. This call must be
|
/// Begin to serialize a dynamically sized sequence. This call must be
|
||||||
/// followed by zero or more calls to `serialize_element`, then a call to
|
/// followed by zero or more calls to `serialize_element`, then a call to
|
||||||
@@ -457,10 +447,7 @@ pub trait Serializer: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// seq.end()
|
/// seq.end()
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_seq(
|
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error>;
|
||||||
self,
|
|
||||||
len: Option<usize>,
|
|
||||||
) -> Result<Self::SerializeSeq, Self::Error>;
|
|
||||||
|
|
||||||
/// Begin to serialize a statically sized sequence whose length will be
|
/// Begin to serialize a statically sized sequence whose length will be
|
||||||
/// known at deserialization time without looking at the serialized data.
|
/// known at deserialization time without looking at the serialized data.
|
||||||
@@ -474,10 +461,7 @@ pub trait Serializer: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// seq.end()
|
/// seq.end()
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_seq_fixed_size(
|
fn serialize_seq_fixed_size(self, size: usize) -> Result<Self::SerializeSeq, Self::Error>;
|
||||||
self,
|
|
||||||
size: usize,
|
|
||||||
) -> Result<Self::SerializeSeq, Self::Error>;
|
|
||||||
|
|
||||||
/// Begin to serialize a tuple. This call must be followed by zero or more
|
/// Begin to serialize a tuple. This call must be followed by zero or more
|
||||||
/// calls to `serialize_field`, then a call to `end`.
|
/// calls to `serialize_field`, then a call to `end`.
|
||||||
@@ -489,10 +473,7 @@ pub trait Serializer: Sized {
|
|||||||
/// tup.serialize_field(&self.2)?;
|
/// tup.serialize_field(&self.2)?;
|
||||||
/// tup.end()
|
/// tup.end()
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_tuple(
|
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error>;
|
||||||
self,
|
|
||||||
len: usize,
|
|
||||||
) -> Result<Self::SerializeTuple, Self::Error>;
|
|
||||||
|
|
||||||
/// Begin to serialize a tuple struct like `struct Rgb(u8, u8, u8)`. This
|
/// Begin to serialize a tuple struct like `struct Rgb(u8, u8, u8)`. This
|
||||||
/// call must be followed by zero or more calls to `serialize_field`, then a
|
/// call must be followed by zero or more calls to `serialize_field`, then a
|
||||||
@@ -508,11 +489,10 @@ pub trait Serializer: Sized {
|
|||||||
/// ts.serialize_field(&self.2)?;
|
/// ts.serialize_field(&self.2)?;
|
||||||
/// ts.end()
|
/// ts.end()
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_tuple_struct(
|
fn serialize_tuple_struct(self,
|
||||||
self,
|
name: &'static str,
|
||||||
name: &'static str,
|
len: usize)
|
||||||
len: usize,
|
-> Result<Self::SerializeTupleStruct, Self::Error>;
|
||||||
) -> Result<Self::SerializeTupleStruct, Self::Error>;
|
|
||||||
|
|
||||||
/// Begin to serialize a tuple variant like `E::T` in `enum E { T(u8, u8)
|
/// 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
|
/// }`. This call must be followed by zero or more calls to
|
||||||
@@ -532,13 +512,12 @@ pub trait Serializer: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_tuple_variant(
|
fn serialize_tuple_variant(self,
|
||||||
self,
|
name: &'static str,
|
||||||
name: &'static str,
|
variant_index: usize,
|
||||||
variant_index: usize,
|
variant: &'static str,
|
||||||
variant: &'static str,
|
len: usize)
|
||||||
len: usize,
|
-> Result<Self::SerializeTupleVariant, Self::Error>;
|
||||||
) -> Result<Self::SerializeTupleVariant, Self::Error>;
|
|
||||||
|
|
||||||
/// Begin to serialize a map. This call must be followed by zero or more
|
/// 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`.
|
/// calls to `serialize_key` and `serialize_value`, then a call to `end`.
|
||||||
@@ -554,10 +533,7 @@ pub trait Serializer: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// map.end()
|
/// map.end()
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_map(
|
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error>;
|
||||||
self,
|
|
||||||
len: Option<usize>,
|
|
||||||
) -> Result<Self::SerializeMap, Self::Error>;
|
|
||||||
|
|
||||||
/// Begin to serialize a struct like `struct Rgb { r: u8, g: u8, b: u8 }`.
|
/// Begin to serialize a struct like `struct Rgb { r: u8, g: u8, b: u8 }`.
|
||||||
/// This call must be followed by zero or more calls to `serialize_field`,
|
/// This call must be followed by zero or more calls to `serialize_field`,
|
||||||
@@ -573,11 +549,10 @@ pub trait Serializer: Sized {
|
|||||||
/// struc.serialize_field("b", &self.b)?;
|
/// struc.serialize_field("b", &self.b)?;
|
||||||
/// struc.end()
|
/// struc.end()
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_struct(
|
fn serialize_struct(self,
|
||||||
self,
|
name: &'static str,
|
||||||
name: &'static str,
|
len: usize)
|
||||||
len: usize,
|
-> Result<Self::SerializeStruct, Self::Error>;
|
||||||
) -> Result<Self::SerializeStruct, Self::Error>;
|
|
||||||
|
|
||||||
/// Begin to serialize a struct variant like `E::S` in `enum E { S { r: u8,
|
/// 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
|
/// g: u8, b: u8 } }`. This call must be followed by zero or more calls to
|
||||||
@@ -598,13 +573,12 @@ pub trait Serializer: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_struct_variant(
|
fn serialize_struct_variant(self,
|
||||||
self,
|
name: &'static str,
|
||||||
name: &'static str,
|
variant_index: usize,
|
||||||
variant_index: usize,
|
variant: &'static str,
|
||||||
variant: &'static str,
|
len: usize)
|
||||||
len: usize,
|
-> Result<Self::SerializeStructVariant, Self::Error>;
|
||||||
) -> Result<Self::SerializeStructVariant, Self::Error>;
|
|
||||||
|
|
||||||
/// Collect an iterator as a sequence.
|
/// Collect an iterator as a sequence.
|
||||||
///
|
///
|
||||||
@@ -613,7 +587,7 @@ pub trait Serializer: Sized {
|
|||||||
/// this method.
|
/// this method.
|
||||||
fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>
|
fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>
|
||||||
where I: IntoIterator,
|
where I: IntoIterator,
|
||||||
<I as IntoIterator>::Item: Serialize,
|
<I as IntoIterator>::Item: Serialize
|
||||||
{
|
{
|
||||||
let iter = iter.into_iter();
|
let iter = iter.into_iter();
|
||||||
let mut serializer = try!(self.serialize_seq(iter.len_hint()));
|
let mut serializer = try!(self.serialize_seq(iter.len_hint()));
|
||||||
@@ -631,7 +605,7 @@ pub trait Serializer: Sized {
|
|||||||
fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
|
fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
|
||||||
where K: Serialize,
|
where K: Serialize,
|
||||||
V: Serialize,
|
V: Serialize,
|
||||||
I: IntoIterator<Item = (K, V)>,
|
I: IntoIterator<Item = (K, V)>
|
||||||
{
|
{
|
||||||
let iter = iter.into_iter();
|
let iter = iter.into_iter();
|
||||||
let mut serializer = try!(self.serialize_map(iter.len_hint()));
|
let mut serializer = try!(self.serialize_map(iter.len_hint()));
|
||||||
@@ -773,11 +747,10 @@ pub trait SerializeMap {
|
|||||||
/// `serialize_value`. This is appropriate for serializers that do not care
|
/// `serialize_value`. This is appropriate for serializers that do not care
|
||||||
/// about performance or are not able to optimize `serialize_entry` any
|
/// about performance or are not able to optimize `serialize_entry` any
|
||||||
/// better than this.
|
/// better than this.
|
||||||
fn serialize_entry<K: ?Sized + Serialize, V: ?Sized + Serialize>(
|
fn serialize_entry<K: ?Sized + Serialize, V: ?Sized + Serialize>(&mut self,
|
||||||
&mut self,
|
key: &K,
|
||||||
key: &K,
|
value: &V)
|
||||||
value: &V,
|
-> Result<(), Self::Error> {
|
||||||
) -> Result<(), Self::Error> {
|
|
||||||
try!(self.serialize_key(key));
|
try!(self.serialize_key(key));
|
||||||
self.serialize_value(value)
|
self.serialize_value(value)
|
||||||
}
|
}
|
||||||
@@ -803,7 +776,10 @@ pub trait SerializeStruct {
|
|||||||
type Error: Error;
|
type Error: Error;
|
||||||
|
|
||||||
/// Serialize a struct field.
|
/// 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.
|
/// Finish serializing a struct.
|
||||||
fn end(self) -> Result<Self::Ok, Self::Error>;
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
@@ -830,7 +806,10 @@ pub trait SerializeStructVariant {
|
|||||||
type Error: Error;
|
type Error: Error;
|
||||||
|
|
||||||
/// Serialize a struct variant field.
|
/// 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.
|
/// Finish serializing a struct variant.
|
||||||
fn end(self) -> Result<Self::Ok, Self::Error>;
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|||||||
+42
-15
@@ -3,14 +3,13 @@ use core::fmt::{self, Display};
|
|||||||
use ser::{self, Serialize, Serializer, SerializeMap, SerializeStruct};
|
use ser::{self, Serialize, Serializer, SerializeMap, SerializeStruct};
|
||||||
|
|
||||||
/// Not public API.
|
/// Not public API.
|
||||||
pub fn serialize_tagged_newtype<S, T>(
|
pub fn serialize_tagged_newtype<S, T>(serializer: S,
|
||||||
serializer: S,
|
type_ident: &'static str,
|
||||||
type_ident: &'static str,
|
variant_ident: &'static str,
|
||||||
variant_ident: &'static str,
|
tag: &'static str,
|
||||||
tag: &'static str,
|
variant_name: &'static str,
|
||||||
variant_name: &'static str,
|
value: T)
|
||||||
value: T,
|
-> Result<S::Ok, S::Error>
|
||||||
) -> Result<S::Ok, S::Error>
|
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
T: Serialize
|
T: Serialize
|
||||||
{
|
{
|
||||||
@@ -181,17 +180,29 @@ impl<S> Serializer for TaggedSerializer<S>
|
|||||||
Err(self.bad_type(Unsupported::UnitStruct))
|
Err(self.bad_type(Unsupported::UnitStruct))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit_variant(self, _: &'static str, _: usize, _: &'static str) -> Result<Self::Ok, Self::Error> {
|
fn serialize_unit_variant(self,
|
||||||
|
_: &'static str,
|
||||||
|
_: usize,
|
||||||
|
_: &'static str)
|
||||||
|
-> Result<Self::Ok, Self::Error> {
|
||||||
Err(self.bad_type(Unsupported::Enum))
|
Err(self.bad_type(Unsupported::Enum))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct<T: ?Sized>(self, _: &'static str, value: &T) -> Result<Self::Ok, Self::Error>
|
fn serialize_newtype_struct<T: ?Sized>(self,
|
||||||
|
_: &'static str,
|
||||||
|
value: &T)
|
||||||
|
-> Result<Self::Ok, Self::Error>
|
||||||
where T: Serialize
|
where T: Serialize
|
||||||
{
|
{
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_variant<T: ?Sized>(self, _: &'static str, _: usize, _: &'static str, _: &T) -> Result<Self::Ok, Self::Error>
|
fn serialize_newtype_variant<T: ?Sized>(self,
|
||||||
|
_: &'static str,
|
||||||
|
_: usize,
|
||||||
|
_: &'static str,
|
||||||
|
_: &T)
|
||||||
|
-> Result<Self::Ok, Self::Error>
|
||||||
where T: Serialize
|
where T: Serialize
|
||||||
{
|
{
|
||||||
Err(self.bad_type(Unsupported::Enum))
|
Err(self.bad_type(Unsupported::Enum))
|
||||||
@@ -209,11 +220,19 @@ impl<S> Serializer for TaggedSerializer<S>
|
|||||||
Err(self.bad_type(Unsupported::Tuple))
|
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))
|
Err(self.bad_type(Unsupported::TupleStruct))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_variant(self, _: &'static str, _: usize, _: &'static str, _: usize) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
fn serialize_tuple_variant(self,
|
||||||
|
_: &'static str,
|
||||||
|
_: usize,
|
||||||
|
_: &'static str,
|
||||||
|
_: usize)
|
||||||
|
-> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||||
Err(self.bad_type(Unsupported::Enum))
|
Err(self.bad_type(Unsupported::Enum))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,13 +242,21 @@ impl<S> Serializer for TaggedSerializer<S>
|
|||||||
Ok(map)
|
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));
|
let mut state = try!(self.delegate.serialize_struct(name, len + 1));
|
||||||
try!(state.serialize_field(self.tag, self.variant_name));
|
try!(state.serialize_field(self.tag, self.variant_name));
|
||||||
Ok(state)
|
Ok(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_variant(self, _: &'static str, _: usize, _: &'static str, _: usize) -> Result<Self::SerializeStructVariant, Self::Error> {
|
fn serialize_struct_variant(self,
|
||||||
|
_: &'static str,
|
||||||
|
_: usize,
|
||||||
|
_: &'static str,
|
||||||
|
_: usize)
|
||||||
|
-> Result<Self::SerializeStructVariant, Self::Error> {
|
||||||
Err(self.bad_type(Unsupported::Enum))
|
Err(self.bad_type(Unsupported::Enum))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-18
@@ -1,12 +1,12 @@
|
|||||||
//! Private utility functions
|
//! Private utility functions
|
||||||
|
|
||||||
const TAG_CONT: u8 = 0b1000_0000;
|
const TAG_CONT: u8 = 0b1000_0000;
|
||||||
const TAG_TWO_B: u8 = 0b1100_0000;
|
const TAG_TWO_B: u8 = 0b1100_0000;
|
||||||
const TAG_THREE_B: u8 = 0b1110_0000;
|
const TAG_THREE_B: u8 = 0b1110_0000;
|
||||||
const TAG_FOUR_B: u8 = 0b1111_0000;
|
const TAG_FOUR_B: u8 = 0b1111_0000;
|
||||||
const MAX_ONE_B: u32 = 0x80;
|
const MAX_ONE_B: u32 = 0x80;
|
||||||
const MAX_TWO_B: u32 = 0x800;
|
const MAX_TWO_B: u32 = 0x800;
|
||||||
const MAX_THREE_B: u32 = 0x10000;
|
const MAX_THREE_B: u32 = 0x10000;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn encode_utf8(c: char) -> EncodeUtf8 {
|
pub fn encode_utf8(c: char) -> EncodeUtf8 {
|
||||||
@@ -21,17 +21,20 @@ pub fn encode_utf8(c: char) -> EncodeUtf8 {
|
|||||||
2
|
2
|
||||||
} else if code < MAX_THREE_B {
|
} else if code < MAX_THREE_B {
|
||||||
buf[1] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
|
buf[1] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
|
||||||
buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
|
buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
|
||||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
buf[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
|
buf[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
|
||||||
buf[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT;
|
buf[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT;
|
||||||
buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
|
buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
|
||||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
EncodeUtf8 { buf: buf, pos: pos }
|
EncodeUtf8 {
|
||||||
|
buf: buf,
|
||||||
|
pos: pos,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EncodeUtf8 {
|
pub struct EncodeUtf8 {
|
||||||
@@ -47,23 +50,22 @@ impl EncodeUtf8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_upper_case_globals)]
|
#[allow(non_upper_case_globals)]
|
||||||
const Pattern_White_Space_table: &'static [(char, char)] = &[
|
const Pattern_White_Space_table: &'static [(char, char)] = &[('\u{9}', '\u{d}'),
|
||||||
('\u{9}', '\u{d}'), ('\u{20}', '\u{20}'), ('\u{85}', '\u{85}'), ('\u{200e}', '\u{200f}'),
|
('\u{20}', '\u{20}'),
|
||||||
('\u{2028}', '\u{2029}')
|
('\u{85}', '\u{85}'),
|
||||||
];
|
('\u{200e}', '\u{200f}'),
|
||||||
|
('\u{2028}', '\u{2029}')];
|
||||||
|
|
||||||
fn bsearch_range_table(c: char, r: &'static [(char, char)]) -> bool {
|
fn bsearch_range_table(c: char, r: &'static [(char, char)]) -> bool {
|
||||||
use core::cmp::Ordering::{Equal, Less, Greater};
|
use core::cmp::Ordering::{Equal, Less, Greater};
|
||||||
r.binary_search_by(|&(lo, hi)| {
|
r.binary_search_by(|&(lo, hi)| if c < lo {
|
||||||
if c < lo {
|
|
||||||
Greater
|
Greater
|
||||||
} else if hi < c {
|
} else if hi < c {
|
||||||
Less
|
Less
|
||||||
} else {
|
} else {
|
||||||
Equal
|
Equal
|
||||||
}
|
})
|
||||||
})
|
.is_ok()
|
||||||
.is_ok()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
|
|||||||
@@ -39,9 +39,7 @@ impl<'a> Item<'a> {
|
|||||||
let attrs = attr::Item::from_ast(cx, item);
|
let attrs = attr::Item::from_ast(cx, item);
|
||||||
|
|
||||||
let body = match item.body {
|
let body = match item.body {
|
||||||
syn::Body::Enum(ref variants) => {
|
syn::Body::Enum(ref variants) => Body::Enum(enum_from_ast(cx, variants)),
|
||||||
Body::Enum(enum_from_ast(cx, variants))
|
|
||||||
}
|
|
||||||
syn::Body::Struct(ref variant_data) => {
|
syn::Body::Struct(ref variant_data) => {
|
||||||
let (style, fields) = struct_from_ast(cx, variant_data);
|
let (style, fields) = struct_from_ast(cx, variant_data);
|
||||||
Body::Struct(style, fields)
|
Body::Struct(style, fields)
|
||||||
@@ -58,15 +56,13 @@ impl<'a> Item<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Body<'a> {
|
impl<'a> Body<'a> {
|
||||||
pub fn all_fields(&'a self) -> Box<Iterator<Item=&'a Field<'a>> + 'a> {
|
pub fn all_fields(&'a self) -> Box<Iterator<Item = &'a Field<'a>> + 'a> {
|
||||||
match *self {
|
match *self {
|
||||||
Body::Enum(ref variants) => {
|
Body::Enum(ref variants) => {
|
||||||
Box::new(variants.iter()
|
Box::new(variants.iter()
|
||||||
.flat_map(|variant| variant.fields.iter()))
|
.flat_map(|variant| variant.fields.iter()))
|
||||||
}
|
|
||||||
Body::Struct(_, ref fields) => {
|
|
||||||
Box::new(fields.iter())
|
|
||||||
}
|
}
|
||||||
|
Body::Struct(_, ref fields) => Box::new(fields.iter()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,18 +83,12 @@ fn enum_from_ast<'a>(cx: &Ctxt, variants: &'a [syn::Variant]) -> Vec<Variant<'a>
|
|||||||
|
|
||||||
fn struct_from_ast<'a>(cx: &Ctxt, data: &'a syn::VariantData) -> (Style, Vec<Field<'a>>) {
|
fn struct_from_ast<'a>(cx: &Ctxt, data: &'a syn::VariantData) -> (Style, Vec<Field<'a>>) {
|
||||||
match *data {
|
match *data {
|
||||||
syn::VariantData::Struct(ref fields) => {
|
syn::VariantData::Struct(ref fields) => (Style::Struct, fields_from_ast(cx, fields)),
|
||||||
(Style::Struct, fields_from_ast(cx, fields))
|
|
||||||
}
|
|
||||||
syn::VariantData::Tuple(ref fields) if fields.len() == 1 => {
|
syn::VariantData::Tuple(ref fields) if fields.len() == 1 => {
|
||||||
(Style::Newtype, fields_from_ast(cx, fields))
|
(Style::Newtype, fields_from_ast(cx, fields))
|
||||||
}
|
}
|
||||||
syn::VariantData::Tuple(ref fields) => {
|
syn::VariantData::Tuple(ref fields) => (Style::Tuple, fields_from_ast(cx, fields)),
|
||||||
(Style::Tuple, fields_from_ast(cx, fields))
|
syn::VariantData::Unit => (Style::Unit, Vec::new()),
|
||||||
}
|
|
||||||
syn::VariantData::Unit => {
|
|
||||||
(Style::Unit, Vec::new())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -157,7 +157,8 @@ impl Item {
|
|||||||
|
|
||||||
// Parse `#[serde(bound="D: Serialize")]`
|
// Parse `#[serde(bound="D: Serialize")]`
|
||||||
MetaItem(NameValue(ref name, ref lit)) if name == "bound" => {
|
MetaItem(NameValue(ref name, ref lit)) if name == "bound" => {
|
||||||
if let Ok(where_predicates) = parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
|
if let Ok(where_predicates) =
|
||||||
|
parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
|
||||||
ser_bound.set(where_predicates.clone());
|
ser_bound.set(where_predicates.clone());
|
||||||
de_bound.set(where_predicates);
|
de_bound.set(where_predicates);
|
||||||
}
|
}
|
||||||
@@ -217,10 +218,12 @@ impl Item {
|
|||||||
if let syn::Body::Enum(ref variants) = item.body {
|
if let syn::Body::Enum(ref variants) = item.body {
|
||||||
for variant in variants {
|
for variant in variants {
|
||||||
match variant.data {
|
match variant.data {
|
||||||
syn::VariantData::Struct(_) | syn::VariantData::Unit => {}
|
syn::VariantData::Struct(_) |
|
||||||
|
syn::VariantData::Unit => {}
|
||||||
syn::VariantData::Tuple(ref fields) => {
|
syn::VariantData::Tuple(ref fields) => {
|
||||||
if fields.len() != 1 {
|
if fields.len() != 1 {
|
||||||
cx.error("#[serde(tag = \"...\")] cannot be used with tuple variants");
|
cx.error("#[serde(tag = \"...\")] cannot be used with tuple \
|
||||||
|
variants");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -311,8 +314,7 @@ impl Variant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MetaItem(ref meta_item) => {
|
MetaItem(ref meta_item) => {
|
||||||
cx.error(format!("unknown serde variant attribute `{}`",
|
cx.error(format!("unknown serde variant attribute `{}`", meta_item.name()));
|
||||||
meta_item.name()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Literal(_) => {
|
Literal(_) => {
|
||||||
@@ -372,9 +374,7 @@ pub enum FieldDefault {
|
|||||||
|
|
||||||
impl Field {
|
impl Field {
|
||||||
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
||||||
pub fn from_ast(cx: &Ctxt,
|
pub fn from_ast(cx: &Ctxt, index: usize, field: &syn::Field) -> Self {
|
||||||
index: usize,
|
|
||||||
field: &syn::Field) -> Self {
|
|
||||||
let mut ser_name = Attr::none(cx, "rename");
|
let mut ser_name = Attr::none(cx, "rename");
|
||||||
let mut de_name = Attr::none(cx, "rename");
|
let mut de_name = Attr::none(cx, "rename");
|
||||||
let mut skip_serializing = BoolAttr::none(cx, "skip_serializing");
|
let mut skip_serializing = BoolAttr::none(cx, "skip_serializing");
|
||||||
@@ -455,7 +455,8 @@ impl Field {
|
|||||||
|
|
||||||
// Parse `#[serde(bound="D: Serialize")]`
|
// Parse `#[serde(bound="D: Serialize")]`
|
||||||
MetaItem(NameValue(ref name, ref lit)) if name == "bound" => {
|
MetaItem(NameValue(ref name, ref lit)) if name == "bound" => {
|
||||||
if let Ok(where_predicates) = parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
|
if let Ok(where_predicates) =
|
||||||
|
parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
|
||||||
ser_bound.set(where_predicates.clone());
|
ser_bound.set(where_predicates.clone());
|
||||||
de_bound.set(where_predicates);
|
de_bound.set(where_predicates);
|
||||||
}
|
}
|
||||||
@@ -470,8 +471,7 @@ impl Field {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MetaItem(ref meta_item) => {
|
MetaItem(ref meta_item) => {
|
||||||
cx.error(format!("unknown serde field attribute `{}`",
|
cx.error(format!("unknown serde field attribute `{}`", meta_item.name()));
|
||||||
meta_item.name()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Literal(_) => {
|
Literal(_) => {
|
||||||
@@ -542,13 +542,12 @@ impl Field {
|
|||||||
|
|
||||||
type SerAndDe<T> = (Option<T>, Option<T>);
|
type SerAndDe<T> = (Option<T>, Option<T>);
|
||||||
|
|
||||||
fn get_ser_and_de<T, F>(
|
fn get_ser_and_de<T, F>(cx: &Ctxt,
|
||||||
cx: &Ctxt,
|
attr_name: &'static str,
|
||||||
attr_name: &'static str,
|
items: &[syn::NestedMetaItem],
|
||||||
items: &[syn::NestedMetaItem],
|
f: F)
|
||||||
f: F
|
-> Result<SerAndDe<T>, ()>
|
||||||
) -> Result<SerAndDe<T>, ()>
|
where F: Fn(&Ctxt, &str, &str, &syn::Lit) -> Result<T, ()>
|
||||||
where F: Fn(&Ctxt, &str, &str, &syn::Lit) -> Result<T, ()>,
|
|
||||||
{
|
{
|
||||||
let mut ser_item = Attr::none(cx, attr_name);
|
let mut ser_item = Attr::none(cx, attr_name);
|
||||||
let mut de_item = Attr::none(cx, attr_name);
|
let mut de_item = Attr::none(cx, attr_name);
|
||||||
@@ -568,7 +567,8 @@ fn get_ser_and_de<T, F>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
cx.error(format!("malformed {0} attribute, expected `{0}(serialize = ..., deserialize = ...)`",
|
cx.error(format!("malformed {0} attribute, expected `{0}(serialize = ..., \
|
||||||
|
deserialize = ...)`",
|
||||||
attr_name));
|
attr_name));
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
@@ -578,35 +578,34 @@ fn get_ser_and_de<T, F>(
|
|||||||
Ok((ser_item.get(), de_item.get()))
|
Ok((ser_item.get(), de_item.get()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_renames(
|
fn get_renames(cx: &Ctxt, items: &[syn::NestedMetaItem]) -> Result<SerAndDe<String>, ()> {
|
||||||
cx: &Ctxt,
|
|
||||||
items: &[syn::NestedMetaItem],
|
|
||||||
) -> Result<SerAndDe<String>, ()> {
|
|
||||||
get_ser_and_de(cx, "rename", items, get_string_from_lit)
|
get_ser_and_de(cx, "rename", items, get_string_from_lit)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_where_predicates(
|
fn get_where_predicates(cx: &Ctxt,
|
||||||
cx: &Ctxt,
|
items: &[syn::NestedMetaItem])
|
||||||
items: &[syn::NestedMetaItem],
|
-> Result<SerAndDe<Vec<syn::WherePredicate>>, ()> {
|
||||||
) -> Result<SerAndDe<Vec<syn::WherePredicate>>, ()> {
|
|
||||||
get_ser_and_de(cx, "bound", items, parse_lit_into_where)
|
get_ser_and_de(cx, "bound", items, parse_lit_into_where)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_serde_meta_items(attr: &syn::Attribute) -> Option<Vec<syn::NestedMetaItem>> {
|
pub fn get_serde_meta_items(attr: &syn::Attribute) -> Option<Vec<syn::NestedMetaItem>> {
|
||||||
match attr.value {
|
match attr.value {
|
||||||
List(ref name, ref items) if name == "serde" => {
|
List(ref name, ref items) if name == "serde" => Some(items.iter().cloned().collect()),
|
||||||
Some(items.iter().cloned().collect())
|
_ => None,
|
||||||
}
|
|
||||||
_ => None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_string_from_lit(cx: &Ctxt, attr_name: &str, meta_item_name: &str, lit: &syn::Lit) -> Result<String, ()> {
|
fn get_string_from_lit(cx: &Ctxt,
|
||||||
|
attr_name: &str,
|
||||||
|
meta_item_name: &str,
|
||||||
|
lit: &syn::Lit)
|
||||||
|
-> Result<String, ()> {
|
||||||
if let syn::Lit::Str(ref s, _) = *lit {
|
if let syn::Lit::Str(ref s, _) = *lit {
|
||||||
Ok(s.clone())
|
Ok(s.clone())
|
||||||
} else {
|
} else {
|
||||||
cx.error(format!("expected serde {} attribute to be a string: `{} = \"...\"`",
|
cx.error(format!("expected serde {} attribute to be a string: `{} = \"...\"`",
|
||||||
attr_name, meta_item_name));
|
attr_name,
|
||||||
|
meta_item_name));
|
||||||
Err(())
|
Err(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -616,7 +615,11 @@ fn parse_lit_into_path(cx: &Ctxt, attr_name: &str, lit: &syn::Lit) -> Result<syn
|
|||||||
syn::parse_path(&string).map_err(|err| cx.error(err))
|
syn::parse_path(&string).map_err(|err| cx.error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_lit_into_where(cx: &Ctxt, attr_name: &str, meta_item_name: &str, lit: &syn::Lit) -> Result<Vec<syn::WherePredicate>, ()> {
|
fn parse_lit_into_where(cx: &Ctxt,
|
||||||
|
attr_name: &str,
|
||||||
|
meta_item_name: &str,
|
||||||
|
lit: &syn::Lit)
|
||||||
|
-> Result<Vec<syn::WherePredicate>, ()> {
|
||||||
let string = try!(get_string_from_lit(cx, attr_name, meta_item_name, lit));
|
let string = try!(get_string_from_lit(cx, attr_name, meta_item_name, lit));
|
||||||
if string.is_empty() {
|
if string.is_empty() {
|
||||||
return Ok(Vec::new());
|
return Ok(Vec::new());
|
||||||
|
|||||||
@@ -8,9 +8,7 @@ pub struct Ctxt {
|
|||||||
|
|
||||||
impl Ctxt {
|
impl Ctxt {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Ctxt {
|
Ctxt { errors: RefCell::new(Some(Vec::new())) }
|
||||||
errors: RefCell::new(Some(Vec::new())),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn error<T: Display>(&self, msg: T) {
|
pub fn error<T: Display>(&self, msg: T) {
|
||||||
|
|||||||
+35
-35
@@ -10,36 +10,33 @@ use internals::attr;
|
|||||||
// allowed here".
|
// allowed here".
|
||||||
pub fn without_defaults(generics: &syn::Generics) -> syn::Generics {
|
pub fn without_defaults(generics: &syn::Generics) -> syn::Generics {
|
||||||
syn::Generics {
|
syn::Generics {
|
||||||
ty_params: generics.ty_params.iter().map(|ty_param| {
|
ty_params: generics.ty_params
|
||||||
syn::TyParam {
|
.iter()
|
||||||
default: None,
|
.map(|ty_param| syn::TyParam { default: None, ..ty_param.clone() })
|
||||||
.. ty_param.clone()
|
.collect(),
|
||||||
}}).collect(),
|
..generics.clone()
|
||||||
.. generics.clone()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_where_predicates(
|
pub fn with_where_predicates(generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
predicates: &[syn::WherePredicate])
|
||||||
predicates: &[syn::WherePredicate],
|
-> syn::Generics {
|
||||||
) -> syn::Generics {
|
|
||||||
aster::from_generics(generics.clone())
|
aster::from_generics(generics.clone())
|
||||||
.with_predicates(predicates.to_vec())
|
.with_predicates(predicates.to_vec())
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_where_predicates_from_fields<F>(
|
pub fn with_where_predicates_from_fields<F>(item: &Item,
|
||||||
item: &Item,
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
from_field: F)
|
||||||
from_field: F,
|
-> syn::Generics
|
||||||
) -> syn::Generics
|
where F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>
|
||||||
where F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
|
|
||||||
{
|
{
|
||||||
aster::from_generics(generics.clone())
|
aster::from_generics(generics.clone())
|
||||||
.with_predicates(
|
.with_predicates(item.body
|
||||||
item.body.all_fields()
|
.all_fields()
|
||||||
.flat_map(|field| from_field(&field.attrs))
|
.flat_map(|field| from_field(&field.attrs))
|
||||||
.flat_map(|predicates| predicates.to_vec()))
|
.flat_map(|predicates| predicates.to_vec()))
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,13 +51,12 @@ pub fn with_where_predicates_from_fields<F>(
|
|||||||
// #[serde(skip_serializing)]
|
// #[serde(skip_serializing)]
|
||||||
// c: C,
|
// c: C,
|
||||||
// }
|
// }
|
||||||
pub fn with_bound<F>(
|
pub fn with_bound<F>(item: &Item,
|
||||||
item: &Item,
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
filter: F,
|
||||||
filter: F,
|
bound: &syn::Path)
|
||||||
bound: &syn::Path,
|
-> syn::Generics
|
||||||
) -> syn::Generics
|
where F: Fn(&attr::Field) -> bool
|
||||||
where F: Fn(&attr::Field) -> bool,
|
|
||||||
{
|
{
|
||||||
struct FindTyParams {
|
struct FindTyParams {
|
||||||
// Set of all generic type parameters on the current struct (A, B, C in
|
// Set of all generic type parameters on the current struct (A, B, C in
|
||||||
@@ -90,11 +86,13 @@ pub fn with_bound<F>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let all_ty_params: HashSet<_> = generics.ty_params.iter()
|
let all_ty_params: HashSet<_> = generics.ty_params
|
||||||
|
.iter()
|
||||||
.map(|ty_param| ty_param.ident.clone())
|
.map(|ty_param| ty_param.ident.clone())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let relevant_tys = item.body.all_fields()
|
let relevant_tys = item.body
|
||||||
|
.all_fields()
|
||||||
.filter(|&field| filter(&field.attrs))
|
.filter(|&field| filter(&field.attrs))
|
||||||
.map(|field| &field.ty);
|
.map(|field| &field.ty);
|
||||||
|
|
||||||
@@ -107,15 +105,17 @@ pub fn with_bound<F>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
aster::from_generics(generics.clone())
|
aster::from_generics(generics.clone())
|
||||||
.with_predicates(
|
.with_predicates(generics.ty_params
|
||||||
generics.ty_params.iter()
|
.iter()
|
||||||
.map(|ty_param| ty_param.ident.clone())
|
.map(|ty_param| ty_param.ident.clone())
|
||||||
.filter(|id| visitor.relevant_ty_params.contains(id))
|
.filter(|id| visitor.relevant_ty_params.contains(id))
|
||||||
.map(|id| aster::where_predicate()
|
.map(|id| {
|
||||||
|
aster::where_predicate()
|
||||||
// the type parameter that is being bounded e.g. T
|
// the type parameter that is being bounded e.g. T
|
||||||
.bound().build(aster::ty().id(id))
|
.bound().build(aster::ty().id(id))
|
||||||
// the bound e.g. Serialize
|
// the bound e.g. Serialize
|
||||||
.bound().trait_(bound.clone()).build()
|
.bound().trait_(bound.clone()).build()
|
||||||
.build()))
|
.build()
|
||||||
|
}))
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|||||||
+242
-348
@@ -18,13 +18,14 @@ pub fn expand_derive_deserialize(item: &syn::DeriveInput) -> Result<Tokens, Stri
|
|||||||
|
|
||||||
let impl_generics = build_impl_generics(&item);
|
let impl_generics = build_impl_generics(&item);
|
||||||
|
|
||||||
let ty = aster::ty().path()
|
let ty = aster::ty()
|
||||||
.segment(item.ident.clone()).with_generics(impl_generics.clone()).build()
|
.path()
|
||||||
|
.segment(item.ident.clone())
|
||||||
|
.with_generics(impl_generics.clone())
|
||||||
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let body = deserialize_body(&item,
|
let body = deserialize_body(&item, &impl_generics, ty.clone());
|
||||||
&impl_generics,
|
|
||||||
ty.clone());
|
|
||||||
|
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
@@ -50,21 +51,21 @@ pub fn expand_derive_deserialize(item: &syn::DeriveInput) -> Result<Tokens, Stri
|
|||||||
fn build_impl_generics(item: &Item) -> syn::Generics {
|
fn build_impl_generics(item: &Item) -> syn::Generics {
|
||||||
let generics = bound::without_defaults(item.generics);
|
let generics = bound::without_defaults(item.generics);
|
||||||
|
|
||||||
let generics = bound::with_where_predicates_from_fields(
|
let generics =
|
||||||
item, &generics,
|
bound::with_where_predicates_from_fields(item, &generics, |attrs| attrs.de_bound());
|
||||||
|attrs| attrs.de_bound());
|
|
||||||
|
|
||||||
match item.attrs.de_bound() {
|
match item.attrs.de_bound() {
|
||||||
Some(predicates) => {
|
Some(predicates) => bound::with_where_predicates(&generics, predicates),
|
||||||
bound::with_where_predicates(&generics, predicates)
|
|
||||||
}
|
|
||||||
None => {
|
None => {
|
||||||
let generics = bound::with_bound(item, &generics,
|
let generics =
|
||||||
needs_deserialize_bound,
|
bound::with_bound(item,
|
||||||
&aster::path().ids(&["_serde", "Deserialize"]).build());
|
&generics,
|
||||||
bound::with_bound(item, &generics,
|
needs_deserialize_bound,
|
||||||
requires_default,
|
&aster::path().ids(&["_serde", "Deserialize"]).build());
|
||||||
&aster::path().global().ids(&["std", "default", "Default"]).build())
|
bound::with_bound(item,
|
||||||
|
&generics,
|
||||||
|
requires_default,
|
||||||
|
&aster::path().global().ids(&["std", "default", "Default"]).build())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,9 +75,7 @@ fn build_impl_generics(item: &Item) -> syn::Generics {
|
|||||||
// attribute specify their own bound so we do not generate one. All other fields
|
// attribute specify their own bound so we do not generate one. All other fields
|
||||||
// may need a `T: Deserialize` bound where T is the type of the field.
|
// may need a `T: Deserialize` bound where T is the type of the field.
|
||||||
fn needs_deserialize_bound(attrs: &attr::Field) -> bool {
|
fn needs_deserialize_bound(attrs: &attr::Field) -> bool {
|
||||||
!attrs.skip_deserializing()
|
!attrs.skip_deserializing() && attrs.deserialize_with().is_none() && attrs.de_bound().is_none()
|
||||||
&& attrs.deserialize_with().is_none()
|
|
||||||
&& attrs.de_bound().is_none()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fields with a `default` attribute (not `default=...`), and fields with a
|
// Fields with a `default` attribute (not `default=...`), and fields with a
|
||||||
@@ -85,33 +84,23 @@ fn requires_default(attrs: &attr::Field) -> bool {
|
|||||||
attrs.default() == &attr::FieldDefault::Default
|
attrs.default() == &attr::FieldDefault::Default
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_body(
|
fn deserialize_body(item: &Item, impl_generics: &syn::Generics, ty: syn::Ty) -> Tokens {
|
||||||
item: &Item,
|
|
||||||
impl_generics: &syn::Generics,
|
|
||||||
ty: syn::Ty,
|
|
||||||
) -> Tokens {
|
|
||||||
match item.body {
|
match item.body {
|
||||||
Body::Enum(ref variants) => {
|
Body::Enum(ref variants) => {
|
||||||
deserialize_item_enum(
|
deserialize_item_enum(&item.ident, impl_generics, ty, variants, &item.attrs)
|
||||||
&item.ident,
|
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
variants,
|
|
||||||
&item.attrs)
|
|
||||||
}
|
}
|
||||||
Body::Struct(Style::Struct, ref fields) => {
|
Body::Struct(Style::Struct, ref fields) => {
|
||||||
if fields.iter().any(|field| field.ident.is_none()) {
|
if fields.iter().any(|field| field.ident.is_none()) {
|
||||||
panic!("struct has unnamed fields");
|
panic!("struct has unnamed fields");
|
||||||
}
|
}
|
||||||
|
|
||||||
deserialize_struct(
|
deserialize_struct(&item.ident,
|
||||||
&item.ident,
|
None,
|
||||||
None,
|
impl_generics,
|
||||||
impl_generics,
|
ty,
|
||||||
ty,
|
fields,
|
||||||
fields,
|
&item.attrs,
|
||||||
&item.attrs,
|
None)
|
||||||
None)
|
|
||||||
}
|
}
|
||||||
Body::Struct(Style::Tuple, ref fields) |
|
Body::Struct(Style::Tuple, ref fields) |
|
||||||
Body::Struct(Style::Newtype, ref fields) => {
|
Body::Struct(Style::Newtype, ref fields) => {
|
||||||
@@ -119,20 +108,15 @@ fn deserialize_body(
|
|||||||
panic!("tuple struct has named fields");
|
panic!("tuple struct has named fields");
|
||||||
}
|
}
|
||||||
|
|
||||||
deserialize_tuple(
|
deserialize_tuple(&item.ident,
|
||||||
&item.ident,
|
None,
|
||||||
None,
|
impl_generics,
|
||||||
impl_generics,
|
ty,
|
||||||
ty,
|
fields,
|
||||||
fields,
|
&item.attrs,
|
||||||
&item.attrs,
|
None)
|
||||||
None)
|
|
||||||
}
|
|
||||||
Body::Struct(Style::Unit, _) => {
|
|
||||||
deserialize_unit_struct(
|
|
||||||
&item.ident,
|
|
||||||
&item.attrs)
|
|
||||||
}
|
}
|
||||||
|
Body::Struct(Style::Unit, _) => deserialize_unit_struct(&item.ident, &item.attrs),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,33 +129,37 @@ fn deserialize_body(
|
|||||||
// 3. the expression for instantiating the visitor
|
// 3. the expression for instantiating the visitor
|
||||||
fn deserialize_visitor(generics: &syn::Generics) -> (Tokens, Tokens, Tokens) {
|
fn deserialize_visitor(generics: &syn::Generics) -> (Tokens, Tokens, Tokens) {
|
||||||
if generics.lifetimes.is_empty() && generics.ty_params.is_empty() {
|
if generics.lifetimes.is_empty() && generics.ty_params.is_empty() {
|
||||||
(
|
(quote! {
|
||||||
quote! {
|
|
||||||
struct __Visitor;
|
struct __Visitor;
|
||||||
},
|
},
|
||||||
quote!(__Visitor),
|
quote!(__Visitor),
|
||||||
quote!(__Visitor),
|
quote!(__Visitor))
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
let where_clause = &generics.where_clause;
|
let where_clause = &generics.where_clause;
|
||||||
|
|
||||||
let num_phantoms = generics.lifetimes.len() + generics.ty_params.len();
|
let num_phantoms = generics.lifetimes.len() + generics.ty_params.len();
|
||||||
|
|
||||||
let phantom_types = generics.lifetimes.iter()
|
let phantom_types = generics.lifetimes
|
||||||
|
.iter()
|
||||||
.map(|lifetime_def| {
|
.map(|lifetime_def| {
|
||||||
let lifetime = &lifetime_def.lifetime;
|
let lifetime = &lifetime_def.lifetime;
|
||||||
quote!(_serde::export::PhantomData<& #lifetime ()>)
|
quote!(_serde::export::PhantomData<& #lifetime ()>)
|
||||||
}).chain(generics.ty_params.iter()
|
})
|
||||||
|
.chain(generics.ty_params
|
||||||
|
.iter()
|
||||||
.map(|ty_param| {
|
.map(|ty_param| {
|
||||||
let ident = &ty_param.ident;
|
let ident = &ty_param.ident;
|
||||||
quote!(_serde::export::PhantomData<#ident>)
|
quote!(_serde::export::PhantomData<#ident>)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let all_params = generics.lifetimes.iter()
|
let all_params = generics.lifetimes
|
||||||
|
.iter()
|
||||||
.map(|lifetime_def| {
|
.map(|lifetime_def| {
|
||||||
let lifetime = &lifetime_def.lifetime;
|
let lifetime = &lifetime_def.lifetime;
|
||||||
quote!(#lifetime)
|
quote!(#lifetime)
|
||||||
}).chain(generics.ty_params.iter()
|
})
|
||||||
|
.chain(generics.ty_params
|
||||||
|
.iter()
|
||||||
.map(|ty_param| {
|
.map(|ty_param| {
|
||||||
let ident = &ty_param.ident;
|
let ident = &ty_param.ident;
|
||||||
quote!(#ident)
|
quote!(#ident)
|
||||||
@@ -186,20 +174,15 @@ fn deserialize_visitor(generics: &syn::Generics) -> (Tokens, Tokens, Tokens) {
|
|||||||
|
|
||||||
let phantom_exprs = iter::repeat(quote!(_serde::export::PhantomData)).take(num_phantoms);
|
let phantom_exprs = iter::repeat(quote!(_serde::export::PhantomData)).take(num_phantoms);
|
||||||
|
|
||||||
(
|
(quote! {
|
||||||
quote! {
|
|
||||||
struct __Visitor #generics ( #(#phantom_types),* ) #where_clause;
|
struct __Visitor #generics ( #(#phantom_types),* ) #where_clause;
|
||||||
},
|
},
|
||||||
quote!(__Visitor <#(#all_params),*> ),
|
quote!(__Visitor <#(#all_params),*> ),
|
||||||
quote!(__Visitor #ty_param_idents ( #(#phantom_exprs),* )),
|
quote!(__Visitor #ty_param_idents ( #(#phantom_exprs),* )))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_unit_struct(
|
fn deserialize_unit_struct(type_ident: &syn::Ident, item_attrs: &attr::Item) -> Tokens {
|
||||||
type_ident: &syn::Ident,
|
|
||||||
item_attrs: &attr::Item,
|
|
||||||
) -> Tokens {
|
|
||||||
let type_name = item_attrs.name().deserialize_name();
|
let type_name = item_attrs.name().deserialize_name();
|
||||||
|
|
||||||
let expecting = format!("unit struct {}", type_ident);
|
let expecting = format!("unit struct {}", type_ident);
|
||||||
@@ -233,15 +216,14 @@ fn deserialize_unit_struct(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_tuple(
|
fn deserialize_tuple(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
variant_ident: Option<&syn::Ident>,
|
||||||
variant_ident: Option<&syn::Ident>,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
fields: &[Field],
|
||||||
fields: &[Field],
|
item_attrs: &attr::Item,
|
||||||
item_attrs: &attr::Item,
|
deserializer: Option<Tokens>)
|
||||||
deserializer: Option<Tokens>,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(impl_generics);
|
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(impl_generics);
|
||||||
@@ -259,23 +241,12 @@ fn deserialize_tuple(
|
|||||||
let nfields = fields.len();
|
let nfields = fields.len();
|
||||||
|
|
||||||
let visit_newtype_struct = if !is_enum && nfields == 1 {
|
let visit_newtype_struct = if !is_enum && nfields == 1 {
|
||||||
Some(deserialize_newtype_struct(
|
Some(deserialize_newtype_struct(type_ident, &type_path, impl_generics, &fields[0]))
|
||||||
type_ident,
|
|
||||||
&type_path,
|
|
||||||
impl_generics,
|
|
||||||
&fields[0],
|
|
||||||
))
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let visit_seq = deserialize_seq(
|
let visit_seq = deserialize_seq(type_ident, &type_path, impl_generics, fields, false);
|
||||||
type_ident,
|
|
||||||
&type_path,
|
|
||||||
impl_generics,
|
|
||||||
fields,
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
let dispatch = if let Some(deserializer) = deserializer {
|
let dispatch = if let Some(deserializer) = deserializer {
|
||||||
quote!(_serde::Deserializer::deserialize(#deserializer, #visitor_expr))
|
quote!(_serde::Deserializer::deserialize(#deserializer, #visitor_expr))
|
||||||
@@ -320,13 +291,12 @@ fn deserialize_tuple(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_seq(
|
fn deserialize_seq(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
type_path: &Tokens,
|
||||||
type_path: &Tokens,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
fields: &[Field],
|
||||||
fields: &[Field],
|
is_struct: bool)
|
||||||
is_struct: bool,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let vars = (0..fields.len()).map(field_i as fn(_) -> _);
|
let vars = (0..fields.len()).map(field_i as fn(_) -> _);
|
||||||
|
|
||||||
let deserialized_count = fields.iter()
|
let deserialized_count = fields.iter()
|
||||||
@@ -389,12 +359,11 @@ fn deserialize_seq(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_newtype_struct(
|
fn deserialize_newtype_struct(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
type_path: &Tokens,
|
||||||
type_path: &Tokens,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
field: &Field)
|
||||||
field: &Field,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let value = match field.attrs.deserialize_with() {
|
let value = match field.attrs.deserialize_with() {
|
||||||
None => {
|
None => {
|
||||||
let field_ty = &field.ty;
|
let field_ty = &field.ty;
|
||||||
@@ -403,8 +372,8 @@ fn deserialize_newtype_struct(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(path) => {
|
Some(path) => {
|
||||||
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
let (wrapper, wrapper_impl, wrapper_ty) =
|
||||||
type_ident, impl_generics, field.ty, path);
|
wrap_deserialize_with(type_ident, impl_generics, field.ty, path);
|
||||||
quote!({
|
quote!({
|
||||||
#wrapper
|
#wrapper
|
||||||
#wrapper_impl
|
#wrapper_impl
|
||||||
@@ -422,15 +391,14 @@ fn deserialize_newtype_struct(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_struct(
|
fn deserialize_struct(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
variant_ident: Option<&syn::Ident>,
|
||||||
variant_ident: Option<&syn::Ident>,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
fields: &[Field],
|
||||||
fields: &[Field],
|
item_attrs: &attr::Item,
|
||||||
item_attrs: &attr::Item,
|
deserializer: Option<Tokens>)
|
||||||
deserializer: Option<Tokens>,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let is_enum = variant_ident.is_some();
|
let is_enum = variant_ident.is_some();
|
||||||
let is_untagged = deserializer.is_some();
|
let is_untagged = deserializer.is_some();
|
||||||
|
|
||||||
@@ -447,21 +415,10 @@ fn deserialize_struct(
|
|||||||
None => format!("struct {}", type_ident),
|
None => format!("struct {}", type_ident),
|
||||||
};
|
};
|
||||||
|
|
||||||
let visit_seq = deserialize_seq(
|
let visit_seq = deserialize_seq(type_ident, &type_path, impl_generics, fields, true);
|
||||||
type_ident,
|
|
||||||
&type_path,
|
|
||||||
impl_generics,
|
|
||||||
fields,
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
|
|
||||||
let (field_visitor, fields_stmt, visit_map) = deserialize_struct_visitor(
|
let (field_visitor, fields_stmt, visit_map) =
|
||||||
type_ident,
|
deserialize_struct_visitor(type_ident, type_path, impl_generics, fields, item_attrs);
|
||||||
type_path,
|
|
||||||
impl_generics,
|
|
||||||
fields,
|
|
||||||
item_attrs,
|
|
||||||
);
|
|
||||||
|
|
||||||
let dispatch = if let Some(deserializer) = deserializer {
|
let dispatch = if let Some(deserializer) = deserializer {
|
||||||
quote! {
|
quote! {
|
||||||
@@ -527,52 +484,36 @@ fn deserialize_struct(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_item_enum(
|
fn deserialize_item_enum(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variants: &[Variant],
|
||||||
variants: &[Variant],
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
match *item_attrs.tag() {
|
match *item_attrs.tag() {
|
||||||
attr::EnumTag::External => {
|
attr::EnumTag::External => {
|
||||||
deserialize_externally_tagged_enum(
|
deserialize_externally_tagged_enum(type_ident, impl_generics, ty, variants, item_attrs)
|
||||||
type_ident,
|
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
variants,
|
|
||||||
item_attrs,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
attr::EnumTag::Internal(ref tag) => {
|
attr::EnumTag::Internal(ref tag) => {
|
||||||
deserialize_internally_tagged_enum(
|
deserialize_internally_tagged_enum(type_ident,
|
||||||
type_ident,
|
impl_generics,
|
||||||
impl_generics,
|
ty,
|
||||||
ty,
|
variants,
|
||||||
variants,
|
item_attrs,
|
||||||
item_attrs,
|
tag)
|
||||||
tag,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
attr::EnumTag::None => {
|
attr::EnumTag::None => {
|
||||||
deserialize_untagged_enum(
|
deserialize_untagged_enum(type_ident, impl_generics, ty, variants, item_attrs)
|
||||||
type_ident,
|
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
variants,
|
|
||||||
item_attrs,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_externally_tagged_enum(
|
fn deserialize_externally_tagged_enum(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variants: &[Variant],
|
||||||
variants: &[Variant],
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
let type_name = item_attrs.name().deserialize_name();
|
let type_name = item_attrs.name().deserialize_name();
|
||||||
@@ -592,11 +533,7 @@ fn deserialize_externally_tagged_enum(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let variant_visitor = deserialize_field_visitor(
|
let variant_visitor = deserialize_field_visitor(variant_names_idents, item_attrs, true);
|
||||||
variant_names_idents,
|
|
||||||
item_attrs,
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Match arms to extract a variant from a string
|
// Match arms to extract a variant from a string
|
||||||
let variant_arms = variants.iter()
|
let variant_arms = variants.iter()
|
||||||
@@ -605,13 +542,11 @@ fn deserialize_externally_tagged_enum(
|
|||||||
.map(|(i, variant)| {
|
.map(|(i, variant)| {
|
||||||
let variant_name = field_i(i);
|
let variant_name = field_i(i);
|
||||||
|
|
||||||
let block = deserialize_externally_tagged_variant(
|
let block = deserialize_externally_tagged_variant(type_ident,
|
||||||
type_ident,
|
impl_generics,
|
||||||
impl_generics,
|
ty.clone(),
|
||||||
ty.clone(),
|
variant,
|
||||||
variant,
|
item_attrs);
|
||||||
item_attrs,
|
|
||||||
);
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
(__Field::#variant_name, visitor) => #block
|
(__Field::#variant_name, visitor) => #block
|
||||||
@@ -664,14 +599,13 @@ fn deserialize_externally_tagged_enum(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_internally_tagged_enum(
|
fn deserialize_internally_tagged_enum(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variants: &[Variant],
|
||||||
variants: &[Variant],
|
item_attrs: &attr::Item,
|
||||||
item_attrs: &attr::Item,
|
tag: &str)
|
||||||
tag: &str,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let variant_names_idents: Vec<_> = variants.iter()
|
let variant_names_idents: Vec<_> = variants.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||||
@@ -685,11 +619,7 @@ fn deserialize_internally_tagged_enum(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let variant_visitor = deserialize_field_visitor(
|
let variant_visitor = deserialize_field_visitor(variant_names_idents, item_attrs, true);
|
||||||
variant_names_idents,
|
|
||||||
item_attrs,
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Match arms to extract a variant from a string
|
// Match arms to extract a variant from a string
|
||||||
let variant_arms = variants.iter()
|
let variant_arms = variants.iter()
|
||||||
@@ -727,13 +657,12 @@ fn deserialize_internally_tagged_enum(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_untagged_enum(
|
fn deserialize_untagged_enum(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variants: &[Variant],
|
||||||
variants: &[Variant],
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let attempts = variants.iter()
|
let attempts = variants.iter()
|
||||||
.filter(|variant| !variant.attrs.skip_deserializing())
|
.filter(|variant| !variant.attrs.skip_deserializing())
|
||||||
.map(|variant| {
|
.map(|variant| {
|
||||||
@@ -768,13 +697,12 @@ fn deserialize_untagged_enum(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_externally_tagged_variant(
|
fn deserialize_externally_tagged_variant(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variant: &Variant,
|
||||||
variant: &Variant,
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let variant_ident = &variant.ident;
|
let variant_ident = &variant.ident;
|
||||||
|
|
||||||
match variant.style {
|
match variant.style {
|
||||||
@@ -785,46 +713,39 @@ fn deserialize_externally_tagged_variant(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
Style::Newtype => {
|
Style::Newtype => {
|
||||||
deserialize_externally_tagged_newtype_variant(
|
deserialize_externally_tagged_newtype_variant(type_ident,
|
||||||
type_ident,
|
variant_ident,
|
||||||
variant_ident,
|
generics,
|
||||||
generics,
|
&variant.fields[0])
|
||||||
&variant.fields[0],
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Style::Tuple => {
|
Style::Tuple => {
|
||||||
deserialize_tuple(
|
deserialize_tuple(type_ident,
|
||||||
type_ident,
|
Some(variant_ident),
|
||||||
Some(variant_ident),
|
generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
&variant.fields,
|
||||||
&variant.fields,
|
item_attrs,
|
||||||
item_attrs,
|
None)
|
||||||
None,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Style::Struct => {
|
Style::Struct => {
|
||||||
deserialize_struct(
|
deserialize_struct(type_ident,
|
||||||
type_ident,
|
Some(variant_ident),
|
||||||
Some(variant_ident),
|
generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
&variant.fields,
|
||||||
&variant.fields,
|
item_attrs,
|
||||||
item_attrs,
|
None)
|
||||||
None,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_internally_tagged_variant(
|
fn deserialize_internally_tagged_variant(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variant: &Variant,
|
||||||
variant: &Variant,
|
item_attrs: &attr::Item,
|
||||||
item_attrs: &attr::Item,
|
deserializer: Tokens)
|
||||||
deserializer: Tokens,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let variant_ident = &variant.ident;
|
let variant_ident = &variant.ident;
|
||||||
|
|
||||||
match variant.style {
|
match variant.style {
|
||||||
@@ -837,27 +758,24 @@ fn deserialize_internally_tagged_variant(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
Style::Newtype | Style::Struct => {
|
Style::Newtype | Style::Struct => {
|
||||||
deserialize_untagged_variant(
|
deserialize_untagged_variant(type_ident,
|
||||||
type_ident,
|
generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
variant,
|
||||||
variant,
|
item_attrs,
|
||||||
item_attrs,
|
deserializer)
|
||||||
deserializer,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Style::Tuple => unreachable!("checked in serde_codegen_internals"),
|
Style::Tuple => unreachable!("checked in serde_codegen_internals"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_untagged_variant(
|
fn deserialize_untagged_variant(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variant: &Variant,
|
||||||
variant: &Variant,
|
item_attrs: &attr::Item,
|
||||||
item_attrs: &attr::Item,
|
deserializer: Tokens)
|
||||||
deserializer: Tokens,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let variant_ident = &variant.ident;
|
let variant_ident = &variant.ident;
|
||||||
|
|
||||||
match variant.style {
|
match variant.style {
|
||||||
@@ -874,45 +792,38 @@ fn deserialize_untagged_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Style::Newtype => {
|
Style::Newtype => {
|
||||||
deserialize_untagged_newtype_variant(
|
deserialize_untagged_newtype_variant(type_ident,
|
||||||
type_ident,
|
variant_ident,
|
||||||
variant_ident,
|
generics,
|
||||||
generics,
|
&variant.fields[0],
|
||||||
&variant.fields[0],
|
deserializer)
|
||||||
deserializer,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Style::Tuple => {
|
Style::Tuple => {
|
||||||
deserialize_tuple(
|
deserialize_tuple(type_ident,
|
||||||
type_ident,
|
Some(variant_ident),
|
||||||
Some(variant_ident),
|
generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
&variant.fields,
|
||||||
&variant.fields,
|
item_attrs,
|
||||||
item_attrs,
|
Some(deserializer))
|
||||||
Some(deserializer),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Style::Struct => {
|
Style::Struct => {
|
||||||
deserialize_struct(
|
deserialize_struct(type_ident,
|
||||||
type_ident,
|
Some(variant_ident),
|
||||||
Some(variant_ident),
|
generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
&variant.fields,
|
||||||
&variant.fields,
|
item_attrs,
|
||||||
item_attrs,
|
Some(deserializer))
|
||||||
Some(deserializer),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_externally_tagged_newtype_variant(
|
fn deserialize_externally_tagged_newtype_variant(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
variant_ident: &syn::Ident,
|
||||||
variant_ident: &syn::Ident,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
field: &Field)
|
||||||
field: &Field,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
match field.attrs.deserialize_with() {
|
match field.attrs.deserialize_with() {
|
||||||
None => {
|
None => {
|
||||||
let field_ty = &field.ty;
|
let field_ty = &field.ty;
|
||||||
@@ -923,8 +834,8 @@ fn deserialize_externally_tagged_newtype_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(path) => {
|
Some(path) => {
|
||||||
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
let (wrapper, wrapper_impl, wrapper_ty) =
|
||||||
type_ident, impl_generics, field.ty, path);
|
wrap_deserialize_with(type_ident, impl_generics, field.ty, path);
|
||||||
quote!({
|
quote!({
|
||||||
#wrapper
|
#wrapper
|
||||||
#wrapper_impl
|
#wrapper_impl
|
||||||
@@ -936,13 +847,12 @@ fn deserialize_externally_tagged_newtype_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_untagged_newtype_variant(
|
fn deserialize_untagged_newtype_variant(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
variant_ident: &syn::Ident,
|
||||||
variant_ident: &syn::Ident,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
field: &Field,
|
||||||
field: &Field,
|
deserializer: Tokens)
|
||||||
deserializer: Tokens,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
match field.attrs.deserialize_with() {
|
match field.attrs.deserialize_with() {
|
||||||
None => {
|
None => {
|
||||||
let field_ty = &field.ty;
|
let field_ty = &field.ty;
|
||||||
@@ -953,8 +863,8 @@ fn deserialize_untagged_newtype_variant(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
Some(path) => {
|
Some(path) => {
|
||||||
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
let (wrapper, wrapper_impl, wrapper_ty) =
|
||||||
type_ident, impl_generics, field.ty, path);
|
wrap_deserialize_with(type_ident, impl_generics, field.ty, path);
|
||||||
quote!({
|
quote!({
|
||||||
#wrapper
|
#wrapper
|
||||||
#wrapper_impl
|
#wrapper_impl
|
||||||
@@ -966,11 +876,10 @@ fn deserialize_untagged_newtype_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_field_visitor(
|
fn deserialize_field_visitor(fields: Vec<(String, Ident)>,
|
||||||
fields: Vec<(String, Ident)>,
|
item_attrs: &attr::Item,
|
||||||
item_attrs: &attr::Item,
|
is_variant: bool)
|
||||||
is_variant: bool,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let field_strs = fields.iter().map(|&(ref name, _)| name);
|
let field_strs = fields.iter().map(|&(ref name, _)| name);
|
||||||
let field_bytes = fields.iter().map(|&(ref name, _)| quote::ByteStr(name));
|
let field_bytes = fields.iter().map(|&(ref name, _)| quote::ByteStr(name));
|
||||||
let field_idents: &Vec<_> = &fields.iter().map(|&(_, ref ident)| ident).collect();
|
let field_idents: &Vec<_> = &fields.iter().map(|&(_, ref ident)| ident).collect();
|
||||||
@@ -1081,13 +990,12 @@ fn deserialize_field_visitor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_struct_visitor(
|
fn deserialize_struct_visitor(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
struct_path: Tokens,
|
||||||
struct_path: Tokens,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
fields: &[Field],
|
||||||
fields: &[Field],
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> (Tokens, Tokens, Tokens) {
|
||||||
) -> (Tokens, Tokens, Tokens) {
|
|
||||||
let field_names_idents: Vec<_> = fields.iter()
|
let field_names_idents: Vec<_> = fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|&(_, field)| !field.attrs.skip_deserializing())
|
.filter(|&(_, field)| !field.attrs.skip_deserializing())
|
||||||
@@ -1101,30 +1009,19 @@ fn deserialize_struct_visitor(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let field_visitor = deserialize_field_visitor(
|
let field_visitor = deserialize_field_visitor(field_names_idents, item_attrs, false);
|
||||||
field_names_idents,
|
|
||||||
item_attrs,
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
let visit_map = deserialize_map(
|
let visit_map = deserialize_map(type_ident, struct_path, impl_generics, fields, item_attrs);
|
||||||
type_ident,
|
|
||||||
struct_path,
|
|
||||||
impl_generics,
|
|
||||||
fields,
|
|
||||||
item_attrs,
|
|
||||||
);
|
|
||||||
|
|
||||||
(field_visitor, fields_stmt, visit_map)
|
(field_visitor, fields_stmt, visit_map)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_map(
|
fn deserialize_map(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
struct_path: Tokens,
|
||||||
struct_path: Tokens,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
fields: &[Field],
|
||||||
fields: &[Field],
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
// Create the field names for the fields.
|
// Create the field names for the fields.
|
||||||
let fields_names: Vec<_> = fields.iter()
|
let fields_names: Vec<_> = fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
@@ -1243,38 +1140,36 @@ fn field_i(i: usize) -> Ident {
|
|||||||
|
|
||||||
/// This function wraps the expression in `#[serde(deserialize_with="...")]` in
|
/// This function wraps the expression in `#[serde(deserialize_with="...")]` in
|
||||||
/// a trait to prevent it from accessing the internal `Deserialize` state.
|
/// a trait to prevent it from accessing the internal `Deserialize` state.
|
||||||
fn wrap_deserialize_with(
|
fn wrap_deserialize_with(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
field_ty: &syn::Ty,
|
||||||
field_ty: &syn::Ty,
|
deserialize_with: &syn::Path)
|
||||||
deserialize_with: &syn::Path,
|
-> (Tokens, Tokens, syn::Path) {
|
||||||
) -> (Tokens, Tokens, syn::Path) {
|
|
||||||
// Quasi-quoting doesn't do a great job of expanding generics into paths,
|
// Quasi-quoting doesn't do a great job of expanding generics into paths,
|
||||||
// so manually build it.
|
// so manually build it.
|
||||||
let wrapper_ty = aster::path()
|
let wrapper_ty = aster::path()
|
||||||
.segment("__SerdeDeserializeWithStruct")
|
.segment("__SerdeDeserializeWithStruct")
|
||||||
.with_generics(impl_generics.clone())
|
.with_generics(impl_generics.clone())
|
||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
let phantom_ty = aster::path()
|
let phantom_ty = aster::path()
|
||||||
.segment(type_ident)
|
.segment(type_ident)
|
||||||
.with_generics(aster::from_generics(impl_generics.clone())
|
.with_generics(aster::from_generics(impl_generics.clone())
|
||||||
.strip_ty_params()
|
.strip_ty_params()
|
||||||
.build())
|
.build())
|
||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
(
|
(quote! {
|
||||||
quote! {
|
|
||||||
struct __SerdeDeserializeWithStruct #impl_generics #where_clause {
|
struct __SerdeDeserializeWithStruct #impl_generics #where_clause {
|
||||||
value: #field_ty,
|
value: #field_ty,
|
||||||
phantom: _serde::export::PhantomData<#phantom_ty>,
|
phantom: _serde::export::PhantomData<#phantom_ty>,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
quote! {
|
quote! {
|
||||||
impl #impl_generics _serde::Deserialize for #wrapper_ty #where_clause {
|
impl #impl_generics _serde::Deserialize for #wrapper_ty #where_clause {
|
||||||
fn deserialize<__D>(__d: __D) -> _serde::export::Result<Self, __D::Error>
|
fn deserialize<__D>(__d: __D) -> _serde::export::Result<Self, __D::Error>
|
||||||
where __D: _serde::Deserializer
|
where __D: _serde::Deserializer
|
||||||
@@ -1287,8 +1182,7 @@ fn wrap_deserialize_with(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
wrapper_ty,
|
wrapper_ty)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expr_is_missing(attrs: &attr::Field) -> Tokens {
|
fn expr_is_missing(attrs: &attr::Field) -> Tokens {
|
||||||
@@ -1319,14 +1213,14 @@ fn expr_is_missing(attrs: &attr::Field) -> Tokens {
|
|||||||
|
|
||||||
fn check_no_str(cx: &internals::Ctxt, item: &Item) {
|
fn check_no_str(cx: &internals::Ctxt, item: &Item) {
|
||||||
let fail = || {
|
let fail = || {
|
||||||
cx.error(
|
cx.error("Serde does not support deserializing fields of type &str; consider using \
|
||||||
"Serde does not support deserializing fields of type &str; \
|
String instead");
|
||||||
consider using String instead");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for field in item.body.all_fields() {
|
for field in item.body.all_fields() {
|
||||||
if field.attrs.skip_deserializing()
|
if field.attrs.skip_deserializing() || field.attrs.deserialize_with().is_some() {
|
||||||
|| field.attrs.deserialize_with().is_some() { continue }
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if let syn::Ty::Rptr(_, ref inner) = *field.ty {
|
if let syn::Ty::Rptr(_, ref inner) = *field.ty {
|
||||||
if let syn::Ty::Path(_, ref path) = inner.ty {
|
if let syn::Ty::Path(_, ref path) = inner.ty {
|
||||||
|
|||||||
+196
-287
@@ -12,13 +12,14 @@ pub fn expand_derive_serialize(item: &syn::DeriveInput) -> Result<Tokens, String
|
|||||||
|
|
||||||
let impl_generics = build_impl_generics(&item);
|
let impl_generics = build_impl_generics(&item);
|
||||||
|
|
||||||
let ty = aster::ty().path()
|
let ty = aster::ty()
|
||||||
.segment(item.ident.clone()).with_generics(impl_generics.clone()).build()
|
.path()
|
||||||
|
.segment(item.ident.clone())
|
||||||
|
.with_generics(impl_generics.clone())
|
||||||
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let body = serialize_body(&item,
|
let body = serialize_body(&item, &impl_generics, ty.clone());
|
||||||
&impl_generics,
|
|
||||||
ty.clone());
|
|
||||||
|
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
@@ -45,18 +46,16 @@ pub fn expand_derive_serialize(item: &syn::DeriveInput) -> Result<Tokens, String
|
|||||||
fn build_impl_generics(item: &Item) -> syn::Generics {
|
fn build_impl_generics(item: &Item) -> syn::Generics {
|
||||||
let generics = bound::without_defaults(item.generics);
|
let generics = bound::without_defaults(item.generics);
|
||||||
|
|
||||||
let generics = bound::with_where_predicates_from_fields(
|
let generics =
|
||||||
item, &generics,
|
bound::with_where_predicates_from_fields(item, &generics, |attrs| attrs.ser_bound());
|
||||||
|attrs| attrs.ser_bound());
|
|
||||||
|
|
||||||
match item.attrs.ser_bound() {
|
match item.attrs.ser_bound() {
|
||||||
Some(predicates) => {
|
Some(predicates) => bound::with_where_predicates(&generics, predicates),
|
||||||
bound::with_where_predicates(&generics, predicates)
|
|
||||||
}
|
|
||||||
None => {
|
None => {
|
||||||
bound::with_bound(item, &generics,
|
bound::with_bound(item,
|
||||||
needs_serialize_bound,
|
&generics,
|
||||||
&aster::path().ids(&["_serde", "Serialize"]).build())
|
needs_serialize_bound,
|
||||||
|
&aster::path().ids(&["_serde", "Serialize"]).build())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,58 +65,32 @@ fn build_impl_generics(item: &Item) -> syn::Generics {
|
|||||||
// attribute specify their own bound so we do not generate one. All other fields
|
// attribute specify their own bound so we do not generate one. All other fields
|
||||||
// may need a `T: Serialize` bound where T is the type of the field.
|
// may need a `T: Serialize` bound where T is the type of the field.
|
||||||
fn needs_serialize_bound(attrs: &attr::Field) -> bool {
|
fn needs_serialize_bound(attrs: &attr::Field) -> bool {
|
||||||
!attrs.skip_serializing()
|
!attrs.skip_serializing() && attrs.serialize_with().is_none() && attrs.ser_bound().is_none()
|
||||||
&& attrs.serialize_with().is_none()
|
|
||||||
&& attrs.ser_bound().is_none()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_body(
|
fn serialize_body(item: &Item, impl_generics: &syn::Generics, ty: syn::Ty) -> Tokens {
|
||||||
item: &Item,
|
|
||||||
impl_generics: &syn::Generics,
|
|
||||||
ty: syn::Ty,
|
|
||||||
) -> Tokens {
|
|
||||||
match item.body {
|
match item.body {
|
||||||
Body::Enum(ref variants) => {
|
Body::Enum(ref variants) => {
|
||||||
serialize_item_enum(
|
serialize_item_enum(&item.ident, impl_generics, ty, variants, &item.attrs)
|
||||||
&item.ident,
|
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
variants,
|
|
||||||
&item.attrs)
|
|
||||||
}
|
}
|
||||||
Body::Struct(Style::Struct, ref fields) => {
|
Body::Struct(Style::Struct, ref fields) => {
|
||||||
if fields.iter().any(|field| field.ident.is_none()) {
|
if fields.iter().any(|field| field.ident.is_none()) {
|
||||||
panic!("struct has unnamed fields");
|
panic!("struct has unnamed fields");
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize_struct(
|
serialize_struct(impl_generics, ty, fields, &item.attrs)
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
fields,
|
|
||||||
&item.attrs)
|
|
||||||
}
|
}
|
||||||
Body::Struct(Style::Tuple, ref fields) => {
|
Body::Struct(Style::Tuple, ref fields) => {
|
||||||
if fields.iter().any(|field| field.ident.is_some()) {
|
if fields.iter().any(|field| field.ident.is_some()) {
|
||||||
panic!("tuple struct has named fields");
|
panic!("tuple struct has named fields");
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize_tuple_struct(
|
serialize_tuple_struct(impl_generics, ty, fields, &item.attrs)
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
fields,
|
|
||||||
&item.attrs)
|
|
||||||
}
|
}
|
||||||
Body::Struct(Style::Newtype, ref fields) => {
|
Body::Struct(Style::Newtype, ref fields) => {
|
||||||
serialize_newtype_struct(
|
serialize_newtype_struct(impl_generics, ty, &fields[0], &item.attrs)
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
&fields[0],
|
|
||||||
&item.attrs)
|
|
||||||
}
|
|
||||||
Body::Struct(Style::Unit, _) => {
|
|
||||||
serialize_unit_struct(
|
|
||||||
&item.attrs)
|
|
||||||
}
|
}
|
||||||
|
Body::Struct(Style::Unit, _) => serialize_unit_struct(&item.attrs),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,18 +102,16 @@ fn serialize_unit_struct(item_attrs: &attr::Item) -> Tokens {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct(
|
fn serialize_newtype_struct(impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
item_ty: syn::Ty,
|
||||||
item_ty: syn::Ty,
|
field: &Field,
|
||||||
field: &Field,
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let type_name = item_attrs.name().serialize_name();
|
let type_name = item_attrs.name().serialize_name();
|
||||||
|
|
||||||
let mut field_expr = quote!(&self.0);
|
let mut field_expr = quote!(&self.0);
|
||||||
if let Some(path) = field.attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(
|
field_expr = wrap_serialize_with(&item_ty, impl_generics, field.ty, path, field_expr);
|
||||||
&item_ty, impl_generics, field.ty, path, field_expr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
@@ -148,19 +119,17 @@ fn serialize_newtype_struct(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct(
|
fn serialize_tuple_struct(impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
fields: &[Field],
|
||||||
fields: &[Field],
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
let serialize_stmts =
|
||||||
let serialize_stmts = serialize_tuple_struct_visitor(
|
serialize_tuple_struct_visitor(ty.clone(),
|
||||||
ty.clone(),
|
fields,
|
||||||
fields,
|
impl_generics,
|
||||||
impl_generics,
|
false,
|
||||||
false,
|
quote!(_serde::ser::SerializeTupleStruct::serialize_field));
|
||||||
quote!(_serde::ser::SerializeTupleStruct::serialize_field),
|
|
||||||
);
|
|
||||||
|
|
||||||
let type_name = item_attrs.name().serialize_name();
|
let type_name = item_attrs.name().serialize_name();
|
||||||
let len = serialize_stmts.len();
|
let len = serialize_stmts.len();
|
||||||
@@ -173,19 +142,17 @@ fn serialize_tuple_struct(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct(
|
fn serialize_struct(impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
fields: &[Field],
|
||||||
fields: &[Field],
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
let serialize_fields =
|
||||||
let serialize_fields = serialize_struct_visitor(
|
serialize_struct_visitor(ty.clone(),
|
||||||
ty.clone(),
|
fields,
|
||||||
fields,
|
impl_generics,
|
||||||
impl_generics,
|
false,
|
||||||
false,
|
quote!(_serde::ser::SerializeStruct::serialize_field));
|
||||||
quote!(_serde::ser::SerializeStruct::serialize_field),
|
|
||||||
);
|
|
||||||
|
|
||||||
let type_name = item_attrs.name().serialize_name();
|
let type_name = item_attrs.name().serialize_name();
|
||||||
|
|
||||||
@@ -195,8 +162,7 @@ fn serialize_struct(
|
|||||||
|
|
||||||
let let_mut = mut_if(serialized_fields.peek().is_some());
|
let let_mut = mut_if(serialized_fields.peek().is_some());
|
||||||
|
|
||||||
let len = serialized_fields
|
let len = serialized_fields.map(|field| {
|
||||||
.map(|field| {
|
|
||||||
let ident = field.ident.clone().expect("struct has unnamed fields");
|
let ident = field.ident.clone().expect("struct has unnamed fields");
|
||||||
let field_expr = quote!(&self.#ident);
|
let field_expr = quote!(&self.#ident);
|
||||||
|
|
||||||
@@ -204,7 +170,7 @@ fn serialize_struct(
|
|||||||
Some(path) => quote!(if #path(#field_expr) { 0 } else { 1 }),
|
Some(path) => quote!(if #path(#field_expr) { 0 } else { 1 }),
|
||||||
None => quote!(1),
|
None => quote!(1),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
@@ -214,27 +180,23 @@ fn serialize_struct(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_item_enum(
|
fn serialize_item_enum(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
impl_generics: &syn::Generics,
|
||||||
impl_generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variants: &[Variant],
|
||||||
variants: &[Variant],
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
let arms: Vec<_> = variants.iter()
|
||||||
let arms: Vec<_> =
|
.enumerate()
|
||||||
variants.iter()
|
.map(|(variant_index, variant)| {
|
||||||
.enumerate()
|
serialize_variant(type_ident,
|
||||||
.map(|(variant_index, variant)| {
|
impl_generics,
|
||||||
serialize_variant(
|
ty.clone(),
|
||||||
type_ident,
|
variant,
|
||||||
impl_generics,
|
variant_index,
|
||||||
ty.clone(),
|
item_attrs)
|
||||||
variant,
|
})
|
||||||
variant_index,
|
.collect();
|
||||||
item_attrs,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
match *self {
|
match *self {
|
||||||
@@ -243,14 +205,13 @@ fn serialize_item_enum(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_variant(
|
fn serialize_variant(type_ident: &syn::Ident,
|
||||||
type_ident: &syn::Ident,
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variant: &Variant,
|
||||||
variant: &Variant,
|
variant_index: usize,
|
||||||
variant_index: usize,
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let variant_ident = variant.ident.clone();
|
let variant_ident = variant.ident.clone();
|
||||||
|
|
||||||
if variant.attrs.skip_serializing() {
|
if variant.attrs.skip_serializing() {
|
||||||
@@ -267,7 +228,8 @@ fn serialize_variant(
|
|||||||
quote! {
|
quote! {
|
||||||
#type_ident::#variant_ident #fields_pat => #skipped_err,
|
#type_ident::#variant_ident #fields_pat => #skipped_err,
|
||||||
}
|
}
|
||||||
} else { // variant wasn't skipped
|
} else {
|
||||||
|
// variant wasn't skipped
|
||||||
let case = match variant.style {
|
let case = match variant.style {
|
||||||
Style::Unit => {
|
Style::Unit => {
|
||||||
quote! {
|
quote! {
|
||||||
@@ -280,14 +242,15 @@ fn serialize_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Style::Tuple => {
|
Style::Tuple => {
|
||||||
let field_names = (0 .. variant.fields.len())
|
let field_names = (0..variant.fields.len())
|
||||||
.map(|i| Ident::new(format!("__field{}", i)));
|
.map(|i| Ident::new(format!("__field{}", i)));
|
||||||
quote! {
|
quote! {
|
||||||
#type_ident::#variant_ident(#(ref #field_names),*)
|
#type_ident::#variant_ident(#(ref #field_names),*)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Style::Struct => {
|
Style::Struct => {
|
||||||
let fields = variant.fields.iter()
|
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! {
|
quote! {
|
||||||
#type_ident::#variant_ident { #(ref #fields),* }
|
#type_ident::#variant_ident { #(ref #fields),* }
|
||||||
@@ -297,33 +260,22 @@ fn serialize_variant(
|
|||||||
|
|
||||||
let body = match *item_attrs.tag() {
|
let body = match *item_attrs.tag() {
|
||||||
attr::EnumTag::External => {
|
attr::EnumTag::External => {
|
||||||
serialize_externally_tagged_variant(
|
serialize_externally_tagged_variant(generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
variant,
|
||||||
variant,
|
variant_index,
|
||||||
variant_index,
|
item_attrs)
|
||||||
item_attrs,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
attr::EnumTag::Internal(ref tag) => {
|
attr::EnumTag::Internal(ref tag) => {
|
||||||
serialize_internally_tagged_variant(
|
serialize_internally_tagged_variant(type_ident.as_ref(),
|
||||||
type_ident.as_ref(),
|
variant_ident.as_ref(),
|
||||||
variant_ident.as_ref(),
|
generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
variant,
|
||||||
variant,
|
item_attrs,
|
||||||
item_attrs,
|
tag)
|
||||||
tag,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
attr::EnumTag::None => {
|
|
||||||
serialize_untagged_variant(
|
|
||||||
generics,
|
|
||||||
ty,
|
|
||||||
variant,
|
|
||||||
item_attrs,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
attr::EnumTag::None => serialize_untagged_variant(generics, ty, variant, item_attrs),
|
||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
@@ -332,13 +284,12 @@ fn serialize_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_externally_tagged_variant(
|
fn serialize_externally_tagged_variant(generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variant: &Variant,
|
||||||
variant: &Variant,
|
variant_index: usize,
|
||||||
variant_index: usize,
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let type_name = item_attrs.name().serialize_name();
|
let type_name = item_attrs.name().serialize_name();
|
||||||
let variant_name = variant.attrs.name().serialize_name();
|
let variant_name = variant.attrs.name().serialize_name();
|
||||||
|
|
||||||
@@ -357,8 +308,7 @@ fn serialize_externally_tagged_variant(
|
|||||||
let field = &variant.fields[0];
|
let field = &variant.fields[0];
|
||||||
let mut field_expr = quote!(__simple_value);
|
let mut field_expr = quote!(__simple_value);
|
||||||
if let Some(path) = field.attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(
|
field_expr = wrap_serialize_with(&ty, generics, field.ty, path, field_expr);
|
||||||
&ty, generics, field.ty, path, field_expr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
@@ -372,32 +322,28 @@ fn serialize_externally_tagged_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Style::Tuple => {
|
Style::Tuple => {
|
||||||
let block = serialize_tuple_variant(
|
let block = serialize_tuple_variant(TupleVariant::ExternallyTagged {
|
||||||
TupleVariant::ExternallyTagged {
|
type_name: type_name,
|
||||||
type_name: type_name,
|
variant_index: variant_index,
|
||||||
variant_index: variant_index,
|
variant_name: variant_name,
|
||||||
variant_name: variant_name,
|
},
|
||||||
},
|
generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
&variant.fields);
|
||||||
&variant.fields,
|
|
||||||
);
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
{ #block }
|
{ #block }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Style::Struct => {
|
Style::Struct => {
|
||||||
let block = serialize_struct_variant(
|
let block = serialize_struct_variant(StructVariant::ExternallyTagged {
|
||||||
StructVariant::ExternallyTagged {
|
variant_index: variant_index,
|
||||||
variant_index: variant_index,
|
variant_name: variant_name,
|
||||||
variant_name: variant_name,
|
},
|
||||||
},
|
generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
&variant.fields,
|
||||||
&variant.fields,
|
item_attrs);
|
||||||
item_attrs,
|
|
||||||
);
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
{ #block }
|
{ #block }
|
||||||
@@ -406,15 +352,14 @@ fn serialize_externally_tagged_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_internally_tagged_variant(
|
fn serialize_internally_tagged_variant(type_ident: &str,
|
||||||
type_ident: &str,
|
variant_ident: &str,
|
||||||
variant_ident: &str,
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variant: &Variant,
|
||||||
variant: &Variant,
|
item_attrs: &attr::Item,
|
||||||
item_attrs: &attr::Item,
|
tag: &str)
|
||||||
tag: &str,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let type_name = item_attrs.name().serialize_name();
|
let type_name = item_attrs.name().serialize_name();
|
||||||
let variant_name = variant.attrs.name().serialize_name();
|
let variant_name = variant.attrs.name().serialize_name();
|
||||||
|
|
||||||
@@ -432,8 +377,7 @@ fn serialize_internally_tagged_variant(
|
|||||||
let field = &variant.fields[0];
|
let field = &variant.fields[0];
|
||||||
let mut field_expr = quote!(__simple_value);
|
let mut field_expr = quote!(__simple_value);
|
||||||
if let Some(path) = field.attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(
|
field_expr = wrap_serialize_with(&ty, generics, field.ty, path, field_expr);
|
||||||
&ty, generics, field.ty, path, field_expr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
@@ -448,16 +392,14 @@ fn serialize_internally_tagged_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Style::Struct => {
|
Style::Struct => {
|
||||||
let block = serialize_struct_variant(
|
let block = serialize_struct_variant(StructVariant::InternallyTagged {
|
||||||
StructVariant::InternallyTagged {
|
tag: tag,
|
||||||
tag: tag,
|
variant_name: variant_name,
|
||||||
variant_name: variant_name,
|
},
|
||||||
},
|
generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
&variant.fields,
|
||||||
&variant.fields,
|
item_attrs);
|
||||||
item_attrs,
|
|
||||||
);
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
{ #block }
|
{ #block }
|
||||||
@@ -467,12 +409,11 @@ fn serialize_internally_tagged_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_untagged_variant(
|
fn serialize_untagged_variant(generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
variant: &Variant,
|
||||||
variant: &Variant,
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
match variant.style {
|
match variant.style {
|
||||||
Style::Unit => {
|
Style::Unit => {
|
||||||
quote! {
|
quote! {
|
||||||
@@ -483,8 +424,7 @@ fn serialize_untagged_variant(
|
|||||||
let field = &variant.fields[0];
|
let field = &variant.fields[0];
|
||||||
let mut field_expr = quote!(__simple_value);
|
let mut field_expr = quote!(__simple_value);
|
||||||
if let Some(path) = field.attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(
|
field_expr = wrap_serialize_with(&ty, generics, field.ty, path, field_expr);
|
||||||
&ty, generics, field.ty, path, field_expr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
@@ -492,25 +432,19 @@ fn serialize_untagged_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Style::Tuple => {
|
Style::Tuple => {
|
||||||
let block = serialize_tuple_variant(
|
let block =
|
||||||
TupleVariant::Untagged,
|
serialize_tuple_variant(TupleVariant::Untagged, generics, ty, &variant.fields);
|
||||||
generics,
|
|
||||||
ty,
|
|
||||||
&variant.fields,
|
|
||||||
);
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
{ #block }
|
{ #block }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Style::Struct => {
|
Style::Struct => {
|
||||||
let block = serialize_struct_variant(
|
let block = serialize_struct_variant(StructVariant::Untagged,
|
||||||
StructVariant::Untagged,
|
generics,
|
||||||
generics,
|
ty,
|
||||||
ty,
|
&variant.fields,
|
||||||
&variant.fields,
|
item_attrs);
|
||||||
item_attrs,
|
|
||||||
);
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
{ #block }
|
{ #block }
|
||||||
@@ -528,28 +462,20 @@ enum TupleVariant {
|
|||||||
Untagged,
|
Untagged,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_variant(
|
fn serialize_tuple_variant(context: TupleVariant,
|
||||||
context: TupleVariant,
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
structure_ty: syn::Ty,
|
||||||
structure_ty: syn::Ty,
|
fields: &[Field])
|
||||||
fields: &[Field],
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let method = match context {
|
let method = match context {
|
||||||
TupleVariant::ExternallyTagged{..} => {
|
TupleVariant::ExternallyTagged { .. } => {
|
||||||
quote!(_serde::ser::SerializeTupleVariant::serialize_field)
|
quote!(_serde::ser::SerializeTupleVariant::serialize_field)
|
||||||
}
|
}
|
||||||
TupleVariant::Untagged => {
|
TupleVariant::Untagged => quote!(_serde::ser::SerializeTuple::serialize_element),
|
||||||
quote!(_serde::ser::SerializeTuple::serialize_element)
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let serialize_stmts = serialize_tuple_struct_visitor(
|
let serialize_stmts =
|
||||||
structure_ty,
|
serialize_tuple_struct_visitor(structure_ty, fields, generics, true, method);
|
||||||
fields,
|
|
||||||
generics,
|
|
||||||
true,
|
|
||||||
method,
|
|
||||||
);
|
|
||||||
|
|
||||||
let len = serialize_stmts.len();
|
let len = serialize_stmts.len();
|
||||||
let let_mut = mut_if(len > 0);
|
let let_mut = mut_if(len > 0);
|
||||||
@@ -584,36 +510,25 @@ enum StructVariant<'a> {
|
|||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
variant_name: String,
|
variant_name: String,
|
||||||
},
|
},
|
||||||
InternallyTagged {
|
InternallyTagged { tag: &'a str, variant_name: String },
|
||||||
tag: &'a str,
|
|
||||||
variant_name: String,
|
|
||||||
},
|
|
||||||
Untagged,
|
Untagged,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_variant<'a>(
|
fn serialize_struct_variant<'a>(context: StructVariant<'a>,
|
||||||
context: StructVariant<'a>,
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
ty: syn::Ty,
|
||||||
ty: syn::Ty,
|
fields: &[Field],
|
||||||
fields: &[Field],
|
item_attrs: &attr::Item)
|
||||||
item_attrs: &attr::Item,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let method = match context {
|
let method = match context {
|
||||||
StructVariant::ExternallyTagged{..} => {
|
StructVariant::ExternallyTagged { .. } => {
|
||||||
quote!(_serde::ser::SerializeStructVariant::serialize_field)
|
quote!(_serde::ser::SerializeStructVariant::serialize_field)
|
||||||
}
|
}
|
||||||
StructVariant::InternallyTagged{..} | StructVariant::Untagged => {
|
StructVariant::InternallyTagged { .. } |
|
||||||
quote!(_serde::ser::SerializeStruct::serialize_field)
|
StructVariant::Untagged => quote!(_serde::ser::SerializeStruct::serialize_field),
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let serialize_fields = serialize_struct_visitor(
|
let serialize_fields = serialize_struct_visitor(ty.clone(), fields, generics, true, method);
|
||||||
ty.clone(),
|
|
||||||
fields,
|
|
||||||
generics,
|
|
||||||
true,
|
|
||||||
method,
|
|
||||||
);
|
|
||||||
|
|
||||||
let item_name = item_attrs.name().serialize_name();
|
let item_name = item_attrs.name().serialize_name();
|
||||||
|
|
||||||
@@ -623,15 +538,14 @@ fn serialize_struct_variant<'a>(
|
|||||||
|
|
||||||
let let_mut = mut_if(serialized_fields.peek().is_some());
|
let let_mut = mut_if(serialized_fields.peek().is_some());
|
||||||
|
|
||||||
let len = serialized_fields
|
let len = serialized_fields.map(|field| {
|
||||||
.map(|field| {
|
|
||||||
let ident = field.ident.clone().expect("struct has unnamed fields");
|
let ident = field.ident.clone().expect("struct has unnamed fields");
|
||||||
|
|
||||||
match field.attrs.skip_serializing_if() {
|
match field.attrs.skip_serializing_if() {
|
||||||
Some(path) => quote!(if #path(#ident) { 0 } else { 1 }),
|
Some(path) => quote!(if #path(#ident) { 0 } else { 1 }),
|
||||||
None => quote!(1),
|
None => quote!(1),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
||||||
|
|
||||||
match context {
|
match context {
|
||||||
@@ -678,13 +592,12 @@ fn serialize_struct_variant<'a>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct_visitor(
|
fn serialize_tuple_struct_visitor(structure_ty: syn::Ty,
|
||||||
structure_ty: syn::Ty,
|
fields: &[Field],
|
||||||
fields: &[Field],
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
is_enum: bool,
|
||||||
is_enum: bool,
|
func: Tokens)
|
||||||
func: Tokens,
|
-> Vec<Tokens> {
|
||||||
) -> Vec<Tokens> {
|
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, field)| {
|
.map(|(i, field)| {
|
||||||
@@ -696,12 +609,13 @@ fn serialize_tuple_struct_visitor(
|
|||||||
quote!(&self.#i)
|
quote!(&self.#i)
|
||||||
};
|
};
|
||||||
|
|
||||||
let skip = field.attrs.skip_serializing_if()
|
let skip = field.attrs
|
||||||
|
.skip_serializing_if()
|
||||||
.map(|path| quote!(#path(#field_expr)));
|
.map(|path| quote!(#path(#field_expr)));
|
||||||
|
|
||||||
if let Some(path) = field.attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(
|
field_expr =
|
||||||
&structure_ty, generics, field.ty, path, field_expr);
|
wrap_serialize_with(&structure_ty, generics, field.ty, path, field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
let ser = quote! {
|
let ser = quote! {
|
||||||
@@ -716,13 +630,12 @@ fn serialize_tuple_struct_visitor(
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_visitor(
|
fn serialize_struct_visitor(structure_ty: syn::Ty,
|
||||||
structure_ty: syn::Ty,
|
fields: &[Field],
|
||||||
fields: &[Field],
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
is_enum: bool,
|
||||||
is_enum: bool,
|
func: Tokens)
|
||||||
func: Tokens,
|
-> Vec<Tokens> {
|
||||||
) -> Vec<Tokens> {
|
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.filter(|&field| !field.attrs.skip_serializing())
|
.filter(|&field| !field.attrs.skip_serializing())
|
||||||
.map(|field| {
|
.map(|field| {
|
||||||
@@ -735,12 +648,13 @@ fn serialize_struct_visitor(
|
|||||||
|
|
||||||
let key_expr = field.attrs.name().serialize_name();
|
let key_expr = field.attrs.name().serialize_name();
|
||||||
|
|
||||||
let skip = field.attrs.skip_serializing_if()
|
let skip = field.attrs
|
||||||
|
.skip_serializing_if()
|
||||||
.map(|path| quote!(#path(#field_expr)));
|
.map(|path| quote!(#path(#field_expr)));
|
||||||
|
|
||||||
if let Some(path) = field.attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(
|
field_expr =
|
||||||
&structure_ty, generics, field.ty, path, field_expr)
|
wrap_serialize_with(&structure_ty, generics, field.ty, path, field_expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
let ser = quote! {
|
let ser = quote! {
|
||||||
@@ -755,13 +669,12 @@ fn serialize_struct_visitor(
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wrap_serialize_with(
|
fn wrap_serialize_with(item_ty: &syn::Ty,
|
||||||
item_ty: &syn::Ty,
|
generics: &syn::Generics,
|
||||||
generics: &syn::Generics,
|
field_ty: &syn::Ty,
|
||||||
field_ty: &syn::Ty,
|
path: &syn::Path,
|
||||||
path: &syn::Path,
|
value: Tokens)
|
||||||
value: Tokens,
|
-> Tokens {
|
||||||
) -> Tokens {
|
|
||||||
let where_clause = &generics.where_clause;
|
let where_clause = &generics.where_clause;
|
||||||
|
|
||||||
let wrapper_generics = aster::from_generics(generics.clone())
|
let wrapper_generics = aster::from_generics(generics.clone())
|
||||||
@@ -771,8 +684,8 @@ fn wrap_serialize_with(
|
|||||||
|
|
||||||
let wrapper_ty = aster::path()
|
let wrapper_ty = aster::path()
|
||||||
.segment("__SerializeWith")
|
.segment("__SerializeWith")
|
||||||
.with_generics(wrapper_generics.clone())
|
.with_generics(wrapper_generics.clone())
|
||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
quote!({
|
quote!({
|
||||||
@@ -803,9 +716,5 @@ fn wrap_serialize_with(
|
|||||||
//
|
//
|
||||||
// where we want to omit the `mut` to avoid a warning.
|
// where we want to omit the `mut` to avoid a warning.
|
||||||
fn mut_if(is_mut: bool) -> Option<Tokens> {
|
fn mut_if(is_mut: bool) -> Option<Tokens> {
|
||||||
if is_mut {
|
if is_mut { Some(quote!(mut)) } else { None }
|
||||||
Some(quote!(mut))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,14 @@ use token::Token;
|
|||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
pub fn assert_tokens<T>(value: &T, tokens: &[Token<'static>])
|
pub fn assert_tokens<T>(value: &T, tokens: &[Token<'static>])
|
||||||
where T: Serialize + Deserialize + PartialEq + Debug,
|
where T: Serialize + Deserialize + PartialEq + Debug
|
||||||
{
|
{
|
||||||
assert_ser_tokens(value, tokens);
|
assert_ser_tokens(value, tokens);
|
||||||
assert_de_tokens(value, tokens);
|
assert_de_tokens(value, tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
||||||
where T: Serialize,
|
where T: Serialize
|
||||||
{
|
{
|
||||||
let mut ser = Serializer::new(tokens.iter());
|
let mut ser = Serializer::new(tokens.iter());
|
||||||
assert_eq!(Serialize::serialize(value, &mut ser), Ok(()));
|
assert_eq!(Serialize::serialize(value, &mut ser), Ok(()));
|
||||||
@@ -24,7 +24,7 @@ pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
|||||||
|
|
||||||
/// Expect an error serializing `T`.
|
/// Expect an error serializing `T`.
|
||||||
pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: 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.iter());
|
let mut ser = Serializer::new(tokens.iter());
|
||||||
let v: Result<(), Error> = Serialize::serialize(value, &mut ser);
|
let v: Result<(), Error> = Serialize::serialize(value, &mut ser);
|
||||||
@@ -33,7 +33,7 @@ pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: Error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn assert_de_tokens<T>(value: &T, tokens: &[Token<'static>])
|
pub fn assert_de_tokens<T>(value: &T, tokens: &[Token<'static>])
|
||||||
where T: Deserialize + PartialEq + Debug,
|
where T: Deserialize + PartialEq + Debug
|
||||||
{
|
{
|
||||||
let mut de = Deserializer::new(tokens.to_vec().into_iter());
|
let mut de = Deserializer::new(tokens.to_vec().into_iter());
|
||||||
let v: Result<T, Error> = Deserialize::deserialize(&mut de);
|
let v: Result<T, Error> = Deserialize::deserialize(&mut de);
|
||||||
@@ -43,7 +43,7 @@ pub fn assert_de_tokens<T>(value: &T, tokens: &[Token<'static>])
|
|||||||
|
|
||||||
/// Expect an error deserializing tokens into a `T`.
|
/// Expect an error deserializing tokens into a `T`.
|
||||||
pub fn assert_de_tokens_error<T>(tokens: &[Token<'static>], error: Error)
|
pub fn assert_de_tokens_error<T>(tokens: &[Token<'static>], error: Error)
|
||||||
where T: Deserialize + PartialEq + Debug,
|
where T: Deserialize + PartialEq + Debug
|
||||||
{
|
{
|
||||||
let mut de = Deserializer::new(tokens.to_vec().into_iter());
|
let mut de = Deserializer::new(tokens.to_vec().into_iter());
|
||||||
let v: Result<T, Error> = Deserialize::deserialize(&mut de);
|
let v: Result<T, Error> = Deserialize::deserialize(&mut de);
|
||||||
|
|||||||
+100
-96
@@ -1,33 +1,23 @@
|
|||||||
use std::iter;
|
use std::iter;
|
||||||
|
|
||||||
use serde::de::{
|
use serde::de::{self, Deserialize, DeserializeSeed, EnumVisitor, MapVisitor, SeqVisitor,
|
||||||
self,
|
VariantVisitor, Visitor};
|
||||||
Deserialize,
|
|
||||||
DeserializeSeed,
|
|
||||||
EnumVisitor,
|
|
||||||
MapVisitor,
|
|
||||||
SeqVisitor,
|
|
||||||
VariantVisitor,
|
|
||||||
Visitor,
|
|
||||||
};
|
|
||||||
use serde::de::value::ValueDeserializer;
|
use serde::de::value::ValueDeserializer;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use token::Token;
|
use token::Token;
|
||||||
|
|
||||||
pub struct Deserializer<I>
|
pub struct Deserializer<I>
|
||||||
where I: Iterator<Item=Token<'static>>,
|
where I: Iterator<Item = Token<'static>>
|
||||||
{
|
{
|
||||||
tokens: iter::Peekable<I>,
|
tokens: iter::Peekable<I>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I> Deserializer<I>
|
impl<I> Deserializer<I>
|
||||||
where I: Iterator<Item=Token<'static>>,
|
where I: Iterator<Item = Token<'static>>
|
||||||
{
|
{
|
||||||
pub fn new(tokens: I) -> Deserializer<I> {
|
pub fn new(tokens: I) -> Deserializer<I> {
|
||||||
Deserializer {
|
Deserializer { tokens: tokens.peekable() }
|
||||||
tokens: tokens.peekable(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_token(&mut self) -> Option<Token<'static>> {
|
pub fn next_token(&mut self) -> Option<Token<'static>> {
|
||||||
@@ -47,8 +37,13 @@ impl<I> Deserializer<I>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_seq<V>(&mut self, len: Option<usize>, sep: Token<'static>, end: Token<'static>, visitor: V) -> Result<V::Value, Error>
|
fn visit_seq<V>(&mut self,
|
||||||
where V: Visitor,
|
len: Option<usize>,
|
||||||
|
sep: Token<'static>,
|
||||||
|
end: Token<'static>,
|
||||||
|
visitor: V)
|
||||||
|
-> Result<V::Value, Error>
|
||||||
|
where V: Visitor
|
||||||
{
|
{
|
||||||
let value = try!(visitor.visit_seq(DeserializerSeqVisitor {
|
let value = try!(visitor.visit_seq(DeserializerSeqVisitor {
|
||||||
de: self,
|
de: self,
|
||||||
@@ -60,8 +55,13 @@ impl<I> Deserializer<I>
|
|||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_map<V>(&mut self, len: Option<usize>, sep: Token<'static>, end: Token<'static>, visitor: V) -> Result<V::Value, Error>
|
fn visit_map<V>(&mut self,
|
||||||
where V: Visitor,
|
len: Option<usize>,
|
||||||
|
sep: Token<'static>,
|
||||||
|
end: Token<'static>,
|
||||||
|
visitor: V)
|
||||||
|
-> Result<V::Value, Error>
|
||||||
|
where V: Visitor
|
||||||
{
|
{
|
||||||
let value = try!(visitor.visit_map(DeserializerMapVisitor {
|
let value = try!(visitor.visit_map(DeserializerMapVisitor {
|
||||||
de: self,
|
de: self,
|
||||||
@@ -75,7 +75,7 @@ impl<I> Deserializer<I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
||||||
where I: Iterator<Item=Token<'static>>,
|
where I: Iterator<Item = Token<'static>>
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
|
||||||
where V: Visitor,
|
where V: Visitor
|
||||||
{
|
{
|
||||||
match self.tokens.next() {
|
match self.tokens.next() {
|
||||||
Some(Token::Bool(v)) => visitor.visit_bool(v),
|
Some(Token::Bool(v)) => visitor.visit_bool(v),
|
||||||
@@ -118,7 +118,10 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
self.visit_seq(Some(len), Token::TupleSep, Token::TupleEnd, visitor)
|
self.visit_seq(Some(len), Token::TupleSep, Token::TupleEnd, visitor)
|
||||||
}
|
}
|
||||||
Some(Token::TupleStructStart(_, len)) => {
|
Some(Token::TupleStructStart(_, len)) => {
|
||||||
self.visit_seq(Some(len), Token::TupleStructSep, Token::TupleStructEnd, visitor)
|
self.visit_seq(Some(len),
|
||||||
|
Token::TupleStructSep,
|
||||||
|
Token::TupleStructEnd,
|
||||||
|
visitor)
|
||||||
}
|
}
|
||||||
Some(Token::MapStart(len)) => {
|
Some(Token::MapStart(len)) => {
|
||||||
self.visit_map(len, Token::MapSep, Token::MapEnd, visitor)
|
self.visit_map(len, Token::MapSep, Token::MapEnd, visitor)
|
||||||
@@ -134,10 +137,11 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
/// Hook into `Option` deserializing so we can treat `Unit` as a
|
/// Hook into `Option` deserializing so we can treat `Unit` as a
|
||||||
/// `None`, or a regular value as `Some(value)`.
|
/// `None`, or a regular value as `Some(value)`.
|
||||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
|
||||||
where V: Visitor,
|
where V: Visitor
|
||||||
{
|
{
|
||||||
match self.tokens.peek() {
|
match self.tokens.peek() {
|
||||||
Some(&Token::Unit) | Some(&Token::Option(false)) => {
|
Some(&Token::Unit) |
|
||||||
|
Some(&Token::Option(false)) => {
|
||||||
self.tokens.next();
|
self.tokens.next();
|
||||||
visitor.visit_none()
|
visitor.visit_none()
|
||||||
}
|
}
|
||||||
@@ -151,26 +155,23 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_enum<V>(self,
|
fn deserialize_enum<V>(self,
|
||||||
name: &str,
|
name: &str,
|
||||||
_variants: &'static [&'static str],
|
_variants: &'static [&'static str],
|
||||||
visitor: V) -> Result<V::Value, Error>
|
visitor: V)
|
||||||
where V: Visitor,
|
-> Result<V::Value, Error>
|
||||||
|
where V: Visitor
|
||||||
{
|
{
|
||||||
match self.tokens.peek() {
|
match self.tokens.peek() {
|
||||||
Some(&Token::EnumStart(n)) if name == n => {
|
Some(&Token::EnumStart(n)) if name == n => {
|
||||||
self.tokens.next();
|
self.tokens.next();
|
||||||
|
|
||||||
visitor.visit_enum(DeserializerEnumVisitor {
|
visitor.visit_enum(DeserializerEnumVisitor { de: self })
|
||||||
de: self,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
Some(&Token::EnumUnit(n, _))
|
Some(&Token::EnumUnit(n, _)) |
|
||||||
| Some(&Token::EnumNewType(n, _))
|
Some(&Token::EnumNewType(n, _)) |
|
||||||
| Some(&Token::EnumSeqStart(n, _, _))
|
Some(&Token::EnumSeqStart(n, _, _)) |
|
||||||
| Some(&Token::EnumMapStart(n, _, _)) if name == n => {
|
Some(&Token::EnumMapStart(n, _, _)) if name == n => {
|
||||||
visitor.visit_enum(DeserializerEnumVisitor {
|
visitor.visit_enum(DeserializerEnumVisitor { de: self })
|
||||||
de: self,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
Some(_) => {
|
Some(_) => {
|
||||||
let token = self.tokens.next().unwrap();
|
let token = self.tokens.next().unwrap();
|
||||||
@@ -181,7 +182,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_unit_struct<V>(self, name: &str, visitor: V) -> Result<V::Value, Error>
|
fn deserialize_unit_struct<V>(self, name: &str, visitor: V) -> Result<V::Value, Error>
|
||||||
where V: Visitor,
|
where V: Visitor
|
||||||
{
|
{
|
||||||
match self.tokens.peek() {
|
match self.tokens.peek() {
|
||||||
Some(&Token::UnitStruct(n)) => {
|
Some(&Token::UnitStruct(n)) => {
|
||||||
@@ -197,10 +198,8 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_newtype_struct<V>(self,
|
fn deserialize_newtype_struct<V>(self, name: &str, visitor: V) -> Result<V::Value, Error>
|
||||||
name: &str,
|
where V: Visitor
|
||||||
visitor: V) -> Result<V::Value, Error>
|
|
||||||
where V: Visitor,
|
|
||||||
{
|
{
|
||||||
match self.tokens.peek() {
|
match self.tokens.peek() {
|
||||||
Some(&Token::StructNewType(n)) => {
|
Some(&Token::StructNewType(n)) => {
|
||||||
@@ -216,10 +215,8 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_seq_fixed_size<V>(self,
|
fn deserialize_seq_fixed_size<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
|
||||||
len: usize,
|
where V: Visitor
|
||||||
visitor: V) -> Result<V::Value, Error>
|
|
||||||
where V: Visitor,
|
|
||||||
{
|
{
|
||||||
match self.tokens.peek() {
|
match self.tokens.peek() {
|
||||||
Some(&Token::SeqArrayStart(_)) => {
|
Some(&Token::SeqArrayStart(_)) => {
|
||||||
@@ -231,13 +228,12 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_tuple<V>(self,
|
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
|
||||||
len: usize,
|
where V: Visitor
|
||||||
visitor: V) -> Result<V::Value, Error>
|
|
||||||
where V: Visitor,
|
|
||||||
{
|
{
|
||||||
match self.tokens.peek() {
|
match self.tokens.peek() {
|
||||||
Some(&Token::Unit) | Some(&Token::UnitStruct(_)) => {
|
Some(&Token::Unit) |
|
||||||
|
Some(&Token::UnitStruct(_)) => {
|
||||||
self.tokens.next();
|
self.tokens.next();
|
||||||
visitor.visit_unit()
|
visitor.visit_unit()
|
||||||
}
|
}
|
||||||
@@ -255,7 +251,10 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
}
|
}
|
||||||
Some(&Token::TupleStructStart(_, _)) => {
|
Some(&Token::TupleStructStart(_, _)) => {
|
||||||
self.tokens.next();
|
self.tokens.next();
|
||||||
self.visit_seq(Some(len), Token::TupleStructSep, Token::TupleStructEnd, visitor)
|
self.visit_seq(Some(len),
|
||||||
|
Token::TupleStructSep,
|
||||||
|
Token::TupleStructEnd,
|
||||||
|
visitor)
|
||||||
}
|
}
|
||||||
Some(_) => self.deserialize(visitor),
|
Some(_) => self.deserialize(visitor),
|
||||||
None => Err(Error::EndOfTokens),
|
None => Err(Error::EndOfTokens),
|
||||||
@@ -265,8 +264,9 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
fn deserialize_tuple_struct<V>(self,
|
fn deserialize_tuple_struct<V>(self,
|
||||||
name: &str,
|
name: &str,
|
||||||
len: usize,
|
len: usize,
|
||||||
visitor: V) -> Result<V::Value, Error>
|
visitor: V)
|
||||||
where V: Visitor,
|
-> Result<V::Value, Error>
|
||||||
|
where V: Visitor
|
||||||
{
|
{
|
||||||
match self.tokens.peek() {
|
match self.tokens.peek() {
|
||||||
Some(&Token::Unit) => {
|
Some(&Token::Unit) => {
|
||||||
@@ -296,7 +296,10 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
Some(&Token::TupleStructStart(n, _)) => {
|
Some(&Token::TupleStructStart(n, _)) => {
|
||||||
self.tokens.next();
|
self.tokens.next();
|
||||||
if name == n {
|
if name == n {
|
||||||
self.visit_seq(Some(len), Token::TupleStructSep, Token::TupleStructEnd, visitor)
|
self.visit_seq(Some(len),
|
||||||
|
Token::TupleStructSep,
|
||||||
|
Token::TupleStructEnd,
|
||||||
|
visitor)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::InvalidName(n))
|
Err(Error::InvalidName(n))
|
||||||
}
|
}
|
||||||
@@ -309,14 +312,18 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
fn deserialize_struct<V>(self,
|
fn deserialize_struct<V>(self,
|
||||||
name: &str,
|
name: &str,
|
||||||
fields: &'static [&'static str],
|
fields: &'static [&'static str],
|
||||||
visitor: V) -> Result<V::Value, Error>
|
visitor: V)
|
||||||
where V: Visitor,
|
-> Result<V::Value, Error>
|
||||||
|
where V: Visitor
|
||||||
{
|
{
|
||||||
match self.tokens.peek() {
|
match self.tokens.peek() {
|
||||||
Some(&Token::StructStart(n, _)) => {
|
Some(&Token::StructStart(n, _)) => {
|
||||||
self.tokens.next();
|
self.tokens.next();
|
||||||
if name == n {
|
if name == n {
|
||||||
self.visit_map(Some(fields.len()), Token::StructSep, Token::StructEnd, visitor)
|
self.visit_map(Some(fields.len()),
|
||||||
|
Token::StructSep,
|
||||||
|
Token::StructEnd,
|
||||||
|
visitor)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::InvalidName(n))
|
Err(Error::InvalidName(n))
|
||||||
}
|
}
|
||||||
@@ -333,7 +340,9 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
|
|||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
struct DeserializerSeqVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>> {
|
struct DeserializerSeqVisitor<'a, I: 'a>
|
||||||
|
where I: Iterator<Item = Token<'static>>
|
||||||
|
{
|
||||||
de: &'a mut Deserializer<I>,
|
de: &'a mut Deserializer<I>,
|
||||||
len: Option<usize>,
|
len: Option<usize>,
|
||||||
sep: Token<'static>,
|
sep: Token<'static>,
|
||||||
@@ -341,12 +350,12 @@ struct DeserializerSeqVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, I> SeqVisitor for DeserializerSeqVisitor<'a, I>
|
impl<'a, I> SeqVisitor for DeserializerSeqVisitor<'a, I>
|
||||||
where I: Iterator<Item=Token<'static>>,
|
where I: Iterator<Item = Token<'static>>
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
|
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
|
||||||
where T: DeserializeSeed,
|
where T: DeserializeSeed
|
||||||
{
|
{
|
||||||
if self.de.tokens.peek() == Some(&self.end) {
|
if self.de.tokens.peek() == Some(&self.end) {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
@@ -369,7 +378,9 @@ impl<'a, I> SeqVisitor for DeserializerSeqVisitor<'a, I>
|
|||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
struct DeserializerMapVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>> {
|
struct DeserializerMapVisitor<'a, I: 'a>
|
||||||
|
where I: Iterator<Item = Token<'static>>
|
||||||
|
{
|
||||||
de: &'a mut Deserializer<I>,
|
de: &'a mut Deserializer<I>,
|
||||||
len: Option<usize>,
|
len: Option<usize>,
|
||||||
sep: Token<'static>,
|
sep: Token<'static>,
|
||||||
@@ -377,12 +388,12 @@ struct DeserializerMapVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I>
|
impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I>
|
||||||
where I: Iterator<Item=Token<'static>>,
|
where I: Iterator<Item = Token<'static>>
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
||||||
where K: DeserializeSeed,
|
where K: DeserializeSeed
|
||||||
{
|
{
|
||||||
if self.de.tokens.peek() == Some(&self.end) {
|
if self.de.tokens.peek() == Some(&self.end) {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
@@ -398,7 +409,7 @@ impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
|
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
|
||||||
where V: DeserializeSeed,
|
where V: DeserializeSeed
|
||||||
{
|
{
|
||||||
seed.deserialize(&mut *self.de)
|
seed.deserialize(&mut *self.de)
|
||||||
}
|
}
|
||||||
@@ -411,24 +422,26 @@ impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I>
|
|||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
struct DeserializerEnumVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>> {
|
struct DeserializerEnumVisitor<'a, I: 'a>
|
||||||
|
where I: Iterator<Item = Token<'static>>
|
||||||
|
{
|
||||||
de: &'a mut Deserializer<I>,
|
de: &'a mut Deserializer<I>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, I> EnumVisitor for DeserializerEnumVisitor<'a, I>
|
impl<'a, I> EnumVisitor for DeserializerEnumVisitor<'a, I>
|
||||||
where I: Iterator<Item=Token<'static>>,
|
where I: Iterator<Item = Token<'static>>
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Variant = Self;
|
type Variant = Self;
|
||||||
|
|
||||||
fn visit_variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Error>
|
fn visit_variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Error>
|
||||||
where V: DeserializeSeed,
|
where V: DeserializeSeed
|
||||||
{
|
{
|
||||||
match self.de.tokens.peek() {
|
match self.de.tokens.peek() {
|
||||||
Some(&Token::EnumUnit(_, v))
|
Some(&Token::EnumUnit(_, v)) |
|
||||||
| Some(&Token::EnumNewType(_, v))
|
Some(&Token::EnumNewType(_, v)) |
|
||||||
| Some(&Token::EnumSeqStart(_, v, _))
|
Some(&Token::EnumSeqStart(_, v, _)) |
|
||||||
| Some(&Token::EnumMapStart(_, v, _)) => {
|
Some(&Token::EnumMapStart(_, v, _)) => {
|
||||||
let de = v.into_deserializer();
|
let de = v.into_deserializer();
|
||||||
let value = try!(seed.deserialize(de));
|
let value = try!(seed.deserialize(de));
|
||||||
Ok((value, self))
|
Ok((value, self))
|
||||||
@@ -443,7 +456,7 @@ impl<'a, I> EnumVisitor for DeserializerEnumVisitor<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I>
|
impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I>
|
||||||
where I: Iterator<Item=Token<'static>>
|
where I: Iterator<Item = Token<'static>>
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
@@ -453,32 +466,26 @@ impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I>
|
|||||||
self.de.tokens.next();
|
self.de.tokens.next();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Some(_) => {
|
Some(_) => Deserialize::deserialize(self.de),
|
||||||
Deserialize::deserialize(self.de)
|
|
||||||
}
|
|
||||||
None => Err(Error::EndOfTokens),
|
None => Err(Error::EndOfTokens),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
|
fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
|
||||||
where T: DeserializeSeed,
|
where T: DeserializeSeed
|
||||||
{
|
{
|
||||||
match self.de.tokens.peek() {
|
match self.de.tokens.peek() {
|
||||||
Some(&Token::EnumNewType(_, _)) => {
|
Some(&Token::EnumNewType(_, _)) => {
|
||||||
self.de.tokens.next();
|
self.de.tokens.next();
|
||||||
seed.deserialize(self.de)
|
seed.deserialize(self.de)
|
||||||
}
|
}
|
||||||
Some(_) => {
|
Some(_) => seed.deserialize(self.de),
|
||||||
seed.deserialize(self.de)
|
|
||||||
}
|
|
||||||
None => Err(Error::EndOfTokens),
|
None => Err(Error::EndOfTokens),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_tuple<V>(self,
|
fn visit_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
|
||||||
len: usize,
|
where V: Visitor
|
||||||
visitor: V) -> Result<V::Value, Error>
|
|
||||||
where V: Visitor,
|
|
||||||
{
|
{
|
||||||
match self.de.tokens.peek() {
|
match self.de.tokens.peek() {
|
||||||
Some(&Token::EnumSeqStart(_, _, enum_len)) => {
|
Some(&Token::EnumSeqStart(_, _, enum_len)) => {
|
||||||
@@ -499,24 +506,23 @@ impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I>
|
|||||||
Err(Error::UnexpectedToken(token))
|
Err(Error::UnexpectedToken(token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(_) => {
|
Some(_) => de::Deserializer::deserialize(self.de, visitor),
|
||||||
de::Deserializer::deserialize(self.de, visitor)
|
|
||||||
}
|
|
||||||
None => Err(Error::EndOfTokens),
|
None => Err(Error::EndOfTokens),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_struct<V>(self,
|
fn visit_struct<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value, Error>
|
||||||
fields: &'static [&'static str],
|
where V: Visitor
|
||||||
visitor: V) -> Result<V::Value, Error>
|
|
||||||
where V: Visitor,
|
|
||||||
{
|
{
|
||||||
match self.de.tokens.peek() {
|
match self.de.tokens.peek() {
|
||||||
Some(&Token::EnumMapStart(_, _, enum_len)) => {
|
Some(&Token::EnumMapStart(_, _, enum_len)) => {
|
||||||
let token = self.de.tokens.next().unwrap();
|
let token = self.de.tokens.next().unwrap();
|
||||||
|
|
||||||
if fields.len() == enum_len {
|
if fields.len() == enum_len {
|
||||||
self.de.visit_map(Some(fields.len()), Token::EnumMapSep, Token::EnumMapEnd, visitor)
|
self.de.visit_map(Some(fields.len()),
|
||||||
|
Token::EnumMapSep,
|
||||||
|
Token::EnumMapEnd,
|
||||||
|
visitor)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::UnexpectedToken(token))
|
Err(Error::UnexpectedToken(token))
|
||||||
}
|
}
|
||||||
@@ -530,9 +536,7 @@ impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I>
|
|||||||
Err(Error::UnexpectedToken(token))
|
Err(Error::UnexpectedToken(token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(_) => {
|
Some(_) => de::Deserializer::deserialize(self.de, visitor),
|
||||||
de::Deserializer::deserialize(self.de, visitor)
|
|
||||||
}
|
|
||||||
None => Err(Error::EndOfTokens),
|
None => Err(Error::EndOfTokens),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,8 @@
|
|||||||
extern crate serde;
|
extern crate serde;
|
||||||
|
|
||||||
mod assert;
|
mod assert;
|
||||||
pub use assert::{
|
pub use assert::{assert_tokens, assert_ser_tokens, assert_ser_tokens_error, assert_de_tokens,
|
||||||
assert_tokens,
|
assert_de_tokens_error};
|
||||||
assert_ser_tokens,
|
|
||||||
assert_ser_tokens_error,
|
|
||||||
assert_de_tokens,
|
|
||||||
assert_de_tokens_error,
|
|
||||||
};
|
|
||||||
|
|
||||||
mod ser;
|
mod ser;
|
||||||
pub use ser::Serializer;
|
pub use ser::Serializer;
|
||||||
|
|||||||
+46
-29
@@ -6,14 +6,14 @@ use error::Error;
|
|||||||
use token::Token;
|
use token::Token;
|
||||||
|
|
||||||
pub struct Serializer<'a, I>
|
pub struct Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
tokens: I,
|
tokens: I,
|
||||||
phantom: PhantomData<&'a Token<'a>>,
|
phantom: PhantomData<&'a Token<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, I> Serializer<'a, I>
|
impl<'a, I> Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
pub fn new(tokens: I) -> Serializer<'a, I> {
|
pub fn new(tokens: I) -> Serializer<'a, I> {
|
||||||
Serializer {
|
Serializer {
|
||||||
@@ -28,7 +28,7 @@ impl<'a, I> Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
@@ -124,15 +124,14 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
|||||||
fn serialize_unit_variant(self,
|
fn serialize_unit_variant(self,
|
||||||
name: &str,
|
name: &str,
|
||||||
_variant_index: usize,
|
_variant_index: usize,
|
||||||
variant: &str) -> Result<(), Error> {
|
variant: &str)
|
||||||
|
-> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant)));
|
assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct<T: ?Sized>(self,
|
fn serialize_newtype_struct<T: ?Sized>(self, name: &'static str, value: &T) -> Result<(), Error>
|
||||||
name: &'static str,
|
where T: Serialize
|
||||||
value: &T) -> Result<(), Error>
|
|
||||||
where T: Serialize,
|
|
||||||
{
|
{
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::StructNewType(name)));
|
assert_eq!(self.tokens.next(), Some(&Token::StructNewType(name)));
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
@@ -142,8 +141,9 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
|||||||
name: &str,
|
name: &str,
|
||||||
_variant_index: usize,
|
_variant_index: usize,
|
||||||
variant: &str,
|
variant: &str,
|
||||||
value: &T) -> Result<(), Error>
|
value: &T)
|
||||||
where T: Serialize,
|
-> Result<(), Error>
|
||||||
|
where T: Serialize
|
||||||
{
|
{
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant)));
|
assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant)));
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
@@ -155,7 +155,7 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Error>
|
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Error>
|
||||||
where T: Serialize,
|
where T: Serialize
|
||||||
{
|
{
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::Option(true)));
|
assert_eq!(self.tokens.next(), Some(&Token::Option(true)));
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
@@ -177,7 +177,8 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
|
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructStart(name, len)));
|
assert_eq!(self.tokens.next(),
|
||||||
|
Some(&Token::TupleStructStart(name, len)));
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,9 +186,10 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
|||||||
name: &str,
|
name: &str,
|
||||||
_variant_index: usize,
|
_variant_index: usize,
|
||||||
variant: &str,
|
variant: &str,
|
||||||
len: usize) -> Result<Self, Error>
|
len: usize)
|
||||||
{
|
-> Result<Self, Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqStart(name, variant, len)));
|
assert_eq!(self.tokens.next(),
|
||||||
|
Some(&Token::EnumSeqStart(name, variant, len)));
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,15 +207,16 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
|||||||
name: &str,
|
name: &str,
|
||||||
_variant_index: usize,
|
_variant_index: usize,
|
||||||
variant: &str,
|
variant: &str,
|
||||||
len: usize) -> Result<Self, Error>
|
len: usize)
|
||||||
{
|
-> Result<Self, Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapStart(name, variant, len)));
|
assert_eq!(self.tokens.next(),
|
||||||
|
Some(&Token::EnumMapStart(name, variant, len)));
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'a, I> ser::SerializeSeq for &'s mut Serializer<'a, I>
|
impl<'s, 'a, I> ser::SerializeSeq for &'s mut Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
@@ -232,7 +235,7 @@ impl<'s, 'a, I> ser::SerializeSeq for &'s mut Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
|
impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
@@ -251,7 +254,7 @@ impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
|
impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
@@ -270,7 +273,7 @@ impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
|
impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
@@ -289,17 +292,21 @@ impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
|
impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error> where T: Serialize {
|
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
||||||
key.serialize(&mut **self)
|
key.serialize(&mut **self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> where T: Serialize {
|
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
value.serialize(&mut **self)
|
value.serialize(&mut **self)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -310,12 +317,17 @@ impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
|
impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = Error;
|
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
|
||||||
|
{
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::StructSep));
|
assert_eq!(self.tokens.next(), Some(&Token::StructSep));
|
||||||
try!(key.serialize(&mut **self));
|
try!(key.serialize(&mut **self));
|
||||||
value.serialize(&mut **self)
|
value.serialize(&mut **self)
|
||||||
@@ -328,12 +340,17 @@ impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'a, I> ser::SerializeStructVariant for &'s mut Serializer<'a, I>
|
impl<'s, 'a, I> ser::SerializeStructVariant for &'s mut Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = Error;
|
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
|
||||||
|
{
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep));
|
assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep));
|
||||||
try!(key.serialize(&mut **self));
|
try!(key.serialize(&mut **self));
|
||||||
value.serialize(&mut **self)
|
value.serialize(&mut **self)
|
||||||
|
|||||||
Reference in New Issue
Block a user