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
!(u32, U32Deserializer
, visit_u32
);
187 primitive_deserializer
!(u64, U64Deserializer
, visit_u64
);
188 primitive_deserializer
!(usize, UsizeDeserializer
, visit_u64
as u64);
189 primitive_deserializer
!(f32, F32Deserializer
, visit_f32
);
190 primitive_deserializer
!(f64, F64Deserializer
, visit_f64
);
191 primitive_deserializer
!(char, CharDeserializer
, visit_char
);
193 ///////////////////////////////////////////////////////////////////////////////
195 /// A helper deserializer that deserializes a `&str`.
196 pub struct StrDeserializer
<'a
, E
> {
198 marker
: PhantomData
<E
>,
201 impl<'a
, E
> ValueDeserializer
<E
> for &'a
str
204 type Deserializer
= StrDeserializer
<'a
, E
>;
206 fn into_deserializer(self) -> StrDeserializer
<'a
, E
> {
214 impl<'a
, E
> de
::Deserializer
for StrDeserializer
<'a
, E
>
219 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
222 visitor
.visit_str(self.value
)
225 fn deserialize_enum
<V
>(self,
227 _variants
: &'
static [&'
static str],
229 -> Result
<V
::Value
, Self::Error
>
232 visitor
.visit_enum(self)
235 forward_to_deserialize
! {
236 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
237 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
238 struct struct_field tuple ignored_any byte_buf
242 impl<'a
, E
> de
::EnumVisitor
for StrDeserializer
<'a
, E
>
246 type Variant
= private
::UnitOnly
<E
>;
248 fn visit_variant_seed
<T
>(self, seed
: T
) -> Result
<(T
::Value
, Self::Variant
), Self::Error
>
249 where T
: de
::DeserializeSeed
251 seed
.deserialize(self).map(private
::unit_only
)
255 ///////////////////////////////////////////////////////////////////////////////
257 /// A helper deserializer that deserializes a `String`.
258 #[cfg(any(feature = "std", feature = "collections"))]
259 pub struct StringDeserializer
<E
> {
261 marker
: PhantomData
<E
>,
264 #[cfg(any(feature = "std", feature = "collections"))]
265 impl<E
> ValueDeserializer
<E
> for String
268 type Deserializer
= StringDeserializer
<E
>;
270 fn into_deserializer(self) -> StringDeserializer
<E
> {
278 #[cfg(any(feature = "std", feature = "collections"))]
279 impl<E
> de
::Deserializer
for StringDeserializer
<E
>
284 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
287 visitor
.visit_string(self.value
)
290 fn deserialize_enum
<V
>(self,
292 _variants
: &'
static [&'
static str],
294 -> Result
<V
::Value
, Self::Error
>
297 visitor
.visit_enum(self)
300 forward_to_deserialize
! {
301 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
302 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
303 struct struct_field tuple ignored_any byte_buf
307 #[cfg(any(feature = "std", feature = "collections"))]
308 impl<'a
, E
> de
::EnumVisitor
for StringDeserializer
<E
>
312 type Variant
= private
::UnitOnly
<E
>;
314 fn visit_variant_seed
<T
>(self, seed
: T
) -> Result
<(T
::Value
, Self::Variant
), Self::Error
>
315 where T
: de
::DeserializeSeed
317 seed
.deserialize(self).map(private
::unit_only
)
321 ///////////////////////////////////////////////////////////////////////////////
323 /// A helper deserializer that deserializes a `String`.
324 #[cfg(any(feature = "std", feature = "collections"))]
325 pub struct CowStrDeserializer
<'a
, E
> {
327 marker
: PhantomData
<E
>,
330 #[cfg(any(feature = "std", feature = "collections"))]
331 impl<'a
, E
> ValueDeserializer
<E
> for Cow
<'a
, str>
334 type Deserializer
= CowStrDeserializer
<'a
, E
>;
336 fn into_deserializer(self) -> CowStrDeserializer
<'a
, E
> {
344 #[cfg(any(feature = "std", feature = "collections"))]
345 impl<'a
, E
> de
::Deserializer
for CowStrDeserializer
<'a
, E
>
350 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
354 Cow
::Borrowed(string
) => visitor
.visit_str(string
),
355 Cow
::Owned(string
) => visitor
.visit_string(string
),
359 fn deserialize_enum
<V
>(self,
361 _variants
: &'
static [&'
static str],
363 -> Result
<V
::Value
, Self::Error
>
366 visitor
.visit_enum(self)
369 forward_to_deserialize
! {
370 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
371 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
372 struct struct_field tuple ignored_any byte_buf
376 #[cfg(any(feature = "std", feature = "collections"))]
377 impl<'a
, E
> de
::EnumVisitor
for CowStrDeserializer
<'a
, E
>
381 type Variant
= private
::UnitOnly
<E
>;
383 fn visit_variant_seed
<T
>(self, seed
: T
) -> Result
<(T
::Value
, Self::Variant
), Self::Error
>
384 where T
: de
::DeserializeSeed
386 seed
.deserialize(self).map(private
::unit_only
)
390 ///////////////////////////////////////////////////////////////////////////////
392 /// A helper deserializer that deserializes a sequence.
393 pub struct SeqDeserializer
<I
, E
> {
396 marker
: PhantomData
<E
>,
399 impl<I
, E
> SeqDeserializer
<I
, E
>
403 /// Construct a new `SeqDeserializer<I>`.
404 pub fn new(iter
: I
) -> Self {
412 /// Check for remaining elements after passing a `SeqDeserializer` to
413 /// `Visitor::visit_seq`.
414 pub fn end(mut self) -> Result
<(), E
> {
415 let mut remaining
= 0;
416 while self.iter
.next().is_some() {
422 // First argument is the number of elements in the data, second
423 // argument is the number of elements expected by the Deserialize.
424 Err(de
::Error
::invalid_length(self.count
+ remaining
, &ExpectedInSeq(self.count
)))
429 impl<I
, T
, E
> de
::Deserializer
for SeqDeserializer
<I
, E
>
430 where I
: Iterator
<Item
= T
>,
431 T
: ValueDeserializer
<E
>,
436 fn deserialize
<V
>(mut self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
439 let v
= try
!(visitor
.visit_seq(&mut self));
444 forward_to_deserialize
! {
445 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
446 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
447 struct struct_field tuple
enum ignored_any byte_buf
451 impl<I
, T
, E
> de
::SeqVisitor
for SeqDeserializer
<I
, E
>
452 where I
: Iterator
<Item
= T
>,
453 T
: ValueDeserializer
<E
>,
458 fn visit_seed
<V
>(&mut self, seed
: V
) -> Result
<Option
<V
::Value
>, Self::Error
>
459 where V
: de
::DeserializeSeed
461 match self.iter
.next() {
464 seed
.deserialize(value
.into_deserializer()).map(Some
)
470 fn size_hint(&self) -> (usize, Option
<usize>) {
471 self.iter
.size_hint()
475 struct ExpectedInSeq(usize);
477 impl Expected
for ExpectedInSeq
{
478 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
480 write
!(formatter
, "1 element in sequence")
482 write
!(formatter
, "{} elements in sequence", self.0)
487 ///////////////////////////////////////////////////////////////////////////////
489 #[cfg(any(feature = "std", feature = "collections"))]
490 impl<T
, E
> ValueDeserializer
<E
> for Vec
<T
>
491 where T
: ValueDeserializer
<E
>,
494 type Deserializer
= SeqDeserializer
<vec
::IntoIter
<T
>, E
>;
496 fn into_deserializer(self) -> Self::Deserializer
{
497 SeqDeserializer
::new(self.into_iter())
501 #[cfg(any(feature = "std", feature = "collections"))]
502 impl<T
, E
> ValueDeserializer
<E
> for BTreeSet
<T
>
503 where T
: ValueDeserializer
<E
> + Eq
+ Ord
,
506 type Deserializer
= SeqDeserializer
<btree_set
::IntoIter
<T
>, E
>;
508 fn into_deserializer(self) -> Self::Deserializer
{
509 SeqDeserializer
::new(self.into_iter())
513 #[cfg(feature = "std")]
514 impl<T
, E
> ValueDeserializer
<E
> for HashSet
<T
>
515 where T
: ValueDeserializer
<E
> + Eq
+ Hash
,
518 type Deserializer
= SeqDeserializer
<hash_set
::IntoIter
<T
>, E
>;
520 fn into_deserializer(self) -> Self::Deserializer
{
521 SeqDeserializer
::new(self.into_iter())
525 ///////////////////////////////////////////////////////////////////////////////
527 /// A helper deserializer that deserializes a sequence using a `SeqVisitor`.
528 pub struct SeqVisitorDeserializer
<V_
, E
> {
530 marker
: PhantomData
<E
>,
533 impl<V_
, E
> SeqVisitorDeserializer
<V_
, E
>
534 where V_
: de
::SeqVisitor
<Error
= E
>,
537 /// Construct a new `SeqVisitorDeserializer<V_, E>`.
538 pub fn new(visitor
: V_
) -> Self {
539 SeqVisitorDeserializer
{
546 impl<V_
, E
> de
::Deserializer
for SeqVisitorDeserializer
<V_
, E
>
547 where V_
: de
::SeqVisitor
<Error
= E
>,
552 fn deserialize
<V
: de
::Visitor
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
> {
553 visitor
.visit_seq(self.visitor
)
556 forward_to_deserialize
! {
557 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
558 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
559 struct struct_field tuple
enum ignored_any byte_buf
563 ///////////////////////////////////////////////////////////////////////////////
565 /// A helper deserializer that deserializes a map.
566 pub struct MapDeserializer
<I
, E
>
568 I
::Item
: private
::Pair
,
569 <I
::Item
as private
::Pair
>::First
: ValueDeserializer
<E
>,
570 <I
::Item
as private
::Pair
>::Second
: ValueDeserializer
<E
>,
574 value
: Option
<<I
::Item
as private
::Pair
>::Second
>,
576 marker
: PhantomData
<E
>,
579 impl<I
, E
> MapDeserializer
<I
, E
>
581 I
::Item
: private
::Pair
,
582 <I
::Item
as private
::Pair
>::First
: ValueDeserializer
<E
>,
583 <I
::Item
as private
::Pair
>::Second
: ValueDeserializer
<E
>,
586 /// Construct a new `MapDeserializer<I, K, V, E>`.
587 pub fn new(iter
: I
) -> Self {
596 /// Check for remaining elements after passing a `MapDeserializer` to
597 /// `Visitor::visit_map`.
598 pub fn end(mut self) -> Result
<(), E
> {
599 let mut remaining
= 0;
600 while self.iter
.next().is_some() {
606 // First argument is the number of elements in the data, second
607 // argument is the number of elements expected by the Deserialize.
608 Err(de
::Error
::invalid_length(self.count
+ remaining
, &ExpectedInMap(self.count
)))
614 -> Option
<(<I
::Item
as private
::Pair
>::First
, <I
::Item
as private
::Pair
>::Second
)> {
615 match self.iter
.next() {
618 Some(private
::Pair
::split(kv
))
625 impl<I
, E
> de
::Deserializer
for 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
>,
634 fn deserialize
<V_
>(mut self, visitor
: V_
) -> Result
<V_
::Value
, Self::Error
>
635 where V_
: de
::Visitor
637 let value
= try
!(visitor
.visit_map(&mut self));
642 fn deserialize_seq
<V_
>(mut self, visitor
: V_
) -> Result
<V_
::Value
, Self::Error
>
643 where V_
: de
::Visitor
645 let value
= try
!(visitor
.visit_seq(&mut self));
650 fn deserialize_seq_fixed_size
<V_
>(self,
653 -> Result
<V_
::Value
, Self::Error
>
654 where V_
: de
::Visitor
656 self.deserialize_seq(visitor
)
659 forward_to_deserialize
! {
660 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
661 bytes map unit_struct newtype_struct tuple_struct
struct struct_field
662 tuple
enum ignored_any byte_buf
666 impl<I
, E
> de
::MapVisitor
for MapDeserializer
<I
, E
>
668 I
::Item
: private
::Pair
,
669 <I
::Item
as private
::Pair
>::First
: ValueDeserializer
<E
>,
670 <I
::Item
as private
::Pair
>::Second
: ValueDeserializer
<E
>,
675 fn visit_key_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
676 where T
: de
::DeserializeSeed
678 match self.next_pair() {
679 Some((key
, value
)) => {
680 self.value
= Some(value
);
681 seed
.deserialize(key
.into_deserializer()).map(Some
)
687 fn visit_value_seed
<T
>(&mut self, seed
: T
) -> Result
<T
::Value
, Self::Error
>
688 where T
: de
::DeserializeSeed
690 let value
= self.value
.take();
691 // Panic because this indicates a bug in the program rather than an
693 let value
= value
.expect("MapVisitor::visit_value called before visit_key");
694 seed
.deserialize(value
.into_deserializer())
697 fn visit_seed
<TK
, TV
>(&mut self,
700 -> Result
<Option
<(TK
::Value
, TV
::Value
)>, Self::Error
>
701 where TK
: de
::DeserializeSeed
,
702 TV
: de
::DeserializeSeed
704 match self.next_pair() {
705 Some((key
, value
)) => {
706 let key
= try
!(kseed
.deserialize(key
.into_deserializer()));
707 let value
= try
!(vseed
.deserialize(value
.into_deserializer()));
708 Ok(Some((key
, value
)))
714 fn size_hint(&self) -> (usize, Option
<usize>) {
715 self.iter
.size_hint()
719 impl<I
, E
> de
::SeqVisitor
for MapDeserializer
<I
, E
>
721 I
::Item
: private
::Pair
,
722 <I
::Item
as private
::Pair
>::First
: ValueDeserializer
<E
>,
723 <I
::Item
as private
::Pair
>::Second
: ValueDeserializer
<E
>,
728 fn visit_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
729 where T
: de
::DeserializeSeed
731 match self.next_pair() {
733 let de
= PairDeserializer(k
, v
, PhantomData
);
734 seed
.deserialize(de
).map(Some
)
740 fn size_hint(&self) -> (usize, Option
<usize>) {
741 self.iter
.size_hint()
745 // Used in the `impl SeqVisitor for MapDeserializer` to visit the map as a
746 // sequence of pairs.
747 struct PairDeserializer
<A
, B
, E
>(A
, B
, PhantomData
<E
>);
749 impl<A
, B
, E
> de
::Deserializer
for PairDeserializer
<A
, B
, E
>
750 where A
: ValueDeserializer
<E
>,
751 B
: ValueDeserializer
<E
>,
756 forward_to_deserialize
! {
757 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
758 bytes map unit_struct newtype_struct tuple_struct
struct struct_field
759 tuple
enum ignored_any byte_buf
762 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
765 self.deserialize_seq(visitor
)
768 fn deserialize_seq
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
771 let mut pair_visitor
= PairVisitor(Some(self.0), Some(self.1), PhantomData
);
772 let pair
= try
!(visitor
.visit_seq(&mut pair_visitor
));
773 if pair_visitor
.1.is_none
() {
776 let remaining
= pair_visitor
.size_hint().0;
777 // First argument is the number of elements in the data, second
778 // argument is the number of elements expected by the Deserialize.
779 Err(de
::Error
::invalid_length(2, &ExpectedInSeq(2 - remaining
)))
783 fn deserialize_seq_fixed_size
<V
>(self, len
: usize, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
787 self.deserialize_seq(visitor
)
789 // First argument is the number of elements in the data, second
790 // argument is the number of elements expected by the Deserialize.
791 Err(de
::Error
::invalid_length(2, &ExpectedInSeq(len
)))
796 struct PairVisitor
<A
, B
, E
>(Option
<A
>, Option
<B
>, PhantomData
<E
>);
798 impl<A
, B
, E
> de
::SeqVisitor
for PairVisitor
<A
, B
, E
>
799 where A
: ValueDeserializer
<E
>,
800 B
: ValueDeserializer
<E
>,
805 fn visit_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
806 where T
: de
::DeserializeSeed
808 if let Some(k
) = self.0.take() {
809 seed
.deserialize(k
.into_deserializer()).map(Some
)
810 } else if let Some(v
) = self.1.take() {
811 seed
.deserialize(v
.into_deserializer()).map(Some
)
817 fn size_hint(&self) -> (usize, Option
<usize>) {
818 let len
= if self.0.is_some
() {
820 } else if self.1.is_some
() {
829 struct ExpectedInMap(usize);
831 impl Expected
for ExpectedInMap
{
832 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
834 write
!(formatter
, "1 element in map")
836 write
!(formatter
, "{} elements in map", self.0)
841 ///////////////////////////////////////////////////////////////////////////////
843 #[cfg(any(feature = "std", feature = "collections"))]
844 impl<K
, V
, E
> ValueDeserializer
<E
> for BTreeMap
<K
, V
>
845 where K
: ValueDeserializer
<E
> + Eq
+ Ord
,
846 V
: ValueDeserializer
<E
>,
849 type Deserializer
= MapDeserializer
<btree_map
::IntoIter
<K
, V
>, E
>;
851 fn into_deserializer(self) -> Self::Deserializer
{
852 MapDeserializer
::new(self.into_iter())
856 #[cfg(feature = "std")]
857 impl<K
, V
, E
> ValueDeserializer
<E
> for HashMap
<K
, V
>
858 where K
: ValueDeserializer
<E
> + Eq
+ Hash
,
859 V
: ValueDeserializer
<E
>,
862 type Deserializer
= MapDeserializer
<hash_map
::IntoIter
<K
, V
>, E
>;
864 fn into_deserializer(self) -> Self::Deserializer
{
865 MapDeserializer
::new(self.into_iter())
869 ///////////////////////////////////////////////////////////////////////////////
871 /// A helper deserializer that deserializes a map using a `MapVisitor`.
872 pub struct MapVisitorDeserializer
<V_
, E
> {
874 marker
: PhantomData
<E
>,
877 impl<V_
, E
> MapVisitorDeserializer
<V_
, E
>
878 where V_
: de
::MapVisitor
<Error
= E
>,
881 /// Construct a new `MapVisitorDeserializer<V_, E>`.
882 pub fn new(visitor
: V_
) -> Self {
883 MapVisitorDeserializer
{
890 impl<V_
, E
> de
::Deserializer
for MapVisitorDeserializer
<V_
, E
>
891 where V_
: de
::MapVisitor
<Error
= E
>,
896 fn deserialize
<V
: de
::Visitor
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
> {
897 visitor
.visit_map(self.visitor
)
900 forward_to_deserialize
! {
901 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
902 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
903 struct struct_field tuple
enum ignored_any byte_buf
907 ///////////////////////////////////////////////////////////////////////////////
909 impl<'a
, E
> ValueDeserializer
<E
> for bytes
::Bytes
<'a
>
912 type Deserializer
= BytesDeserializer
<'a
, E
>;
914 fn into_deserializer(self) -> BytesDeserializer
<'a
, E
> {
922 /// A helper deserializer that deserializes a `&[u8]`.
923 pub struct BytesDeserializer
<'a
, E
> {
925 marker
: PhantomData
<E
>,
928 impl<'a
, E
> de
::Deserializer
for BytesDeserializer
<'a
, E
>
933 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
936 visitor
.visit_bytes(self.value
)
939 forward_to_deserialize
! {
940 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
941 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
942 struct struct_field tuple
enum ignored_any byte_buf
946 ///////////////////////////////////////////////////////////////////////////////
948 #[cfg(any(feature = "std", feature = "collections"))]
949 impl<E
> ValueDeserializer
<E
> for bytes
::ByteBuf
952 type Deserializer
= ByteBufDeserializer
<E
>;
954 fn into_deserializer(self) -> Self::Deserializer
{
955 ByteBufDeserializer
{
962 /// A helper deserializer that deserializes a `Vec<u8>`.
963 #[cfg(any(feature = "std", feature = "collections"))]
964 pub struct ByteBufDeserializer
<E
> {
966 marker
: PhantomData
<E
>,
969 #[cfg(any(feature = "std", feature = "collections"))]
970 impl<E
> de
::Deserializer
for ByteBufDeserializer
<E
>
975 fn deserialize
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
978 visitor
.visit_byte_buf(self.value
)
981 forward_to_deserialize
! {
982 bool
u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
983 seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
984 struct struct_field tuple
enum ignored_any byte_buf
988 ///////////////////////////////////////////////////////////////////////////////
991 use de
::{self, Unexpected}
;
992 use core
::marker
::PhantomData
;
994 pub struct UnitOnly
<E
> {
995 marker
: PhantomData
<E
>,
998 pub fn unit_only
<T
, E
>(t
: T
) -> (T
, UnitOnly
<E
>) {
999 (t
, UnitOnly { marker: PhantomData }
)
1002 impl<E
> de
::VariantVisitor
for UnitOnly
<E
>
1007 fn visit_unit(self) -> Result
<(), Self::Error
> {
1011 fn visit_newtype_seed
<T
>(self, _seed
: T
) -> Result
<T
::Value
, Self::Error
>
1012 where T
: de
::DeserializeSeed
1014 Err(de
::Error
::invalid_type(Unexpected
::UnitVariant
, &"newtype variant"))
1017 fn visit_tuple
<V
>(self, _len
: usize, _visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1018 where V
: de
::Visitor
1020 Err(de
::Error
::invalid_type(Unexpected
::UnitVariant
, &"tuple variant"))
1023 fn visit_struct
<V
>(self,
1024 _fields
: &'
static [&'
static str],
1026 -> Result
<V
::Value
, Self::Error
>
1027 where V
: de
::Visitor
1029 Err(de
::Error
::invalid_type(Unexpected
::UnitVariant
, &"struct variant"))
1033 /// Avoid having to restate the generic types on `MapDeserializer`. The
1034 /// `Iterator::Item` contains enough information to figure out K and V.
1038 fn split(self) -> (Self::First
, Self::Second
);
1041 impl<A
, B
> Pair
for (A
, B
) {
1044 fn split(self) -> (A
, B
) {