1pub(crate) use self::macros::*;
2use crate::{formats::*, prelude::*};
3#[cfg(feature = "hashbrown_0_14")]
4use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
5#[cfg(feature = "hashbrown_0_15")]
6use hashbrown_0_15::{HashMap as HashbrownMap015, HashSet as HashbrownSet015};
7#[cfg(feature = "hashbrown_0_16")]
8use hashbrown_0_16::{HashMap as HashbrownMap016, HashSet as HashbrownSet016};
9#[cfg(feature = "indexmap_1")]
10use indexmap_1::{IndexMap, IndexSet};
11#[cfg(feature = "indexmap_2")]
12use indexmap_2::{IndexMap as IndexMap2, IndexSet as IndexSet2};
13
14#[cfg(feature = "alloc")]
18type BoxedSlice<T> = Box<[T]>;
19
20pub(crate) mod macros {
21 #![allow(unused_imports)]
24
25 macro_rules! foreach_map {
26 ($m:ident) => {
27 #[cfg(feature = "alloc")]
28 $m!(BTreeMap<K: Ord, V>, (|_size| BTreeMap::new()));
29 #[cfg(feature = "std")]
30 $m!(
31 HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
32 (|size| HashMap::with_capacity_and_hasher(size, Default::default()))
33 );
34 #[cfg(feature = "hashbrown_0_14")]
35 $m!(
36 HashbrownMap014<K: Eq + Hash, V, S: BuildHasher + Default>,
37 (|size| HashbrownMap014::with_capacity_and_hasher(size, Default::default()))
38 );
39 #[cfg(feature = "hashbrown_0_15")]
40 $m!(
41 HashbrownMap015<K: Eq + Hash, V, S: BuildHasher + Default>,
42 (|size| HashbrownMap015::with_capacity_and_hasher(size, Default::default()))
43 );
44 #[cfg(feature = "hashbrown_0_16")]
45 $m!(
46 HashbrownMap016<K: Eq + Hash, V, S: BuildHasher + Default>,
47 (|size| HashbrownMap016::with_capacity_and_hasher(size, Default::default()))
48 );
49 #[cfg(feature = "indexmap_1")]
50 $m!(
51 IndexMap<K: Eq + Hash, V, S: BuildHasher + Default>,
52 (|size| IndexMap::with_capacity_and_hasher(size, Default::default()))
53 );
54 #[cfg(feature = "indexmap_2")]
55 $m!(
56 IndexMap2<K: Eq + Hash, V, S: BuildHasher + Default>,
57 (|size| IndexMap2::with_capacity_and_hasher(size, Default::default()))
58 );
59 };
60 }
61
62 macro_rules! foreach_set {
63 ($m:ident) => {
64 #[cfg(feature = "alloc")]
65 $m!(BTreeSet<T: Ord>, (|_| BTreeSet::new()), insert);
66 #[cfg(feature = "std")]
67 $m!(
68 HashSet<T: Eq + Hash, S: BuildHasher + Default>,
69 (|size| HashSet::with_capacity_and_hasher(size, S::default())),
70 insert
71 );
72 #[cfg(feature = "hashbrown_0_14")]
73 $m!(
74 HashbrownSet014<T: Eq + Hash, S: BuildHasher + Default>,
75 (|size| HashbrownSet014::with_capacity_and_hasher(size, S::default())),
76 insert
77 );
78 #[cfg(feature = "hashbrown_0_15")]
79 $m!(
80 HashbrownSet015<T: Eq + Hash, S: BuildHasher + Default>,
81 (|size| HashbrownSet015::with_capacity_and_hasher(size, S::default())),
82 insert
83 );
84 #[cfg(feature = "hashbrown_0_16")]
85 $m!(
86 HashbrownSet016<T: Eq + Hash, S: BuildHasher + Default>,
87 (|size| HashbrownSet016::with_capacity_and_hasher(size, S::default())),
88 insert
89 );
90 #[cfg(feature = "indexmap_1")]
91 $m!(
92 IndexSet<T: Eq + Hash, S: BuildHasher + Default>,
93 (|size| IndexSet::with_capacity_and_hasher(size, S::default())),
94 insert
95 );
96 #[cfg(feature = "indexmap_2")]
97 $m!(
98 IndexSet2<T: Eq + Hash, S: BuildHasher + Default>,
99 (|size| IndexSet2::with_capacity_and_hasher(size, S::default())),
100 insert
101 );
102 };
103 }
104
105 macro_rules! foreach_seq {
106 ($m:ident) => {
107 foreach_set!($m);
108
109 #[cfg(feature = "alloc")]
110 $m!(
111 BinaryHeap<T: Ord>,
112 (|size| BinaryHeap::with_capacity(size)),
113 push
114 );
115 #[cfg(feature = "alloc")]
116 $m!(BoxedSlice<T>, (|size| Vec::with_capacity(size)), push);
117 #[cfg(feature = "alloc")]
118 $m!(LinkedList<T>, (|_| LinkedList::new()), push_back);
119 #[cfg(feature = "alloc")]
120 $m!(Vec<T>, (|size| Vec::with_capacity(size)), push);
121 #[cfg(feature = "alloc")]
122 $m!(
123 VecDeque<T>,
124 (|size| VecDeque::with_capacity(size)),
125 push_back
126 );
127 };
128 }
129
130 pub(crate) use foreach_map;
132 pub(crate) use foreach_seq;
133 pub(crate) use foreach_set;
134}
135
136#[allow(unused_macros)]
140macro_rules! pinned_wrapper {
141 ($wrapper:ident) => {
142 impl<'de, T, U> DeserializeAs<'de, Pin<$wrapper<T>>> for Pin<$wrapper<U>>
143 where
144 U: DeserializeAs<'de, T>,
145 {
146 fn deserialize_as<D>(deserializer: D) -> Result<Pin<$wrapper<T>>, D::Error>
147 where
148 D: Deserializer<'de>,
149 {
150 Ok($wrapper::pin(
151 DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(),
152 ))
153 }
154 }
155 };
156}
157
158#[cfg(feature = "alloc")]
159impl<'de, T, U> DeserializeAs<'de, Box<T>> for Box<U>
160where
161 U: DeserializeAs<'de, T>,
162{
163 fn deserialize_as<D>(deserializer: D) -> Result<Box<T>, D::Error>
164 where
165 D: Deserializer<'de>,
166 {
167 Ok(Box::new(
168 DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(),
169 ))
170 }
171}
172
173#[cfg(feature = "alloc")]
174pinned_wrapper!(Box);
175
176impl<'de, T, U> DeserializeAs<'de, Option<T>> for Option<U>
177where
178 U: DeserializeAs<'de, T>,
179{
180 fn deserialize_as<D>(deserializer: D) -> Result<Option<T>, D::Error>
181 where
182 D: Deserializer<'de>,
183 {
184 struct OptionVisitor<T, U>(PhantomData<(T, U)>);
185
186 impl<'de, T, U> Visitor<'de> for OptionVisitor<T, U>
187 where
188 U: DeserializeAs<'de, T>,
189 {
190 type Value = Option<T>;
191
192 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
193 formatter.write_str("option")
194 }
195
196 #[inline]
197 fn visit_unit<E>(self) -> Result<Self::Value, E>
198 where
199 E: DeError,
200 {
201 Ok(None)
202 }
203
204 #[inline]
205 fn visit_none<E>(self) -> Result<Self::Value, E>
206 where
207 E: DeError,
208 {
209 Ok(None)
210 }
211
212 #[inline]
213 fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
214 where
215 D: Deserializer<'de>,
216 {
217 U::deserialize_as(deserializer).map(Some)
218 }
219 }
220
221 deserializer.deserialize_option(OptionVisitor::<T, U>(PhantomData))
222 }
223}
224
225impl<'de, T, U> DeserializeAs<'de, Bound<T>> for Bound<U>
226where
227 U: DeserializeAs<'de, T>,
228{
229 fn deserialize_as<D>(deserializer: D) -> Result<Bound<T>, D::Error>
230 where
231 D: Deserializer<'de>,
232 {
233 Ok(
234 match Bound::<DeserializeAsWrap<T, U>>::deserialize(deserializer)? {
235 Bound::Unbounded => Bound::Unbounded,
236 Bound::Included(v) => Bound::Included(v.into_inner()),
237 Bound::Excluded(v) => Bound::Excluded(v.into_inner()),
238 },
239 )
240 }
241}
242
243#[cfg(feature = "alloc")]
244impl<'de, T, U> DeserializeAs<'de, Rc<T>> for Rc<U>
245where
246 U: DeserializeAs<'de, T>,
247{
248 fn deserialize_as<D>(deserializer: D) -> Result<Rc<T>, D::Error>
249 where
250 D: Deserializer<'de>,
251 {
252 Ok(Rc::new(
253 DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(),
254 ))
255 }
256}
257
258#[cfg(feature = "alloc")]
259pinned_wrapper!(Rc);
260
261#[cfg(feature = "alloc")]
262impl<'de, T, U> DeserializeAs<'de, RcWeak<T>> for RcWeak<U>
263where
264 U: DeserializeAs<'de, T>,
265{
266 fn deserialize_as<D>(deserializer: D) -> Result<RcWeak<T>, D::Error>
267 where
268 D: Deserializer<'de>,
269 {
270 DeserializeAsWrap::<Option<Rc<T>>, Option<Rc<U>>>::deserialize(deserializer)?;
271 Ok(RcWeak::new())
272 }
273}
274
275#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
276impl<'de, T, U> DeserializeAs<'de, Arc<T>> for Arc<U>
277where
278 U: DeserializeAs<'de, T>,
279{
280 fn deserialize_as<D>(deserializer: D) -> Result<Arc<T>, D::Error>
281 where
282 D: Deserializer<'de>,
283 {
284 Ok(Arc::new(
285 DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(),
286 ))
287 }
288}
289
290#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
291pinned_wrapper!(Arc);
292
293#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
294impl<'de, T, U> DeserializeAs<'de, ArcWeak<T>> for ArcWeak<U>
295where
296 U: DeserializeAs<'de, T>,
297{
298 fn deserialize_as<D>(deserializer: D) -> Result<ArcWeak<T>, D::Error>
299 where
300 D: Deserializer<'de>,
301 {
302 DeserializeAsWrap::<Option<Arc<T>>, Option<Arc<U>>>::deserialize(deserializer)?;
303 Ok(ArcWeak::new())
304 }
305}
306
307impl<'de, T, U> DeserializeAs<'de, Cell<T>> for Cell<U>
308where
309 U: DeserializeAs<'de, T>,
310{
311 fn deserialize_as<D>(deserializer: D) -> Result<Cell<T>, D::Error>
312 where
313 D: Deserializer<'de>,
314 {
315 Ok(Cell::new(
316 DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(),
317 ))
318 }
319}
320
321impl<'de, T, U> DeserializeAs<'de, RefCell<T>> for RefCell<U>
322where
323 U: DeserializeAs<'de, T>,
324{
325 fn deserialize_as<D>(deserializer: D) -> Result<RefCell<T>, D::Error>
326 where
327 D: Deserializer<'de>,
328 {
329 Ok(RefCell::new(
330 DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(),
331 ))
332 }
333}
334
335#[cfg(feature = "std")]
336impl<'de, T, U> DeserializeAs<'de, Mutex<T>> for Mutex<U>
337where
338 U: DeserializeAs<'de, T>,
339{
340 fn deserialize_as<D>(deserializer: D) -> Result<Mutex<T>, D::Error>
341 where
342 D: Deserializer<'de>,
343 {
344 Ok(Mutex::new(
345 DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(),
346 ))
347 }
348}
349
350#[cfg(feature = "std")]
351impl<'de, T, U> DeserializeAs<'de, RwLock<T>> for RwLock<U>
352where
353 U: DeserializeAs<'de, T>,
354{
355 fn deserialize_as<D>(deserializer: D) -> Result<RwLock<T>, D::Error>
356 where
357 D: Deserializer<'de>,
358 {
359 Ok(RwLock::new(
360 DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(),
361 ))
362 }
363}
364
365impl<'de, T, TAs, E, EAs> DeserializeAs<'de, Result<T, E>> for Result<TAs, EAs>
366where
367 TAs: DeserializeAs<'de, T>,
368 EAs: DeserializeAs<'de, E>,
369{
370 fn deserialize_as<D>(deserializer: D) -> Result<Result<T, E>, D::Error>
371 where
372 D: Deserializer<'de>,
373 {
374 Ok(
375 match Result::<DeserializeAsWrap<T, TAs>, DeserializeAsWrap<E, EAs>>::deserialize(
376 deserializer,
377 )? {
378 Ok(value) => Ok(value.into_inner()),
379 Err(err) => Err(err.into_inner()),
380 },
381 )
382 }
383}
384
385impl<'de, T, As, const N: usize> DeserializeAs<'de, [T; N]> for [As; N]
386where
387 As: DeserializeAs<'de, T>,
388{
389 fn deserialize_as<D>(deserializer: D) -> Result<[T; N], D::Error>
390 where
391 D: Deserializer<'de>,
392 {
393 struct ArrayVisitor<T, const M: usize>(PhantomData<T>);
394
395 impl<'de, T, As, const M: usize> Visitor<'de> for ArrayVisitor<DeserializeAsWrap<T, As>, M>
396 where
397 As: DeserializeAs<'de, T>,
398 {
399 type Value = [T; M];
400
401 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
402 formatter.write_fmt(format_args!("an array of size {M}"))
403 }
404
405 fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
406 where
407 A: SeqAccess<'de>,
408 {
409 utils::array_from_iterator(
410 utils::SeqIter::new(seq).map(
411 |res: Result<DeserializeAsWrap<T, As>, A::Error>| {
412 res.map(DeserializeAsWrap::into_inner)
413 },
414 ),
415 &self,
416 )
417 }
418 }
419
420 deserializer.deserialize_tuple(N, ArrayVisitor::<DeserializeAsWrap<T, As>, N>(PhantomData))
421 }
422}
423
424impl<'de, Idx, IdxAs> DeserializeAs<'de, Range<Idx>> for Range<IdxAs>
429where
430 IdxAs: DeserializeAs<'de, Idx>,
431{
432 fn deserialize_as<D>(deserializer: D) -> Result<Range<Idx>, D::Error>
433 where
434 D: Deserializer<'de>,
435 {
436 let Range::<DeserializeAsWrap<Idx, IdxAs>> { start, end } =
437 Deserialize::deserialize(deserializer)?;
438
439 Ok(Range {
440 start: start.into_inner(),
441 end: end.into_inner(),
442 })
443 }
444}
445
446impl<'de, Idx, IdxAs> DeserializeAs<'de, RangeFrom<Idx>> for RangeFrom<IdxAs>
447where
448 IdxAs: DeserializeAs<'de, Idx>,
449{
450 fn deserialize_as<D>(deserializer: D) -> Result<RangeFrom<Idx>, D::Error>
451 where
452 D: Deserializer<'de>,
453 {
454 let RangeFrom::<DeserializeAsWrap<Idx, IdxAs>> { start } =
455 Deserialize::deserialize(deserializer)?;
456
457 Ok(RangeFrom {
458 start: start.into_inner(),
459 })
460 }
461}
462
463impl<'de, Idx, IdxAs> DeserializeAs<'de, RangeInclusive<Idx>> for RangeInclusive<IdxAs>
464where
465 IdxAs: DeserializeAs<'de, Idx>,
466{
467 fn deserialize_as<D>(deserializer: D) -> Result<RangeInclusive<Idx>, D::Error>
468 where
469 D: Deserializer<'de>,
470 {
471 let (start, end) =
472 RangeInclusive::<DeserializeAsWrap<Idx, IdxAs>>::deserialize(deserializer)?
473 .into_inner();
474
475 Ok(RangeInclusive::new(start.into_inner(), end.into_inner()))
476 }
477}
478
479impl<'de, Idx, IdxAs> DeserializeAs<'de, RangeTo<Idx>> for RangeTo<IdxAs>
480where
481 IdxAs: DeserializeAs<'de, Idx>,
482{
483 fn deserialize_as<D>(deserializer: D) -> Result<RangeTo<Idx>, D::Error>
484 where
485 D: Deserializer<'de>,
486 {
487 let RangeTo::<DeserializeAsWrap<Idx, IdxAs>> { end } =
488 Deserialize::deserialize(deserializer)?;
489
490 Ok(RangeTo {
491 end: end.into_inner(),
492 })
493 }
494}
495
496#[cfg(feature = "alloc")]
501macro_rules! seq_impl {
502 (
503 $ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)? $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)* )* >,
504 $with_capacity:expr,
505 $append:ident
506 ) => {
507 impl<'de, T, U $(, $typaram)*> DeserializeAs<'de, $ty<T $(, $typaram)*>> for $ty<U $(, $typaram)*>
508 where
509 U: DeserializeAs<'de, T>,
510 $(T: $tbound1 $(+ $tbound2)*,)?
511 $($typaram: $bound1 $(+ $bound2)*),*
512 {
513 fn deserialize_as<D>(deserializer: D) -> Result<$ty<T $(, $typaram)*>, D::Error>
514 where
515 D: Deserializer<'de>,
516 {
517 struct SeqVisitor<T, U $(, $typaram)*> {
518 marker: PhantomData<(T, U $(, $typaram)*)>,
519 }
520
521 impl<'de, T, U $(, $typaram)*> Visitor<'de> for SeqVisitor<T, U $(, $typaram)*>
522 where
523 U: DeserializeAs<'de, T>,
524 $(T: $tbound1 $(+ $tbound2)*,)?
525 $($typaram: $bound1 $(+ $bound2)*),*
526 {
527 type Value = $ty<T $(, $typaram)*>;
528
529 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
530 formatter.write_str("a sequence")
531 }
532
533 fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
534 where
535 A: SeqAccess<'de>,
536 {
537 #[allow(clippy::redundant_closure_call)]
538 let mut values = ($with_capacity)(utils::size_hint_cautious::<T>(seq.size_hint()));
539
540 while let Some(value) = seq
541 .next_element()?
542 .map(|v: DeserializeAsWrap<T, U>| v.into_inner())
543 {
544 values.$append(value);
545 }
546
547 Ok(values.into())
548 }
549 }
550
551 let visitor = SeqVisitor::<T, U $(, $typaram)*> {
552 marker: PhantomData,
553 };
554 deserializer.deserialize_seq(visitor)
555 }
556 }
557 };
558}
559foreach_seq!(seq_impl);
560
561#[cfg(feature = "alloc")]
562macro_rules! map_impl {
563 (
564 $ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
565 $with_capacity:expr
566 ) => {
567 impl<'de, K, V, KU, VU $(, $typaram)*> DeserializeAs<'de, $ty<K, V $(, $typaram)*>> for $ty<KU, VU $(, $typaram)*>
568 where
569 KU: DeserializeAs<'de, K>,
570 VU: DeserializeAs<'de, V>,
571 $(K: $kbound1 $(+ $kbound2)*,)*
572 $($typaram: $bound1 $(+ $bound2)*),*
573 {
574 fn deserialize_as<D>(deserializer: D) -> Result<$ty<K, V $(, $typaram)*>, D::Error>
575 where
576 D: Deserializer<'de>,
577 {
578 struct MapVisitor<K, V, KU, VU $(, $typaram)*>(PhantomData<(K, V, KU, VU $(, $typaram)*)>);
579
580 impl<'de, K, V, KU, VU $(, $typaram)*> Visitor<'de> for MapVisitor<K, V, KU, VU $(, $typaram)*>
581 where
582 KU: DeserializeAs<'de, K>,
583 VU: DeserializeAs<'de, V>,
584 $(K: $kbound1 $(+ $kbound2)*,)*
585 $($typaram: $bound1 $(+ $bound2)*),*
586 {
587 type Value = $ty<K, V $(, $typaram)*>;
588
589 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
590 formatter.write_str("a map")
591 }
592
593 #[inline]
594 fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
595 where
596 A: MapAccess<'de>,
597 {
598 #[allow(clippy::redundant_closure_call)]
599 let mut values = ($with_capacity)(utils::size_hint_cautious::<(K, V)>(map.size_hint()));
600
601 while let Some((key, value)) = (map.next_entry())?.map(|(k, v): (DeserializeAsWrap::<K, KU>, DeserializeAsWrap::<V, VU>)| (k.into_inner(), v.into_inner())) {
602 values.insert(key, value);
603 }
604
605 Ok(values)
606 }
607 }
608
609 let visitor = MapVisitor::<K, V, KU, VU $(, $typaram)*> (PhantomData);
610 deserializer.deserialize_map(visitor)
611 }
612 }
613 }
614}
615foreach_map!(map_impl);
616
617macro_rules! tuple_impl {
618 ($len:literal $($n:tt $t:ident $tas:ident)+) => {
619 impl<'de, $($t, $tas,)+> DeserializeAs<'de, ($($t,)+)> for ($($tas,)+)
620 where
621 $($tas: DeserializeAs<'de, $t>,)+
622 {
623 fn deserialize_as<D>(deserializer: D) -> Result<($($t,)+), D::Error>
624 where
625 D: Deserializer<'de>,
626 {
627 struct TupleVisitor<$($t,)+>(PhantomData<($($t,)+)>);
628
629 impl<'de, $($t, $tas,)+> Visitor<'de>
630 for TupleVisitor<$(DeserializeAsWrap<$t, $tas>,)+>
631 where
632 $($tas: DeserializeAs<'de, $t>,)+
633 {
634 type Value = ($($t,)+);
635
636 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
637 formatter.write_str(concat!("a tuple of size ", $len))
638 }
639
640 #[allow(non_snake_case)]
641 fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
642 where
643 A: SeqAccess<'de>,
644 {
645 $(
646 let $t: DeserializeAsWrap<$t, $tas> = match seq.next_element()? {
647 Some(value) => value,
648 None => return Err(DeError::invalid_length($n, &self)),
649 };
650 )+
651
652 Ok(($($t.into_inner(),)+))
653 }
654 }
655
656 deserializer.deserialize_tuple(
657 $len,
658 TupleVisitor::<$(DeserializeAsWrap<$t, $tas>,)+>(PhantomData),
659 )
660 }
661 }
662 };
663}
664
665tuple_impl!(1 0 T0 As0);
666tuple_impl!(2 0 T0 As0 1 T1 As1);
667tuple_impl!(3 0 T0 As0 1 T1 As1 2 T2 As2);
668tuple_impl!(4 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3);
669tuple_impl!(5 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4);
670tuple_impl!(6 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5);
671tuple_impl!(7 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6);
672tuple_impl!(8 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7);
673tuple_impl!(9 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8);
674tuple_impl!(10 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9);
675tuple_impl!(11 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10);
676tuple_impl!(12 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11);
677tuple_impl!(13 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12);
678tuple_impl!(14 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12 13 T13 As13);
679tuple_impl!(15 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12 13 T13 As13 14 T14 As14);
680tuple_impl!(16 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12 13 T13 As13 14 T14 As14 15 T15 As15);
681
682#[cfg(feature = "alloc")]
683macro_rules! map_as_tuple_seq_intern {
684 (
685 $tyorig:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)?, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
686 $with_capacity:expr,
687 $ty:ident <(KAs, VAs)>
688 ) => {
689 impl<'de, K, KAs, V, VAs $(, $typaram)*> DeserializeAs<'de, $tyorig<K, V $(, $typaram)*>> for $ty<(KAs, VAs)>
690 where
691 KAs: DeserializeAs<'de, K>,
692 VAs: DeserializeAs<'de, V>,
693 $(K: $kbound1 $(+ $kbound2)*,)?
694 $($typaram: $bound1 $(+ $bound2)*,)*
695 {
696 fn deserialize_as<D>(deserializer: D) -> Result<$tyorig<K, V $(, $typaram)*>, D::Error>
697 where
698 D: Deserializer<'de>,
699 {
700 struct SeqVisitor<K, KAs, V, VAs $(, $typaram)*> {
701 marker: PhantomData<(K, KAs, V, VAs $(, $typaram)*)>,
702 }
703
704 impl<'de, K, KAs, V, VAs $(, $typaram)*> Visitor<'de> for SeqVisitor<K, KAs, V, VAs $(, $typaram)*>
705 where
706 KAs: DeserializeAs<'de, K>,
707 VAs: DeserializeAs<'de, V>,
708 $(K: $kbound1 $(+ $kbound2)*,)?
709 $($typaram: $bound1 $(+ $bound2)*,)*
710 {
711 type Value = $tyorig<K, V $(, $typaram)*>;
712
713 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
714 formatter.write_str("a sequence")
715 }
716
717 #[inline]
718 fn visit_seq<A>(self, access: A) -> Result<Self::Value, A::Error>
719 where
720 A: SeqAccess<'de>,
721 {
722 let iter = utils::SeqIter::new(access);
723 iter.map(|res| {
724 res.map(
725 |(k, v): (DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>)| {
726 (k.into_inner(), v.into_inner())
727 },
728 )
729 })
730 .collect()
731 }
732 }
733
734 let visitor = SeqVisitor::<K, KAs, V, VAs $(, $typaram)*> {
735 marker: PhantomData,
736 };
737 deserializer.deserialize_seq(visitor)
738 }
739 }
740 };
741}
742#[cfg(feature = "alloc")]
743macro_rules! map_as_tuple_seq {
744 (
745 $tyorig:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)?, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
746 $with_capacity:expr
747 ) => {
748 map_as_tuple_seq_intern!($tyorig < K $(: $kbound1 $(+ $kbound2)*)? , V $(, $typaram : $bound1 $(+ $bound2)*)* >, $with_capacity, Seq<(KAs, VAs)>);
749 #[cfg(feature = "alloc")]
750 map_as_tuple_seq_intern!($tyorig < K $(: $kbound1 $(+ $kbound2)*)? , V $(, $typaram : $bound1 $(+ $bound2)*)* >, $with_capacity, Vec<(KAs, VAs)>);
751 }
752}
753foreach_map!(map_as_tuple_seq);
754
755#[cfg(feature = "alloc")]
756macro_rules! tuple_seq_as_map_impl_intern {
757 (
758 $tyorig:ident < (K, V) $(: $($bound:ident $(+)?)+)? $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)* )* >,
759 $with_capacity:expr,
760 $append:ident,
761 $ty:ident <KAs, VAs>
762 ) => {
763 #[allow(clippy::implicit_hasher)]
764 impl<'de, K, KAs, V, VAs $(, $typaram)*> DeserializeAs<'de, $tyorig < (K, V) $(, $typaram)* >> for $ty<KAs, VAs>
765 where
766 KAs: DeserializeAs<'de, K>,
767 VAs: DeserializeAs<'de, V>,
768 (K, V): $($($bound +)*)?,
769 $($typaram: $bound1 $(+ $bound2)*,)*
770 {
771 fn deserialize_as<D>(deserializer: D) -> Result<$tyorig < (K, V) $(, $typaram)* >, D::Error>
772 where
773 D: Deserializer<'de>,
774 {
775 struct MapVisitor<K, KAs, V, VAs $(, $typaram)*> {
776 marker: PhantomData<(K, KAs, V, VAs $(, $typaram)*)>,
777 }
778
779 impl<'de, K, KAs, V, VAs $(, $typaram)*> Visitor<'de> for MapVisitor<K, KAs, V, VAs $(, $typaram)*>
780 where
781 KAs: DeserializeAs<'de, K>,
782 VAs: DeserializeAs<'de, V>,
783 (K, V): $($($bound +)*)?,
784 $($typaram: $bound1 $(+ $bound2)*,)*
785 {
786 type Value = $tyorig < (K, V) $(, $typaram)* >;
787
788 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
789 formatter.write_str("a map")
790 }
791
792 #[inline]
793 fn visit_map<A>(self, access: A) -> Result<Self::Value, A::Error>
794 where
795 A: MapAccess<'de>,
796 {
797 let iter = utils::MapIter::new(access);
798 iter.map(|res| {
799 res.map(
800 |(k, v): (DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>)| {
801 (k.into_inner(), v.into_inner())
802 },
803 )
804 })
805 .collect()
806 }
807 }
808
809 let visitor = MapVisitor::<K, KAs, V, VAs $(, $typaram)*> {
810 marker: PhantomData,
811 };
812 deserializer.deserialize_map(visitor)
813 }
814 }
815 }
816}
817#[cfg(feature = "alloc")]
818macro_rules! tuple_seq_as_map_impl {
819 (
820 $tyorig:ident < T $(: $($bound:ident $(+)?)+)? $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)* )* >,
821 $with_capacity:expr,
822 $append:ident
823 ) => {
824 tuple_seq_as_map_impl_intern!($tyorig < (K, V) $(: $($bound +)+)? $(, $typaram: $bound1 $(+ $bound2)*)*>, $with_capacity, $append, Map<KAs, VAs>);
825 #[cfg(feature = "alloc")]
826 tuple_seq_as_map_impl_intern!($tyorig < (K, V) $(: $($bound +)+)? $(, $typaram: $bound1 $(+ $bound2)*)*>, $with_capacity, $append, BTreeMap<KAs, VAs>);
827 #[cfg(feature = "std")]
828 tuple_seq_as_map_impl_intern!($tyorig < (K, V) $(: $($bound +)+)? $(, $typaram: $bound1 $(+ $bound2)*)*>, $with_capacity, $append, HashMap<KAs, VAs>);
829 }
830}
831foreach_seq!(tuple_seq_as_map_impl);
832
833#[cfg(feature = "alloc")]
835macro_rules! tuple_seq_as_map_option_impl {
836 ($ty:ident) => {
837 #[allow(clippy::implicit_hasher)]
838 impl<'de, K, KAs, V, VAs> DeserializeAs<'de, Option<(K, V)>> for $ty<KAs, VAs>
839 where
840 KAs: DeserializeAs<'de, K>,
841 VAs: DeserializeAs<'de, V>,
842 {
843 fn deserialize_as<D>(deserializer: D) -> Result<Option<(K, V)>, D::Error>
844 where
845 D: Deserializer<'de>,
846 {
847 struct MapVisitor<K, KAs, V, VAs> {
848 marker: PhantomData<(K, KAs, V, VAs)>,
849 }
850
851 impl<'de, K, KAs, V, VAs> Visitor<'de> for MapVisitor<K, KAs, V, VAs>
852 where
853 KAs: DeserializeAs<'de, K>,
854 VAs: DeserializeAs<'de, V>,
855 {
856 type Value = Option<(K, V)>;
857
858 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
859 formatter.write_str("a map of size 1")
860 }
861
862 #[inline]
863 fn visit_map<A>(self, access: A) -> Result<Self::Value, A::Error>
864 where
865 A: MapAccess<'de>,
866 {
867 let iter = utils::MapIter::new(access);
868 iter.map(|res| {
869 res.map(
870 |(k, v): (DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>)| {
871 (k.into_inner(), v.into_inner())
872 },
873 )
874 })
875 .next()
876 .transpose()
877 }
878 }
879
880 let visitor = MapVisitor::<K, KAs, V, VAs> {
881 marker: PhantomData,
882 };
883 deserializer.deserialize_map(visitor)
884 }
885 }
886 };
887}
888#[cfg(feature = "alloc")]
889tuple_seq_as_map_option_impl!(BTreeMap);
890#[cfg(feature = "std")]
891tuple_seq_as_map_option_impl!(HashMap);
892
893macro_rules! tuple_seq_as_map_arr {
894 ($ty:ident <KAs, VAs>) => {
895 #[allow(clippy::implicit_hasher)]
896 impl<'de, K, KAs, V, VAs, const N: usize> DeserializeAs<'de, [(K, V); N]> for $ty<KAs, VAs>
897 where
898 KAs: DeserializeAs<'de, K>,
899 VAs: DeserializeAs<'de, V>,
900 {
901 fn deserialize_as<D>(deserializer: D) -> Result<[(K, V); N], D::Error>
902 where
903 D: Deserializer<'de>,
904 {
905 struct MapVisitor<K, KAs, V, VAs, const M: usize> {
906 marker: PhantomData<(K, KAs, V, VAs)>,
907 }
908
909 impl<'de, K, KAs, V, VAs, const M: usize> Visitor<'de> for MapVisitor<K, KAs, V, VAs, M>
910 where
911 KAs: DeserializeAs<'de, K>,
912 VAs: DeserializeAs<'de, V>,
913 {
914 type Value = [(K, V); M];
915
916 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
917 formatter.write_fmt(format_args!("a map of length {}", M))
918 }
919
920 fn visit_map<A>(self, access: A) -> Result<Self::Value, A::Error>
921 where
922 A: MapAccess<'de>,
923 {
924 utils::array_from_iterator(utils::MapIter::new(access).map(
925 |res: Result<(DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>), A::Error>| {
926 res.map(|(k, v)| (k.into_inner(), v.into_inner()))
927 }
928 ), &self)
929 }
930 }
931
932 let visitor = MapVisitor::<K, KAs, V, VAs, N> {
933 marker: PhantomData,
934 };
935 deserializer.deserialize_map(visitor)
936 }
937 }
938 }
939}
940tuple_seq_as_map_arr!(Map<KAs, VAs>);
941#[cfg(feature = "alloc")]
942tuple_seq_as_map_arr!(BTreeMap<KAs, VAs>);
943#[cfg(feature = "std")]
944tuple_seq_as_map_arr!(HashMap<KAs, VAs>);
945
946impl<'de, T: Deserialize<'de>> DeserializeAs<'de, T> for Same {
951 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
952 where
953 D: Deserializer<'de>,
954 {
955 T::deserialize(deserializer)
956 }
957}
958
959impl<'de, T> DeserializeAs<'de, T> for DisplayFromStr
960where
961 T: FromStr,
962 T::Err: Display,
963{
964 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
965 where
966 D: Deserializer<'de>,
967 {
968 struct Helper<S>(PhantomData<S>);
969 impl<S> Visitor<'_> for Helper<S>
970 where
971 S: FromStr,
972 <S as FromStr>::Err: Display,
973 {
974 type Value = S;
975
976 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
977 formatter.write_str("a string")
978 }
979
980 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
981 where
982 E: DeError,
983 {
984 value.parse::<Self::Value>().map_err(DeError::custom)
985 }
986 }
987
988 deserializer.deserialize_str(Helper(PhantomData))
989 }
990}
991
992impl<'de, T, H, F> DeserializeAs<'de, T> for IfIsHumanReadable<H, F>
993where
994 H: DeserializeAs<'de, T>,
995 F: DeserializeAs<'de, T>,
996{
997 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
998 where
999 D: Deserializer<'de>,
1000 {
1001 if deserializer.is_human_readable() {
1002 H::deserialize_as(deserializer)
1003 } else {
1004 F::deserialize_as(deserializer)
1005 }
1006 }
1007}
1008
1009impl<'de, Str> DeserializeAs<'de, Option<Str>> for NoneAsEmptyString
1010where
1011 Str: FromStr,
1012 Str::Err: Display,
1013{
1014 fn deserialize_as<D>(deserializer: D) -> Result<Option<Str>, D::Error>
1015 where
1016 D: Deserializer<'de>,
1017 {
1018 struct OptionStringEmptyNone<S>(PhantomData<S>);
1019 impl<S> Visitor<'_> for OptionStringEmptyNone<S>
1020 where
1021 S: FromStr,
1022 S::Err: Display,
1023 {
1024 type Value = Option<S>;
1025
1026 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1027 formatter.write_str("a string")
1028 }
1029
1030 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
1031 where
1032 E: DeError,
1033 {
1034 match value {
1035 "" => Ok(None),
1036 v => S::from_str(v).map(Some).map_err(DeError::custom),
1037 }
1038 }
1039
1040 fn visit_unit<E>(self) -> Result<Self::Value, E>
1042 where
1043 E: DeError,
1044 {
1045 Ok(None)
1046 }
1047 }
1048
1049 deserializer.deserialize_any(OptionStringEmptyNone(PhantomData))
1050 }
1051}
1052
1053#[cfg(feature = "alloc")]
1054impl<'de, T, TAs> DeserializeAs<'de, T> for DefaultOnError<TAs>
1055where
1056 TAs: DeserializeAs<'de, T>,
1057 T: Default,
1058{
1059 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
1060 where
1061 D: Deserializer<'de>,
1062 {
1063 let is_hr = deserializer.is_human_readable();
1064 let content: content::de::Content<'de> = match Deserialize::deserialize(deserializer) {
1065 Ok(content) => content,
1066 Err(_) => return Ok(Default::default()),
1067 };
1068
1069 Ok(
1070 match <DeserializeAsWrap<T, TAs>>::deserialize(content::de::ContentDeserializer::<
1071 D::Error,
1072 >::new(content, is_hr))
1073 {
1074 Ok(elem) => elem.into_inner(),
1075 Err(_) => Default::default(),
1076 },
1077 )
1078 }
1079}
1080
1081#[cfg(feature = "alloc")]
1082impl<'de> DeserializeAs<'de, Vec<u8>> for BytesOrString {
1083 fn deserialize_as<D>(deserializer: D) -> Result<Vec<u8>, D::Error>
1084 where
1085 D: Deserializer<'de>,
1086 {
1087 struct BytesOrStringVisitor;
1088 impl<'de> Visitor<'de> for BytesOrStringVisitor {
1089 type Value = Vec<u8>;
1090
1091 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1092 formatter.write_str("a list of bytes or a string")
1093 }
1094
1095 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> {
1096 Ok(v.to_vec())
1097 }
1098
1099 #[cfg(feature = "alloc")]
1100 fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> {
1101 Ok(v)
1102 }
1103
1104 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> {
1105 Ok(v.as_bytes().to_vec())
1106 }
1107
1108 #[cfg(feature = "alloc")]
1109 fn visit_string<E>(self, v: String) -> Result<Self::Value, E> {
1110 Ok(v.into_bytes())
1111 }
1112
1113 fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
1114 where
1115 A: SeqAccess<'de>,
1116 {
1117 utils::SeqIter::new(seq).collect()
1118 }
1119 }
1120 deserializer.deserialize_any(BytesOrStringVisitor)
1121 }
1122}
1123
1124impl<'de, SEPARATOR, I, T> DeserializeAs<'de, I> for StringWithSeparator<SEPARATOR, T>
1125where
1126 SEPARATOR: Separator,
1127 I: FromIterator<T>,
1128 T: FromStr,
1129 T::Err: Display,
1130{
1131 fn deserialize_as<D>(deserializer: D) -> Result<I, D::Error>
1132 where
1133 D: Deserializer<'de>,
1134 {
1135 struct Helper<SEPARATOR, I, T>(PhantomData<(SEPARATOR, I, T)>);
1136
1137 impl<SEPARATOR, I, T> Visitor<'_> for Helper<SEPARATOR, I, T>
1138 where
1139 SEPARATOR: Separator,
1140 I: FromIterator<T>,
1141 T: FromStr,
1142 T::Err: Display,
1143 {
1144 type Value = I;
1145
1146 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1147 formatter.write_str("a string")
1148 }
1149
1150 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
1151 where
1152 E: DeError,
1153 {
1154 if value.is_empty() {
1155 Ok(None.into_iter().collect())
1156 } else {
1157 value
1158 .split(SEPARATOR::separator())
1159 .map(FromStr::from_str)
1160 .collect::<Result<_, _>>()
1161 .map_err(DeError::custom)
1162 }
1163 }
1164 }
1165
1166 deserializer.deserialize_str(Helper::<SEPARATOR, I, T>(PhantomData))
1167 }
1168}
1169
1170macro_rules! use_signed_duration {
1171 (
1172 $main_trait:ident $internal_trait:ident =>
1173 {
1174 $ty:ty; $converter:ident =>
1175 $({
1176 $format:ty, $strictness:ty =>
1177 $($tbound:ident: $bound:ident $(,)?)*
1178 })*
1179 }
1180 ) => {
1181 $(
1182 impl<'de, $($tbound,)*> DeserializeAs<'de, $ty> for $main_trait<$format, $strictness>
1183 where
1184 $($tbound: $bound,)*
1185 {
1186 fn deserialize_as<D>(deserializer: D) -> Result<$ty, D::Error>
1187 where
1188 D: Deserializer<'de>,
1189 {
1190 let dur: DurationSigned = $internal_trait::<$format, $strictness>::deserialize_as(deserializer)?;
1191 dur.$converter::<D>()
1192 }
1193 }
1194 )*
1195 };
1196 (
1197 $( $main_trait:ident $internal_trait:ident, )+ => $rest:tt
1198 ) => {
1199 $( use_signed_duration!($main_trait $internal_trait => $rest); )+
1200 };
1201}
1202
1203use_signed_duration!(
1204 DurationSeconds DurationSeconds,
1205 DurationMilliSeconds DurationMilliSeconds,
1206 DurationMicroSeconds DurationMicroSeconds,
1207 DurationNanoSeconds DurationNanoSeconds,
1208 => {
1209 Duration; to_std_duration =>
1210 {u64, Strict =>}
1211 {FORMAT, Flexible => FORMAT: Format}
1212 }
1213);
1214#[cfg(feature = "alloc")]
1215use_signed_duration!(
1216 DurationSeconds DurationSeconds,
1217 DurationMilliSeconds DurationMilliSeconds,
1218 DurationMicroSeconds DurationMicroSeconds,
1219 DurationNanoSeconds DurationNanoSeconds,
1220 => {
1221 Duration; to_std_duration =>
1222 {String, Strict =>}
1223 }
1224);
1225#[cfg(feature = "std")]
1226use_signed_duration!(
1227 DurationSeconds DurationSeconds,
1228 DurationMilliSeconds DurationMilliSeconds,
1229 DurationMicroSeconds DurationMicroSeconds,
1230 DurationNanoSeconds DurationNanoSeconds,
1231 => {
1232 Duration; to_std_duration =>
1233 {f64, Strict =>}
1235 }
1236);
1237use_signed_duration!(
1238 DurationSecondsWithFrac DurationSecondsWithFrac,
1239 DurationMilliSecondsWithFrac DurationMilliSecondsWithFrac,
1240 DurationMicroSecondsWithFrac DurationMicroSecondsWithFrac,
1241 DurationNanoSecondsWithFrac DurationNanoSecondsWithFrac,
1242 => {
1243 Duration; to_std_duration =>
1244 {f64, Strict =>}
1245 {FORMAT, Flexible => FORMAT: Format}
1246 }
1247);
1248#[cfg(feature = "alloc")]
1249use_signed_duration!(
1250 DurationSecondsWithFrac DurationSecondsWithFrac,
1251 DurationMilliSecondsWithFrac DurationMilliSecondsWithFrac,
1252 DurationMicroSecondsWithFrac DurationMicroSecondsWithFrac,
1253 DurationNanoSecondsWithFrac DurationNanoSecondsWithFrac,
1254 => {
1255 Duration; to_std_duration =>
1256 {String, Strict =>}
1257 }
1258);
1259
1260#[cfg(feature = "std")]
1261use_signed_duration!(
1262 TimestampSeconds DurationSeconds,
1263 TimestampMilliSeconds DurationMilliSeconds,
1264 TimestampMicroSeconds DurationMicroSeconds,
1265 TimestampNanoSeconds DurationNanoSeconds,
1266 => {
1267 SystemTime; to_system_time =>
1268 {i64, Strict =>}
1269 {f64, Strict =>}
1270 {String, Strict =>}
1271 {FORMAT, Flexible => FORMAT: Format}
1272 }
1273);
1274#[cfg(feature = "std")]
1275use_signed_duration!(
1276 TimestampSecondsWithFrac DurationSecondsWithFrac,
1277 TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac,
1278 TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac,
1279 TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac,
1280 => {
1281 SystemTime; to_system_time =>
1282 {f64, Strict =>}
1283 {String, Strict =>}
1284 {FORMAT, Flexible => FORMAT: Format}
1285 }
1286);
1287
1288impl<'de, T, U> DeserializeAs<'de, T> for DefaultOnNull<U>
1289where
1290 U: DeserializeAs<'de, T>,
1291 T: Default,
1292{
1293 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
1294 where
1295 D: Deserializer<'de>,
1296 {
1297 Ok(Option::<U>::deserialize_as(deserializer)?.unwrap_or_default())
1298 }
1299}
1300
1301impl<'de> DeserializeAs<'de, &'de [u8]> for Bytes {
1302 fn deserialize_as<D>(deserializer: D) -> Result<&'de [u8], D::Error>
1303 where
1304 D: Deserializer<'de>,
1305 {
1306 <&'de [u8]>::deserialize(deserializer)
1307 }
1308}
1309
1310#[cfg(feature = "alloc")]
1320impl<'de> DeserializeAs<'de, Vec<u8>> for Bytes {
1321 fn deserialize_as<D>(deserializer: D) -> Result<Vec<u8>, D::Error>
1322 where
1323 D: Deserializer<'de>,
1324 {
1325 struct VecVisitor;
1326
1327 impl<'de> Visitor<'de> for VecVisitor {
1328 type Value = Vec<u8>;
1329
1330 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1331 formatter.write_str("a byte array")
1332 }
1333
1334 fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
1335 where
1336 A: SeqAccess<'de>,
1337 {
1338 utils::SeqIter::new(seq).collect::<Result<_, _>>()
1339 }
1340
1341 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
1342 where
1343 E: DeError,
1344 {
1345 Ok(v.to_vec())
1346 }
1347
1348 fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
1349 where
1350 E: DeError,
1351 {
1352 Ok(v)
1353 }
1354
1355 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
1356 where
1357 E: DeError,
1358 {
1359 Ok(v.as_bytes().to_vec())
1360 }
1361
1362 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
1363 where
1364 E: DeError,
1365 {
1366 Ok(v.into_bytes())
1367 }
1368 }
1369
1370 deserializer.deserialize_byte_buf(VecVisitor)
1371 }
1372}
1373
1374#[cfg(feature = "alloc")]
1375impl<'de> DeserializeAs<'de, Box<[u8]>> for Bytes {
1376 fn deserialize_as<D>(deserializer: D) -> Result<Box<[u8]>, D::Error>
1377 where
1378 D: Deserializer<'de>,
1379 {
1380 <Bytes as DeserializeAs<'de, Vec<u8>>>::deserialize_as(deserializer)
1381 .map(Vec::into_boxed_slice)
1382 }
1383}
1384
1385#[cfg(feature = "alloc")]
1397impl<'de> DeserializeAs<'de, Cow<'de, [u8]>> for Bytes {
1398 fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8]>, D::Error>
1399 where
1400 D: Deserializer<'de>,
1401 {
1402 struct CowVisitor;
1403
1404 impl<'de> Visitor<'de> for CowVisitor {
1405 type Value = Cow<'de, [u8]>;
1406
1407 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1408 formatter.write_str("a byte array")
1409 }
1410
1411 fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
1412 where
1413 E: DeError,
1414 {
1415 Ok(Cow::Borrowed(v))
1416 }
1417
1418 fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
1419 where
1420 E: DeError,
1421 {
1422 Ok(Cow::Borrowed(v.as_bytes()))
1423 }
1424
1425 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
1426 where
1427 E: DeError,
1428 {
1429 Ok(Cow::Owned(v.to_vec()))
1430 }
1431
1432 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
1433 where
1434 E: DeError,
1435 {
1436 Ok(Cow::Owned(v.as_bytes().to_vec()))
1437 }
1438
1439 fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
1440 where
1441 E: DeError,
1442 {
1443 Ok(Cow::Owned(v))
1444 }
1445
1446 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
1447 where
1448 E: DeError,
1449 {
1450 Ok(Cow::Owned(v.into_bytes()))
1451 }
1452
1453 fn visit_seq<V>(self, seq: V) -> Result<Self::Value, V::Error>
1454 where
1455 V: SeqAccess<'de>,
1456 {
1457 Ok(Cow::Owned(
1458 utils::SeqIter::new(seq).collect::<Result<_, _>>()?,
1459 ))
1460 }
1461 }
1462
1463 deserializer.deserialize_bytes(CowVisitor)
1464 }
1465}
1466
1467impl<'de, const N: usize> DeserializeAs<'de, [u8; N]> for Bytes {
1468 fn deserialize_as<D>(deserializer: D) -> Result<[u8; N], D::Error>
1469 where
1470 D: Deserializer<'de>,
1471 {
1472 struct ArrayVisitor<const M: usize>;
1473
1474 impl<'de, const M: usize> Visitor<'de> for ArrayVisitor<M> {
1475 type Value = [u8; M];
1476
1477 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1478 formatter.write_fmt(format_args!("an byte array of size {M}"))
1479 }
1480
1481 fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
1482 where
1483 A: SeqAccess<'de>,
1484 {
1485 utils::array_from_iterator(utils::SeqIter::new(seq), &self)
1486 }
1487
1488 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
1489 where
1490 E: DeError,
1491 {
1492 v.try_into()
1493 .map_err(|_| DeError::invalid_length(v.len(), &self))
1494 }
1495
1496 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
1497 where
1498 E: DeError,
1499 {
1500 v.as_bytes()
1501 .try_into()
1502 .map_err(|_| DeError::invalid_length(v.len(), &self))
1503 }
1504 }
1505
1506 deserializer.deserialize_bytes(ArrayVisitor::<N>)
1507 }
1508}
1509
1510impl<'de, const N: usize> DeserializeAs<'de, &'de [u8; N]> for Bytes {
1511 fn deserialize_as<D>(deserializer: D) -> Result<&'de [u8; N], D::Error>
1512 where
1513 D: Deserializer<'de>,
1514 {
1515 struct ArrayVisitor<const M: usize>;
1516
1517 impl<'de, const M: usize> Visitor<'de> for ArrayVisitor<M> {
1518 type Value = &'de [u8; M];
1519
1520 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1521 formatter.write_fmt(format_args!("a borrowed byte array of size {M}"))
1522 }
1523
1524 fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
1525 where
1526 E: DeError,
1527 {
1528 v.try_into()
1529 .map_err(|_| DeError::invalid_length(v.len(), &self))
1530 }
1531
1532 fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
1533 where
1534 E: DeError,
1535 {
1536 v.as_bytes()
1537 .try_into()
1538 .map_err(|_| DeError::invalid_length(v.len(), &self))
1539 }
1540 }
1541
1542 deserializer.deserialize_bytes(ArrayVisitor::<N>)
1543 }
1544}
1545
1546#[cfg(feature = "alloc")]
1547impl<'de, const N: usize> DeserializeAs<'de, Cow<'de, [u8; N]>> for Bytes {
1548 fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8; N]>, D::Error>
1549 where
1550 D: Deserializer<'de>,
1551 {
1552 struct CowVisitor<const M: usize>;
1553
1554 impl<'de, const M: usize> Visitor<'de> for CowVisitor<M> {
1555 type Value = Cow<'de, [u8; M]>;
1556
1557 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1558 formatter.write_str("a byte array")
1559 }
1560
1561 fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
1562 where
1563 E: DeError,
1564 {
1565 Ok(Cow::Borrowed(
1566 v.try_into()
1567 .map_err(|_| DeError::invalid_length(v.len(), &self))?,
1568 ))
1569 }
1570
1571 fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
1572 where
1573 E: DeError,
1574 {
1575 Ok(Cow::Borrowed(
1576 v.as_bytes()
1577 .try_into()
1578 .map_err(|_| DeError::invalid_length(v.len(), &self))?,
1579 ))
1580 }
1581
1582 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
1583 where
1584 E: DeError,
1585 {
1586 Ok(Cow::Owned(
1587 v.to_vec()
1588 .try_into()
1589 .map_err(|_| DeError::invalid_length(v.len(), &self))?,
1590 ))
1591 }
1592
1593 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
1594 where
1595 E: DeError,
1596 {
1597 Ok(Cow::Owned(
1598 v.as_bytes()
1599 .to_vec()
1600 .try_into()
1601 .map_err(|_| DeError::invalid_length(v.len(), &self))?,
1602 ))
1603 }
1604
1605 fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
1606 where
1607 E: DeError,
1608 {
1609 let len = v.len();
1610 Ok(Cow::Owned(
1611 v.try_into()
1612 .map_err(|_| DeError::invalid_length(len, &self))?,
1613 ))
1614 }
1615
1616 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
1617 where
1618 E: DeError,
1619 {
1620 let len = v.len();
1621 Ok(Cow::Owned(
1622 v.into_bytes()
1623 .try_into()
1624 .map_err(|_| DeError::invalid_length(len, &self))?,
1625 ))
1626 }
1627
1628 fn visit_seq<V>(self, seq: V) -> Result<Self::Value, V::Error>
1629 where
1630 V: SeqAccess<'de>,
1631 {
1632 Ok(Cow::Owned(utils::array_from_iterator(
1633 utils::SeqIter::new(seq),
1634 &self,
1635 )?))
1636 }
1637 }
1638
1639 deserializer.deserialize_bytes(CowVisitor)
1640 }
1641}
1642
1643#[cfg(feature = "alloc")]
1644impl<'de, const N: usize> DeserializeAs<'de, Box<[u8; N]>> for Bytes {
1645 fn deserialize_as<D>(deserializer: D) -> Result<Box<[u8; N]>, D::Error>
1646 where
1647 D: Deserializer<'de>,
1648 {
1649 Bytes::deserialize_as(deserializer).map(Box::new)
1650 }
1651}
1652
1653#[cfg(feature = "alloc")]
1654impl<'de, T, TAs, FORMAT> DeserializeAs<'de, Vec<T>> for OneOrMany<TAs, FORMAT>
1655where
1656 TAs: DeserializeAs<'de, T>,
1657 FORMAT: Format,
1658{
1659 fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error>
1660 where
1661 D: Deserializer<'de>,
1662 {
1663 let is_hr = deserializer.is_human_readable();
1664 let content: content::de::Content<'de> = Deserialize::deserialize(deserializer)?;
1665
1666 let one_err: D::Error = match <DeserializeAsWrap<T, TAs>>::deserialize(
1667 content::de::ContentRefDeserializer::new(&content, is_hr),
1668 ) {
1669 Ok(one) => return Ok(alloc::vec![one.into_inner()]),
1670 Err(err) => err,
1671 };
1672 let many_err: D::Error = match <DeserializeAsWrap<Vec<T>, Vec<TAs>>>::deserialize(
1673 content::de::ContentDeserializer::new(content, is_hr),
1674 ) {
1675 Ok(many) => return Ok(many.into_inner()),
1676 Err(err) => err,
1677 };
1678 Err(DeError::custom(format_args!(
1679 "OneOrMany could not deserialize any variant:\n One: {one_err}\n Many: {many_err}"
1680 )))
1681 }
1682}
1683
1684#[cfg(feature = "alloc")]
1685impl<'de, T, TAs1> DeserializeAs<'de, T> for PickFirst<(TAs1,)>
1686where
1687 TAs1: DeserializeAs<'de, T>,
1688{
1689 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
1690 where
1691 D: Deserializer<'de>,
1692 {
1693 Ok(DeserializeAsWrap::<T, TAs1>::deserialize(deserializer)?.into_inner())
1694 }
1695}
1696
1697#[cfg(feature = "alloc")]
1698impl<'de, T, TAs1, TAs2> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2)>
1699where
1700 TAs1: DeserializeAs<'de, T>,
1701 TAs2: DeserializeAs<'de, T>,
1702{
1703 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
1704 where
1705 D: Deserializer<'de>,
1706 {
1707 let is_hr = deserializer.is_human_readable();
1708 let content: content::de::Content<'de> = Deserialize::deserialize(deserializer)?;
1709
1710 let first_err: D::Error = match <DeserializeAsWrap<T, TAs1>>::deserialize(
1711 content::de::ContentRefDeserializer::new(&content, is_hr),
1712 ) {
1713 Ok(first) => return Ok(first.into_inner()),
1714 Err(err) => err,
1715 };
1716 let second_err: D::Error = match <DeserializeAsWrap<T, TAs2>>::deserialize(
1717 content::de::ContentDeserializer::new(content, is_hr),
1718 ) {
1719 Ok(second) => return Ok(second.into_inner()),
1720 Err(err) => err,
1721 };
1722 Err(DeError::custom(format_args!(
1723 "PickFirst could not deserialize any variant:\n First: {first_err}\n Second: {second_err}"
1724 )))
1725 }
1726}
1727
1728#[cfg(feature = "alloc")]
1729impl<'de, T, TAs1, TAs2, TAs3> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2, TAs3)>
1730where
1731 TAs1: DeserializeAs<'de, T>,
1732 TAs2: DeserializeAs<'de, T>,
1733 TAs3: DeserializeAs<'de, T>,
1734{
1735 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
1736 where
1737 D: Deserializer<'de>,
1738 {
1739 let is_hr = deserializer.is_human_readable();
1740 let content: content::de::Content<'de> = Deserialize::deserialize(deserializer)?;
1741
1742 let first_err: D::Error = match <DeserializeAsWrap<T, TAs1>>::deserialize(
1743 content::de::ContentRefDeserializer::new(&content, is_hr),
1744 ) {
1745 Ok(first) => return Ok(first.into_inner()),
1746 Err(err) => err,
1747 };
1748 let second_err: D::Error = match <DeserializeAsWrap<T, TAs2>>::deserialize(
1749 content::de::ContentRefDeserializer::new(&content, is_hr),
1750 ) {
1751 Ok(second) => return Ok(second.into_inner()),
1752 Err(err) => err,
1753 };
1754 let third_err: D::Error = match <DeserializeAsWrap<T, TAs3>>::deserialize(
1755 content::de::ContentDeserializer::new(content, is_hr),
1756 ) {
1757 Ok(third) => return Ok(third.into_inner()),
1758 Err(err) => err,
1759 };
1760 Err(DeError::custom(format_args!(
1761 "PickFirst could not deserialize any variant:\n First: {first_err}\n Second: {second_err}\n Third: {third_err}",
1762 )))
1763 }
1764}
1765
1766#[cfg(feature = "alloc")]
1767impl<'de, T, TAs1, TAs2, TAs3, TAs4> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2, TAs3, TAs4)>
1768where
1769 TAs1: DeserializeAs<'de, T>,
1770 TAs2: DeserializeAs<'de, T>,
1771 TAs3: DeserializeAs<'de, T>,
1772 TAs4: DeserializeAs<'de, T>,
1773{
1774 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
1775 where
1776 D: Deserializer<'de>,
1777 {
1778 let is_hr = deserializer.is_human_readable();
1779 let content: content::de::Content<'de> = Deserialize::deserialize(deserializer)?;
1780
1781 let first_err: D::Error = match <DeserializeAsWrap<T, TAs1>>::deserialize(
1782 content::de::ContentRefDeserializer::new(&content, is_hr),
1783 ) {
1784 Ok(first) => return Ok(first.into_inner()),
1785 Err(err) => err,
1786 };
1787 let second_err: D::Error = match <DeserializeAsWrap<T, TAs2>>::deserialize(
1788 content::de::ContentRefDeserializer::new(&content, is_hr),
1789 ) {
1790 Ok(second) => return Ok(second.into_inner()),
1791 Err(err) => err,
1792 };
1793 let third_err: D::Error = match <DeserializeAsWrap<T, TAs3>>::deserialize(
1794 content::de::ContentRefDeserializer::new(&content, is_hr),
1795 ) {
1796 Ok(third) => return Ok(third.into_inner()),
1797 Err(err) => err,
1798 };
1799 let fourth_err: D::Error = match <DeserializeAsWrap<T, TAs4>>::deserialize(
1800 content::de::ContentDeserializer::new(content, is_hr),
1801 ) {
1802 Ok(fourth) => return Ok(fourth.into_inner()),
1803 Err(err) => err,
1804 };
1805 Err(DeError::custom(format_args!(
1806 "PickFirst could not deserialize any variant:\n First: {first_err}\n Second: {second_err}\n Third: {third_err}\n Fourth: {fourth_err}",
1807 )))
1808 }
1809}
1810
1811impl<'de, T, U> DeserializeAs<'de, T> for FromInto<U>
1812where
1813 U: Into<T>,
1814 U: Deserialize<'de>,
1815{
1816 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
1817 where
1818 D: Deserializer<'de>,
1819 {
1820 Ok(U::deserialize(deserializer)?.into())
1821 }
1822}
1823
1824impl<'de, T, U> DeserializeAs<'de, T> for TryFromInto<U>
1825where
1826 U: TryInto<T>,
1827 <U as TryInto<T>>::Error: Display,
1828 U: Deserialize<'de>,
1829{
1830 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
1831 where
1832 D: Deserializer<'de>,
1833 {
1834 U::deserialize(deserializer)?
1835 .try_into()
1836 .map_err(DeError::custom)
1837 }
1838}
1839
1840impl<'de, T, U> DeserializeAs<'de, T> for FromIntoRef<U>
1841where
1842 U: Into<T>,
1843 U: Deserialize<'de>,
1844{
1845 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
1846 where
1847 D: Deserializer<'de>,
1848 {
1849 Ok(U::deserialize(deserializer)?.into())
1850 }
1851}
1852
1853impl<'de, T, U> DeserializeAs<'de, T> for TryFromIntoRef<U>
1854where
1855 U: TryInto<T>,
1856 <U as TryInto<T>>::Error: Display,
1857 U: Deserialize<'de>,
1858{
1859 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
1860 where
1861 D: Deserializer<'de>,
1862 {
1863 U::deserialize(deserializer)?
1864 .try_into()
1865 .map_err(DeError::custom)
1866 }
1867}
1868
1869#[cfg(feature = "alloc")]
1870impl<'de> DeserializeAs<'de, Cow<'de, str>> for BorrowCow {
1871 fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, str>, D::Error>
1872 where
1873 D: Deserializer<'de>,
1874 {
1875 struct CowVisitor;
1876
1877 impl<'de> Visitor<'de> for CowVisitor {
1878 type Value = Cow<'de, str>;
1879
1880 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1881 formatter.write_str("an optionally borrowed string")
1882 }
1883
1884 fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
1885 where
1886 E: DeError,
1887 {
1888 Ok(Cow::Borrowed(v))
1889 }
1890
1891 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
1892 where
1893 E: DeError,
1894 {
1895 Ok(Cow::Owned(v.to_owned()))
1896 }
1897
1898 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
1899 where
1900 E: DeError,
1901 {
1902 Ok(Cow::Owned(v))
1903 }
1904 }
1905
1906 deserializer.deserialize_string(CowVisitor)
1907 }
1908}
1909
1910#[cfg(feature = "alloc")]
1911impl<'de> DeserializeAs<'de, Cow<'de, [u8]>> for BorrowCow {
1912 fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8]>, D::Error>
1913 where
1914 D: Deserializer<'de>,
1915 {
1916 Bytes::deserialize_as(deserializer)
1917 }
1918}
1919
1920#[cfg(feature = "alloc")]
1921impl<'de, const N: usize> DeserializeAs<'de, Cow<'de, [u8; N]>> for BorrowCow {
1922 fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8; N]>, D::Error>
1923 where
1924 D: Deserializer<'de>,
1925 {
1926 Bytes::deserialize_as(deserializer)
1927 }
1928}
1929
1930impl<'de> DeserializeAs<'de, bool> for BoolFromInt<Strict> {
1931 fn deserialize_as<D>(deserializer: D) -> Result<bool, D::Error>
1932 where
1933 D: Deserializer<'de>,
1934 {
1935 struct U8Visitor;
1936 impl Visitor<'_> for U8Visitor {
1937 type Value = bool;
1938
1939 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1940 formatter.write_str("an integer 0 or 1")
1941 }
1942
1943 fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
1944 where
1945 E: DeError,
1946 {
1947 match v {
1948 0 => Ok(false),
1949 1 => Ok(true),
1950 unexp => Err(DeError::invalid_value(
1951 Unexpected::Unsigned(u64::from(unexp)),
1952 &"0 or 1",
1953 )),
1954 }
1955 }
1956
1957 fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
1958 where
1959 E: DeError,
1960 {
1961 match v {
1962 0 => Ok(false),
1963 1 => Ok(true),
1964 unexp => Err(DeError::invalid_value(
1965 Unexpected::Signed(i64::from(unexp)),
1966 &"0 or 1",
1967 )),
1968 }
1969 }
1970
1971 fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
1972 where
1973 E: DeError,
1974 {
1975 match v {
1976 0 => Ok(false),
1977 1 => Ok(true),
1978 unexp => Err(DeError::invalid_value(
1979 Unexpected::Unsigned(unexp),
1980 &"0 or 1",
1981 )),
1982 }
1983 }
1984
1985 fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
1986 where
1987 E: DeError,
1988 {
1989 match v {
1990 0 => Ok(false),
1991 1 => Ok(true),
1992 unexp => Err(DeError::invalid_value(Unexpected::Signed(unexp), &"0 or 1")),
1993 }
1994 }
1995
1996 fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
1997 where
1998 E: DeError,
1999 {
2000 match v {
2001 0 => Ok(false),
2002 1 => Ok(true),
2003 unexp => {
2004 let mut buf: [u8; 58] = [0u8; 58];
2005 Err(DeError::invalid_value(
2006 crate::utils::get_unexpected_u128(unexp, &mut buf),
2007 &self,
2008 ))
2009 }
2010 }
2011 }
2012
2013 fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
2014 where
2015 E: DeError,
2016 {
2017 match v {
2018 0 => Ok(false),
2019 1 => Ok(true),
2020 unexp => {
2021 let mut buf: [u8; 58] = [0u8; 58];
2022 Err(DeError::invalid_value(
2023 crate::utils::get_unexpected_i128(unexp, &mut buf),
2024 &"0 or 1",
2025 ))
2026 }
2027 }
2028 }
2029 }
2030
2031 deserializer.deserialize_u8(U8Visitor)
2032 }
2033}
2034
2035impl<'de> DeserializeAs<'de, bool> for BoolFromInt<Flexible> {
2036 fn deserialize_as<D>(deserializer: D) -> Result<bool, D::Error>
2037 where
2038 D: Deserializer<'de>,
2039 {
2040 struct U8Visitor;
2041 impl Visitor<'_> for U8Visitor {
2042 type Value = bool;
2043
2044 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2045 formatter.write_str("an integer")
2046 }
2047
2048 fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
2049 where
2050 E: DeError,
2051 {
2052 Ok(v != 0)
2053 }
2054
2055 fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
2056 where
2057 E: DeError,
2058 {
2059 Ok(v != 0)
2060 }
2061
2062 fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
2063 where
2064 E: DeError,
2065 {
2066 Ok(v != 0)
2067 }
2068
2069 fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
2070 where
2071 E: DeError,
2072 {
2073 Ok(v != 0)
2074 }
2075
2076 fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
2077 where
2078 E: DeError,
2079 {
2080 Ok(v != 0)
2081 }
2082
2083 fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
2084 where
2085 E: DeError,
2086 {
2087 Ok(v != 0)
2088 }
2089 }
2090
2091 deserializer.deserialize_u8(U8Visitor)
2092 }
2093}
2094
2095