1 //! This module supports deserializing from primitives with the `ValueDeserializer` trait.
3 #[cfg(feature = "std")]
4 use std
::collections
::{BTreeMap
, BTreeSet
, HashMap
, HashSet
, btree_map
, btree_set
, hash_map
,
6 #[cfg(feature = "std")]
8 #[cfg(feature = "std")]
11 #[cfg(all(feature = "collections", not(feature = "std")))]
12 use collections
::{BTreeMap, BTreeSet, Vec, String, btree_map, btree_set, vec}
;
13 #[cfg(all(feature = "collections", not(feature = "std")))]
14 use collections
::borrow
::Cow
;
15 #[cfg(all(feature = "collections", not(feature = "std")))]
16 use collections
::boxed
::Box
;
17 #[cfg(all(feature = "collections", not(feature = "std")))]
18 use collections
::string
::ToString
;
20 #[cfg(feature = "std")]
22 #[cfg(feature = "std")]
24 #[cfg(not(feature = "std"))]
27 use core
::fmt
::{self, Display}
;
28 use core
::iter
::{self, Iterator}
;
29 use core
::marker
::PhantomData
;
31 use de
::{self, Expected, SeqVisitor}
;
34 ///////////////////////////////////////////////////////////////////////////////
36 /// This represents all the possible errors that can occur using the `ValueDeserializer`.
37 #[derive(Clone, Debug, PartialEq)]
42 #[cfg(any(feature = "std", feature = "collections"))]
43 type ErrorImpl
= Box
<str>;
44 #[cfg(not(any(feature = "std", feature = "collections")))]
47 impl de
::Error
for Error
{
48 #[cfg(any(feature = "std", feature = "collections"))]
49 fn custom
<T
: Display
>(msg
: T
) -> Self {
50 Error { err: msg.to_string().into_boxed_str() }
53 #[cfg(not(any(feature = "std", feature = "collections")))]
54 fn custom
<T
: Display
>(_msg
: T
) -> Self {
59 impl Display
for Error
{
60 #[cfg(any(feature = "std", feature = "collections"))]
61 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> Result
<(), fmt
::Error
> {
62 formatter
.write_str(&self.err
)
65 #[cfg(not(any(feature = "std", feature = "collections")))]
66 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> Result
<(), fmt
::Error
> {
67 formatter
.write_str("Serde deserialization error")
71 impl error
::Error
for Error
{
72 #[cfg(any(feature = "std", feature = "collections"))]
73 fn description(&self) -> &str {
77 #[cfg(not(any(feature = "std", feature = "collections")))]
78 fn description(&self) -> &str {
79 "Serde deserialization error"
83 ///////////////////////////////////////////////////////////////////////////////
85 /// This trait converts primitive types into a deserializer.
86 pub trait ValueDeserializer
<E
: de
::Error
= Error
> {
87 /// The actual deserializer type.
88 type Deserializer
: de
::Deserializer
<Error
= E
>;
90 /// Convert this value into a deserializer.
91 fn into_deserializer(self) -> Self::Deserializer
;
94 ///////////////////////////////////////////////////////////////////////////////
96 impl<E
> ValueDeserializer
<E
> for ()
99 type Deserializer
= UnitDeserializer
<E
>;
101 fn into_deserializer(self) -> UnitDeserializer
<E
> {
102 UnitDeserializer { marker: PhantomData }
106 /// A helper deserializer that deserializes a `()`.
107 pub struct UnitDeserializer
<E
> {
108 marker
: PhantomData
<E
>,
111 impl<E
> de
::Deserializer
for UnitDeserializer
<E
>
116 forward_to_deserialize
! {
117 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq
118 seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
struct
119 struct_field tuple
enum ignored_any byte_buf
122 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
128 fn deserialize_option
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
135 ///////////////////////////////////////////////////////////////////////////////
137 macro_rules
! primitive_deserializer
{
138 ($ty
:ty
, $name
:ident
, $method
:ident $
($cast
:tt
)*) => {
139 /// A helper deserializer that deserializes a number.
140 pub struct $name
<E
> {
142 marker
: PhantomData
<E
>
145 impl<E
> ValueDeserializer
<E
> for $ty
148 type Deserializer
= $name
<E
>;
150 fn into_deserializer(self) -> $name
<E
> {
158 impl<E
> de
::Deserializer
for $name
<E
>
163 forward_to_deserialize
! {
164 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit
165 option seq seq_fixed_size bytes map unit_struct newtype_struct
166 tuple_struct
struct struct_field tuple
enum ignored_any byte_buf
169 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
170 where V
: de
::Visitor
,
172 visitor
.$
method(self.value $
($cast
)*)
178 primitive_deserializer
!(bool
, BoolDeserializer
, visit_bool
);
179 primitive_deserializer
!(i8, I8Deserializer
, visit_i8
);
180 primitive_deserializer
!(i16, I16Deserializer
, visit_i16
);
181 primitive_deserializer
!(i32, I32Deserializer
, visit_i32
);
182 primitive_deserializer
!(i64, I64Deserializer
, visit_i64
);
183 primitive_deserializer
!(isize, IsizeDeserializer
, visit_i64
as i64);
184 primitive_deserializer
!(u8, U8Deserializer
, visit_u8
);
185 primitive_deserializer
!(u16, U16Deserializer
, visit_u16
);
186 primitive_deserializer
!(u64, U64Deserializer
, visit_u64
);
187 primitive_deserializer
!(usize, UsizeDeserializer
, visit_u64
as u64);
188 primitive_deserializer
!(f32, F32Deserializer
, visit_f32
);
189 primitive_deserializer
!(f64, F64Deserializer
, visit_f64
);
190 primitive_deserializer
!(char, CharDeserializer
, visit_char
);
192 /// A helper deserializer that deserializes a number.
193 pub struct U32Deserializer
<E
> {
195 marker
: PhantomData
<E
>,
198 impl<E
> ValueDeserializer
<E
> for u32
201 type Deserializer
= U32Deserializer
<E
>;
203 fn into_deserializer(self) -> U32Deserializer
<E
> {
211 impl<E
> de
::Deserializer
for U32Deserializer
<E
>
216 forward_to_deserialize
! {
217 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
218 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
219 struct struct_field tuple ignored_any byte_buf
222 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
225 visitor
.visit_u32(self.value
)
228 fn deserialize_enum
<V
>(self,
230 _variants
: &'
static [&'
static str],
232 -> Result
<V
::Value
, Self::Error
>
235 visitor
.visit_enum(self)
239 impl<E
> de
::EnumVisitor
for U32Deserializer
<E
>
243 type Variant
= private
::UnitOnly
<E
>;
245 fn visit_variant_seed
<T
>(self, seed
: T
) -> Result
<(T
::Value
, Self::Variant
), Self::Error
>
246 where T
: de
::DeserializeSeed
248 seed
.deserialize(self).map(private
::unit_only
)
252 ///////////////////////////////////////////////////////////////////////////////
254 /// A helper deserializer that deserializes a `&str`.
255 pub struct StrDeserializer
<'a
, E
> {
257 marker
: PhantomData
<E
>,
260 impl<'a
, E
> ValueDeserializer
<E
> for &'a
str
263 type Deserializer
= StrDeserializer
<'a
, E
>;
265 fn into_deserializer(self) -> StrDeserializer
<'a
, E
> {
273 impl<'a
, E
> de
::Deserializer
for StrDeserializer
<'a
, E
>
278 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
281 visitor
.visit_str(self.value
)
284 fn deserialize_enum
<V
>(self,
286 _variants
: &'
static [&'
static str],
288 -> Result
<V
::Value
, Self::Error
>
291 visitor
.visit_enum(self)
294 forward_to_deserialize
! {
295 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
296 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
297 struct struct_field tuple ignored_any byte_buf
301 impl<'a
, E
> de
::EnumVisitor
for StrDeserializer
<'a
, E
>
305 type Variant
= private
::UnitOnly
<E
>;
307 fn visit_variant_seed
<T
>(self, seed
: T
) -> Result
<(T
::Value
, Self::Variant
), Self::Error
>
308 where T
: de
::DeserializeSeed
310 seed
.deserialize(self).map(private
::unit_only
)
314 ///////////////////////////////////////////////////////////////////////////////
316 /// A helper deserializer that deserializes a `String`.
317 #[cfg(any(feature = "std", feature = "collections"))]
318 pub struct StringDeserializer
<E
> {
320 marker
: PhantomData
<E
>,
323 #[cfg(any(feature = "std", feature = "collections"))]
324 impl<E
> ValueDeserializer
<E
> for String
327 type Deserializer
= StringDeserializer
<E
>;
329 fn into_deserializer(self) -> StringDeserializer
<E
> {
337 #[cfg(any(feature = "std", feature = "collections"))]
338 impl<E
> de
::Deserializer
for StringDeserializer
<E
>
343 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
346 visitor
.visit_string(self.value
)
349 fn deserialize_enum
<V
>(self,
351 _variants
: &'
static [&'
static str],
353 -> Result
<V
::Value
, Self::Error
>
356 visitor
.visit_enum(self)
359 forward_to_deserialize
! {
360 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
361 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
362 struct struct_field tuple ignored_any byte_buf
366 #[cfg(any(feature = "std", feature = "collections"))]
367 impl<'a
, E
> de
::EnumVisitor
for StringDeserializer
<E
>
371 type Variant
= private
::UnitOnly
<E
>;
373 fn visit_variant_seed
<T
>(self, seed
: T
) -> Result
<(T
::Value
, Self::Variant
), Self::Error
>
374 where T
: de
::DeserializeSeed
376 seed
.deserialize(self).map(private
::unit_only
)
380 ///////////////////////////////////////////////////////////////////////////////
382 /// A helper deserializer that deserializes a `String`.
383 #[cfg(any(feature = "std", feature = "collections"))]
384 pub struct CowStrDeserializer
<'a
, E
> {
386 marker
: PhantomData
<E
>,
389 #[cfg(any(feature = "std", feature = "collections"))]
390 impl<'a
, E
> ValueDeserializer
<E
> for Cow
<'a
, str>
393 type Deserializer
= CowStrDeserializer
<'a
, E
>;
395 fn into_deserializer(self) -> CowStrDeserializer
<'a
, E
> {
403 #[cfg(any(feature = "std", feature = "collections"))]
404 impl<'a
, E
> de
::Deserializer
for CowStrDeserializer
<'a
, E
>
409 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
413 Cow
::Borrowed(string
) => visitor
.visit_str(string
),
414 Cow
::Owned(string
) => visitor
.visit_string(string
),
418 fn deserialize_enum
<V
>(self,
420 _variants
: &'
static [&'
static str],
422 -> Result
<V
::Value
, Self::Error
>
425 visitor
.visit_enum(self)
428 forward_to_deserialize
! {
429 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
430 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
431 struct struct_field tuple ignored_any byte_buf
435 #[cfg(any(feature = "std", feature = "collections"))]
436 impl<'a
, E
> de
::EnumVisitor
for CowStrDeserializer
<'a
, E
>
440 type Variant
= private
::UnitOnly
<E
>;
442 fn visit_variant_seed
<T
>(self, seed
: T
) -> Result
<(T
::Value
, Self::Variant
), Self::Error
>
443 where T
: de
::DeserializeSeed
445 seed
.deserialize(self).map(private
::unit_only
)
449 ///////////////////////////////////////////////////////////////////////////////
451 /// A helper deserializer that deserializes a sequence.
452 pub struct SeqDeserializer
<I
, E
> {
455 marker
: PhantomData
<E
>,
458 impl<I
, E
> SeqDeserializer
<I
, E
>
462 /// Construct a new `SeqDeserializer<I>`.
463 pub fn new(iter
: I
) -> Self {
471 /// Check for remaining elements after passing a `SeqDeserializer` to
472 /// `Visitor::visit_seq`.
473 pub fn end(mut self) -> Result
<(), E
> {
474 let mut remaining
= 0;
475 while self.iter
.next().is_some() {
481 // First argument is the number of elements in the data, second
482 // argument is the number of elements expected by the Deserialize.
483 Err(de
::Error
::invalid_length(self.count
+ remaining
, &ExpectedInSeq(self.count
)))
488 impl<I
, T
, E
> de
::Deserializer
for SeqDeserializer
<I
, E
>
489 where I
: Iterator
<Item
= T
>,
490 T
: ValueDeserializer
<E
>,
495 fn deserialize
<V
>(mut self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
498 let v
= try
!(visitor
.visit_seq(&mut self));
503 forward_to_deserialize
! {
504 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
505 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
506 struct struct_field tuple
enum ignored_any byte_buf
510 impl<I
, T
, E
> de
::SeqVisitor
for SeqDeserializer
<I
, E
>
511 where I
: Iterator
<Item
= T
>,
512 T
: ValueDeserializer
<E
>,
517 fn visit_seed
<V
>(&mut self, seed
: V
) -> Result
<Option
<V
::Value
>, Self::Error
>
518 where V
: de
::DeserializeSeed
520 match self.iter
.next() {
523 seed
.deserialize(value
.into_deserializer()).map(Some
)
529 fn size_hint(&self) -> (usize, Option
<usize>) {
530 self.iter
.size_hint()
534 struct ExpectedInSeq(usize);
536 impl Expected
for ExpectedInSeq
{
537 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
539 write
!(formatter
, "1 element in sequence")
541 write
!(formatter
, "{} elements in sequence", self.0)
546 ///////////////////////////////////////////////////////////////////////////////
548 #[cfg(any(feature = "std", feature = "collections"))]
549 impl<T
, E
> ValueDeserializer
<E
> for Vec
<T
>
550 where T
: ValueDeserializer
<E
>,
553 type Deserializer
= SeqDeserializer
<vec
::IntoIter
<T
>, E
>;
555 fn into_deserializer(self) -> Self::Deserializer
{
556 SeqDeserializer
::new(self.into_iter())
560 #[cfg(any(feature = "std", feature = "collections"))]
561 impl<T
, E
> ValueDeserializer
<E
> for BTreeSet
<T
>
562 where T
: ValueDeserializer
<E
> + Eq
+ Ord
,
565 type Deserializer
= SeqDeserializer
<btree_set
::IntoIter
<T
>, E
>;
567 fn into_deserializer(self) -> Self::Deserializer
{
568 SeqDeserializer
::new(self.into_iter())
572 #[cfg(feature = "std")]
573 impl<T
, E
> ValueDeserializer
<E
> for HashSet
<T
>
574 where T
: ValueDeserializer
<E
> + Eq
+ Hash
,
577 type Deserializer
= SeqDeserializer
<hash_set
::IntoIter
<T
>, E
>;
579 fn into_deserializer(self) -> Self::Deserializer
{
580 SeqDeserializer
::new(self.into_iter())
584 ///////////////////////////////////////////////////////////////////////////////
586 /// A helper deserializer that deserializes a sequence using a `SeqVisitor`.
587 pub struct SeqVisitorDeserializer
<V_
, E
> {
589 marker
: PhantomData
<E
>,
592 impl<V_
, E
> SeqVisitorDeserializer
<V_
, E
>
593 where V_
: de
::SeqVisitor
<Error
= E
>,
596 /// Construct a new `SeqVisitorDeserializer<V_, E>`.
597 pub fn new(visitor
: V_
) -> Self {
598 SeqVisitorDeserializer
{
605 impl<V_
, E
> de
::Deserializer
for SeqVisitorDeserializer
<V_
, E
>
606 where V_
: de
::SeqVisitor
<Error
= E
>,
611 fn deserialize
<V
: de
::Visitor
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
> {
612 visitor
.visit_seq(self.visitor
)
615 forward_to_deserialize
! {
616 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
617 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
618 struct struct_field tuple
enum ignored_any byte_buf
622 ///////////////////////////////////////////////////////////////////////////////
624 /// A helper deserializer that deserializes a map.
625 pub struct MapDeserializer
<I
, E
>
627 I
::Item
: private
::Pair
,
628 <I
::Item
as private
::Pair
>::First
: ValueDeserializer
<E
>,
629 <I
::Item
as private
::Pair
>::Second
: ValueDeserializer
<E
>,
633 value
: Option
<<I
::Item
as private
::Pair
>::Second
>,
635 marker
: PhantomData
<E
>,
638 impl<I
, E
> MapDeserializer
<I
, E
>
640 I
::Item
: private
::Pair
,
641 <I
::Item
as private
::Pair
>::First
: ValueDeserializer
<E
>,
642 <I
::Item
as private
::Pair
>::Second
: ValueDeserializer
<E
>,
645 /// Construct a new `MapDeserializer<I, K, V, E>`.
646 pub fn new(iter
: I
) -> Self {
655 /// Check for remaining elements after passing a `MapDeserializer` to
656 /// `Visitor::visit_map`.
657 pub fn end(mut self) -> Result
<(), E
> {
658 let mut remaining
= 0;
659 while self.iter
.next().is_some() {
665 // First argument is the number of elements in the data, second
666 // argument is the number of elements expected by the Deserialize.
667 Err(de
::Error
::invalid_length(self.count
+ remaining
, &ExpectedInMap(self.count
)))
673 -> Option
<(<I
::Item
as private
::Pair
>::First
, <I
::Item
as private
::Pair
>::Second
)> {
674 match self.iter
.next() {
677 Some(private
::Pair
::split(kv
))
684 impl<I
, E
> de
::Deserializer
for MapDeserializer
<I
, E
>
686 I
::Item
: private
::Pair
,
687 <I
::Item
as private
::Pair
>::First
: ValueDeserializer
<E
>,
688 <I
::Item
as private
::Pair
>::Second
: ValueDeserializer
<E
>,
693 fn deserialize
<V_
>(mut self, visitor
: V_
) -> Result
<V_
::Value
, Self::Error
>
694 where V_
: de
::Visitor
696 let value
= try
!(visitor
.visit_map(&mut self));
701 fn deserialize_seq
<V_
>(mut self, visitor
: V_
) -> Result
<V_
::Value
, Self::Error
>
702 where V_
: de
::Visitor
704 let value
= try
!(visitor
.visit_seq(&mut self));
709 fn deserialize_seq_fixed_size
<V_
>(self,
712 -> Result
<V_
::Value
, Self::Error
>
713 where V_
: de
::Visitor
715 self.deserialize_seq(visitor
)
718 forward_to_deserialize
! {
719 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
720 bytes map unit_struct newtype_struct tuple_struct
struct struct_field
721 tuple
enum ignored_any byte_buf
725 impl<I
, E
> de
::MapVisitor
for MapDeserializer
<I
, E
>
727 I
::Item
: private
::Pair
,
728 <I
::Item
as private
::Pair
>::First
: ValueDeserializer
<E
>,
729 <I
::Item
as private
::Pair
>::Second
: ValueDeserializer
<E
>,
734 fn visit_key_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
735 where T
: de
::DeserializeSeed
737 match self.next_pair() {
738 Some((key
, value
)) => {
739 self.value
= Some(value
);
740 seed
.deserialize(key
.into_deserializer()).map(Some
)
746 fn visit_value_seed
<T
>(&mut self, seed
: T
) -> Result
<T
::Value
, Self::Error
>
747 where T
: de
::DeserializeSeed
749 let value
= self.value
.take();
750 // Panic because this indicates a bug in the program rather than an
752 let value
= value
.expect("MapVisitor::visit_value called before visit_key");
753 seed
.deserialize(value
.into_deserializer())
756 fn visit_seed
<TK
, TV
>(&mut self,
759 -> Result
<Option
<(TK
::Value
, TV
::Value
)>, Self::Error
>
760 where TK
: de
::DeserializeSeed
,
761 TV
: de
::DeserializeSeed
763 match self.next_pair() {
764 Some((key
, value
)) => {
765 let key
= try
!(kseed
.deserialize(key
.into_deserializer()));
766 let value
= try
!(vseed
.deserialize(value
.into_deserializer()));
767 Ok(Some((key
, value
)))
773 fn size_hint(&self) -> (usize, Option
<usize>) {
774 self.iter
.size_hint()
778 impl<I
, E
> de
::SeqVisitor
for MapDeserializer
<I
, E
>
780 I
::Item
: private
::Pair
,
781 <I
::Item
as private
::Pair
>::First
: ValueDeserializer
<E
>,
782 <I
::Item
as private
::Pair
>::Second
: ValueDeserializer
<E
>,
787 fn visit_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
788 where T
: de
::DeserializeSeed
790 match self.next_pair() {
792 let de
= PairDeserializer(k
, v
, PhantomData
);
793 seed
.deserialize(de
).map(Some
)
799 fn size_hint(&self) -> (usize, Option
<usize>) {
800 self.iter
.size_hint()
804 // Used in the `impl SeqVisitor for MapDeserializer` to visit the map as a
805 // sequence of pairs.
806 struct PairDeserializer
<A
, B
, E
>(A
, B
, PhantomData
<E
>);
808 impl<A
, B
, E
> de
::Deserializer
for PairDeserializer
<A
, B
, E
>
809 where A
: ValueDeserializer
<E
>,
810 B
: ValueDeserializer
<E
>,
815 forward_to_deserialize
! {
816 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
817 bytes map unit_struct newtype_struct tuple_struct
struct struct_field
818 tuple
enum ignored_any byte_buf
821 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
824 self.deserialize_seq(visitor
)
827 fn deserialize_seq
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
830 let mut pair_visitor
= PairVisitor(Some(self.0), Some(self.1), PhantomData
);
831 let pair
= try
!(visitor
.visit_seq(&mut pair_visitor
));
832 if pair_visitor
.1.is_none
() {
835 let remaining
= pair_visitor
.size_hint().0;
836 // First argument is the number of elements in the data, second
837 // argument is the number of elements expected by the Deserialize.
838 Err(de
::Error
::invalid_length(2, &ExpectedInSeq(2 - remaining
)))
842 fn deserialize_seq_fixed_size
<V
>(self, len
: usize, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
846 self.deserialize_seq(visitor
)
848 // First argument is the number of elements in the data, second
849 // argument is the number of elements expected by the Deserialize.
850 Err(de
::Error
::invalid_length(2, &ExpectedInSeq(len
)))
855 struct PairVisitor
<A
, B
, E
>(Option
<A
>, Option
<B
>, PhantomData
<E
>);
857 impl<A
, B
, E
> de
::SeqVisitor
for PairVisitor
<A
, B
, E
>
858 where A
: ValueDeserializer
<E
>,
859 B
: ValueDeserializer
<E
>,
864 fn visit_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
865 where T
: de
::DeserializeSeed
867 if let Some(k
) = self.0.take() {
868 seed
.deserialize(k
.into_deserializer()).map(Some
)
869 } else if let Some(v
) = self.1.take() {
870 seed
.deserialize(v
.into_deserializer()).map(Some
)
876 fn size_hint(&self) -> (usize, Option
<usize>) {
877 let len
= if self.0.is_some
() {
879 } else if self.1.is_some
() {
888 struct ExpectedInMap(usize);
890 impl Expected
for ExpectedInMap
{
891 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
893 write
!(formatter
, "1 element in map")
895 write
!(formatter
, "{} elements in map", self.0)
900 ///////////////////////////////////////////////////////////////////////////////
902 #[cfg(any(feature = "std", feature = "collections"))]
903 impl<K
, V
, E
> ValueDeserializer
<E
> for BTreeMap
<K
, V
>
904 where K
: ValueDeserializer
<E
> + Eq
+ Ord
,
905 V
: ValueDeserializer
<E
>,
908 type Deserializer
= MapDeserializer
<btree_map
::IntoIter
<K
, V
>, E
>;
910 fn into_deserializer(self) -> Self::Deserializer
{
911 MapDeserializer
::new(self.into_iter())
915 #[cfg(feature = "std")]
916 impl<K
, V
, E
> ValueDeserializer
<E
> for HashMap
<K
, V
>
917 where K
: ValueDeserializer
<E
> + Eq
+ Hash
,
918 V
: ValueDeserializer
<E
>,
921 type Deserializer
= MapDeserializer
<hash_map
::IntoIter
<K
, V
>, E
>;
923 fn into_deserializer(self) -> Self::Deserializer
{
924 MapDeserializer
::new(self.into_iter())
928 ///////////////////////////////////////////////////////////////////////////////
930 /// A helper deserializer that deserializes a map using a `MapVisitor`.
931 pub struct MapVisitorDeserializer
<V_
, E
> {
933 marker
: PhantomData
<E
>,
936 impl<V_
, E
> MapVisitorDeserializer
<V_
, E
>
937 where V_
: de
::MapVisitor
<Error
= E
>,
940 /// Construct a new `MapVisitorDeserializer<V_, E>`.
941 pub fn new(visitor
: V_
) -> Self {
942 MapVisitorDeserializer
{
949 impl<V_
, E
> de
::Deserializer
for MapVisitorDeserializer
<V_
, E
>
950 where V_
: de
::MapVisitor
<Error
= E
>,
955 fn deserialize
<V
: de
::Visitor
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
> {
956 visitor
.visit_map(self.visitor
)
959 forward_to_deserialize
! {
960 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
961 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
962 struct struct_field tuple
enum ignored_any byte_buf
966 ///////////////////////////////////////////////////////////////////////////////
968 impl<'a
, E
> ValueDeserializer
<E
> for bytes
::Bytes
<'a
>
971 type Deserializer
= BytesDeserializer
<'a
, E
>;
973 fn into_deserializer(self) -> BytesDeserializer
<'a
, E
> {
981 /// A helper deserializer that deserializes a `&[u8]`.
982 pub struct BytesDeserializer
<'a
, E
> {
984 marker
: PhantomData
<E
>,
987 impl<'a
, E
> de
::Deserializer
for BytesDeserializer
<'a
, E
>
992 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
995 visitor
.visit_bytes(self.value
)
998 forward_to_deserialize
! {
999 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
1000 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
1001 struct struct_field tuple
enum ignored_any byte_buf
1005 ///////////////////////////////////////////////////////////////////////////////
1007 #[cfg(any(feature = "std", feature = "collections"))]
1008 impl<E
> ValueDeserializer
<E
> for bytes
::ByteBuf
1011 type Deserializer
= ByteBufDeserializer
<E
>;
1013 fn into_deserializer(self) -> Self::Deserializer
{
1014 ByteBufDeserializer
{
1016 marker
: PhantomData
,
1021 /// A helper deserializer that deserializes a `Vec<u8>`.
1022 #[cfg(any(feature = "std", feature = "collections"))]
1023 pub struct ByteBufDeserializer
<E
> {
1025 marker
: PhantomData
<E
>,
1028 #[cfg(any(feature = "std", feature = "collections"))]
1029 impl<E
> de
::Deserializer
for ByteBufDeserializer
<E
>
1034 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1035 where V
: de
::Visitor
1037 visitor
.visit_byte_buf(self.value
)
1040 forward_to_deserialize
! {
1041 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
1042 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
1043 struct struct_field tuple
enum ignored_any byte_buf
1047 ///////////////////////////////////////////////////////////////////////////////
1050 use de
::{self, Unexpected}
;
1051 use core
::marker
::PhantomData
;
1053 pub struct UnitOnly
<E
> {
1054 marker
: PhantomData
<E
>,
1057 pub fn unit_only
<T
, E
>(t
: T
) -> (T
, UnitOnly
<E
>) {
1058 (t
, UnitOnly { marker: PhantomData }
)
1061 impl<E
> de
::VariantVisitor
for UnitOnly
<E
>
1066 fn visit_unit(self) -> Result
<(), Self::Error
> {
1070 fn visit_newtype_seed
<T
>(self, _seed
: T
) -> Result
<T
::Value
, Self::Error
>
1071 where T
: de
::DeserializeSeed
1073 Err(de
::Error
::invalid_type(Unexpected
::UnitVariant
, &"newtype variant"))
1076 fn visit_tuple
<V
>(self, _len
: usize, _visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1077 where V
: de
::Visitor
1079 Err(de
::Error
::invalid_type(Unexpected
::UnitVariant
, &"tuple variant"))
1082 fn visit_struct
<V
>(self,
1083 _fields
: &'
static [&'
static str],
1085 -> Result
<V
::Value
, Self::Error
>
1086 where V
: de
::Visitor
1088 Err(de
::Error
::invalid_type(Unexpected
::UnitVariant
, &"struct variant"))
1092 /// Avoid having to restate the generic types on `MapDeserializer`. The
1093 /// `Iterator::Item` contains enough information to figure out K and V.
1097 fn split(self) -> (Self::First
, Self::Second
);
1100 impl<A
, B
> Pair
for (A
, B
) {
1103 fn split(self) -> (A
, B
) {