4 Deserialize
, Deserializer
, EnumAccess
, Error
, SeqAccess
, Unexpected
, VariantAccess
, Visitor
,
7 #[cfg(any(core_duration, feature = "std", feature = "alloc"))]
10 use seed
::InPlaceSeed
;
12 #[cfg(any(feature = "std", feature = "alloc"))]
13 use __private
::size_hint
;
15 ////////////////////////////////////////////////////////////////////////////////
19 impl<'de
> Visitor
<'de
> for UnitVisitor
{
22 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
23 formatter
.write_str("unit")
26 fn visit_unit
<E
>(self) -> Result
<Self::Value
, E
>
34 impl<'de
> Deserialize
<'de
> for () {
35 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
39 deserializer
.deserialize_unit(UnitVisitor
)
43 #[cfg(feature = "unstable")]
44 impl<'de
> Deserialize
<'de
> for ! {
45 fn deserialize
<D
>(_deserializer
: D
) -> Result
<Self, D
::Error
>
49 Err(Error
::custom("cannot deserialize `!`"))
53 ////////////////////////////////////////////////////////////////////////////////
57 impl<'de
> Visitor
<'de
> for BoolVisitor
{
60 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
61 formatter
.write_str("a boolean")
64 fn visit_bool
<E
>(self, v
: bool
) -> Result
<Self::Value
, E
>
72 impl<'de
> Deserialize
<'de
> for bool
{
73 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
77 deserializer
.deserialize_bool(BoolVisitor
)
81 ////////////////////////////////////////////////////////////////////////////////
83 macro_rules
! impl_deserialize_num
{
84 ($ty
:ident
, $deserialize
:ident $
($methods
:tt
)*) => {
85 impl<'de
> Deserialize
<'de
> for $ty
{
87 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
91 struct PrimitiveVisitor
;
93 impl<'de
> Visitor
<'de
> for PrimitiveVisitor
{
96 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
97 formatter
.write_str(stringify
!($ty
))
103 deserializer
.$
deserialize(PrimitiveVisitor
)
109 macro_rules
! num_self
{
110 ($ty
:ident
: $visit
:ident
) => {
112 fn $visit
<E
>(self, v
: $ty
) -> Result
<Self::Value
, E
>
121 macro_rules
! num_as_self
{
122 ($
($ty
:ident
: $visit
:ident
)*) => {
125 fn $visit
<E
>(self, v
: $ty
) -> Result
<Self::Value
, E
>
135 macro_rules
! int_to_int
{
136 ($
($ty
:ident
: $visit
:ident
)*) => {
139 fn $visit
<E
>(self, v
: $ty
) -> Result
<Self::Value
, E
>
143 if Self::Value
::min_value() as i64 <= v
as i64 && v
as i64 <= Self::Value
::max_value() as i64 {
146 Err(Error
::invalid_value(Unexpected
::Signed(v
as i64), &self))
153 macro_rules
! int_to_uint
{
154 ($
($ty
:ident
: $visit
:ident
)*) => {
157 fn $visit
<E
>(self, v
: $ty
) -> Result
<Self::Value
, E
>
161 if 0 <= v
&& v
as u64 <= Self::Value
::max_value() as u64 {
164 Err(Error
::invalid_value(Unexpected
::Signed(v
as i64), &self))
171 macro_rules
! uint_to_self
{
172 ($
($ty
:ident
: $visit
:ident
)*) => {
175 fn $visit
<E
>(self, v
: $ty
) -> Result
<Self::Value
, E
>
179 if v
as u64 <= Self::Value
::max_value() as u64 {
182 Err(Error
::invalid_value(Unexpected
::Unsigned(v
as u64), &self))
189 impl_deserialize_num
! {
191 num_self
!(i8:visit_i8
);
192 int_to_int
!(i16:visit_i16
i32:visit_i32
i64:visit_i64
);
193 uint_to_self
!(u8:visit_u8
u16:visit_u16
u32:visit_u32
u64:visit_u64
);
196 impl_deserialize_num
! {
198 num_self
!(i16:visit_i16
);
199 num_as_self
!(i8:visit_i8
);
200 int_to_int
!(i32:visit_i32
i64:visit_i64
);
201 uint_to_self
!(u8:visit_u8
u16:visit_u16
u32:visit_u32
u64:visit_u64
);
204 impl_deserialize_num
! {
206 num_self
!(i32:visit_i32
);
207 num_as_self
!(i8:visit_i8
i16:visit_i16
);
208 int_to_int
!(i64:visit_i64
);
209 uint_to_self
!(u8:visit_u8
u16:visit_u16
u32:visit_u32
u64:visit_u64
);
212 impl_deserialize_num
! {
214 num_self
!(i64:visit_i64
);
215 num_as_self
!(i8:visit_i8
i16:visit_i16
i32:visit_i32
);
216 uint_to_self
!(u8:visit_u8
u16:visit_u16
u32:visit_u32
u64:visit_u64
);
219 impl_deserialize_num
! {
220 isize, deserialize_i64
221 num_as_self
!(i8:visit_i8
i16:visit_i16
);
222 int_to_int
!(i32:visit_i32
i64:visit_i64
);
223 uint_to_self
!(u8:visit_u8
u16:visit_u16
u32:visit_u32
u64:visit_u64
);
226 impl_deserialize_num
! {
228 num_self
!(u8:visit_u8
);
229 int_to_uint
!(i8:visit_i8
i16:visit_i16
i32:visit_i32
i64:visit_i64
);
230 uint_to_self
!(u16:visit_u16
u32:visit_u32
u64:visit_u64
);
233 impl_deserialize_num
! {
235 num_self
!(u16:visit_u16
);
236 num_as_self
!(u8:visit_u8
);
237 int_to_uint
!(i8:visit_i8
i16:visit_i16
i32:visit_i32
i64:visit_i64
);
238 uint_to_self
!(u32:visit_u32
u64:visit_u64
);
241 impl_deserialize_num
! {
243 num_self
!(u32:visit_u32
);
244 num_as_self
!(u8:visit_u8
u16:visit_u16
);
245 int_to_uint
!(i8:visit_i8
i16:visit_i16
i32:visit_i32
i64:visit_i64
);
246 uint_to_self
!(u64:visit_u64
);
249 impl_deserialize_num
! {
251 num_self
!(u64:visit_u64
);
252 num_as_self
!(u8:visit_u8
u16:visit_u16
u32:visit_u32
);
253 int_to_uint
!(i8:visit_i8
i16:visit_i16
i32:visit_i32
i64:visit_i64
);
256 impl_deserialize_num
! {
257 usize, deserialize_u64
258 num_as_self
!(u8:visit_u8
u16:visit_u16
);
259 int_to_uint
!(i8:visit_i8
i16:visit_i16
i32:visit_i32
i64:visit_i64
);
260 uint_to_self
!(u32:visit_u32
u64:visit_u64
);
263 impl_deserialize_num
! {
265 num_self
!(f32:visit_f32
);
266 num_as_self
!(f64:visit_f64
);
267 num_as_self
!(i8:visit_i8
i16:visit_i16
i32:visit_i32
i64:visit_i64
);
268 num_as_self
!(u8:visit_u8
u16:visit_u16
u32:visit_u32
u64:visit_u64
);
271 impl_deserialize_num
! {
273 num_self
!(f64:visit_f64
);
274 num_as_self
!(f32:visit_f32
);
275 num_as_self
!(i8:visit_i8
i16:visit_i16
i32:visit_i32
i64:visit_i64
);
276 num_as_self
!(u8:visit_u8
u16:visit_u16
u32:visit_u32
u64:visit_u64
);
279 serde_if_integer128
! {
280 impl_deserialize_num
! {
281 i128
, deserialize_i128
282 num_self
!(i128
:visit_i128
);
283 num_as_self
!(i8:visit_i8
i16:visit_i16
i32:visit_i32
i64:visit_i64
);
284 num_as_self
!(u8:visit_u8
u16:visit_u16
u32:visit_u32
u64:visit_u64
);
287 fn visit_u128
<E
>(self, v
: u128
) -> Result
<Self::Value
, E
>
291 if v
<= i128
::max_value() as u128
{
294 Err(Error
::invalid_value(Unexpected
::Other("u128"), &self))
299 impl_deserialize_num
! {
300 u128
, deserialize_u128
301 num_self
!(u128
:visit_u128
);
302 num_as_self
!(u8:visit_u8
u16:visit_u16
u32:visit_u32
u64:visit_u64
);
303 int_to_uint
!(i8:visit_i8
i16:visit_i16
i32:visit_i32
i64:visit_i64
);
306 fn visit_i128
<E
>(self, v
: i128
) -> Result
<Self::Value
, E
>
313 Err(Error
::invalid_value(Unexpected
::Other("i128"), &self))
319 ////////////////////////////////////////////////////////////////////////////////
323 impl<'de
> Visitor
<'de
> for CharVisitor
{
326 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
327 formatter
.write_str("a character")
331 fn visit_char
<E
>(self, v
: char) -> Result
<Self::Value
, E
>
339 fn visit_str
<E
>(self, v
: &str) -> Result
<Self::Value
, E
>
343 let mut iter
= v
.chars();
344 match (iter
.next(), iter
.next()) {
345 (Some(c
), None
) => Ok(c
),
346 _
=> Err(Error
::invalid_value(Unexpected
::Str(v
), &self)),
351 impl<'de
> Deserialize
<'de
> for char {
353 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
355 D
: Deserializer
<'de
>,
357 deserializer
.deserialize_char(CharVisitor
)
361 ////////////////////////////////////////////////////////////////////////////////
363 #[cfg(any(feature = "std", feature = "alloc"))]
364 struct StringVisitor
;
365 #[cfg(any(feature = "std", feature = "alloc"))]
366 struct StringInPlaceVisitor
<'a
>(&'a
mut String
);
368 #[cfg(any(feature = "std", feature = "alloc"))]
369 impl<'de
> Visitor
<'de
> for StringVisitor
{
372 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
373 formatter
.write_str("a string")
376 fn visit_str
<E
>(self, v
: &str) -> Result
<Self::Value
, E
>
383 fn visit_string
<E
>(self, v
: String
) -> Result
<Self::Value
, E
>
390 fn visit_bytes
<E
>(self, v
: &[u8]) -> Result
<Self::Value
, E
>
394 match str::from_utf8(v
) {
395 Ok(s
) => Ok(s
.to_owned()),
396 Err(_
) => Err(Error
::invalid_value(Unexpected
::Bytes(v
), &self)),
400 fn visit_byte_buf
<E
>(self, v
: Vec
<u8>) -> Result
<Self::Value
, E
>
404 match String
::from_utf8(v
) {
406 Err(e
) => Err(Error
::invalid_value(
407 Unexpected
::Bytes(&e
.into_bytes()),
414 #[cfg(any(feature = "std", feature = "alloc"))]
415 impl<'a
, 'de
> Visitor
<'de
> for StringInPlaceVisitor
<'a
> {
418 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
419 formatter
.write_str("a string")
422 fn visit_str
<E
>(self, v
: &str) -> Result
<Self::Value
, E
>
431 fn visit_string
<E
>(self, v
: String
) -> Result
<Self::Value
, E
>
439 fn visit_bytes
<E
>(self, v
: &[u8]) -> Result
<Self::Value
, E
>
443 match str::from_utf8(v
) {
449 Err(_
) => Err(Error
::invalid_value(Unexpected
::Bytes(v
), &self)),
453 fn visit_byte_buf
<E
>(self, v
: Vec
<u8>) -> Result
<Self::Value
, E
>
457 match String
::from_utf8(v
) {
462 Err(e
) => Err(Error
::invalid_value(
463 Unexpected
::Bytes(&e
.into_bytes()),
470 #[cfg(any(feature = "std", feature = "alloc"))]
471 impl<'de
> Deserialize
<'de
> for String
{
472 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
474 D
: Deserializer
<'de
>,
476 deserializer
.deserialize_string(StringVisitor
)
479 fn deserialize_in_place
<D
>(deserializer
: D
, place
: &mut Self) -> Result
<(), D
::Error
>
481 D
: Deserializer
<'de
>,
483 deserializer
.deserialize_string(StringInPlaceVisitor(place
))
487 ////////////////////////////////////////////////////////////////////////////////
491 impl<'a
> Visitor
<'a
> for StrVisitor
{
492 type Value
= &'a
str;
494 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
495 formatter
.write_str("a borrowed string")
498 fn visit_borrowed_str
<E
>(self, v
: &'a
str) -> Result
<Self::Value
, E
>
505 fn visit_borrowed_bytes
<E
>(self, v
: &'a
[u8]) -> Result
<Self::Value
, E
>
509 str::from_utf8(v
).map_err(|_
| Error
::invalid_value(Unexpected
::Bytes(v
), &self))
513 impl<'de
: 'a
, 'a
> Deserialize
<'de
> for &'a
str {
514 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
516 D
: Deserializer
<'de
>,
518 deserializer
.deserialize_str(StrVisitor
)
522 ////////////////////////////////////////////////////////////////////////////////
526 impl<'a
> Visitor
<'a
> for BytesVisitor
{
527 type Value
= &'a
[u8];
529 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
530 formatter
.write_str("a borrowed byte array")
533 fn visit_borrowed_bytes
<E
>(self, v
: &'a
[u8]) -> Result
<Self::Value
, E
>
540 fn visit_borrowed_str
<E
>(self, v
: &'a
str) -> Result
<Self::Value
, E
>
548 impl<'de
: 'a
, 'a
> Deserialize
<'de
> for &'a
[u8] {
549 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
551 D
: Deserializer
<'de
>,
553 deserializer
.deserialize_bytes(BytesVisitor
)
557 ////////////////////////////////////////////////////////////////////////////////
559 #[cfg(feature = "std")]
560 struct CStringVisitor
;
562 #[cfg(feature = "std")]
563 impl<'de
> Visitor
<'de
> for CStringVisitor
{
564 type Value
= CString
;
566 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
567 formatter
.write_str("byte array")
570 fn visit_seq
<A
>(self, mut seq
: A
) -> Result
<Self::Value
, A
::Error
>
574 let len
= size_hint
::cautious(seq
.size_hint());
575 let mut values
= Vec
::with_capacity(len
);
577 while let Some(value
) = try
!(seq
.next_element()) {
581 CString
::new(values
).map_err(Error
::custom
)
584 fn visit_bytes
<E
>(self, v
: &[u8]) -> Result
<Self::Value
, E
>
588 CString
::new(v
).map_err(Error
::custom
)
591 fn visit_byte_buf
<E
>(self, v
: Vec
<u8>) -> Result
<Self::Value
, E
>
595 CString
::new(v
).map_err(Error
::custom
)
598 fn visit_str
<E
>(self, v
: &str) -> Result
<Self::Value
, E
>
602 CString
::new(v
).map_err(Error
::custom
)
605 fn visit_string
<E
>(self, v
: String
) -> Result
<Self::Value
, E
>
609 CString
::new(v
).map_err(Error
::custom
)
613 #[cfg(feature = "std")]
614 impl<'de
> Deserialize
<'de
> for CString
{
615 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
617 D
: Deserializer
<'de
>,
619 deserializer
.deserialize_byte_buf(CStringVisitor
)
623 macro_rules
! forwarded_impl
{
626 ( $
($id
: ident
),* ), $ty
: ty
, $func
: expr
629 impl<'de $
(, $id
: Deserialize
<'de
>,)*> Deserialize
<'de
> for $ty
{
630 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
632 D
: Deserializer
<'de
>,
634 Deserialize
::deserialize(deserializer
).map($func
)
640 #[cfg(all(feature = "std", de_boxed_c_str))]
641 forwarded_impl
!((), Box
<CStr
>, CString
::into_boxed_c_str
);
644 forwarded_impl
!((T
), Reverse
<T
>, Reverse
);
646 ////////////////////////////////////////////////////////////////////////////////
648 struct OptionVisitor
<T
> {
649 marker
: PhantomData
<T
>,
652 impl<'de
, T
> Visitor
<'de
> for OptionVisitor
<T
>
656 type Value
= Option
<T
>;
658 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
659 formatter
.write_str("option")
663 fn visit_unit
<E
>(self) -> Result
<Self::Value
, E
>
671 fn visit_none
<E
>(self) -> Result
<Self::Value
, E
>
679 fn visit_some
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
681 D
: Deserializer
<'de
>,
683 T
::deserialize(deserializer
).map(Some
)
687 fn __private_visit_untagged_option
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, ()>
689 D
: Deserializer
<'de
>,
691 Ok(T
::deserialize(deserializer
).ok())
695 impl<'de
, T
> Deserialize
<'de
> for Option
<T
>
699 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
701 D
: Deserializer
<'de
>,
703 deserializer
.deserialize_option(OptionVisitor
{
708 // The Some variant's repr is opaque, so we can't play cute tricks with its
709 // tag to have deserialize_in_place build the content in place unconditionally.
711 // FIXME: investigate whether branching on the old value being Some to
712 // deserialize_in_place the value is profitable (probably data-dependent?)
715 ////////////////////////////////////////////////////////////////////////////////
717 struct PhantomDataVisitor
<T
: ?Sized
> {
718 marker
: PhantomData
<T
>,
721 impl<'de
, T
: ?Sized
> Visitor
<'de
> for PhantomDataVisitor
<T
> {
722 type Value
= PhantomData
<T
>;
724 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
725 formatter
.write_str("unit")
729 fn visit_unit
<E
>(self) -> Result
<Self::Value
, E
>
737 impl<'de
, T
: ?Sized
> Deserialize
<'de
> for PhantomData
<T
> {
738 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
740 D
: Deserializer
<'de
>,
742 let visitor
= PhantomDataVisitor
{
745 deserializer
.deserialize_unit_struct("PhantomData", visitor
)
749 ////////////////////////////////////////////////////////////////////////////////
751 #[cfg(any(feature = "std", feature = "alloc"))]
752 macro_rules
! seq_impl
{
754 $ty
:ident
< T $
(: $tbound1
:ident $
(+ $tbound2
:ident
)*)* $
(, $typaram
:ident
: $bound1
:ident $
(+ $bound2
:ident
)*)* >,
761 impl<'de
, T $
(, $typaram
)*> Deserialize
<'de
> for $ty
<T $
(, $typaram
)*>
763 T
: Deserialize
<'de
> $
(+ $tbound1 $
(+ $tbound2
)*)*,
764 $
($typaram
: $bound1 $
(+ $bound2
)*,)*
766 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
768 D
: Deserializer
<'de
>,
770 struct SeqVisitor
<T $
(, $typaram
)*> {
771 marker
: PhantomData
<$ty
<T $
(, $typaram
)*>>,
774 impl<'de
, T $
(, $typaram
)*> Visitor
<'de
> for SeqVisitor
<T $
(, $typaram
)*>
776 T
: Deserialize
<'de
> $
(+ $tbound1 $
(+ $tbound2
)*)*,
777 $
($typaram
: $bound1 $
(+ $bound2
)*,)*
779 type Value
= $ty
<T $
(, $typaram
)*>;
781 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
782 formatter
.write_str("a sequence")
786 fn visit_seq
<A
>(self, mut $access
: A
) -> Result
<Self::Value
, A
::Error
>
790 let mut values
= $with_capacity
;
792 while let Some(value
) = try
!($access
.next_element()) {
793 $
insert(&mut values
, value
);
800 let visitor
= SeqVisitor { marker: PhantomData }
;
801 deserializer
.deserialize_seq(visitor
)
804 fn deserialize_in_place
<D
>(deserializer
: D
, place
: &mut Self) -> Result
<(), D
::Error
>
806 D
: Deserializer
<'de
>,
808 struct SeqInPlaceVisitor
<'a
, T
: 'a $
(, $typaram
: 'a
)*>(&'a
mut $ty
<T $
(, $typaram
)*>);
810 impl<'a
, 'de
, T $
(, $typaram
)*> Visitor
<'de
> for SeqInPlaceVisitor
<'a
, T $
(, $typaram
)*>
812 T
: Deserialize
<'de
> $
(+ $tbound1 $
(+ $tbound2
)*)*,
813 $
($typaram
: $bound1 $
(+ $bound2
)*,)*
817 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
818 formatter
.write_str("a sequence")
822 fn visit_seq
<A
>(mut self, mut $access
: A
) -> Result
<Self::Value
, A
::Error
>
827 $
reserve(&mut self.0, size_hint
::cautious($access
.size_hint()));
829 // FIXME: try to overwrite old values here? (Vec, VecDeque, LinkedList)
830 while let Some(value
) = try
!($access
.next_element()) {
831 $
insert(&mut self.0, value
);
838 deserializer
.deserialize_seq(SeqInPlaceVisitor(place
))
844 // Dummy impl of reserve
845 #[cfg(any(feature = "std", feature = "alloc"))]
846 fn nop_reserve
<T
>(_seq
: T
, _n
: usize) {}
848 #[cfg(any(feature = "std", feature = "alloc"))]
853 BinaryHeap
::with_capacity(size_hint
::cautious(seq
.size_hint())),
858 #[cfg(any(feature = "std", feature = "alloc"))]
860 BTreeSet
<T
: Eq
+ Ord
>,
868 #[cfg(any(feature = "std", feature = "alloc"))]
875 LinkedList
::push_back
878 #[cfg(feature = "std")]
880 HashSet
<T
: Eq
+ Hash
, S
: BuildHasher
+ Default
>,
883 HashSet
::with_capacity_and_hasher(size_hint
::cautious(seq
.size_hint()), S
::default()),
887 #[cfg(any(feature = "std", feature = "alloc"))]
892 VecDeque
::with_capacity(size_hint
::cautious(seq
.size_hint())),
897 ////////////////////////////////////////////////////////////////////////////////
899 #[cfg(any(feature = "std", feature = "alloc"))]
900 impl<'de
, T
> Deserialize
<'de
> for Vec
<T
>
904 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
906 D
: Deserializer
<'de
>,
908 struct VecVisitor
<T
> {
909 marker
: PhantomData
<T
>,
912 impl<'de
, T
> Visitor
<'de
> for VecVisitor
<T
>
918 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
919 formatter
.write_str("a sequence")
922 fn visit_seq
<A
>(self, mut seq
: A
) -> Result
<Self::Value
, A
::Error
>
926 let mut values
= Vec
::with_capacity(size_hint
::cautious(seq
.size_hint()));
928 while let Some(value
) = try
!(seq
.next_element()) {
936 let visitor
= VecVisitor
{
939 deserializer
.deserialize_seq(visitor
)
942 fn deserialize_in_place
<D
>(deserializer
: D
, place
: &mut Self) -> Result
<(), D
::Error
>
944 D
: Deserializer
<'de
>,
946 struct VecInPlaceVisitor
<'a
, T
: 'a
>(&'a
mut Vec
<T
>);
948 impl<'a
, 'de
, T
> Visitor
<'de
> for VecInPlaceVisitor
<'a
, T
>
954 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
955 formatter
.write_str("a sequence")
958 fn visit_seq
<A
>(self, mut seq
: A
) -> Result
<Self::Value
, A
::Error
>
962 let hint
= size_hint
::cautious(seq
.size_hint());
963 if let Some(additional
) = hint
.checked_sub(self.0.len()) {
964 self.0.reserve(additional
);
967 for i
in 0..self.0.len() {
969 let next_place
= InPlaceSeed(&mut self.0[i
]);
970 try
!(seq
.next_element_seed(next_place
))
978 while let Some(value
) = try
!(seq
.next_element()) {
986 deserializer
.deserialize_seq(VecInPlaceVisitor(place
))
990 ////////////////////////////////////////////////////////////////////////////////
992 struct ArrayVisitor
<A
> {
993 marker
: PhantomData
<A
>,
995 struct ArrayInPlaceVisitor
<'a
, A
: 'a
>(&'a
mut A
);
997 impl<A
> ArrayVisitor
<A
> {
1000 marker
: PhantomData
,
1005 impl<'de
, T
> Visitor
<'de
> for ArrayVisitor
<[T
; 0]> {
1006 type Value
= [T
; 0];
1008 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1009 formatter
.write_str("an empty array")
1013 fn visit_seq
<A
>(self, _
: A
) -> Result
<Self::Value
, A
::Error
>
1021 // Does not require T: Deserialize<'de>.
1022 impl<'de
, T
> Deserialize
<'de
> for [T
; 0] {
1023 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1025 D
: Deserializer
<'de
>,
1027 deserializer
.deserialize_tuple(0, ArrayVisitor
::<[T
; 0]>::new())
1031 macro_rules
! array_impls
{
1032 ($
($len
:expr
=> ($
($n
:tt
)+))+) => {
1034 impl<'de
, T
> Visitor
<'de
> for ArrayVisitor
<[T
; $len
]>
1036 T
: Deserialize
<'de
>,
1038 type Value
= [T
; $len
];
1040 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1041 formatter
.write_str(concat
!("an array of length ", $len
))
1045 fn visit_seq
<A
>(self, mut seq
: A
) -> Result
<Self::Value
, A
::Error
>
1050 match try
!(seq
.next_element()) {
1052 None
=> return Err(Error
::invalid_length($n
, &self)),
1058 impl<'a
, 'de
, T
> Visitor
<'de
> for ArrayInPlaceVisitor
<'a
, [T
; $len
]>
1060 T
: Deserialize
<'de
>,
1064 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1065 formatter
.write_str(concat
!("an array of length ", $len
))
1069 fn visit_seq
<A
>(self, mut seq
: A
) -> Result
<Self::Value
, A
::Error
>
1073 let mut fail_idx
= None
;
1074 for (idx
, dest
) in self.0[..].iter_mut().enumerate() {
1075 if try
!(seq
.next_element_seed(InPlaceSeed(dest
))).is_none() {
1076 fail_idx
= Some(idx
);
1080 if let Some(idx
) = fail_idx
{
1081 return Err(Error
::invalid_length(idx
, &self));
1087 impl<'de
, T
> Deserialize
<'de
> for [T
; $len
]
1089 T
: Deserialize
<'de
>,
1091 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1093 D
: Deserializer
<'de
>,
1095 deserializer
.deserialize_tuple($len
, ArrayVisitor
::<[T
; $len
]>::new())
1098 fn deserialize_in_place
<D
>(deserializer
: D
, place
: &mut Self) -> Result
<(), D
::Error
>
1100 D
: Deserializer
<'de
>,
1102 deserializer
.deserialize_tuple($len
, ArrayInPlaceVisitor(place
))
1116 7 => (0 1 2 3 4 5 6)
1117 8 => (0 1 2 3 4 5 6 7)
1118 9 => (0 1 2 3 4 5 6 7 8)
1119 10 => (0 1 2 3 4 5 6 7 8 9)
1120 11 => (0 1 2 3 4 5 6 7 8 9 10)
1121 12 => (0 1 2 3 4 5 6 7 8 9 10 11)
1122 13 => (0 1 2 3 4 5 6 7 8 9 10 11 12)
1123 14 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13)
1124 15 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14)
1125 16 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
1126 17 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)
1127 18 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17)
1128 19 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18)
1129 20 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)
1130 21 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
1131 22 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21)
1132 23 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22)
1133 24 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23)
1134 25 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24)
1135 26 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25)
1136 27 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26)
1137 28 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27)
1138 29 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28)
1139 30 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29)
1140 31 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30)
1141 32 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31)
1144 ////////////////////////////////////////////////////////////////////////////////
1146 macro_rules
! tuple_impls
{
1147 ($
($len
:tt
=> ($
($n
:tt $name
:ident
)+))+) => {
1149 impl<'de
, $
($name
: Deserialize
<'de
>),+> Deserialize
<'de
> for ($
($name
,)+) {
1151 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1153 D
: Deserializer
<'de
>,
1155 struct TupleVisitor
<$
($name
,)+> {
1156 marker
: PhantomData
<($
($name
,)+)>,
1159 impl<'de
, $
($name
: Deserialize
<'de
>),+> Visitor
<'de
> for TupleVisitor
<$
($name
,)+> {
1160 type Value
= ($
($name
,)+);
1162 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1163 formatter
.write_str(concat
!("a tuple of size ", $len
))
1167 #[allow(non_snake_case)]
1168 fn visit_seq
<A
>(self, mut seq
: A
) -> Result
<Self::Value
, A
::Error
>
1173 let $name
= match try
!(seq
.next_element()) {
1174 Some(value
) => value
,
1175 None
=> return Err(Error
::invalid_length($n
, &self)),
1183 deserializer
.deserialize_tuple($len
, TupleVisitor { marker: PhantomData }
)
1187 fn deserialize_in_place
<D
>(deserializer
: D
, place
: &mut Self) -> Result
<(), D
::Error
>
1189 D
: Deserializer
<'de
>,
1191 struct TupleInPlaceVisitor
<'a
, $
($name
: 'a
,)+>(&'a
mut ($
($name
,)+));
1193 impl<'a
, 'de
, $
($name
: Deserialize
<'de
>),+> Visitor
<'de
> for TupleInPlaceVisitor
<'a
, $
($name
,)+> {
1196 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1197 formatter
.write_str(concat
!("a tuple of size ", $len
))
1201 #[allow(non_snake_case)]
1202 fn visit_seq
<A
>(self, mut seq
: A
) -> Result
<Self::Value
, A
::Error
>
1207 if try
!(seq
.next_element_seed(InPlaceSeed(&mut (self.0).$n
))).is_none() {
1208 return Err(Error
::invalid_length($n
, &self));
1216 deserializer
.deserialize_tuple($len
, TupleInPlaceVisitor(place
))
1226 3 => (0 T0
1 T1
2 T2
)
1227 4 => (0 T0
1 T1
2 T2
3 T3
)
1228 5 => (0 T0
1 T1
2 T2
3 T3
4 T4
)
1229 6 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
)
1230 7 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
6 T6
)
1231 8 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
6 T6
7 T7
)
1232 9 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
6 T6
7 T7
8 T8
)
1233 10 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
6 T6
7 T7
8 T8
9 T9
)
1234 11 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
6 T6
7 T7
8 T8
9 T9
10 T10
)
1235 12 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
6 T6
7 T7
8 T8
9 T9
10 T10
11 T11
)
1236 13 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
6 T6
7 T7
8 T8
9 T9
10 T10
11 T11
12 T12
)
1237 14 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
6 T6
7 T7
8 T8
9 T9
10 T10
11 T11
12 T12
13 T13
)
1238 15 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
6 T6
7 T7
8 T8
9 T9
10 T10
11 T11
12 T12
13 T13
14 T14
)
1239 16 => (0 T0
1 T1
2 T2
3 T3
4 T4
5 T5
6 T6
7 T7
8 T8
9 T9
10 T10
11 T11
12 T12
13 T13
14 T14
15 T15
)
1242 ////////////////////////////////////////////////////////////////////////////////
1244 #[cfg(any(feature = "std", feature = "alloc"))]
1245 macro_rules
! map_impl
{
1247 $ty
:ident
< K $
(: $kbound1
:ident $
(+ $kbound2
:ident
)*)*, V $
(, $typaram
:ident
: $bound1
:ident $
(+ $bound2
:ident
)*)* >,
1251 impl<'de
, K
, V $
(, $typaram
)*> Deserialize
<'de
> for $ty
<K
, V $
(, $typaram
)*>
1253 K
: Deserialize
<'de
> $
(+ $kbound1 $
(+ $kbound2
)*)*,
1254 V
: Deserialize
<'de
>,
1255 $
($typaram
: $bound1 $
(+ $bound2
)*),*
1257 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1259 D
: Deserializer
<'de
>,
1261 struct MapVisitor
<K
, V $
(, $typaram
)*> {
1262 marker
: PhantomData
<$ty
<K
, V $
(, $typaram
)*>>,
1265 impl<'de
, K
, V $
(, $typaram
)*> Visitor
<'de
> for MapVisitor
<K
, V $
(, $typaram
)*>
1267 K
: Deserialize
<'de
> $
(+ $kbound1 $
(+ $kbound2
)*)*,
1268 V
: Deserialize
<'de
>,
1269 $
($typaram
: $bound1 $
(+ $bound2
)*),*
1271 type Value
= $ty
<K
, V $
(, $typaram
)*>;
1273 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1274 formatter
.write_str("a map")
1278 fn visit_map
<A
>(self, mut $access
: A
) -> Result
<Self::Value
, A
::Error
>
1282 let mut values
= $with_capacity
;
1284 while let Some((key
, value
)) = try
!($access
.next_entry()) {
1285 values
.insert(key
, value
);
1292 let visitor
= MapVisitor { marker: PhantomData }
;
1293 deserializer
.deserialize_map(visitor
)
1299 #[cfg(any(feature = "std", feature = "alloc"))]
1301 BTreeMap
<K
: Ord
, V
>,
1305 #[cfg(feature = "std")]
1307 HashMap
<K
: Eq
+ Hash
, V
, S
: BuildHasher
+ Default
>,
1309 HashMap
::with_capacity_and_hasher(size_hint
::cautious(map
.size_hint()), S
::default()));
1311 ////////////////////////////////////////////////////////////////////////////////
1313 #[cfg(feature = "std")]
1314 macro_rules
! parse_ip_impl
{
1315 ($expecting
:tt $ty
:ty
; $size
:tt
) => {
1316 impl<'de
> Deserialize
<'de
> for $ty
{
1317 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1319 D
: Deserializer
<'de
>,
1321 if deserializer
.is_human_readable() {
1322 deserializer
.deserialize_str(FromStrVisitor
::new($expecting
))
1324 <[u8; $size
]>::deserialize(deserializer
).map(<$ty
>::from
)
1331 #[cfg(feature = "std")]
1332 macro_rules
! variant_identifier
{
1334 $name_kind
: ident ( $
($variant
: ident
; $bytes
: expr
; $index
: expr
),* )
1335 $expecting_message
: expr
,
1336 $variants_name
: ident
1342 static $variants_name
: &'
static [&'
static str] = &[ $
( stringify
!($variant
) ),*];
1344 impl<'de
> Deserialize
<'de
> for $name_kind
{
1345 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1347 D
: Deserializer
<'de
>,
1351 impl<'de
> Visitor
<'de
> for KindVisitor
{
1352 type Value
= $name_kind
;
1354 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1355 formatter
.write_str($expecting_message
)
1358 fn visit_u64
<E
>(self, value
: u64) -> Result
<Self::Value
, E
>
1364 $index
=> Ok($name_kind
:: $variant
),
1366 _
=> Err(Error
::invalid_value(Unexpected
::Unsigned(value
), &self),),
1370 fn visit_str
<E
>(self, value
: &str) -> Result
<Self::Value
, E
>
1376 stringify
!($variant
) => Ok($name_kind
:: $variant
),
1378 _
=> Err(Error
::unknown_variant(value
, $variants_name
)),
1382 fn visit_bytes
<E
>(self, value
: &[u8]) -> Result
<Self::Value
, E
>
1388 $bytes
=> Ok($name_kind
:: $variant
),
1391 match str::from_utf8(value
) {
1392 Ok(value
) => Err(Error
::unknown_variant(value
, $variants_name
)),
1393 Err(_
) => Err(Error
::invalid_value(Unexpected
::Bytes(value
), &self)),
1400 deserializer
.deserialize_identifier(KindVisitor
)
1406 #[cfg(feature = "std")]
1407 macro_rules
! deserialize_enum
{
1409 $name
: ident $name_kind
: ident ( $
($variant
: ident
; $bytes
: expr
; $index
: expr
),* )
1410 $expecting_message
: expr
,
1413 variant_identifier
!{
1414 $
name_kind ( $
($variant
; $bytes
; $index
),* )
1420 impl<'de
> Visitor
<'de
> for EnumVisitor
{
1423 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1424 formatter
.write_str(concat
!("a ", stringify
!($name
)))
1428 fn visit_enum
<A
>(self, data
: A
) -> Result
<Self::Value
, A
::Error
>
1432 match try
!(data
.variant()) {
1434 ($name_kind
:: $variant
, v
) => v
.newtype_variant().map($name
:: $variant
),
1439 $deserializer
.deserialize_enum(stringify
!($name
), VARIANTS
, EnumVisitor
)
1443 #[cfg(feature = "std")]
1444 impl<'de
> Deserialize
<'de
> for net
::IpAddr
{
1445 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1447 D
: Deserializer
<'de
>,
1449 if deserializer
.is_human_readable() {
1450 deserializer
.deserialize_str(FromStrVisitor
::new("IP address"))
1452 use lib
::net
::IpAddr
;
1454 IpAddr
IpAddrKind (V4
; b
"V4"; 0, V6
; b
"V6"; 1)
1462 #[cfg(feature = "std")]
1463 parse_ip_impl
!("IPv4 address" net
::Ipv4Addr
; 4);
1465 #[cfg(feature = "std")]
1466 parse_ip_impl
!("IPv6 address" net
::Ipv6Addr
; 16);
1468 #[cfg(feature = "std")]
1469 macro_rules
! parse_socket_impl
{
1470 ($expecting
:tt $ty
:ty
, $new
:expr
) => {
1471 impl<'de
> Deserialize
<'de
> for $ty
{
1472 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1474 D
: Deserializer
<'de
>,
1476 if deserializer
.is_human_readable() {
1477 deserializer
.deserialize_str(FromStrVisitor
::new($expecting
))
1479 <(_
, u16)>::deserialize(deserializer
).map(|(ip
, port
)| $
new(ip
, port
))
1486 #[cfg(feature = "std")]
1487 impl<'de
> Deserialize
<'de
> for net
::SocketAddr
{
1488 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1490 D
: Deserializer
<'de
>,
1492 if deserializer
.is_human_readable() {
1493 deserializer
.deserialize_str(FromStrVisitor
::new("socket address"))
1495 use lib
::net
::SocketAddr
;
1497 SocketAddr
SocketAddrKind (V4
; b
"V4"; 0, V6
; b
"V6"; 1)
1505 #[cfg(feature = "std")]
1506 parse_socket_impl
!("IPv4 socket address" net
::SocketAddrV4
, net
::SocketAddrV4
::new
);
1508 #[cfg(feature = "std")]
1509 parse_socket_impl
!("IPv6 socket address" net
::SocketAddrV6
, |ip
, port
| net
::SocketAddrV6
::new(
1513 ////////////////////////////////////////////////////////////////////////////////
1515 #[cfg(feature = "std")]
1518 #[cfg(feature = "std")]
1519 impl<'a
> Visitor
<'a
> for PathVisitor
{
1520 type Value
= &'a Path
;
1522 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1523 formatter
.write_str("a borrowed path")
1526 fn visit_borrowed_str
<E
>(self, v
: &'a
str) -> Result
<Self::Value
, E
>
1533 fn visit_borrowed_bytes
<E
>(self, v
: &'a
[u8]) -> Result
<Self::Value
, E
>
1539 .map_err(|_
| Error
::invalid_value(Unexpected
::Bytes(v
), &self))
1543 #[cfg(feature = "std")]
1544 impl<'de
: 'a
, 'a
> Deserialize
<'de
> for &'a Path
{
1545 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1547 D
: Deserializer
<'de
>,
1549 deserializer
.deserialize_str(PathVisitor
)
1553 #[cfg(feature = "std")]
1554 struct PathBufVisitor
;
1556 #[cfg(feature = "std")]
1557 impl<'de
> Visitor
<'de
> for PathBufVisitor
{
1558 type Value
= PathBuf
;
1560 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1561 formatter
.write_str("path string")
1564 fn visit_str
<E
>(self, v
: &str) -> Result
<Self::Value
, E
>
1571 fn visit_string
<E
>(self, v
: String
) -> Result
<Self::Value
, E
>
1578 fn visit_bytes
<E
>(self, v
: &[u8]) -> Result
<Self::Value
, E
>
1584 .map_err(|_
| Error
::invalid_value(Unexpected
::Bytes(v
), &self))
1587 fn visit_byte_buf
<E
>(self, v
: Vec
<u8>) -> Result
<Self::Value
, E
>
1591 String
::from_utf8(v
)
1593 .map_err(|e
| Error
::invalid_value(Unexpected
::Bytes(&e
.into_bytes()), &self))
1597 #[cfg(feature = "std")]
1598 impl<'de
> Deserialize
<'de
> for PathBuf
{
1599 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1601 D
: Deserializer
<'de
>,
1603 deserializer
.deserialize_string(PathBufVisitor
)
1607 #[cfg(all(feature = "std", de_boxed_path))]
1608 forwarded_impl
!((), Box
<Path
>, PathBuf
::into_boxed_path
);
1610 ////////////////////////////////////////////////////////////////////////////////
1612 // If this were outside of the serde crate, it would just use:
1614 // #[derive(Deserialize)]
1615 // #[serde(variant_identifier)]
1616 #[cfg(all(feature = "std", any(unix, windows)))]
1617 variant_identifier
! {
1618 OsStringKind (Unix
; b
"Unix"; 0, Windows
; b
"Windows"; 1)
1619 "`Unix` or `Windows`",
1623 #[cfg(all(feature = "std", any(unix, windows)))]
1624 struct OsStringVisitor
;
1626 #[cfg(all(feature = "std", any(unix, windows)))]
1627 impl<'de
> Visitor
<'de
> for OsStringVisitor
{
1628 type Value
= OsString
;
1630 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1631 formatter
.write_str("os string")
1635 fn visit_enum
<A
>(self, data
: A
) -> Result
<Self::Value
, A
::Error
>
1639 use std
::os
::unix
::ffi
::OsStringExt
;
1641 match try
!(data
.variant()) {
1642 (OsStringKind
::Unix
, v
) => v
.newtype_variant().map(OsString
::from_vec
),
1643 (OsStringKind
::Windows
, _
) => Err(Error
::custom(
1644 "cannot deserialize Windows OS string on Unix",
1650 fn visit_enum
<A
>(self, data
: A
) -> Result
<Self::Value
, A
::Error
>
1654 use std
::os
::windows
::ffi
::OsStringExt
;
1656 match try
!(data
.variant()) {
1657 (OsStringKind
::Windows
, v
) => v
1658 .newtype_variant
::<Vec
<u16>>()
1659 .map(|vec
| OsString
::from_wide(&vec
)),
1660 (OsStringKind
::Unix
, _
) => Err(Error
::custom(
1661 "cannot deserialize Unix OS string on Windows",
1667 #[cfg(all(feature = "std", any(unix, windows)))]
1668 impl<'de
> Deserialize
<'de
> for OsString
{
1669 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1671 D
: Deserializer
<'de
>,
1673 deserializer
.deserialize_enum("OsString", OSSTR_VARIANTS
, OsStringVisitor
)
1677 ////////////////////////////////////////////////////////////////////////////////
1679 #[cfg(any(feature = "std", feature = "alloc"))]
1680 forwarded_impl
!((T
), Box
<T
>, Box
::new
);
1682 #[cfg(any(feature = "std", feature = "alloc"))]
1683 forwarded_impl
!((T
), Box
<[T
]>, Vec
::into_boxed_slice
);
1685 #[cfg(any(feature = "std", feature = "alloc"))]
1686 forwarded_impl
!((), Box
<str>, String
::into_boxed_str
);
1691 any(feature
= "std", feature
= "alloc")
1694 /// This impl requires the [`"rc"`] Cargo feature of Serde.
1696 /// Deserializing a data structure containing `Arc` will not attempt to
1697 /// deduplicate `Arc` references to the same data. Every deserialized `Arc`
1698 /// will end up with a strong count of 1.
1700 /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
1701 (T
), Arc
<T
>, Arc
::new
1707 any(feature
= "std", feature
= "alloc")
1710 /// This impl requires the [`"rc"`] Cargo feature of Serde.
1712 /// Deserializing a data structure containing `Rc` will not attempt to
1713 /// deduplicate `Rc` references to the same data. Every deserialized `Rc`
1714 /// will end up with a strong count of 1.
1716 /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
1720 #[cfg(any(feature = "std", feature = "alloc"))]
1721 impl<'de
, 'a
, T
: ?Sized
> Deserialize
<'de
> for Cow
<'a
, T
>
1724 T
::Owned
: Deserialize
<'de
>,
1727 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1729 D
: Deserializer
<'de
>,
1731 T
::Owned
::deserialize(deserializer
).map(Cow
::Owned
)
1735 ////////////////////////////////////////////////////////////////////////////////
1737 /// This impl requires the [`"rc"`] Cargo feature of Serde. The resulting
1738 /// `Weak<T>` has a reference count of 0 and cannot be upgraded.
1740 /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
1741 #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
1742 impl<'de
, T
: ?Sized
> Deserialize
<'de
> for RcWeak
<T
>
1744 T
: Deserialize
<'de
>,
1746 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1748 D
: Deserializer
<'de
>,
1750 try
!(Option
::<T
>::deserialize(deserializer
));
1755 /// This impl requires the [`"rc"`] Cargo feature of Serde. The resulting
1756 /// `Weak<T>` has a reference count of 0 and cannot be upgraded.
1758 /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
1759 #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
1760 impl<'de
, T
: ?Sized
> Deserialize
<'de
> for ArcWeak
<T
>
1762 T
: Deserialize
<'de
>,
1764 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1766 D
: Deserializer
<'de
>,
1768 try
!(Option
::<T
>::deserialize(deserializer
));
1773 ////////////////////////////////////////////////////////////////////////////////
1775 #[cfg(all(de_rc_dst, feature = "rc", any(feature = "std", feature = "alloc")))]
1776 macro_rules
! box_forwarded_impl
{
1778 $
(#[doc = $doc:tt])*
1782 impl<'de
, T
: ?Sized
> Deserialize
<'de
> for $t
<T
>
1784 Box
<T
>: Deserialize
<'de
>,
1786 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1788 D
: Deserializer
<'de
>,
1790 Box
::deserialize(deserializer
).map(Into
::into
)
1796 #[cfg(all(de_rc_dst, feature = "rc", any(feature = "std", feature = "alloc")))]
1797 box_forwarded_impl
! {
1798 /// This impl requires the [`"rc"`] Cargo feature of Serde.
1800 /// Deserializing a data structure containing `Rc` will not attempt to
1801 /// deduplicate `Rc` references to the same data. Every deserialized `Rc`
1802 /// will end up with a strong count of 1.
1804 /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
1808 #[cfg(all(de_rc_dst, feature = "rc", any(feature = "std", feature = "alloc")))]
1809 box_forwarded_impl
! {
1810 /// This impl requires the [`"rc"`] Cargo feature of Serde.
1812 /// Deserializing a data structure containing `Arc` will not attempt to
1813 /// deduplicate `Arc` references to the same data. Every deserialized `Arc`
1814 /// will end up with a strong count of 1.
1816 /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
1820 ////////////////////////////////////////////////////////////////////////////////
1822 impl<'de
, T
> Deserialize
<'de
> for Cell
<T
>
1824 T
: Deserialize
<'de
> + Copy
,
1826 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1828 D
: Deserializer
<'de
>,
1830 T
::deserialize(deserializer
).map(Cell
::new
)
1834 forwarded_impl
!((T
), RefCell
<T
>, RefCell
::new
);
1836 #[cfg(feature = "std")]
1837 forwarded_impl
!((T
), Mutex
<T
>, Mutex
::new
);
1839 #[cfg(feature = "std")]
1840 forwarded_impl
!((T
), RwLock
<T
>, RwLock
::new
);
1842 ////////////////////////////////////////////////////////////////////////////////
1844 // This is a cleaned-up version of the impl generated by:
1846 // #[derive(Deserialize)]
1847 // #[serde(deny_unknown_fields)]
1848 // struct Duration {
1852 #[cfg(any(core_duration, feature = "std"))]
1853 impl<'de
> Deserialize
<'de
> for Duration
{
1854 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1856 D
: Deserializer
<'de
>,
1858 // If this were outside of the serde crate, it would just use:
1860 // #[derive(Deserialize)]
1861 // #[serde(field_identifier, rename_all = "lowercase")]
1867 impl<'de
> Deserialize
<'de
> for Field
{
1868 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1870 D
: Deserializer
<'de
>,
1872 struct FieldVisitor
;
1874 impl<'de
> Visitor
<'de
> for FieldVisitor
{
1877 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1878 formatter
.write_str("`secs` or `nanos`")
1881 fn visit_str
<E
>(self, value
: &str) -> Result
<Self::Value
, E
>
1886 "secs" => Ok(Field
::Secs
),
1887 "nanos" => Ok(Field
::Nanos
),
1888 _
=> Err(Error
::unknown_field(value
, FIELDS
)),
1892 fn visit_bytes
<E
>(self, value
: &[u8]) -> Result
<Self::Value
, E
>
1897 b
"secs" => Ok(Field
::Secs
),
1898 b
"nanos" => Ok(Field
::Nanos
),
1900 let value
= ::__private
::from_utf8_lossy(value
);
1901 Err(Error
::unknown_field(&value
, FIELDS
))
1907 deserializer
.deserialize_identifier(FieldVisitor
)
1911 fn check_overflow
<E
>(secs
: u64, nanos
: u32) -> Result
<(), E
>
1915 static NANOS_PER_SEC
: u32 = 1_000_000_000;
1916 match secs
.checked_add((nanos
/ NANOS_PER_SEC
) as u64) {
1918 None
=> Err(E
::custom("overflow deserializing Duration")),
1922 struct DurationVisitor
;
1924 impl<'de
> Visitor
<'de
> for DurationVisitor
{
1925 type Value
= Duration
;
1927 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1928 formatter
.write_str("struct Duration")
1931 fn visit_seq
<A
>(self, mut seq
: A
) -> Result
<Self::Value
, A
::Error
>
1935 let secs
: u64 = match try
!(seq
.next_element()) {
1936 Some(value
) => value
,
1938 return Err(Error
::invalid_length(0, &self));
1941 let nanos
: u32 = match try
!(seq
.next_element()) {
1942 Some(value
) => value
,
1944 return Err(Error
::invalid_length(1, &self));
1947 try
!(check_overflow(secs
, nanos
));
1948 Ok(Duration
::new(secs
, nanos
))
1951 fn visit_map
<A
>(self, mut map
: A
) -> Result
<Self::Value
, A
::Error
>
1955 let mut secs
: Option
<u64> = None
;
1956 let mut nanos
: Option
<u32> = None
;
1957 while let Some(key
) = try
!(map
.next_key()) {
1961 return Err(<A
::Error
as Error
>::duplicate_field("secs"));
1963 secs
= Some(try
!(map
.next_value()));
1966 if nanos
.is_some() {
1967 return Err(<A
::Error
as Error
>::duplicate_field("nanos"));
1969 nanos
= Some(try
!(map
.next_value()));
1973 let secs
= match secs
{
1975 None
=> return Err(<A
::Error
as Error
>::missing_field("secs")),
1977 let nanos
= match nanos
{
1978 Some(nanos
) => nanos
,
1979 None
=> return Err(<A
::Error
as Error
>::missing_field("nanos")),
1981 try
!(check_overflow(secs
, nanos
));
1982 Ok(Duration
::new(secs
, nanos
))
1986 const FIELDS
: &'
static [&'
static str] = &["secs", "nanos"];
1987 deserializer
.deserialize_struct("Duration", FIELDS
, DurationVisitor
)
1991 ////////////////////////////////////////////////////////////////////////////////
1993 #[cfg(feature = "std")]
1994 impl<'de
> Deserialize
<'de
> for SystemTime
{
1995 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
1997 D
: Deserializer
<'de
>,
2005 impl<'de
> Deserialize
<'de
> for Field
{
2006 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2008 D
: Deserializer
<'de
>,
2010 struct FieldVisitor
;
2012 impl<'de
> Visitor
<'de
> for FieldVisitor
{
2015 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2016 formatter
.write_str("`secs_since_epoch` or `nanos_since_epoch`")
2019 fn visit_str
<E
>(self, value
: &str) -> Result
<Self::Value
, E
>
2024 "secs_since_epoch" => Ok(Field
::Secs
),
2025 "nanos_since_epoch" => Ok(Field
::Nanos
),
2026 _
=> Err(Error
::unknown_field(value
, FIELDS
)),
2030 fn visit_bytes
<E
>(self, value
: &[u8]) -> Result
<Self::Value
, E
>
2035 b
"secs_since_epoch" => Ok(Field
::Secs
),
2036 b
"nanos_since_epoch" => Ok(Field
::Nanos
),
2038 let value
= String
::from_utf8_lossy(value
);
2039 Err(Error
::unknown_field(&value
, FIELDS
))
2045 deserializer
.deserialize_identifier(FieldVisitor
)
2049 fn check_overflow
<E
>(secs
: u64, nanos
: u32) -> Result
<(), E
>
2053 static NANOS_PER_SEC
: u32 = 1_000_000_000;
2054 match secs
.checked_add((nanos
/ NANOS_PER_SEC
) as u64) {
2056 None
=> Err(E
::custom("overflow deserializing SystemTime epoch offset")),
2060 struct DurationVisitor
;
2062 impl<'de
> Visitor
<'de
> for DurationVisitor
{
2063 type Value
= Duration
;
2065 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2066 formatter
.write_str("struct SystemTime")
2069 fn visit_seq
<A
>(self, mut seq
: A
) -> Result
<Self::Value
, A
::Error
>
2073 let secs
: u64 = match try
!(seq
.next_element()) {
2074 Some(value
) => value
,
2076 return Err(Error
::invalid_length(0, &self));
2079 let nanos
: u32 = match try
!(seq
.next_element()) {
2080 Some(value
) => value
,
2082 return Err(Error
::invalid_length(1, &self));
2085 try
!(check_overflow(secs
, nanos
));
2086 Ok(Duration
::new(secs
, nanos
))
2089 fn visit_map
<A
>(self, mut map
: A
) -> Result
<Self::Value
, A
::Error
>
2093 let mut secs
: Option
<u64> = None
;
2094 let mut nanos
: Option
<u32> = None
;
2095 while let Some(key
) = try
!(map
.next_key()) {
2099 return Err(<A
::Error
as Error
>::duplicate_field(
2103 secs
= Some(try
!(map
.next_value()));
2106 if nanos
.is_some() {
2107 return Err(<A
::Error
as Error
>::duplicate_field(
2108 "nanos_since_epoch",
2111 nanos
= Some(try
!(map
.next_value()));
2115 let secs
= match secs
{
2117 None
=> return Err(<A
::Error
as Error
>::missing_field("secs_since_epoch")),
2119 let nanos
= match nanos
{
2120 Some(nanos
) => nanos
,
2121 None
=> return Err(<A
::Error
as Error
>::missing_field("nanos_since_epoch")),
2123 try
!(check_overflow(secs
, nanos
));
2124 Ok(Duration
::new(secs
, nanos
))
2128 const FIELDS
: &'
static [&'
static str] = &["secs_since_epoch", "nanos_since_epoch"];
2129 let duration
= try
!(deserializer
.deserialize_struct("SystemTime", FIELDS
, DurationVisitor
));
2130 #[cfg(systemtime_checked_add)]
2131 let ret
= UNIX_EPOCH
2132 .checked_add(duration
)
2133 .ok_or_else(|| D
::Error
::custom("overflow deserializing SystemTime"));
2134 #[cfg(not(systemtime_checked_add))]
2135 let ret
= Ok(UNIX_EPOCH
+ duration
);
2140 ////////////////////////////////////////////////////////////////////////////////
2144 // #[derive(Deserialize)]
2145 // #[serde(deny_unknown_fields)]
2150 impl<'de
, Idx
> Deserialize
<'de
> for Range
<Idx
>
2152 Idx
: Deserialize
<'de
>,
2154 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2156 D
: Deserializer
<'de
>,
2158 let (start
, end
) = deserializer
.deserialize_struct(
2161 range
::RangeVisitor
{
2162 expecting
: "struct Range",
2163 phantom
: PhantomData
,
2170 #[cfg(range_inclusive)]
2171 impl<'de
, Idx
> Deserialize
<'de
> for RangeInclusive
<Idx
>
2173 Idx
: Deserialize
<'de
>,
2175 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2177 D
: Deserializer
<'de
>,
2179 let (start
, end
) = deserializer
.deserialize_struct(
2182 range
::RangeVisitor
{
2183 expecting
: "struct RangeInclusive",
2184 phantom
: PhantomData
,
2187 Ok(RangeInclusive
::new(start
, end
))
2194 use de
::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor}
;
2196 pub const FIELDS
: &'
static [&'
static str] = &["start", "end"];
2198 // If this were outside of the serde crate, it would just use:
2200 // #[derive(Deserialize)]
2201 // #[serde(field_identifier, rename_all = "lowercase")]
2207 impl<'de
> Deserialize
<'de
> for Field
{
2208 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2210 D
: Deserializer
<'de
>,
2212 struct FieldVisitor
;
2214 impl<'de
> Visitor
<'de
> for FieldVisitor
{
2217 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2218 formatter
.write_str("`start` or `end`")
2221 fn visit_str
<E
>(self, value
: &str) -> Result
<Self::Value
, E
>
2226 "start" => Ok(Field
::Start
),
2227 "end" => Ok(Field
::End
),
2228 _
=> Err(Error
::unknown_field(value
, FIELDS
)),
2232 fn visit_bytes
<E
>(self, value
: &[u8]) -> Result
<Self::Value
, E
>
2237 b
"start" => Ok(Field
::Start
),
2238 b
"end" => Ok(Field
::End
),
2240 let value
= ::__private
::from_utf8_lossy(value
);
2241 Err(Error
::unknown_field(&value
, FIELDS
))
2247 deserializer
.deserialize_identifier(FieldVisitor
)
2251 pub struct RangeVisitor
<Idx
> {
2252 pub expecting
: &'
static str,
2253 pub phantom
: PhantomData
<Idx
>,
2256 impl<'de
, Idx
> Visitor
<'de
> for RangeVisitor
<Idx
>
2258 Idx
: Deserialize
<'de
>,
2260 type Value
= (Idx
, Idx
);
2262 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2263 formatter
.write_str(self.expecting
)
2266 fn visit_seq
<A
>(self, mut seq
: A
) -> Result
<Self::Value
, A
::Error
>
2270 let start
: Idx
= match try
!(seq
.next_element()) {
2271 Some(value
) => value
,
2273 return Err(Error
::invalid_length(0, &self));
2276 let end
: Idx
= match try
!(seq
.next_element()) {
2277 Some(value
) => value
,
2279 return Err(Error
::invalid_length(1, &self));
2285 fn visit_map
<A
>(self, mut map
: A
) -> Result
<Self::Value
, A
::Error
>
2289 let mut start
: Option
<Idx
> = None
;
2290 let mut end
: Option
<Idx
> = None
;
2291 while let Some(key
) = try
!(map
.next_key()) {
2294 if start
.is_some() {
2295 return Err(<A
::Error
as Error
>::duplicate_field("start"));
2297 start
= Some(try
!(map
.next_value()));
2301 return Err(<A
::Error
as Error
>::duplicate_field("end"));
2303 end
= Some(try
!(map
.next_value()));
2307 let start
= match start
{
2308 Some(start
) => start
,
2309 None
=> return Err(<A
::Error
as Error
>::missing_field("start")),
2311 let end
= match end
{
2313 None
=> return Err(<A
::Error
as Error
>::missing_field("end")),
2320 ////////////////////////////////////////////////////////////////////////////////
2322 #[cfg(any(ops_bound, collections_bound))]
2323 impl<'de
, T
> Deserialize
<'de
> for Bound
<T
>
2325 T
: Deserialize
<'de
>,
2327 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2329 D
: Deserializer
<'de
>,
2337 impl<'de
> Deserialize
<'de
> for Field
{
2339 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2341 D
: Deserializer
<'de
>,
2343 struct FieldVisitor
;
2345 impl<'de
> Visitor
<'de
> for FieldVisitor
{
2348 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2349 formatter
.write_str("`Unbounded`, `Included` or `Excluded`")
2352 fn visit_u64
<E
>(self, value
: u64) -> Result
<Self::Value
, E
>
2357 0 => Ok(Field
::Unbounded
),
2358 1 => Ok(Field
::Included
),
2359 2 => Ok(Field
::Excluded
),
2360 _
=> Err(Error
::invalid_value(Unexpected
::Unsigned(value
), &self)),
2364 fn visit_str
<E
>(self, value
: &str) -> Result
<Self::Value
, E
>
2369 "Unbounded" => Ok(Field
::Unbounded
),
2370 "Included" => Ok(Field
::Included
),
2371 "Excluded" => Ok(Field
::Excluded
),
2372 _
=> Err(Error
::unknown_variant(value
, VARIANTS
)),
2376 fn visit_bytes
<E
>(self, value
: &[u8]) -> Result
<Self::Value
, E
>
2381 b
"Unbounded" => Ok(Field
::Unbounded
),
2382 b
"Included" => Ok(Field
::Included
),
2383 b
"Excluded" => Ok(Field
::Excluded
),
2384 _
=> match str::from_utf8(value
) {
2385 Ok(value
) => Err(Error
::unknown_variant(value
, VARIANTS
)),
2387 Err(Error
::invalid_value(Unexpected
::Bytes(value
), &self))
2394 deserializer
.deserialize_identifier(FieldVisitor
)
2398 struct BoundVisitor
<T
>(PhantomData
<Bound
<T
>>);
2400 impl<'de
, T
> Visitor
<'de
> for BoundVisitor
<T
>
2402 T
: Deserialize
<'de
>,
2404 type Value
= Bound
<T
>;
2406 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2407 formatter
.write_str("enum Bound")
2410 fn visit_enum
<A
>(self, data
: A
) -> Result
<Self::Value
, A
::Error
>
2414 match try
!(data
.variant()) {
2415 (Field
::Unbounded
, v
) => v
.unit_variant().map(|()| Bound
::Unbounded
),
2416 (Field
::Included
, v
) => v
.newtype_variant().map(Bound
::Included
),
2417 (Field
::Excluded
, v
) => v
.newtype_variant().map(Bound
::Excluded
),
2422 const VARIANTS
: &'
static [&'
static str] = &["Unbounded", "Included", "Excluded"];
2424 deserializer
.deserialize_enum("Bound", VARIANTS
, BoundVisitor(PhantomData
))
2428 ////////////////////////////////////////////////////////////////////////////////
2430 macro_rules
! nonzero_integers
{
2431 ( $
( $T
: ident
, )+ ) => {
2434 impl<'de
> Deserialize
<'de
> for num
::$T
{
2435 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2437 D
: Deserializer
<'de
>,
2439 let value
= try
!(Deserialize
::deserialize(deserializer
));
2440 match <num
::$T
>::new(value
) {
2441 Some(nonzero
) => Ok(nonzero
),
2442 None
=> Err(Error
::custom("expected a non-zero value")),
2458 #[cfg(num_nonzero_signed)]
2467 // Currently 128-bit integers do not work on Emscripten targets so we need an
2468 // additional `#[cfg]`
2469 serde_if_integer128
! {
2474 #[cfg(num_nonzero_signed)]
2480 ////////////////////////////////////////////////////////////////////////////////
2482 impl<'de
, T
, E
> Deserialize
<'de
> for Result
<T
, E
>
2484 T
: Deserialize
<'de
>,
2485 E
: Deserialize
<'de
>,
2487 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2489 D
: Deserializer
<'de
>,
2491 // If this were outside of the serde crate, it would just use:
2493 // #[derive(Deserialize)]
2494 // #[serde(variant_identifier)]
2500 impl<'de
> Deserialize
<'de
> for Field
{
2502 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2504 D
: Deserializer
<'de
>,
2506 struct FieldVisitor
;
2508 impl<'de
> Visitor
<'de
> for FieldVisitor
{
2511 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2512 formatter
.write_str("`Ok` or `Err`")
2515 fn visit_u64
<E
>(self, value
: u64) -> Result
<Self::Value
, E
>
2521 1 => Ok(Field
::Err
),
2522 _
=> Err(Error
::invalid_value(Unexpected
::Unsigned(value
), &self)),
2526 fn visit_str
<E
>(self, value
: &str) -> Result
<Self::Value
, E
>
2531 "Ok" => Ok(Field
::Ok
),
2532 "Err" => Ok(Field
::Err
),
2533 _
=> Err(Error
::unknown_variant(value
, VARIANTS
)),
2537 fn visit_bytes
<E
>(self, value
: &[u8]) -> Result
<Self::Value
, E
>
2542 b
"Ok" => Ok(Field
::Ok
),
2543 b
"Err" => Ok(Field
::Err
),
2544 _
=> match str::from_utf8(value
) {
2545 Ok(value
) => Err(Error
::unknown_variant(value
, VARIANTS
)),
2547 Err(Error
::invalid_value(Unexpected
::Bytes(value
), &self))
2554 deserializer
.deserialize_identifier(FieldVisitor
)
2558 struct ResultVisitor
<T
, E
>(PhantomData
<Result
<T
, E
>>);
2560 impl<'de
, T
, E
> Visitor
<'de
> for ResultVisitor
<T
, E
>
2562 T
: Deserialize
<'de
>,
2563 E
: Deserialize
<'de
>,
2565 type Value
= Result
<T
, E
>;
2567 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2568 formatter
.write_str("enum Result")
2571 fn visit_enum
<A
>(self, data
: A
) -> Result
<Self::Value
, A
::Error
>
2575 match try
!(data
.variant()) {
2576 (Field
::Ok
, v
) => v
.newtype_variant().map(Ok
),
2577 (Field
::Err
, v
) => v
.newtype_variant().map(Err
),
2582 const VARIANTS
: &'
static [&'
static str] = &["Ok", "Err"];
2584 deserializer
.deserialize_enum("Result", VARIANTS
, ResultVisitor(PhantomData
))
2588 ////////////////////////////////////////////////////////////////////////////////
2590 impl<'de
, T
> Deserialize
<'de
> for Wrapping
<T
>
2592 T
: Deserialize
<'de
>,
2594 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2596 D
: Deserializer
<'de
>,
2598 Deserialize
::deserialize(deserializer
).map(Wrapping
)
2602 #[cfg(all(feature = "std", std_atomic))]
2603 macro_rules
! atomic_impl
{
2604 ($
($ty
:ident
)*) => {
2606 impl<'de
> Deserialize
<'de
> for $ty
{
2607 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
2609 D
: Deserializer
<'de
>,
2611 Deserialize
::deserialize(deserializer
).map(Self::new
)
2618 #[cfg(all(feature = "std", std_atomic))]
2621 AtomicI8 AtomicI16 AtomicI32 AtomicIsize
2622 AtomicU8 AtomicU16 AtomicU32 AtomicUsize
2625 #[cfg(all(feature = "std", std_atomic64))]
2630 #[cfg(feature = "std")]
2631 struct FromStrVisitor
<T
> {
2632 expecting
: &'
static str,
2636 #[cfg(feature = "std")]
2637 impl<T
> FromStrVisitor
<T
> {
2638 fn new(expecting
: &'
static str) -> Self {
2640 expecting
: expecting
,
2646 #[cfg(feature = "std")]
2647 impl<'de
, T
> Visitor
<'de
> for FromStrVisitor
<T
>
2650 T
::Err
: fmt
::Display
,
2654 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
2655 formatter
.write_str(self.expecting
)
2658 fn visit_str
<E
>(self, s
: &str) -> Result
<Self::Value
, E
>
2662 s
.parse().map_err(Error
::custom
)