3 use de
::{Deserialize, DeserializeSeed, Deserializer, Error, IntoDeserializer, Visitor}
;
5 #[cfg(any(feature = "std", feature = "alloc"))]
6 use de
::{MapAccess, Unexpected}
;
8 #[cfg(any(feature = "std", feature = "alloc"))]
9 pub use self::content
::{
10 Content
, ContentDeserializer
, ContentRefDeserializer
, EnumDeserializer
,
11 InternallyTaggedUnitVisitor
, TagContentOtherField
, TagContentOtherFieldVisitor
,
12 TagOrContentField
, TagOrContentFieldVisitor
, TaggedContentVisitor
, UntaggedUnitVisitor
,
15 /// If the missing field is of type `Option<T>` then treat is as `None`,
16 /// otherwise it is an error.
17 pub fn missing_field
<'de
, V
, E
>(field
: &'
static str) -> Result
<V
, E
>
22 struct MissingFieldDeserializer
<E
>(&'
static str, PhantomData
<E
>);
24 impl<'de
, E
> Deserializer
<'de
> for MissingFieldDeserializer
<E
>
30 fn deserialize_any
<V
>(self, _visitor
: V
) -> Result
<V
::Value
, E
>
34 Err(Error
::missing_field(self.0))
37 fn deserialize_option
<V
>(self, visitor
: V
) -> Result
<V
::Value
, E
>
44 forward_to_deserialize_any
! {
45 bool
i8 i16 i32 i64 i128
u8 u16 u32 u64 u128
f32 f64 char str string
46 bytes byte_buf unit unit_struct newtype_struct seq tuple
47 tuple_struct map
struct enum identifier ignored_any
51 let deserializer
= MissingFieldDeserializer(field
, PhantomData
);
52 Deserialize
::deserialize(deserializer
)
55 #[cfg(any(feature = "std", feature = "alloc"))]
56 pub fn borrow_cow_str
<'de
: 'a
, 'a
, D
>(deserializer
: D
) -> Result
<Cow
<'a
, str>, D
::Error
>
62 impl<'a
> Visitor
<'a
> for CowStrVisitor
{
63 type Value
= Cow
<'a
, str>;
65 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
66 formatter
.write_str("a string")
69 fn visit_str
<E
>(self, v
: &str) -> Result
<Self::Value
, E
>
73 Ok(Cow
::Owned(v
.to_owned()))
76 fn visit_borrowed_str
<E
>(self, v
: &'a
str) -> Result
<Self::Value
, E
>
83 fn visit_string
<E
>(self, v
: String
) -> Result
<Self::Value
, E
>
90 fn visit_bytes
<E
>(self, v
: &[u8]) -> Result
<Self::Value
, E
>
94 match str::from_utf8(v
) {
95 Ok(s
) => Ok(Cow
::Owned(s
.to_owned())),
96 Err(_
) => Err(Error
::invalid_value(Unexpected
::Bytes(v
), &self)),
100 fn visit_borrowed_bytes
<E
>(self, v
: &'a
[u8]) -> Result
<Self::Value
, E
>
104 match str::from_utf8(v
) {
105 Ok(s
) => Ok(Cow
::Borrowed(s
)),
106 Err(_
) => Err(Error
::invalid_value(Unexpected
::Bytes(v
), &self)),
110 fn visit_byte_buf
<E
>(self, v
: Vec
<u8>) -> Result
<Self::Value
, E
>
114 match String
::from_utf8(v
) {
115 Ok(s
) => Ok(Cow
::Owned(s
)),
116 Err(e
) => Err(Error
::invalid_value(
117 Unexpected
::Bytes(&e
.into_bytes()),
124 deserializer
.deserialize_str(CowStrVisitor
)
127 #[cfg(any(feature = "std", feature = "alloc"))]
128 pub fn borrow_cow_bytes
<'de
: 'a
, 'a
, D
>(deserializer
: D
) -> Result
<Cow
<'a
, [u8]>, D
::Error
>
130 D
: Deserializer
<'de
>,
132 struct CowBytesVisitor
;
134 impl<'a
> Visitor
<'a
> for CowBytesVisitor
{
135 type Value
= Cow
<'a
, [u8]>;
137 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
138 formatter
.write_str("a byte array")
141 fn visit_str
<E
>(self, v
: &str) -> Result
<Self::Value
, E
>
145 Ok(Cow
::Owned(v
.as_bytes().to_vec()))
148 fn visit_borrowed_str
<E
>(self, v
: &'a
str) -> Result
<Self::Value
, E
>
152 Ok(Cow
::Borrowed(v
.as_bytes()))
155 fn visit_string
<E
>(self, v
: String
) -> Result
<Self::Value
, E
>
159 Ok(Cow
::Owned(v
.into_bytes()))
162 fn visit_bytes
<E
>(self, v
: &[u8]) -> Result
<Self::Value
, E
>
166 Ok(Cow
::Owned(v
.to_vec()))
169 fn visit_borrowed_bytes
<E
>(self, v
: &'a
[u8]) -> Result
<Self::Value
, E
>
176 fn visit_byte_buf
<E
>(self, v
: Vec
<u8>) -> Result
<Self::Value
, E
>
184 deserializer
.deserialize_bytes(CowBytesVisitor
)
190 pub fn from_bounds
<I
>(iter
: &I
) -> Option
<usize>
194 helper(iter
.size_hint())
198 pub fn cautious(hint
: Option
<usize>) -> usize {
199 cmp
::min(hint
.unwrap_or(0), 4096)
202 fn helper(bounds
: (usize, Option
<usize>)) -> Option
<usize> {
204 (lower
, Some(upper
)) if lower
== upper
=> Some(upper
),
210 #[cfg(any(feature = "std", feature = "alloc"))]
212 // This module is private and nothing here should be used outside of
215 // We will iterate on the implementation for a few releases and only have to
216 // worry about backward compatibility for the `untagged` and `tag` attributes
217 // rather than for this entire mechanism.
219 // This issue is tracking making some of this stuff public:
220 // https://github.com/serde-rs/serde/issues/741
224 use super::size_hint
;
226 self, Deserialize
, DeserializeSeed
, Deserializer
, EnumAccess
, Expected
, IgnoredAny
,
227 MapAccess
, SeqAccess
, Unexpected
, Visitor
,
230 /// Used from generated code to buffer the contents of the Deserializer when
231 /// deserializing untagged enums and internally tagged enums.
233 /// Not public API. Use serde-value instead.
235 pub enum Content
<'de
> {
258 Some(Box
<Content
<'de
>>),
261 Newtype(Box
<Content
<'de
>>),
262 Seq(Vec
<Content
<'de
>>),
263 Map(Vec
<(Content
<'de
>, Content
<'de
>)>),
266 impl<'de
> Content
<'de
> {
267 pub fn as_str(&self) -> Option
<&str> {
269 Content
::Str(x
) => Some(x
),
270 Content
::String(ref x
) => Some(x
),
271 Content
::Bytes(x
) => str::from_utf8(x
).ok(),
272 Content
::ByteBuf(ref x
) => str::from_utf8(x
).ok(),
278 fn unexpected(&self) -> Unexpected
{
280 Content
::Bool(b
) => Unexpected
::Bool(b
),
281 Content
::U8(n
) => Unexpected
::Unsigned(n
as u64),
282 Content
::U16(n
) => Unexpected
::Unsigned(n
as u64),
283 Content
::U32(n
) => Unexpected
::Unsigned(n
as u64),
284 Content
::U64(n
) => Unexpected
::Unsigned(n
),
285 Content
::I8(n
) => Unexpected
::Signed(n
as i64),
286 Content
::I16(n
) => Unexpected
::Signed(n
as i64),
287 Content
::I32(n
) => Unexpected
::Signed(n
as i64),
288 Content
::I64(n
) => Unexpected
::Signed(n
),
289 Content
::F32(f
) => Unexpected
::Float(f
as f64),
290 Content
::F64(f
) => Unexpected
::Float(f
),
291 Content
::Char(c
) => Unexpected
::Char(c
),
292 Content
::String(ref s
) => Unexpected
::Str(s
),
293 Content
::Str(s
) => Unexpected
::Str(s
),
294 Content
::ByteBuf(ref b
) => Unexpected
::Bytes(b
),
295 Content
::Bytes(b
) => Unexpected
::Bytes(b
),
296 Content
::None
| Content
::Some(_
) => Unexpected
::Option
,
297 Content
::Unit
=> Unexpected
::Unit
,
298 Content
::Newtype(_
) => Unexpected
::NewtypeStruct
,
299 Content
::Seq(_
) => Unexpected
::Seq
,
300 Content
::Map(_
) => Unexpected
::Map
,
305 impl<'de
> Deserialize
<'de
> for Content
<'de
> {
306 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
308 D
: Deserializer
<'de
>,
310 // Untagged and internally tagged enums are only supported in
311 // self-describing formats.
312 let visitor
= ContentVisitor { value: PhantomData }
;
313 deserializer
.deserialize_any(visitor
)
317 struct ContentVisitor
<'de
> {
318 value
: PhantomData
<Content
<'de
>>,
321 impl<'de
> ContentVisitor
<'de
> {
323 ContentVisitor { value: PhantomData }
327 impl<'de
> Visitor
<'de
> for ContentVisitor
<'de
> {
328 type Value
= Content
<'de
>;
330 fn expecting(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
331 fmt
.write_str("any value")
334 fn visit_bool
<F
>(self, value
: bool
) -> Result
<Self::Value
, F
>
338 Ok(Content
::Bool(value
))
341 fn visit_i8
<F
>(self, value
: i8) -> Result
<Self::Value
, F
>
345 Ok(Content
::I8(value
))
348 fn visit_i16
<F
>(self, value
: i16) -> Result
<Self::Value
, F
>
352 Ok(Content
::I16(value
))
355 fn visit_i32
<F
>(self, value
: i32) -> Result
<Self::Value
, F
>
359 Ok(Content
::I32(value
))
362 fn visit_i64
<F
>(self, value
: i64) -> Result
<Self::Value
, F
>
366 Ok(Content
::I64(value
))
369 fn visit_u8
<F
>(self, value
: u8) -> Result
<Self::Value
, F
>
373 Ok(Content
::U8(value
))
376 fn visit_u16
<F
>(self, value
: u16) -> Result
<Self::Value
, F
>
380 Ok(Content
::U16(value
))
383 fn visit_u32
<F
>(self, value
: u32) -> Result
<Self::Value
, F
>
387 Ok(Content
::U32(value
))
390 fn visit_u64
<F
>(self, value
: u64) -> Result
<Self::Value
, F
>
394 Ok(Content
::U64(value
))
397 fn visit_f32
<F
>(self, value
: f32) -> Result
<Self::Value
, F
>
401 Ok(Content
::F32(value
))
404 fn visit_f64
<F
>(self, value
: f64) -> Result
<Self::Value
, F
>
408 Ok(Content
::F64(value
))
411 fn visit_char
<F
>(self, value
: char) -> Result
<Self::Value
, F
>
415 Ok(Content
::Char(value
))
418 fn visit_str
<F
>(self, value
: &str) -> Result
<Self::Value
, F
>
422 Ok(Content
::String(value
.into()))
425 fn visit_borrowed_str
<F
>(self, value
: &'de
str) -> Result
<Self::Value
, F
>
429 Ok(Content
::Str(value
))
432 fn visit_string
<F
>(self, value
: String
) -> Result
<Self::Value
, F
>
436 Ok(Content
::String(value
))
439 fn visit_bytes
<F
>(self, value
: &[u8]) -> Result
<Self::Value
, F
>
443 Ok(Content
::ByteBuf(value
.into()))
446 fn visit_borrowed_bytes
<F
>(self, value
: &'de
[u8]) -> Result
<Self::Value
, F
>
450 Ok(Content
::Bytes(value
))
453 fn visit_byte_buf
<F
>(self, value
: Vec
<u8>) -> Result
<Self::Value
, F
>
457 Ok(Content
::ByteBuf(value
))
460 fn visit_unit
<F
>(self) -> Result
<Self::Value
, F
>
467 fn visit_none
<F
>(self) -> Result
<Self::Value
, F
>
474 fn visit_some
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
476 D
: Deserializer
<'de
>,
478 Deserialize
::deserialize(deserializer
).map(|v
| Content
::Some(Box
::new(v
)))
481 fn visit_newtype_struct
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
483 D
: Deserializer
<'de
>,
485 Deserialize
::deserialize(deserializer
).map(|v
| Content
::Newtype(Box
::new(v
)))
488 fn visit_seq
<V
>(self, mut visitor
: V
) -> Result
<Self::Value
, V
::Error
>
492 let mut vec
= Vec
::with_capacity(size_hint
::cautious(visitor
.size_hint()));
493 while let Some(e
) = try
!(visitor
.next_element()) {
496 Ok(Content
::Seq(vec
))
499 fn visit_map
<V
>(self, mut visitor
: V
) -> Result
<Self::Value
, V
::Error
>
503 let mut vec
= Vec
::with_capacity(size_hint
::cautious(visitor
.size_hint()));
504 while let Some(kv
) = try
!(visitor
.next_entry()) {
507 Ok(Content
::Map(vec
))
510 fn visit_enum
<V
>(self, _visitor
: V
) -> Result
<Self::Value
, V
::Error
>
514 Err(de
::Error
::custom(
515 "untagged and internally tagged enums do not support enum input",
520 /// This is the type of the map keys in an internally tagged enum.
523 pub enum TagOrContent
<'de
> {
525 Content(Content
<'de
>),
528 struct TagOrContentVisitor
<'de
> {
530 value
: PhantomData
<TagOrContent
<'de
>>,
533 impl<'de
> TagOrContentVisitor
<'de
> {
534 fn new(name
: &'
static str) -> Self {
535 TagOrContentVisitor
{
542 impl<'de
> DeserializeSeed
<'de
> for TagOrContentVisitor
<'de
> {
543 type Value
= TagOrContent
<'de
>;
545 fn deserialize
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
547 D
: Deserializer
<'de
>,
549 // Internally tagged enums are only supported in self-describing
551 deserializer
.deserialize_any(self)
555 impl<'de
> Visitor
<'de
> for TagOrContentVisitor
<'de
> {
556 type Value
= TagOrContent
<'de
>;
558 fn expecting(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
559 write
!(fmt
, "a type tag `{}` or any other value", self.name
)
562 fn visit_bool
<F
>(self, value
: bool
) -> Result
<Self::Value
, F
>
566 ContentVisitor
::new()
568 .map(TagOrContent
::Content
)
571 fn visit_i8
<F
>(self, value
: i8) -> Result
<Self::Value
, F
>
575 ContentVisitor
::new()
577 .map(TagOrContent
::Content
)
580 fn visit_i16
<F
>(self, value
: i16) -> Result
<Self::Value
, F
>
584 ContentVisitor
::new()
586 .map(TagOrContent
::Content
)
589 fn visit_i32
<F
>(self, value
: i32) -> Result
<Self::Value
, F
>
593 ContentVisitor
::new()
595 .map(TagOrContent
::Content
)
598 fn visit_i64
<F
>(self, value
: i64) -> Result
<Self::Value
, F
>
602 ContentVisitor
::new()
604 .map(TagOrContent
::Content
)
607 fn visit_u8
<F
>(self, value
: u8) -> Result
<Self::Value
, F
>
611 ContentVisitor
::new()
613 .map(TagOrContent
::Content
)
616 fn visit_u16
<F
>(self, value
: u16) -> Result
<Self::Value
, F
>
620 ContentVisitor
::new()
622 .map(TagOrContent
::Content
)
625 fn visit_u32
<F
>(self, value
: u32) -> Result
<Self::Value
, F
>
629 ContentVisitor
::new()
631 .map(TagOrContent
::Content
)
634 fn visit_u64
<F
>(self, value
: u64) -> Result
<Self::Value
, F
>
638 ContentVisitor
::new()
640 .map(TagOrContent
::Content
)
643 fn visit_f32
<F
>(self, value
: f32) -> Result
<Self::Value
, F
>
647 ContentVisitor
::new()
649 .map(TagOrContent
::Content
)
652 fn visit_f64
<F
>(self, value
: f64) -> Result
<Self::Value
, F
>
656 ContentVisitor
::new()
658 .map(TagOrContent
::Content
)
661 fn visit_char
<F
>(self, value
: char) -> Result
<Self::Value
, F
>
665 ContentVisitor
::new()
667 .map(TagOrContent
::Content
)
670 fn visit_str
<F
>(self, value
: &str) -> Result
<Self::Value
, F
>
674 if value
== self.name
{
675 Ok(TagOrContent
::Tag
)
677 ContentVisitor
::new()
679 .map(TagOrContent
::Content
)
683 fn visit_borrowed_str
<F
>(self, value
: &'de
str) -> Result
<Self::Value
, F
>
687 if value
== self.name
{
688 Ok(TagOrContent
::Tag
)
690 ContentVisitor
::new()
691 .visit_borrowed_str(value
)
692 .map(TagOrContent
::Content
)
696 fn visit_string
<F
>(self, value
: String
) -> Result
<Self::Value
, F
>
700 if value
== self.name
{
701 Ok(TagOrContent
::Tag
)
703 ContentVisitor
::new()
705 .map(TagOrContent
::Content
)
709 fn visit_bytes
<F
>(self, value
: &[u8]) -> Result
<Self::Value
, F
>
713 if value
== self.name
.as_bytes() {
714 Ok(TagOrContent
::Tag
)
716 ContentVisitor
::new()
718 .map(TagOrContent
::Content
)
722 fn visit_borrowed_bytes
<F
>(self, value
: &'de
[u8]) -> Result
<Self::Value
, F
>
726 if value
== self.name
.as_bytes() {
727 Ok(TagOrContent
::Tag
)
729 ContentVisitor
::new()
730 .visit_borrowed_bytes(value
)
731 .map(TagOrContent
::Content
)
735 fn visit_byte_buf
<F
>(self, value
: Vec
<u8>) -> Result
<Self::Value
, F
>
739 if value
== self.name
.as_bytes() {
740 Ok(TagOrContent
::Tag
)
742 ContentVisitor
::new()
743 .visit_byte_buf(value
)
744 .map(TagOrContent
::Content
)
748 fn visit_unit
<F
>(self) -> Result
<Self::Value
, F
>
752 ContentVisitor
::new()
754 .map(TagOrContent
::Content
)
757 fn visit_none
<F
>(self) -> Result
<Self::Value
, F
>
761 ContentVisitor
::new()
763 .map(TagOrContent
::Content
)
766 fn visit_some
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
768 D
: Deserializer
<'de
>,
770 ContentVisitor
::new()
771 .visit_some(deserializer
)
772 .map(TagOrContent
::Content
)
775 fn visit_newtype_struct
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
777 D
: Deserializer
<'de
>,
779 ContentVisitor
::new()
780 .visit_newtype_struct(deserializer
)
781 .map(TagOrContent
::Content
)
784 fn visit_seq
<V
>(self, visitor
: V
) -> Result
<Self::Value
, V
::Error
>
788 ContentVisitor
::new()
790 .map(TagOrContent
::Content
)
793 fn visit_map
<V
>(self, visitor
: V
) -> Result
<Self::Value
, V
::Error
>
797 ContentVisitor
::new()
799 .map(TagOrContent
::Content
)
802 fn visit_enum
<V
>(self, visitor
: V
) -> Result
<Self::Value
, V
::Error
>
806 ContentVisitor
::new()
808 .map(TagOrContent
::Content
)
812 /// Used by generated code to deserialize an internally tagged enum.
815 pub struct TaggedContent
<'de
, T
> {
817 pub content
: Content
<'de
>,
821 pub struct TaggedContentVisitor
<'de
, T
> {
822 tag_name
: &'
static str,
823 value
: PhantomData
<TaggedContent
<'de
, T
>>,
826 impl<'de
, T
> TaggedContentVisitor
<'de
, T
> {
827 /// Visitor for the content of an internally tagged enum with the given
829 pub fn new(name
: &'
static str) -> Self {
830 TaggedContentVisitor
{
837 impl<'de
, T
> DeserializeSeed
<'de
> for TaggedContentVisitor
<'de
, T
>
841 type Value
= TaggedContent
<'de
, T
>;
843 fn deserialize
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
845 D
: Deserializer
<'de
>,
847 // Internally tagged enums are only supported in self-describing
849 deserializer
.deserialize_any(self)
853 impl<'de
, T
> Visitor
<'de
> for TaggedContentVisitor
<'de
, T
>
857 type Value
= TaggedContent
<'de
, T
>;
859 fn expecting(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
860 fmt
.write_str("internally tagged enum")
863 fn visit_seq
<S
>(self, mut seq
: S
) -> Result
<Self::Value
, S
::Error
>
867 let tag
= match try
!(seq
.next_element()) {
870 return Err(de
::Error
::missing_field(self.tag_name
));
873 let rest
= de
::value
::SeqAccessDeserializer
::new(seq
);
876 content
: try
!(Content
::deserialize(rest
)),
880 fn visit_map
<M
>(self, mut map
: M
) -> Result
<Self::Value
, M
::Error
>
885 let mut vec
= Vec
::with_capacity(size_hint
::cautious(map
.size_hint()));
886 while let Some(k
) = try
!(map
.next_key_seed(TagOrContentVisitor
::new(self.tag_name
))) {
888 TagOrContent
::Tag
=> {
890 return Err(de
::Error
::duplicate_field(self.tag_name
));
892 tag
= Some(try
!(map
.next_value()));
894 TagOrContent
::Content(k
) => {
895 let v
= try
!(map
.next_value());
901 None
=> Err(de
::Error
::missing_field(self.tag_name
)),
902 Some(tag
) => Ok(TaggedContent
{
904 content
: Content
::Map(vec
),
910 /// Used by generated code to deserialize an adjacently tagged enum.
913 pub enum TagOrContentField
{
919 pub struct TagOrContentFieldVisitor
{
920 pub tag
: &'
static str,
921 pub content
: &'
static str,
924 impl<'de
> DeserializeSeed
<'de
> for TagOrContentFieldVisitor
{
925 type Value
= TagOrContentField
;
927 fn deserialize
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
929 D
: Deserializer
<'de
>,
931 deserializer
.deserialize_str(self)
935 impl<'de
> Visitor
<'de
> for TagOrContentFieldVisitor
{
936 type Value
= TagOrContentField
;
938 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
939 write
!(formatter
, "{:?} or {:?}", self.tag
, self.content
)
942 fn visit_str
<E
>(self, field
: &str) -> Result
<Self::Value
, E
>
946 if field
== self.tag
{
947 Ok(TagOrContentField
::Tag
)
948 } else if field
== self.content
{
949 Ok(TagOrContentField
::Content
)
951 Err(de
::Error
::invalid_value(Unexpected
::Str(field
), &self))
956 /// Used by generated code to deserialize an adjacently tagged enum when
957 /// ignoring unrelated fields is allowed.
960 pub enum TagContentOtherField
{
967 pub struct TagContentOtherFieldVisitor
{
968 pub tag
: &'
static str,
969 pub content
: &'
static str,
972 impl<'de
> DeserializeSeed
<'de
> for TagContentOtherFieldVisitor
{
973 type Value
= TagContentOtherField
;
975 fn deserialize
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
977 D
: Deserializer
<'de
>,
979 deserializer
.deserialize_str(self)
983 impl<'de
> Visitor
<'de
> for TagContentOtherFieldVisitor
{
984 type Value
= TagContentOtherField
;
986 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
989 "{:?}, {:?}, or other ignored fields",
990 self.tag
, self.content
994 fn visit_str
<E
>(self, field
: &str) -> Result
<Self::Value
, E
>
998 if field
== self.tag
{
999 Ok(TagContentOtherField
::Tag
)
1000 } else if field
== self.content
{
1001 Ok(TagContentOtherField
::Content
)
1003 Ok(TagContentOtherField
::Other
)
1009 pub struct ContentDeserializer
<'de
, E
> {
1010 content
: Content
<'de
>,
1011 err
: PhantomData
<E
>,
1014 impl<'de
, E
> ContentDeserializer
<'de
, E
>
1019 fn invalid_type(self, exp
: &Expected
) -> E
{
1020 de
::Error
::invalid_type(self.content
.unexpected(), exp
)
1023 fn deserialize_integer
<V
>(self, visitor
: V
) -> Result
<V
::Value
, E
>
1027 match self.content
{
1028 Content
::U8(v
) => visitor
.visit_u8(v
),
1029 Content
::U16(v
) => visitor
.visit_u16(v
),
1030 Content
::U32(v
) => visitor
.visit_u32(v
),
1031 Content
::U64(v
) => visitor
.visit_u64(v
),
1032 Content
::I8(v
) => visitor
.visit_i8(v
),
1033 Content
::I16(v
) => visitor
.visit_i16(v
),
1034 Content
::I32(v
) => visitor
.visit_i32(v
),
1035 Content
::I64(v
) => visitor
.visit_i64(v
),
1036 _
=> Err(self.invalid_type(&visitor
)),
1041 fn visit_content_seq
<'de
, V
, E
>(content
: Vec
<Content
<'de
>>, visitor
: V
) -> Result
<V
::Value
, E
>
1046 let seq
= content
.into_iter().map(ContentDeserializer
::new
);
1047 let mut seq_visitor
= de
::value
::SeqDeserializer
::new(seq
);
1048 let value
= try
!(visitor
.visit_seq(&mut seq_visitor
));
1049 try
!(seq_visitor
.end());
1053 fn visit_content_map
<'de
, V
, E
>(
1054 content
: Vec
<(Content
<'de
>, Content
<'de
>)>,
1056 ) -> Result
<V
::Value
, E
>
1063 .map(|(k
, v
)| (ContentDeserializer
::new(k
), ContentDeserializer
::new(v
)));
1064 let mut map_visitor
= de
::value
::MapDeserializer
::new(map
);
1065 let value
= try
!(visitor
.visit_map(&mut map_visitor
));
1066 try
!(map_visitor
.end());
1070 /// Used when deserializing an internally tagged enum because the content
1071 /// will be used exactly once.
1072 impl<'de
, E
> Deserializer
<'de
> for ContentDeserializer
<'de
, E
>
1078 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1082 match self.content
{
1083 Content
::Bool(v
) => visitor
.visit_bool(v
),
1084 Content
::U8(v
) => visitor
.visit_u8(v
),
1085 Content
::U16(v
) => visitor
.visit_u16(v
),
1086 Content
::U32(v
) => visitor
.visit_u32(v
),
1087 Content
::U64(v
) => visitor
.visit_u64(v
),
1088 Content
::I8(v
) => visitor
.visit_i8(v
),
1089 Content
::I16(v
) => visitor
.visit_i16(v
),
1090 Content
::I32(v
) => visitor
.visit_i32(v
),
1091 Content
::I64(v
) => visitor
.visit_i64(v
),
1092 Content
::F32(v
) => visitor
.visit_f32(v
),
1093 Content
::F64(v
) => visitor
.visit_f64(v
),
1094 Content
::Char(v
) => visitor
.visit_char(v
),
1095 Content
::String(v
) => visitor
.visit_string(v
),
1096 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1097 Content
::ByteBuf(v
) => visitor
.visit_byte_buf(v
),
1098 Content
::Bytes(v
) => visitor
.visit_borrowed_bytes(v
),
1099 Content
::Unit
=> visitor
.visit_unit(),
1100 Content
::None
=> visitor
.visit_none(),
1101 Content
::Some(v
) => visitor
.visit_some(ContentDeserializer
::new(*v
)),
1102 Content
::Newtype(v
) => visitor
.visit_newtype_struct(ContentDeserializer
::new(*v
)),
1103 Content
::Seq(v
) => visit_content_seq(v
, visitor
),
1104 Content
::Map(v
) => visit_content_map(v
, visitor
),
1108 fn deserialize_bool
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1112 match self.content
{
1113 Content
::Bool(v
) => visitor
.visit_bool(v
),
1114 _
=> Err(self.invalid_type(&visitor
)),
1118 fn deserialize_i8
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1122 self.deserialize_integer(visitor
)
1125 fn deserialize_i16
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1129 self.deserialize_integer(visitor
)
1132 fn deserialize_i32
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1136 self.deserialize_integer(visitor
)
1139 fn deserialize_i64
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1143 self.deserialize_integer(visitor
)
1146 fn deserialize_u8
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1150 self.deserialize_integer(visitor
)
1153 fn deserialize_u16
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1157 self.deserialize_integer(visitor
)
1160 fn deserialize_u32
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1164 self.deserialize_integer(visitor
)
1167 fn deserialize_u64
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1171 self.deserialize_integer(visitor
)
1174 fn deserialize_f32
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1178 match self.content
{
1179 Content
::F32(v
) => visitor
.visit_f32(v
),
1180 Content
::F64(v
) => visitor
.visit_f64(v
),
1181 Content
::U64(v
) => visitor
.visit_u64(v
),
1182 Content
::I64(v
) => visitor
.visit_i64(v
),
1183 _
=> Err(self.invalid_type(&visitor
)),
1187 fn deserialize_f64
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1191 match self.content
{
1192 Content
::F64(v
) => visitor
.visit_f64(v
),
1193 Content
::U64(v
) => visitor
.visit_u64(v
),
1194 Content
::I64(v
) => visitor
.visit_i64(v
),
1195 _
=> Err(self.invalid_type(&visitor
)),
1199 fn deserialize_char
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1203 match self.content
{
1204 Content
::Char(v
) => visitor
.visit_char(v
),
1205 Content
::String(v
) => visitor
.visit_string(v
),
1206 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1207 _
=> Err(self.invalid_type(&visitor
)),
1211 fn deserialize_str
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1215 self.deserialize_string(visitor
)
1218 fn deserialize_string
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1222 match self.content
{
1223 Content
::String(v
) => visitor
.visit_string(v
),
1224 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1225 Content
::ByteBuf(v
) => visitor
.visit_byte_buf(v
),
1226 Content
::Bytes(v
) => visitor
.visit_borrowed_bytes(v
),
1227 _
=> Err(self.invalid_type(&visitor
)),
1231 fn deserialize_bytes
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1235 self.deserialize_byte_buf(visitor
)
1238 fn deserialize_byte_buf
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1242 match self.content
{
1243 Content
::String(v
) => visitor
.visit_string(v
),
1244 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1245 Content
::ByteBuf(v
) => visitor
.visit_byte_buf(v
),
1246 Content
::Bytes(v
) => visitor
.visit_borrowed_bytes(v
),
1247 Content
::Seq(v
) => visit_content_seq(v
, visitor
),
1248 _
=> Err(self.invalid_type(&visitor
)),
1252 fn deserialize_option
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1256 match self.content
{
1257 Content
::None
=> visitor
.visit_none(),
1258 Content
::Some(v
) => visitor
.visit_some(ContentDeserializer
::new(*v
)),
1259 Content
::Unit
=> visitor
.visit_unit(),
1260 _
=> visitor
.visit_some(self),
1264 fn deserialize_unit
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1268 match self.content
{
1269 Content
::Unit
=> visitor
.visit_unit(),
1270 _
=> Err(self.invalid_type(&visitor
)),
1274 fn deserialize_unit_struct
<V
>(
1276 _name
: &'
static str,
1278 ) -> Result
<V
::Value
, Self::Error
>
1282 match self.content
{
1283 // As a special case, allow deserializing untagged newtype
1284 // variant containing unit struct.
1286 // #[derive(Deserialize)]
1289 // #[derive(Deserialize)]
1290 // #[serde(tag = "topic")]
1295 // We want {"topic":"Info"} to deserialize even though
1296 // ordinarily unit structs do not deserialize from empty map.
1297 Content
::Map(ref v
) if v
.is_empty() => visitor
.visit_unit(),
1298 _
=> self.deserialize_any(visitor
),
1302 fn deserialize_newtype_struct
<V
>(
1306 ) -> Result
<V
::Value
, Self::Error
>
1310 match self.content
{
1311 Content
::Newtype(v
) => visitor
.visit_newtype_struct(ContentDeserializer
::new(*v
)),
1312 _
=> visitor
.visit_newtype_struct(self),
1316 fn deserialize_seq
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1320 match self.content
{
1321 Content
::Seq(v
) => visit_content_seq(v
, visitor
),
1322 _
=> Err(self.invalid_type(&visitor
)),
1326 fn deserialize_tuple
<V
>(self, _len
: usize, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1330 self.deserialize_seq(visitor
)
1333 fn deserialize_tuple_struct
<V
>(
1335 _name
: &'
static str,
1338 ) -> Result
<V
::Value
, Self::Error
>
1342 self.deserialize_seq(visitor
)
1345 fn deserialize_map
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1349 match self.content
{
1350 Content
::Map(v
) => visit_content_map(v
, visitor
),
1351 _
=> Err(self.invalid_type(&visitor
)),
1355 fn deserialize_struct
<V
>(
1357 _name
: &'
static str,
1358 _fields
: &'
static [&'
static str],
1360 ) -> Result
<V
::Value
, Self::Error
>
1364 match self.content
{
1365 Content
::Seq(v
) => visit_content_seq(v
, visitor
),
1366 Content
::Map(v
) => visit_content_map(v
, visitor
),
1367 _
=> Err(self.invalid_type(&visitor
)),
1371 fn deserialize_enum
<V
>(
1374 _variants
: &'
static [&'
static str],
1376 ) -> Result
<V
::Value
, Self::Error
>
1380 let (variant
, value
) = match self.content
{
1381 Content
::Map(value
) => {
1382 let mut iter
= value
.into_iter();
1383 let (variant
, value
) = match iter
.next() {
1386 return Err(de
::Error
::invalid_value(
1387 de
::Unexpected
::Map
,
1388 &"map with a single key",
1392 // enums are encoded in json as maps with a single key:value pair
1393 if iter
.next().is_some() {
1394 return Err(de
::Error
::invalid_value(
1395 de
::Unexpected
::Map
,
1396 &"map with a single key",
1399 (variant
, Some(value
))
1401 s @ Content
::String(_
) | s @ Content
::Str(_
) => (s
, None
),
1403 return Err(de
::Error
::invalid_type(
1410 visitor
.visit_enum(EnumDeserializer
::new(variant
, value
))
1413 fn deserialize_identifier
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1417 match self.content
{
1418 Content
::String(v
) => visitor
.visit_string(v
),
1419 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1420 Content
::ByteBuf(v
) => visitor
.visit_byte_buf(v
),
1421 Content
::Bytes(v
) => visitor
.visit_borrowed_bytes(v
),
1422 Content
::U8(v
) => visitor
.visit_u8(v
),
1423 _
=> Err(self.invalid_type(&visitor
)),
1427 fn deserialize_ignored_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1432 visitor
.visit_unit()
1436 impl<'de
, E
> ContentDeserializer
<'de
, E
> {
1437 /// private API, don't use
1438 pub fn new(content
: Content
<'de
>) -> Self {
1439 ContentDeserializer
{
1446 pub struct EnumDeserializer
<'de
, E
>
1450 variant
: Content
<'de
>,
1451 value
: Option
<Content
<'de
>>,
1452 err
: PhantomData
<E
>,
1455 impl<'de
, E
> EnumDeserializer
<'de
, E
>
1459 pub fn new(variant
: Content
<'de
>, value
: Option
<Content
<'de
>>) -> EnumDeserializer
<'de
, E
> {
1468 impl<'de
, E
> de
::EnumAccess
<'de
> for EnumDeserializer
<'de
, E
>
1473 type Variant
= VariantDeserializer
<'de
, Self::Error
>;
1475 fn variant_seed
<V
>(self, seed
: V
) -> Result
<(V
::Value
, Self::Variant
), E
>
1477 V
: de
::DeserializeSeed
<'de
>,
1479 let visitor
= VariantDeserializer
{
1483 seed
.deserialize(ContentDeserializer
::new(self.variant
))
1484 .map(|v
| (v
, visitor
))
1488 pub struct VariantDeserializer
<'de
, E
>
1492 value
: Option
<Content
<'de
>>,
1493 err
: PhantomData
<E
>,
1496 impl<'de
, E
> de
::VariantAccess
<'de
> for VariantDeserializer
<'de
, E
>
1502 fn unit_variant(self) -> Result
<(), E
> {
1504 Some(value
) => de
::Deserialize
::deserialize(ContentDeserializer
::new(value
)),
1509 fn newtype_variant_seed
<T
>(self, seed
: T
) -> Result
<T
::Value
, E
>
1511 T
: de
::DeserializeSeed
<'de
>,
1514 Some(value
) => seed
.deserialize(ContentDeserializer
::new(value
)),
1515 None
=> Err(de
::Error
::invalid_type(
1516 de
::Unexpected
::UnitVariant
,
1522 fn tuple_variant
<V
>(self, _len
: usize, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1524 V
: de
::Visitor
<'de
>,
1527 Some(Content
::Seq(v
)) => {
1528 de
::Deserializer
::deserialize_any(SeqDeserializer
::new(v
), visitor
)
1530 Some(other
) => Err(de
::Error
::invalid_type(
1534 None
=> Err(de
::Error
::invalid_type(
1535 de
::Unexpected
::UnitVariant
,
1541 fn struct_variant
<V
>(
1543 _fields
: &'
static [&'
static str],
1545 ) -> Result
<V
::Value
, Self::Error
>
1547 V
: de
::Visitor
<'de
>,
1550 Some(Content
::Map(v
)) => {
1551 de
::Deserializer
::deserialize_any(MapDeserializer
::new(v
), visitor
)
1553 Some(Content
::Seq(v
)) => {
1554 de
::Deserializer
::deserialize_any(SeqDeserializer
::new(v
), visitor
)
1556 Some(other
) => Err(de
::Error
::invalid_type(
1560 _
=> Err(de
::Error
::invalid_type(
1561 de
::Unexpected
::UnitVariant
,
1568 struct SeqDeserializer
<'de
, E
>
1572 iter
: <Vec
<Content
<'de
>> as IntoIterator
>::IntoIter
,
1573 err
: PhantomData
<E
>,
1576 impl<'de
, E
> SeqDeserializer
<'de
, E
>
1580 fn new(vec
: Vec
<Content
<'de
>>) -> Self {
1582 iter
: vec
.into_iter(),
1588 impl<'de
, E
> de
::Deserializer
<'de
> for SeqDeserializer
<'de
, E
>
1595 fn deserialize_any
<V
>(mut self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1597 V
: de
::Visitor
<'de
>,
1599 let len
= self.iter
.len();
1601 visitor
.visit_unit()
1603 let ret
= try
!(visitor
.visit_seq(&mut self));
1604 let remaining
= self.iter
.len();
1608 Err(de
::Error
::invalid_length(len
, &"fewer elements in array"))
1613 forward_to_deserialize_any
! {
1614 bool
i8 i16 i32 i64 i128
u8 u16 u32 u64 u128
f32 f64 char str string
1615 bytes byte_buf option unit unit_struct newtype_struct seq tuple
1616 tuple_struct map
struct enum identifier ignored_any
1620 impl<'de
, E
> de
::SeqAccess
<'de
> for SeqDeserializer
<'de
, E
>
1626 fn next_element_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
1628 T
: de
::DeserializeSeed
<'de
>,
1630 match self.iter
.next() {
1631 Some(value
) => seed
.deserialize(ContentDeserializer
::new(value
)).map(Some
),
1636 fn size_hint(&self) -> Option
<usize> {
1637 size_hint
::from_bounds(&self.iter
)
1641 struct MapDeserializer
<'de
, E
>
1645 iter
: <Vec
<(Content
<'de
>, Content
<'de
>)> as IntoIterator
>::IntoIter
,
1646 value
: Option
<Content
<'de
>>,
1647 err
: PhantomData
<E
>,
1650 impl<'de
, E
> MapDeserializer
<'de
, E
>
1654 fn new(map
: Vec
<(Content
<'de
>, Content
<'de
>)>) -> Self {
1656 iter
: map
.into_iter(),
1663 impl<'de
, E
> de
::MapAccess
<'de
> for MapDeserializer
<'de
, E
>
1669 fn next_key_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
1671 T
: de
::DeserializeSeed
<'de
>,
1673 match self.iter
.next() {
1674 Some((key
, value
)) => {
1675 self.value
= Some(value
);
1676 seed
.deserialize(ContentDeserializer
::new(key
)).map(Some
)
1682 fn next_value_seed
<T
>(&mut self, seed
: T
) -> Result
<T
::Value
, Self::Error
>
1684 T
: de
::DeserializeSeed
<'de
>,
1686 match self.value
.take() {
1687 Some(value
) => seed
.deserialize(ContentDeserializer
::new(value
)),
1688 None
=> Err(de
::Error
::custom("value is missing")),
1692 fn size_hint(&self) -> Option
<usize> {
1693 size_hint
::from_bounds(&self.iter
)
1697 impl<'de
, E
> de
::Deserializer
<'de
> for MapDeserializer
<'de
, E
>
1704 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1706 V
: de
::Visitor
<'de
>,
1708 visitor
.visit_map(self)
1711 forward_to_deserialize_any
! {
1712 bool
i8 i16 i32 i64 i128
u8 u16 u32 u64 u128
f32 f64 char str string
1713 bytes byte_buf option unit unit_struct newtype_struct seq tuple
1714 tuple_struct map
struct enum identifier ignored_any
1719 pub struct ContentRefDeserializer
<'a
, 'de
: 'a
, E
> {
1720 content
: &'a Content
<'de
>,
1721 err
: PhantomData
<E
>,
1724 impl<'a
, 'de
, E
> ContentRefDeserializer
<'a
, 'de
, E
>
1729 fn invalid_type(self, exp
: &Expected
) -> E
{
1730 de
::Error
::invalid_type(self.content
.unexpected(), exp
)
1733 fn deserialize_integer
<V
>(self, visitor
: V
) -> Result
<V
::Value
, E
>
1737 match *self.content
{
1738 Content
::U8(v
) => visitor
.visit_u8(v
),
1739 Content
::U16(v
) => visitor
.visit_u16(v
),
1740 Content
::U32(v
) => visitor
.visit_u32(v
),
1741 Content
::U64(v
) => visitor
.visit_u64(v
),
1742 Content
::I8(v
) => visitor
.visit_i8(v
),
1743 Content
::I16(v
) => visitor
.visit_i16(v
),
1744 Content
::I32(v
) => visitor
.visit_i32(v
),
1745 Content
::I64(v
) => visitor
.visit_i64(v
),
1746 _
=> Err(self.invalid_type(&visitor
)),
1751 fn visit_content_seq_ref
<'a
, 'de
, V
, E
>(
1752 content
: &'a
[Content
<'de
>],
1754 ) -> Result
<V
::Value
, E
>
1759 let seq
= content
.iter().map(ContentRefDeserializer
::new
);
1760 let mut seq_visitor
= de
::value
::SeqDeserializer
::new(seq
);
1761 let value
= try
!(visitor
.visit_seq(&mut seq_visitor
));
1762 try
!(seq_visitor
.end());
1766 fn visit_content_map_ref
<'a
, 'de
, V
, E
>(
1767 content
: &'a
[(Content
<'de
>, Content
<'de
>)],
1769 ) -> Result
<V
::Value
, E
>
1774 let map
= content
.iter().map(|&(ref k
, ref v
)| {
1776 ContentRefDeserializer
::new(k
),
1777 ContentRefDeserializer
::new(v
),
1780 let mut map_visitor
= de
::value
::MapDeserializer
::new(map
);
1781 let value
= try
!(visitor
.visit_map(&mut map_visitor
));
1782 try
!(map_visitor
.end());
1786 /// Used when deserializing an untagged enum because the content may need
1787 /// to be used more than once.
1788 impl<'de
, 'a
, E
> Deserializer
<'de
> for ContentRefDeserializer
<'a
, 'de
, E
>
1794 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, E
>
1798 match *self.content
{
1799 Content
::Bool(v
) => visitor
.visit_bool(v
),
1800 Content
::U8(v
) => visitor
.visit_u8(v
),
1801 Content
::U16(v
) => visitor
.visit_u16(v
),
1802 Content
::U32(v
) => visitor
.visit_u32(v
),
1803 Content
::U64(v
) => visitor
.visit_u64(v
),
1804 Content
::I8(v
) => visitor
.visit_i8(v
),
1805 Content
::I16(v
) => visitor
.visit_i16(v
),
1806 Content
::I32(v
) => visitor
.visit_i32(v
),
1807 Content
::I64(v
) => visitor
.visit_i64(v
),
1808 Content
::F32(v
) => visitor
.visit_f32(v
),
1809 Content
::F64(v
) => visitor
.visit_f64(v
),
1810 Content
::Char(v
) => visitor
.visit_char(v
),
1811 Content
::String(ref v
) => visitor
.visit_str(v
),
1812 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1813 Content
::ByteBuf(ref v
) => visitor
.visit_bytes(v
),
1814 Content
::Bytes(v
) => visitor
.visit_borrowed_bytes(v
),
1815 Content
::Unit
=> visitor
.visit_unit(),
1816 Content
::None
=> visitor
.visit_none(),
1817 Content
::Some(ref v
) => visitor
.visit_some(ContentRefDeserializer
::new(v
)),
1818 Content
::Newtype(ref v
) => {
1819 visitor
.visit_newtype_struct(ContentRefDeserializer
::new(v
))
1821 Content
::Seq(ref v
) => visit_content_seq_ref(v
, visitor
),
1822 Content
::Map(ref v
) => visit_content_map_ref(v
, visitor
),
1826 fn deserialize_bool
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1830 match *self.content
{
1831 Content
::Bool(v
) => visitor
.visit_bool(v
),
1832 _
=> Err(self.invalid_type(&visitor
)),
1836 fn deserialize_i8
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1840 self.deserialize_integer(visitor
)
1843 fn deserialize_i16
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1847 self.deserialize_integer(visitor
)
1850 fn deserialize_i32
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1854 self.deserialize_integer(visitor
)
1857 fn deserialize_i64
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1861 self.deserialize_integer(visitor
)
1864 fn deserialize_u8
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1868 self.deserialize_integer(visitor
)
1871 fn deserialize_u16
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1875 self.deserialize_integer(visitor
)
1878 fn deserialize_u32
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1882 self.deserialize_integer(visitor
)
1885 fn deserialize_u64
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1889 self.deserialize_integer(visitor
)
1892 fn deserialize_f32
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1896 match *self.content
{
1897 Content
::F32(v
) => visitor
.visit_f32(v
),
1898 Content
::F64(v
) => visitor
.visit_f64(v
),
1899 Content
::U64(v
) => visitor
.visit_u64(v
),
1900 Content
::I64(v
) => visitor
.visit_i64(v
),
1901 _
=> Err(self.invalid_type(&visitor
)),
1905 fn deserialize_f64
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1909 match *self.content
{
1910 Content
::F64(v
) => visitor
.visit_f64(v
),
1911 Content
::U64(v
) => visitor
.visit_u64(v
),
1912 Content
::I64(v
) => visitor
.visit_i64(v
),
1913 _
=> Err(self.invalid_type(&visitor
)),
1917 fn deserialize_char
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1921 match *self.content
{
1922 Content
::Char(v
) => visitor
.visit_char(v
),
1923 Content
::String(ref v
) => visitor
.visit_str(v
),
1924 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1925 _
=> Err(self.invalid_type(&visitor
)),
1929 fn deserialize_str
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1933 match *self.content
{
1934 Content
::String(ref v
) => visitor
.visit_str(v
),
1935 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1936 Content
::ByteBuf(ref v
) => visitor
.visit_bytes(v
),
1937 Content
::Bytes(v
) => visitor
.visit_borrowed_bytes(v
),
1938 _
=> Err(self.invalid_type(&visitor
)),
1942 fn deserialize_string
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1946 self.deserialize_str(visitor
)
1949 fn deserialize_bytes
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1953 match *self.content
{
1954 Content
::String(ref v
) => visitor
.visit_str(v
),
1955 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1956 Content
::ByteBuf(ref v
) => visitor
.visit_bytes(v
),
1957 Content
::Bytes(v
) => visitor
.visit_borrowed_bytes(v
),
1958 Content
::Seq(ref v
) => visit_content_seq_ref(v
, visitor
),
1959 _
=> Err(self.invalid_type(&visitor
)),
1963 fn deserialize_byte_buf
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1967 self.deserialize_bytes(visitor
)
1970 fn deserialize_option
<V
>(self, visitor
: V
) -> Result
<V
::Value
, E
>
1974 match *self.content
{
1975 Content
::None
=> visitor
.visit_none(),
1976 Content
::Some(ref v
) => visitor
.visit_some(ContentRefDeserializer
::new(v
)),
1977 Content
::Unit
=> visitor
.visit_unit(),
1978 _
=> visitor
.visit_some(self),
1982 fn deserialize_unit
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1986 match *self.content
{
1987 Content
::Unit
=> visitor
.visit_unit(),
1988 _
=> Err(self.invalid_type(&visitor
)),
1992 fn deserialize_unit_struct
<V
>(
1994 _name
: &'
static str,
1996 ) -> Result
<V
::Value
, Self::Error
>
2000 self.deserialize_unit(visitor
)
2003 fn deserialize_newtype_struct
<V
>(self, _name
: &str, visitor
: V
) -> Result
<V
::Value
, E
>
2007 match *self.content
{
2008 Content
::Newtype(ref v
) => {
2009 visitor
.visit_newtype_struct(ContentRefDeserializer
::new(v
))
2011 _
=> visitor
.visit_newtype_struct(self),
2015 fn deserialize_seq
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2019 match *self.content
{
2020 Content
::Seq(ref v
) => visit_content_seq_ref(v
, visitor
),
2021 _
=> Err(self.invalid_type(&visitor
)),
2025 fn deserialize_tuple
<V
>(self, _len
: usize, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2029 self.deserialize_seq(visitor
)
2032 fn deserialize_tuple_struct
<V
>(
2034 _name
: &'
static str,
2037 ) -> Result
<V
::Value
, Self::Error
>
2041 self.deserialize_seq(visitor
)
2044 fn deserialize_map
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2048 match *self.content
{
2049 Content
::Map(ref v
) => visit_content_map_ref(v
, visitor
),
2050 _
=> Err(self.invalid_type(&visitor
)),
2054 fn deserialize_struct
<V
>(
2056 _name
: &'
static str,
2057 _fields
: &'
static [&'
static str],
2059 ) -> Result
<V
::Value
, Self::Error
>
2063 match *self.content
{
2064 Content
::Seq(ref v
) => visit_content_seq_ref(v
, visitor
),
2065 Content
::Map(ref v
) => visit_content_map_ref(v
, visitor
),
2066 _
=> Err(self.invalid_type(&visitor
)),
2070 fn deserialize_enum
<V
>(
2073 _variants
: &'
static [&'
static str],
2075 ) -> Result
<V
::Value
, Self::Error
>
2079 let (variant
, value
) = match *self.content
{
2080 Content
::Map(ref value
) => {
2081 let mut iter
= value
.iter();
2082 let &(ref variant
, ref value
) = match iter
.next() {
2085 return Err(de
::Error
::invalid_value(
2086 de
::Unexpected
::Map
,
2087 &"map with a single key",
2091 // enums are encoded in json as maps with a single key:value pair
2092 if iter
.next().is_some() {
2093 return Err(de
::Error
::invalid_value(
2094 de
::Unexpected
::Map
,
2095 &"map with a single key",
2098 (variant
, Some(value
))
2100 ref s @ Content
::String(_
) | ref s @ Content
::Str(_
) => (s
, None
),
2102 return Err(de
::Error
::invalid_type(
2109 visitor
.visit_enum(EnumRefDeserializer
{
2116 fn deserialize_identifier
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2120 match *self.content
{
2121 Content
::String(ref v
) => visitor
.visit_str(v
),
2122 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
2123 Content
::ByteBuf(ref v
) => visitor
.visit_bytes(v
),
2124 Content
::Bytes(v
) => visitor
.visit_borrowed_bytes(v
),
2125 Content
::U8(v
) => visitor
.visit_u8(v
),
2126 _
=> Err(self.invalid_type(&visitor
)),
2130 fn deserialize_ignored_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2134 visitor
.visit_unit()
2138 impl<'a
, 'de
, E
> ContentRefDeserializer
<'a
, 'de
, E
> {
2139 /// private API, don't use
2140 pub fn new(content
: &'a Content
<'de
>) -> Self {
2141 ContentRefDeserializer
{
2148 struct EnumRefDeserializer
<'a
, 'de
: 'a
, E
>
2152 variant
: &'a Content
<'de
>,
2153 value
: Option
<&'a Content
<'de
>>,
2154 err
: PhantomData
<E
>,
2157 impl<'de
, 'a
, E
> de
::EnumAccess
<'de
> for EnumRefDeserializer
<'a
, 'de
, E
>
2162 type Variant
= VariantRefDeserializer
<'a
, 'de
, Self::Error
>;
2164 fn variant_seed
<V
>(self, seed
: V
) -> Result
<(V
::Value
, Self::Variant
), Self::Error
>
2166 V
: de
::DeserializeSeed
<'de
>,
2168 let visitor
= VariantRefDeserializer
{
2172 seed
.deserialize(ContentRefDeserializer
::new(self.variant
))
2173 .map(|v
| (v
, visitor
))
2177 struct VariantRefDeserializer
<'a
, 'de
: 'a
, E
>
2181 value
: Option
<&'a Content
<'de
>>,
2182 err
: PhantomData
<E
>,
2185 impl<'de
, 'a
, E
> de
::VariantAccess
<'de
> for VariantRefDeserializer
<'a
, 'de
, E
>
2191 fn unit_variant(self) -> Result
<(), E
> {
2193 Some(value
) => de
::Deserialize
::deserialize(ContentRefDeserializer
::new(value
)),
2198 fn newtype_variant_seed
<T
>(self, seed
: T
) -> Result
<T
::Value
, E
>
2200 T
: de
::DeserializeSeed
<'de
>,
2203 Some(value
) => seed
.deserialize(ContentRefDeserializer
::new(value
)),
2204 None
=> Err(de
::Error
::invalid_type(
2205 de
::Unexpected
::UnitVariant
,
2211 fn tuple_variant
<V
>(self, _len
: usize, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2213 V
: de
::Visitor
<'de
>,
2216 Some(&Content
::Seq(ref v
)) => {
2217 de
::Deserializer
::deserialize_any(SeqRefDeserializer
::new(v
), visitor
)
2219 Some(other
) => Err(de
::Error
::invalid_type(
2223 None
=> Err(de
::Error
::invalid_type(
2224 de
::Unexpected
::UnitVariant
,
2230 fn struct_variant
<V
>(
2232 _fields
: &'
static [&'
static str],
2234 ) -> Result
<V
::Value
, Self::Error
>
2236 V
: de
::Visitor
<'de
>,
2239 Some(&Content
::Map(ref v
)) => {
2240 de
::Deserializer
::deserialize_any(MapRefDeserializer
::new(v
), visitor
)
2242 Some(&Content
::Seq(ref v
)) => {
2243 de
::Deserializer
::deserialize_any(SeqRefDeserializer
::new(v
), visitor
)
2245 Some(other
) => Err(de
::Error
::invalid_type(
2249 _
=> Err(de
::Error
::invalid_type(
2250 de
::Unexpected
::UnitVariant
,
2257 struct SeqRefDeserializer
<'a
, 'de
: 'a
, E
>
2261 iter
: <&'a
[Content
<'de
>] as IntoIterator
>::IntoIter
,
2262 err
: PhantomData
<E
>,
2265 impl<'a
, 'de
, E
> SeqRefDeserializer
<'a
, 'de
, E
>
2269 fn new(slice
: &'a
[Content
<'de
>]) -> Self {
2270 SeqRefDeserializer
{
2277 impl<'de
, 'a
, E
> de
::Deserializer
<'de
> for SeqRefDeserializer
<'a
, 'de
, E
>
2284 fn deserialize_any
<V
>(mut self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2286 V
: de
::Visitor
<'de
>,
2288 let len
= self.iter
.len();
2290 visitor
.visit_unit()
2292 let ret
= try
!(visitor
.visit_seq(&mut self));
2293 let remaining
= self.iter
.len();
2297 Err(de
::Error
::invalid_length(len
, &"fewer elements in array"))
2302 forward_to_deserialize_any
! {
2303 bool
i8 i16 i32 i64 i128
u8 u16 u32 u64 u128
f32 f64 char str string
2304 bytes byte_buf option unit unit_struct newtype_struct seq tuple
2305 tuple_struct map
struct enum identifier ignored_any
2309 impl<'de
, 'a
, E
> de
::SeqAccess
<'de
> for SeqRefDeserializer
<'a
, 'de
, E
>
2315 fn next_element_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
2317 T
: de
::DeserializeSeed
<'de
>,
2319 match self.iter
.next() {
2321 .deserialize(ContentRefDeserializer
::new(value
))
2327 fn size_hint(&self) -> Option
<usize> {
2328 size_hint
::from_bounds(&self.iter
)
2332 struct MapRefDeserializer
<'a
, 'de
: 'a
, E
>
2336 iter
: <&'a
[(Content
<'de
>, Content
<'de
>)] as IntoIterator
>::IntoIter
,
2337 value
: Option
<&'a Content
<'de
>>,
2338 err
: PhantomData
<E
>,
2341 impl<'a
, 'de
, E
> MapRefDeserializer
<'a
, 'de
, E
>
2345 fn new(map
: &'a
[(Content
<'de
>, Content
<'de
>)]) -> Self {
2346 MapRefDeserializer
{
2354 impl<'de
, 'a
, E
> de
::MapAccess
<'de
> for MapRefDeserializer
<'a
, 'de
, E
>
2360 fn next_key_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
2362 T
: de
::DeserializeSeed
<'de
>,
2364 match self.iter
.next() {
2365 Some(&(ref key
, ref value
)) => {
2366 self.value
= Some(value
);
2367 seed
.deserialize(ContentRefDeserializer
::new(key
)).map(Some
)
2373 fn next_value_seed
<T
>(&mut self, seed
: T
) -> Result
<T
::Value
, Self::Error
>
2375 T
: de
::DeserializeSeed
<'de
>,
2377 match self.value
.take() {
2378 Some(value
) => seed
.deserialize(ContentRefDeserializer
::new(value
)),
2379 None
=> Err(de
::Error
::custom("value is missing")),
2383 fn size_hint(&self) -> Option
<usize> {
2384 size_hint
::from_bounds(&self.iter
)
2388 impl<'de
, 'a
, E
> de
::Deserializer
<'de
> for MapRefDeserializer
<'a
, 'de
, E
>
2395 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2397 V
: de
::Visitor
<'de
>,
2399 visitor
.visit_map(self)
2402 forward_to_deserialize_any
! {
2403 bool
i8 i16 i32 i64 i128
u8 u16 u32 u64 u128
f32 f64 char str string
2404 bytes byte_buf option unit unit_struct newtype_struct seq tuple
2405 tuple_struct map
struct enum identifier ignored_any
2409 impl<'de
, E
> de
::IntoDeserializer
<'de
, E
> for ContentDeserializer
<'de
, E
>
2413 type Deserializer
= Self;
2415 fn into_deserializer(self) -> Self {
2420 impl<'de
, 'a
, E
> de
::IntoDeserializer
<'de
, E
> for ContentRefDeserializer
<'a
, 'de
, E
>
2424 type Deserializer
= Self;
2426 fn into_deserializer(self) -> Self {
2431 /// Visitor for deserializing an internally tagged unit variant.
2434 pub struct InternallyTaggedUnitVisitor
<'a
> {
2436 variant_name
: &'a
str,
2439 impl<'a
> InternallyTaggedUnitVisitor
<'a
> {
2441 pub fn new(type_name
: &'a
str, variant_name
: &'a
str) -> Self {
2442 InternallyTaggedUnitVisitor
{
2443 type_name
: type_name
,
2444 variant_name
: variant_name
,
2449 impl<'de
, 'a
> Visitor
<'de
> for InternallyTaggedUnitVisitor
<'a
> {
2452 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2455 "unit variant {}::{}",
2456 self.type_name
, self.variant_name
2460 fn visit_seq
<S
>(self, _
: S
) -> Result
<(), S
::Error
>
2467 fn visit_map
<M
>(self, mut access
: M
) -> Result
<(), M
::Error
>
2471 while let Some(_
) = try
!(access
.next_entry
::<IgnoredAny
, IgnoredAny
>()) {}
2476 /// Visitor for deserializing an untagged unit variant.
2479 pub struct UntaggedUnitVisitor
<'a
> {
2481 variant_name
: &'a
str,
2484 impl<'a
> UntaggedUnitVisitor
<'a
> {
2486 pub fn new(type_name
: &'a
str, variant_name
: &'a
str) -> Self {
2487 UntaggedUnitVisitor
{
2488 type_name
: type_name
,
2489 variant_name
: variant_name
,
2494 impl<'de
, 'a
> Visitor
<'de
> for UntaggedUnitVisitor
<'a
> {
2497 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2500 "unit variant {}::{}",
2501 self.type_name
, self.variant_name
2505 fn visit_unit
<E
>(self) -> Result
<(), E
>
2514 ////////////////////////////////////////////////////////////////////////////////
2516 // Like `IntoDeserializer` but also implemented for `&[u8]`. This is used for
2517 // the newtype fallthrough case of `field_identifier`.
2519 // #[derive(Deserialize)]
2520 // #[serde(field_identifier)]
2524 // Other(String), // deserialized using IdentifierDeserializer
2526 pub trait IdentifierDeserializer
<'de
, E
: Error
> {
2527 type Deserializer
: Deserializer
<'de
, Error
= E
>;
2529 fn from(self) -> Self::Deserializer
;
2532 impl<'de
, E
> IdentifierDeserializer
<'de
, E
> for u32
2536 type Deserializer
= <u32 as IntoDeserializer
<'de
, E
>>::Deserializer
;
2538 fn from(self) -> Self::Deserializer
{
2539 self.into_deserializer()
2543 pub struct StrDeserializer
<'a
, E
> {
2545 marker
: PhantomData
<E
>,
2548 impl<'a
, E
> IdentifierDeserializer
<'a
, E
> for &'a
str
2552 type Deserializer
= StrDeserializer
<'a
, E
>;
2554 fn from(self) -> Self::Deserializer
{
2557 marker
: PhantomData
,
2562 impl<'de
, 'a
, E
> Deserializer
<'de
> for StrDeserializer
<'a
, E
>
2568 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2572 visitor
.visit_str(self.value
)
2575 forward_to_deserialize_any
! {
2576 bool
i8 i16 i32 i64 i128
u8 u16 u32 u64 u128
f32 f64 char str string
2577 bytes byte_buf option unit unit_struct newtype_struct seq tuple
2578 tuple_struct map
struct enum identifier ignored_any
2582 pub struct BytesDeserializer
<'a
, E
> {
2584 marker
: PhantomData
<E
>,
2587 impl<'a
, E
> IdentifierDeserializer
<'a
, E
> for &'a
[u8]
2591 type Deserializer
= BytesDeserializer
<'a
, E
>;
2593 fn from(self) -> Self::Deserializer
{
2596 marker
: PhantomData
,
2601 impl<'de
, 'a
, E
> Deserializer
<'de
> for BytesDeserializer
<'a
, E
>
2607 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2611 visitor
.visit_bytes(self.value
)
2614 forward_to_deserialize_any
! {
2615 bool
i8 i16 i32 i64 i128
u8 u16 u32 u64 u128
f32 f64 char str string
2616 bytes byte_buf option unit unit_struct newtype_struct seq tuple
2617 tuple_struct map
struct enum identifier ignored_any
2621 /// A DeserializeSeed helper for implementing deserialize_in_place Visitors.
2623 /// Wraps a mutable reference and calls deserialize_in_place on it.
2624 pub struct InPlaceSeed
<'a
, T
: 'a
>(pub &'a
mut T
);
2626 impl<'a
, 'de
, T
> DeserializeSeed
<'de
> for InPlaceSeed
<'a
, T
>
2628 T
: Deserialize
<'de
>,
2631 fn deserialize
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
2633 D
: Deserializer
<'de
>,
2635 T
::deserialize_in_place(deserializer
, self.0)
2639 #[cfg(any(feature = "std", feature = "alloc"))]
2640 pub struct FlatMapDeserializer
<'a
, 'de
: 'a
, E
>(
2641 pub &'a
mut Vec
<Option
<(Content
<'de
>, Content
<'de
>)>>,
2645 #[cfg(any(feature = "std", feature = "alloc"))]
2646 impl<'a
, 'de
, E
> FlatMapDeserializer
<'a
, 'de
, E
>
2650 fn deserialize_other
<V
>() -> Result
<V
, E
> {
2651 Err(Error
::custom("can only flatten structs and maps"))
2655 #[cfg(any(feature = "std", feature = "alloc"))]
2656 macro_rules
! forward_to_deserialize_other
{
2657 ($
($func
:ident ( $
($arg
:ty
),* ))*) => {
2659 fn $func
<V
>(self, $
(_
: $arg
,)* _visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2663 Self::deserialize_other()
2669 #[cfg(any(feature = "std", feature = "alloc"))]
2670 impl<'a
, 'de
, E
> Deserializer
<'de
> for FlatMapDeserializer
<'a
, 'de
, E
>
2676 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2680 visitor
.visit_map(FlatInternallyTaggedAccess
{
2681 iter
: self.0.iter_mut
(),
2683 _marker
: PhantomData
,
2687 fn deserialize_enum
<V
>(
2690 variants
: &'
static [&'
static str],
2692 ) -> Result
<V
::Value
, Self::Error
>
2696 for item
in self.0.iter_mut
() {
2697 // items in the vector are nulled out when used. So we can only use
2698 // an item if it's still filled in and if the field is one we care
2700 let use_item
= match *item
{
2702 Some((ref c
, _
)) => c
.as_str().map_or(false, |x
| variants
.contains(&x
)),
2706 let (key
, value
) = item
.take().unwrap();
2707 return visitor
.visit_enum(EnumDeserializer
::new(key
, Some(value
)));
2711 Err(Error
::custom(format_args
!(
2712 "no variant of enum {} found in flattened data",
2717 fn deserialize_map
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2721 visitor
.visit_map(FlatMapAccess
::new(self.0.iter
()))
2724 fn deserialize_struct
<V
>(
2727 fields
: &'
static [&'
static str],
2729 ) -> Result
<V
::Value
, Self::Error
>
2733 visitor
.visit_map(FlatStructAccess
::new(self.0.iter_mut
(), fields
))
2736 fn deserialize_newtype_struct
<V
>(self, _name
: &str, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2740 visitor
.visit_newtype_struct(self)
2743 fn deserialize_option
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2747 match visitor
.__private_visit_untagged_option(self) {
2748 Ok(value
) => Ok(value
),
2749 Err(()) => Self::deserialize_other(),
2753 forward_to_deserialize_other
! {
2767 deserialize_string()
2769 deserialize_byte_buf()
2771 deserialize_unit_struct(&'
static str)
2773 deserialize_tuple(usize)
2774 deserialize_tuple_struct(&'
static str, usize)
2775 deserialize_identifier()
2776 deserialize_ignored_any()
2780 #[cfg(any(feature = "std", feature = "alloc"))]
2781 pub struct FlatMapAccess
<'a
, 'de
: 'a
, E
> {
2782 iter
: slice
::Iter
<'a
, Option
<(Content
<'de
>, Content
<'de
>)>>,
2783 pending_content
: Option
<&'a Content
<'de
>>,
2784 _marker
: PhantomData
<E
>,
2787 #[cfg(any(feature = "std", feature = "alloc"))]
2788 impl<'a
, 'de
, E
> FlatMapAccess
<'a
, 'de
, E
> {
2790 iter
: slice
::Iter
<'a
, Option
<(Content
<'de
>, Content
<'de
>)>>,
2791 ) -> FlatMapAccess
<'a
, 'de
, E
> {
2794 pending_content
: None
,
2795 _marker
: PhantomData
,
2800 #[cfg(any(feature = "std", feature = "alloc"))]
2801 impl<'a
, 'de
, E
> MapAccess
<'de
> for FlatMapAccess
<'a
, 'de
, E
>
2807 fn next_key_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
2809 T
: DeserializeSeed
<'de
>,
2811 while let Some(item
) = self.iter
.next() {
2812 // Items in the vector are nulled out when used by a struct.
2813 if let Some((ref key
, ref content
)) = *item
{
2814 self.pending_content
= Some(content
);
2815 return seed
.deserialize(ContentRefDeserializer
::new(key
)).map(Some
);
2821 fn next_value_seed
<T
>(&mut self, seed
: T
) -> Result
<T
::Value
, Self::Error
>
2823 T
: DeserializeSeed
<'de
>,
2825 match self.pending_content
.take() {
2826 Some(value
) => seed
.deserialize(ContentRefDeserializer
::new(value
)),
2827 None
=> Err(Error
::custom("value is missing")),
2832 #[cfg(any(feature = "std", feature = "alloc"))]
2833 pub struct FlatStructAccess
<'a
, 'de
: 'a
, E
> {
2834 iter
: slice
::IterMut
<'a
, Option
<(Content
<'de
>, Content
<'de
>)>>,
2835 pending_content
: Option
<Content
<'de
>>,
2836 fields
: &'
static [&'
static str],
2837 _marker
: PhantomData
<E
>,
2840 #[cfg(any(feature = "std", feature = "alloc"))]
2841 impl<'a
, 'de
, E
> FlatStructAccess
<'a
, 'de
, E
> {
2843 iter
: slice
::IterMut
<'a
, Option
<(Content
<'de
>, Content
<'de
>)>>,
2844 fields
: &'
static [&'
static str],
2845 ) -> FlatStructAccess
<'a
, 'de
, E
> {
2848 pending_content
: None
,
2850 _marker
: PhantomData
,
2855 #[cfg(any(feature = "std", feature = "alloc"))]
2856 impl<'a
, 'de
, E
> MapAccess
<'de
> for FlatStructAccess
<'a
, 'de
, E
>
2862 fn next_key_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
2864 T
: DeserializeSeed
<'de
>,
2866 while let Some(item
) = self.iter
.next() {
2867 // items in the vector are nulled out when used. So we can only use
2868 // an item if it's still filled in and if the field is one we care
2869 // about. In case we do not know which fields we want, we take them all.
2870 let use_item
= match *item
{
2872 Some((ref c
, _
)) => c
.as_str().map_or(false, |key
| self.fields
.contains(&key
)),
2876 let (key
, content
) = item
.take().unwrap();
2877 self.pending_content
= Some(content
);
2878 return seed
.deserialize(ContentDeserializer
::new(key
)).map(Some
);
2884 fn next_value_seed
<T
>(&mut self, seed
: T
) -> Result
<T
::Value
, Self::Error
>
2886 T
: DeserializeSeed
<'de
>,
2888 match self.pending_content
.take() {
2889 Some(value
) => seed
.deserialize(ContentDeserializer
::new(value
)),
2890 None
=> Err(Error
::custom("value is missing")),
2895 #[cfg(any(feature = "std", feature = "alloc"))]
2896 pub struct FlatInternallyTaggedAccess
<'a
, 'de
: 'a
, E
> {
2897 iter
: slice
::IterMut
<'a
, Option
<(Content
<'de
>, Content
<'de
>)>>,
2898 pending
: Option
<&'a Content
<'de
>>,
2899 _marker
: PhantomData
<E
>,
2902 #[cfg(any(feature = "std", feature = "alloc"))]
2903 impl<'a
, 'de
, E
> MapAccess
<'de
> for FlatInternallyTaggedAccess
<'a
, 'de
, E
>
2909 fn next_key_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
2911 T
: DeserializeSeed
<'de
>,
2913 while let Some(item
) = self.iter
.next() {
2914 if let Some((ref key
, ref content
)) = *item
{
2915 // Do not take(), instead borrow this entry. The internally tagged
2916 // enum does its own buffering so we can't tell whether this entry
2917 // is going to be consumed. Borrowing here leaves the entry
2918 // available for later flattened fields.
2919 self.pending
= Some(content
);
2920 return seed
.deserialize(ContentRefDeserializer
::new(key
)).map(Some
);
2926 fn next_value_seed
<T
>(&mut self, seed
: T
) -> Result
<T
::Value
, Self::Error
>
2928 T
: DeserializeSeed
<'de
>,
2930 match self.pending
.take() {
2931 Some(value
) => seed
.deserialize(ContentRefDeserializer
::new(value
)),
2932 None
=> panic
!("value is missing"),