/rust/registry/src/github.com-1ecc6299db9ec823/serde-1.0.123/src/de/value.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Building blocks for deserializing basic values using the `IntoDeserializer` |
2 | | //! trait. |
3 | | //! |
4 | | //! ```edition2018 |
5 | | //! use std::str::FromStr; |
6 | | //! use serde::Deserialize; |
7 | | //! use serde::de::{value, IntoDeserializer}; |
8 | | //! |
9 | | //! #[derive(Deserialize)] |
10 | | //! enum Setting { |
11 | | //! On, |
12 | | //! Off, |
13 | | //! } |
14 | | //! |
15 | | //! impl FromStr for Setting { |
16 | | //! type Err = value::Error; |
17 | | //! |
18 | | //! fn from_str(s: &str) -> Result<Self, Self::Err> { |
19 | | //! Self::deserialize(s.into_deserializer()) |
20 | | //! } |
21 | | //! } |
22 | | //! ``` |
23 | | |
24 | | use lib::*; |
25 | | |
26 | | use self::private::{First, Second}; |
27 | | use __private::size_hint; |
28 | | use de::{self, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor}; |
29 | | use ser; |
30 | | |
31 | | //////////////////////////////////////////////////////////////////////////////// |
32 | | |
33 | | // For structs that contain a PhantomData. We do not want the trait |
34 | | // bound `E: Clone` inferred by derive(Clone). |
35 | | macro_rules! impl_copy_clone { |
36 | | ($ty:ident $(<$lifetime:tt>)*) => { |
37 | | impl<$($lifetime,)* E> Copy for $ty<$($lifetime,)* E> {} |
38 | | |
39 | | impl<$($lifetime,)* E> Clone for $ty<$($lifetime,)* E> { |
40 | 0 | fn clone(&self) -> Self { |
41 | | *self |
42 | | } |
43 | | } |
44 | | }; |
45 | | } |
46 | | |
47 | | //////////////////////////////////////////////////////////////////////////////// |
48 | | |
49 | | /// A minimal representation of all possible errors that can occur using the |
50 | | /// `IntoDeserializer` trait. |
51 | 0 | #[derive(Clone, PartialEq)] |
52 | | pub struct Error { |
53 | | err: ErrorImpl, |
54 | | } |
55 | | |
56 | | #[cfg(any(feature = "std", feature = "alloc"))] |
57 | | type ErrorImpl = Box<str>; |
58 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
59 | | type ErrorImpl = (); |
60 | | |
61 | | impl de::Error for Error { |
62 | | #[cfg(any(feature = "std", feature = "alloc"))] |
63 | | #[cold] |
64 | 0 | fn custom<T>(msg: T) -> Self |
65 | 0 | where |
66 | 0 | T: Display, |
67 | 0 | { |
68 | 0 | Error { |
69 | 0 | err: msg.to_string().into_boxed_str(), |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
74 | | #[cold] |
75 | | fn custom<T>(msg: T) -> Self |
76 | | where |
77 | | T: Display, |
78 | | { |
79 | | let _ = msg; |
80 | | Error { err: () } |
81 | | } |
82 | | } |
83 | | |
84 | | impl ser::Error for Error { |
85 | | #[cold] |
86 | 0 | fn custom<T>(msg: T) -> Self |
87 | 0 | where |
88 | 0 | T: Display, |
89 | 0 | { |
90 | 0 | de::Error::custom(msg) |
91 | 0 | } |
92 | | } |
93 | | |
94 | | impl Display for Error { |
95 | | #[cfg(any(feature = "std", feature = "alloc"))] |
96 | | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
97 | | formatter.write_str(&self.err) |
98 | | } |
99 | | |
100 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
101 | | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
102 | | formatter.write_str("Serde deserialization error") |
103 | | } |
104 | | } |
105 | | |
106 | | impl Debug for Error { |
107 | | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
108 | | let mut debug = formatter.debug_tuple("Error"); |
109 | | #[cfg(any(feature = "std", feature = "alloc"))] |
110 | | debug.field(&self.err); |
111 | | debug.finish() |
112 | | } |
113 | | } |
114 | | |
115 | | #[cfg(feature = "std")] |
116 | | impl error::Error for Error { |
117 | 0 | fn description(&self) -> &str { |
118 | 0 | &self.err |
119 | 0 | } |
120 | | } |
121 | | |
122 | | //////////////////////////////////////////////////////////////////////////////// |
123 | | |
124 | | impl<'de, E> IntoDeserializer<'de, E> for () |
125 | | where |
126 | | E: de::Error, |
127 | | { |
128 | | type Deserializer = UnitDeserializer<E>; |
129 | | |
130 | 0 | fn into_deserializer(self) -> UnitDeserializer<E> { |
131 | 0 | UnitDeserializer { |
132 | 0 | marker: PhantomData, |
133 | 0 | } |
134 | 0 | } |
135 | | } |
136 | | |
137 | | /// A deserializer holding a `()`. |
138 | | pub struct UnitDeserializer<E> { |
139 | | marker: PhantomData<E>, |
140 | | } |
141 | | |
142 | | impl_copy_clone!(UnitDeserializer); |
143 | | |
144 | | impl<'de, E> de::Deserializer<'de> for UnitDeserializer<E> |
145 | | where |
146 | | E: de::Error, |
147 | | { |
148 | | type Error = E; |
149 | | |
150 | | forward_to_deserialize_any! { |
151 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
152 | | bytes byte_buf unit unit_struct newtype_struct seq tuple tuple_struct |
153 | | map struct enum identifier ignored_any |
154 | | } |
155 | | |
156 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
157 | 0 | where |
158 | 0 | V: de::Visitor<'de>, |
159 | 0 | { |
160 | 0 | visitor.visit_unit() |
161 | 0 | } |
162 | | |
163 | 0 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
164 | 0 | where |
165 | 0 | V: de::Visitor<'de>, |
166 | 0 | { |
167 | 0 | visitor.visit_none() |
168 | 0 | } |
169 | | } |
170 | | |
171 | | impl<E> Debug for UnitDeserializer<E> { |
172 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
173 | 0 | formatter.debug_struct("UnitDeserializer").finish() |
174 | 0 | } |
175 | | } |
176 | | |
177 | | //////////////////////////////////////////////////////////////////////////////// |
178 | | |
179 | | /// A deserializer that cannot be instantiated. |
180 | | #[cfg(feature = "unstable")] |
181 | | pub struct NeverDeserializer<E> { |
182 | | never: !, |
183 | | marker: PhantomData<E>, |
184 | | } |
185 | | |
186 | | #[cfg(feature = "unstable")] |
187 | | impl<'de, E> IntoDeserializer<'de, E> for ! |
188 | | where |
189 | | E: de::Error, |
190 | | { |
191 | | type Deserializer = NeverDeserializer<E>; |
192 | | |
193 | | fn into_deserializer(self) -> Self::Deserializer { |
194 | | self |
195 | | } |
196 | | } |
197 | | |
198 | | #[cfg(feature = "unstable")] |
199 | | impl<'de, E> de::Deserializer<'de> for NeverDeserializer<E> |
200 | | where |
201 | | E: de::Error, |
202 | | { |
203 | | type Error = E; |
204 | | |
205 | | fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error> |
206 | | where |
207 | | V: de::Visitor<'de>, |
208 | | { |
209 | | self.never |
210 | | } |
211 | | |
212 | | forward_to_deserialize_any! { |
213 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
214 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
215 | | tuple_struct map struct enum identifier ignored_any |
216 | | } |
217 | | } |
218 | | |
219 | | //////////////////////////////////////////////////////////////////////////////// |
220 | | |
221 | | macro_rules! primitive_deserializer { |
222 | | ($ty:ty, $doc:tt, $name:ident, $method:ident $($cast:tt)*) => { |
223 | | #[doc = "A deserializer holding"] |
224 | | #[doc = $doc] |
225 | | pub struct $name<E> { |
226 | | value: $ty, |
227 | | marker: PhantomData<E> |
228 | | } |
229 | | |
230 | | impl_copy_clone!($name); |
231 | | |
232 | | impl<'de, E> IntoDeserializer<'de, E> for $ty |
233 | | where |
234 | | E: de::Error, |
235 | | { |
236 | | type Deserializer = $name<E>; |
237 | | |
238 | 0 | fn into_deserializer(self) -> $name<E> { |
239 | | $name { |
240 | | value: self, |
241 | | marker: PhantomData, |
242 | | } |
243 | | } |
244 | | } |
245 | | |
246 | | impl<'de, E> de::Deserializer<'de> for $name<E> |
247 | | where |
248 | | E: de::Error, |
249 | | { |
250 | | type Error = E; |
251 | | |
252 | | forward_to_deserialize_any! { |
253 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str |
254 | | string bytes byte_buf option unit unit_struct newtype_struct seq |
255 | | tuple tuple_struct map struct enum identifier ignored_any |
256 | | } |
257 | | |
258 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
259 | 0 | where |
260 | 0 | V: de::Visitor<'de>, |
261 | 0 | { |
262 | | visitor.$method(self.value $($cast)*) |
263 | | } |
264 | | } |
265 | | |
266 | | impl<E> Debug for $name<E> { |
267 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
268 | | formatter |
269 | | .debug_struct(stringify!($name)) |
270 | | .field("value", &self.value) |
271 | | .finish() |
272 | | } |
273 | | } |
274 | | } |
275 | | } |
276 | | |
277 | | primitive_deserializer!(bool, "a `bool`.", BoolDeserializer, visit_bool); |
278 | | primitive_deserializer!(i8, "an `i8`.", I8Deserializer, visit_i8); |
279 | | primitive_deserializer!(i16, "an `i16`.", I16Deserializer, visit_i16); |
280 | | primitive_deserializer!(i32, "an `i32`.", I32Deserializer, visit_i32); |
281 | | primitive_deserializer!(i64, "an `i64`.", I64Deserializer, visit_i64); |
282 | | primitive_deserializer!(isize, "an `isize`.", IsizeDeserializer, visit_i64 as i64); |
283 | | primitive_deserializer!(u8, "a `u8`.", U8Deserializer, visit_u8); |
284 | | primitive_deserializer!(u16, "a `u16`.", U16Deserializer, visit_u16); |
285 | | primitive_deserializer!(u64, "a `u64`.", U64Deserializer, visit_u64); |
286 | | primitive_deserializer!(usize, "a `usize`.", UsizeDeserializer, visit_u64 as u64); |
287 | | primitive_deserializer!(f32, "an `f32`.", F32Deserializer, visit_f32); |
288 | | primitive_deserializer!(f64, "an `f64`.", F64Deserializer, visit_f64); |
289 | | primitive_deserializer!(char, "a `char`.", CharDeserializer, visit_char); |
290 | | |
291 | | serde_if_integer128! { |
292 | | primitive_deserializer!(i128, "an `i128`.", I128Deserializer, visit_i128); |
293 | | primitive_deserializer!(u128, "a `u128`.", U128Deserializer, visit_u128); |
294 | | } |
295 | | |
296 | | /// A deserializer holding a `u32`. |
297 | | pub struct U32Deserializer<E> { |
298 | | value: u32, |
299 | | marker: PhantomData<E>, |
300 | | } |
301 | | |
302 | | impl_copy_clone!(U32Deserializer); |
303 | | |
304 | | impl<'de, E> IntoDeserializer<'de, E> for u32 |
305 | | where |
306 | | E: de::Error, |
307 | | { |
308 | | type Deserializer = U32Deserializer<E>; |
309 | | |
310 | 0 | fn into_deserializer(self) -> U32Deserializer<E> { |
311 | 0 | U32Deserializer { |
312 | 0 | value: self, |
313 | 0 | marker: PhantomData, |
314 | 0 | } |
315 | 0 | } |
316 | | } |
317 | | |
318 | | impl<'de, E> de::Deserializer<'de> for U32Deserializer<E> |
319 | | where |
320 | | E: de::Error, |
321 | | { |
322 | | type Error = E; |
323 | | |
324 | | forward_to_deserialize_any! { |
325 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
326 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
327 | | tuple_struct map struct identifier ignored_any |
328 | | } |
329 | | |
330 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
331 | 0 | where |
332 | 0 | V: de::Visitor<'de>, |
333 | 0 | { |
334 | 0 | visitor.visit_u32(self.value) |
335 | 0 | } |
336 | | |
337 | 0 | fn deserialize_enum<V>( |
338 | 0 | self, |
339 | 0 | name: &str, |
340 | 0 | variants: &'static [&'static str], |
341 | 0 | visitor: V, |
342 | 0 | ) -> Result<V::Value, Self::Error> |
343 | 0 | where |
344 | 0 | V: de::Visitor<'de>, |
345 | 0 | { |
346 | 0 | let _ = name; |
347 | 0 | let _ = variants; |
348 | 0 | visitor.visit_enum(self) |
349 | 0 | } |
350 | | } |
351 | | |
352 | | impl<'de, E> de::EnumAccess<'de> for U32Deserializer<E> |
353 | | where |
354 | | E: de::Error, |
355 | | { |
356 | | type Error = E; |
357 | | type Variant = private::UnitOnly<E>; |
358 | | |
359 | 0 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
360 | 0 | where |
361 | 0 | T: de::DeserializeSeed<'de>, |
362 | 0 | { |
363 | 0 | seed.deserialize(self).map(private::unit_only) |
364 | 0 | } |
365 | | } |
366 | | |
367 | | impl<E> Debug for U32Deserializer<E> { |
368 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
369 | 0 | formatter |
370 | 0 | .debug_struct("U32Deserializer") |
371 | 0 | .field("value", &self.value) |
372 | 0 | .finish() |
373 | 0 | } |
374 | | } |
375 | | |
376 | | //////////////////////////////////////////////////////////////////////////////// |
377 | | |
378 | | /// A deserializer holding a `&str`. |
379 | | pub struct StrDeserializer<'a, E> { |
380 | | value: &'a str, |
381 | | marker: PhantomData<E>, |
382 | | } |
383 | | |
384 | | impl_copy_clone!(StrDeserializer<'de>); |
385 | | |
386 | | impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a str |
387 | | where |
388 | | E: de::Error, |
389 | | { |
390 | | type Deserializer = StrDeserializer<'a, E>; |
391 | | |
392 | 0 | fn into_deserializer(self) -> StrDeserializer<'a, E> { |
393 | 0 | StrDeserializer { |
394 | 0 | value: self, |
395 | 0 | marker: PhantomData, |
396 | 0 | } |
397 | 0 | } |
398 | | } |
399 | | |
400 | | impl<'de, 'a, E> de::Deserializer<'de> for StrDeserializer<'a, E> |
401 | | where |
402 | | E: de::Error, |
403 | | { |
404 | | type Error = E; |
405 | | |
406 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
407 | 0 | where |
408 | 0 | V: de::Visitor<'de>, |
409 | 0 | { |
410 | 0 | visitor.visit_str(self.value) |
411 | 0 | } |
412 | | |
413 | 0 | fn deserialize_enum<V>( |
414 | 0 | self, |
415 | 0 | name: &str, |
416 | 0 | variants: &'static [&'static str], |
417 | 0 | visitor: V, |
418 | 0 | ) -> Result<V::Value, Self::Error> |
419 | 0 | where |
420 | 0 | V: de::Visitor<'de>, |
421 | 0 | { |
422 | 0 | let _ = name; |
423 | 0 | let _ = variants; |
424 | 0 | visitor.visit_enum(self) |
425 | 0 | } |
426 | | |
427 | | forward_to_deserialize_any! { |
428 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
429 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
430 | | tuple_struct map struct identifier ignored_any |
431 | | } |
432 | | } |
433 | | |
434 | | impl<'de, 'a, E> de::EnumAccess<'de> for StrDeserializer<'a, E> |
435 | | where |
436 | | E: de::Error, |
437 | | { |
438 | | type Error = E; |
439 | | type Variant = private::UnitOnly<E>; |
440 | | |
441 | 0 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
442 | 0 | where |
443 | 0 | T: de::DeserializeSeed<'de>, |
444 | 0 | { |
445 | 0 | seed.deserialize(self).map(private::unit_only) |
446 | 0 | } |
447 | | } |
448 | | |
449 | | impl<'a, E> Debug for StrDeserializer<'a, E> { |
450 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
451 | 0 | formatter |
452 | 0 | .debug_struct("StrDeserializer") |
453 | 0 | .field("value", &self.value) |
454 | 0 | .finish() |
455 | 0 | } |
456 | | } |
457 | | |
458 | | //////////////////////////////////////////////////////////////////////////////// |
459 | | |
460 | | /// A deserializer holding a `&str` with a lifetime tied to another |
461 | | /// deserializer. |
462 | | pub struct BorrowedStrDeserializer<'de, E> { |
463 | | value: &'de str, |
464 | | marker: PhantomData<E>, |
465 | | } |
466 | | |
467 | | impl_copy_clone!(BorrowedStrDeserializer<'de>); |
468 | | |
469 | | impl<'de, E> BorrowedStrDeserializer<'de, E> { |
470 | | /// Create a new borrowed deserializer from the given string. |
471 | 0 | pub fn new(value: &'de str) -> BorrowedStrDeserializer<'de, E> { |
472 | 0 | BorrowedStrDeserializer { |
473 | 0 | value: value, |
474 | 0 | marker: PhantomData, |
475 | 0 | } |
476 | 0 | } |
477 | | } |
478 | | |
479 | | impl<'de, E> de::Deserializer<'de> for BorrowedStrDeserializer<'de, E> |
480 | | where |
481 | | E: de::Error, |
482 | | { |
483 | | type Error = E; |
484 | | |
485 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
486 | 0 | where |
487 | 0 | V: de::Visitor<'de>, |
488 | 0 | { |
489 | 0 | visitor.visit_borrowed_str(self.value) |
490 | 0 | } |
491 | | |
492 | 0 | fn deserialize_enum<V>( |
493 | 0 | self, |
494 | 0 | name: &str, |
495 | 0 | variants: &'static [&'static str], |
496 | 0 | visitor: V, |
497 | 0 | ) -> Result<V::Value, Self::Error> |
498 | 0 | where |
499 | 0 | V: de::Visitor<'de>, |
500 | 0 | { |
501 | 0 | let _ = name; |
502 | 0 | let _ = variants; |
503 | 0 | visitor.visit_enum(self) |
504 | 0 | } |
505 | | |
506 | | forward_to_deserialize_any! { |
507 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
508 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
509 | | tuple_struct map struct identifier ignored_any |
510 | | } |
511 | | } |
512 | | |
513 | | impl<'de, E> de::EnumAccess<'de> for BorrowedStrDeserializer<'de, E> |
514 | | where |
515 | | E: de::Error, |
516 | | { |
517 | | type Error = E; |
518 | | type Variant = private::UnitOnly<E>; |
519 | | |
520 | 0 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
521 | 0 | where |
522 | 0 | T: de::DeserializeSeed<'de>, |
523 | 0 | { |
524 | 0 | seed.deserialize(self).map(private::unit_only) |
525 | 0 | } |
526 | | } |
527 | | |
528 | | impl<'de, E> Debug for BorrowedStrDeserializer<'de, E> { |
529 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
530 | 0 | formatter |
531 | 0 | .debug_struct("BorrowedStrDeserializer") |
532 | 0 | .field("value", &self.value) |
533 | 0 | .finish() |
534 | 0 | } |
535 | | } |
536 | | |
537 | | //////////////////////////////////////////////////////////////////////////////// |
538 | | |
539 | | /// A deserializer holding a `String`. |
540 | | #[cfg(any(feature = "std", feature = "alloc"))] |
541 | | pub struct StringDeserializer<E> { |
542 | | value: String, |
543 | | marker: PhantomData<E>, |
544 | | } |
545 | | |
546 | | #[cfg(any(feature = "std", feature = "alloc"))] |
547 | | impl<E> Clone for StringDeserializer<E> { |
548 | 0 | fn clone(&self) -> Self { |
549 | 0 | StringDeserializer { |
550 | 0 | value: self.value.clone(), |
551 | 0 | marker: PhantomData, |
552 | 0 | } |
553 | 0 | } |
554 | | } |
555 | | |
556 | | #[cfg(any(feature = "std", feature = "alloc"))] |
557 | | impl<'de, E> IntoDeserializer<'de, E> for String |
558 | | where |
559 | | E: de::Error, |
560 | | { |
561 | | type Deserializer = StringDeserializer<E>; |
562 | | |
563 | 0 | fn into_deserializer(self) -> StringDeserializer<E> { |
564 | 0 | StringDeserializer { |
565 | 0 | value: self, |
566 | 0 | marker: PhantomData, |
567 | 0 | } |
568 | 0 | } |
569 | | } |
570 | | |
571 | | #[cfg(any(feature = "std", feature = "alloc"))] |
572 | | impl<'de, E> de::Deserializer<'de> for StringDeserializer<E> |
573 | | where |
574 | | E: de::Error, |
575 | | { |
576 | | type Error = E; |
577 | | |
578 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
579 | 0 | where |
580 | 0 | V: de::Visitor<'de>, |
581 | 0 | { |
582 | 0 | visitor.visit_string(self.value) |
583 | 0 | } |
584 | | |
585 | 0 | fn deserialize_enum<V>( |
586 | 0 | self, |
587 | 0 | name: &str, |
588 | 0 | variants: &'static [&'static str], |
589 | 0 | visitor: V, |
590 | 0 | ) -> Result<V::Value, Self::Error> |
591 | 0 | where |
592 | 0 | V: de::Visitor<'de>, |
593 | 0 | { |
594 | 0 | let _ = name; |
595 | 0 | let _ = variants; |
596 | 0 | visitor.visit_enum(self) |
597 | 0 | } |
598 | | |
599 | | forward_to_deserialize_any! { |
600 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
601 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
602 | | tuple_struct map struct identifier ignored_any |
603 | | } |
604 | | } |
605 | | |
606 | | #[cfg(any(feature = "std", feature = "alloc"))] |
607 | | impl<'de, 'a, E> de::EnumAccess<'de> for StringDeserializer<E> |
608 | | where |
609 | | E: de::Error, |
610 | | { |
611 | | type Error = E; |
612 | | type Variant = private::UnitOnly<E>; |
613 | | |
614 | 0 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
615 | 0 | where |
616 | 0 | T: de::DeserializeSeed<'de>, |
617 | 0 | { |
618 | 0 | seed.deserialize(self).map(private::unit_only) |
619 | 0 | } |
620 | | } |
621 | | |
622 | | #[cfg(any(feature = "std", feature = "alloc"))] |
623 | | impl<E> Debug for StringDeserializer<E> { |
624 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
625 | 0 | formatter |
626 | 0 | .debug_struct("StringDeserializer") |
627 | 0 | .field("value", &self.value) |
628 | 0 | .finish() |
629 | 0 | } |
630 | | } |
631 | | |
632 | | //////////////////////////////////////////////////////////////////////////////// |
633 | | |
634 | | /// A deserializer holding a `Cow<str>`. |
635 | | #[cfg(any(feature = "std", feature = "alloc"))] |
636 | | pub struct CowStrDeserializer<'a, E> { |
637 | | value: Cow<'a, str>, |
638 | | marker: PhantomData<E>, |
639 | | } |
640 | | |
641 | | #[cfg(any(feature = "std", feature = "alloc"))] |
642 | | impl<'a, E> Clone for CowStrDeserializer<'a, E> { |
643 | 0 | fn clone(&self) -> Self { |
644 | 0 | CowStrDeserializer { |
645 | 0 | value: self.value.clone(), |
646 | 0 | marker: PhantomData, |
647 | 0 | } |
648 | 0 | } |
649 | | } |
650 | | |
651 | | #[cfg(any(feature = "std", feature = "alloc"))] |
652 | | impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str> |
653 | | where |
654 | | E: de::Error, |
655 | | { |
656 | | type Deserializer = CowStrDeserializer<'a, E>; |
657 | | |
658 | 0 | fn into_deserializer(self) -> CowStrDeserializer<'a, E> { |
659 | 0 | CowStrDeserializer { |
660 | 0 | value: self, |
661 | 0 | marker: PhantomData, |
662 | 0 | } |
663 | 0 | } |
664 | | } |
665 | | |
666 | | #[cfg(any(feature = "std", feature = "alloc"))] |
667 | | impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E> |
668 | | where |
669 | | E: de::Error, |
670 | | { |
671 | | type Error = E; |
672 | | |
673 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
674 | 0 | where |
675 | 0 | V: de::Visitor<'de>, |
676 | 0 | { |
677 | 0 | match self.value { |
678 | 0 | Cow::Borrowed(string) => visitor.visit_str(string), |
679 | 0 | Cow::Owned(string) => visitor.visit_string(string), |
680 | | } |
681 | 0 | } |
682 | | |
683 | 0 | fn deserialize_enum<V>( |
684 | 0 | self, |
685 | 0 | name: &str, |
686 | 0 | variants: &'static [&'static str], |
687 | 0 | visitor: V, |
688 | 0 | ) -> Result<V::Value, Self::Error> |
689 | 0 | where |
690 | 0 | V: de::Visitor<'de>, |
691 | 0 | { |
692 | 0 | let _ = name; |
693 | 0 | let _ = variants; |
694 | 0 | visitor.visit_enum(self) |
695 | 0 | } |
696 | | |
697 | | forward_to_deserialize_any! { |
698 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
699 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
700 | | tuple_struct map struct identifier ignored_any |
701 | | } |
702 | | } |
703 | | |
704 | | #[cfg(any(feature = "std", feature = "alloc"))] |
705 | | impl<'de, 'a, E> de::EnumAccess<'de> for CowStrDeserializer<'a, E> |
706 | | where |
707 | | E: de::Error, |
708 | | { |
709 | | type Error = E; |
710 | | type Variant = private::UnitOnly<E>; |
711 | | |
712 | 0 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
713 | 0 | where |
714 | 0 | T: de::DeserializeSeed<'de>, |
715 | 0 | { |
716 | 0 | seed.deserialize(self).map(private::unit_only) |
717 | 0 | } |
718 | | } |
719 | | |
720 | | #[cfg(any(feature = "std", feature = "alloc"))] |
721 | | impl<'a, E> Debug for CowStrDeserializer<'a, E> { |
722 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
723 | 0 | formatter |
724 | 0 | .debug_struct("CowStrDeserializer") |
725 | 0 | .field("value", &self.value) |
726 | 0 | .finish() |
727 | 0 | } |
728 | | } |
729 | | |
730 | | //////////////////////////////////////////////////////////////////////////////// |
731 | | |
732 | | /// A deserializer holding a `&[u8]`. Always calls [`Visitor::visit_bytes`]. |
733 | | pub struct BytesDeserializer<'a, E> { |
734 | | value: &'a [u8], |
735 | | marker: PhantomData<E>, |
736 | | } |
737 | | |
738 | | impl<'a, E> BytesDeserializer<'a, E> { |
739 | | /// Create a new deserializer from the given bytes. |
740 | 0 | pub fn new(value: &'a [u8]) -> Self { |
741 | 0 | BytesDeserializer { |
742 | 0 | value: value, |
743 | 0 | marker: PhantomData, |
744 | 0 | } |
745 | 0 | } |
746 | | } |
747 | | |
748 | | impl_copy_clone!(BytesDeserializer<'a>); |
749 | | |
750 | | impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a [u8] |
751 | | where |
752 | | E: de::Error, |
753 | | { |
754 | | type Deserializer = BytesDeserializer<'a, E>; |
755 | | |
756 | 0 | fn into_deserializer(self) -> BytesDeserializer<'a, E> { |
757 | 0 | BytesDeserializer::new(self) |
758 | 0 | } |
759 | | } |
760 | | |
761 | | impl<'de, 'a, E> Deserializer<'de> for BytesDeserializer<'a, E> |
762 | | where |
763 | | E: de::Error, |
764 | | { |
765 | | type Error = E; |
766 | | |
767 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
768 | 0 | where |
769 | 0 | V: Visitor<'de>, |
770 | 0 | { |
771 | 0 | visitor.visit_bytes(self.value) |
772 | 0 | } |
773 | | |
774 | | forward_to_deserialize_any! { |
775 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
776 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
777 | | tuple_struct map struct enum identifier ignored_any |
778 | | } |
779 | | } |
780 | | |
781 | | impl<'a, E> Debug for BytesDeserializer<'a, E> { |
782 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
783 | 0 | formatter |
784 | 0 | .debug_struct("BytesDeserializer") |
785 | 0 | .field("value", &self.value) |
786 | 0 | .finish() |
787 | 0 | } |
788 | | } |
789 | | |
790 | | /// A deserializer holding a `&[u8]` with a lifetime tied to another |
791 | | /// deserializer. Always calls [`Visitor::visit_borrowed_bytes`]. |
792 | | pub struct BorrowedBytesDeserializer<'de, E> { |
793 | | value: &'de [u8], |
794 | | marker: PhantomData<E>, |
795 | | } |
796 | | |
797 | | impl<'de, E> BorrowedBytesDeserializer<'de, E> { |
798 | | /// Create a new borrowed deserializer from the given borrowed bytes. |
799 | 0 | pub fn new(value: &'de [u8]) -> Self { |
800 | 0 | BorrowedBytesDeserializer { |
801 | 0 | value: value, |
802 | 0 | marker: PhantomData, |
803 | 0 | } |
804 | 0 | } |
805 | | } |
806 | | |
807 | | impl_copy_clone!(BorrowedBytesDeserializer<'de>); |
808 | | |
809 | | impl<'de, E> Deserializer<'de> for BorrowedBytesDeserializer<'de, E> |
810 | | where |
811 | | E: de::Error, |
812 | | { |
813 | | type Error = E; |
814 | | |
815 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
816 | 0 | where |
817 | 0 | V: Visitor<'de>, |
818 | 0 | { |
819 | 0 | visitor.visit_borrowed_bytes(self.value) |
820 | 0 | } |
821 | | |
822 | | forward_to_deserialize_any! { |
823 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
824 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
825 | | tuple_struct map struct enum identifier ignored_any |
826 | | } |
827 | | } |
828 | | |
829 | | impl<'de, E> Debug for BorrowedBytesDeserializer<'de, E> { |
830 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
831 | 0 | formatter |
832 | 0 | .debug_struct("BorrowedBytesDeserializer") |
833 | 0 | .field("value", &self.value) |
834 | 0 | .finish() |
835 | 0 | } |
836 | | } |
837 | | |
838 | | //////////////////////////////////////////////////////////////////////////////// |
839 | | |
840 | | /// A deserializer that iterates over a sequence. |
841 | 0 | #[derive(Clone)] |
842 | | pub struct SeqDeserializer<I, E> { |
843 | | iter: iter::Fuse<I>, |
844 | | count: usize, |
845 | | marker: PhantomData<E>, |
846 | | } |
847 | | |
848 | | impl<I, E> SeqDeserializer<I, E> |
849 | | where |
850 | | I: Iterator, |
851 | | { |
852 | | /// Construct a new `SeqDeserializer<I, E>`. |
853 | 0 | pub fn new(iter: I) -> Self { |
854 | 0 | SeqDeserializer { |
855 | 0 | iter: iter.fuse(), |
856 | 0 | count: 0, |
857 | 0 | marker: PhantomData, |
858 | 0 | } |
859 | 0 | } |
860 | | } |
861 | | |
862 | | impl<I, E> SeqDeserializer<I, E> |
863 | | where |
864 | | I: Iterator, |
865 | | E: de::Error, |
866 | | { |
867 | | /// Check for remaining elements after passing a `SeqDeserializer` to |
868 | | /// `Visitor::visit_seq`. |
869 | 0 | pub fn end(self) -> Result<(), E> { |
870 | 0 | let remaining = self.iter.count(); |
871 | 0 | if remaining == 0 { |
872 | 0 | Ok(()) |
873 | | } else { |
874 | | // First argument is the number of elements in the data, second |
875 | | // argument is the number of elements expected by the Deserialize. |
876 | 0 | Err(de::Error::invalid_length( |
877 | 0 | self.count + remaining, |
878 | 0 | &ExpectedInSeq(self.count), |
879 | 0 | )) |
880 | | } |
881 | 0 | } |
882 | | } |
883 | | |
884 | | impl<'de, I, T, E> de::Deserializer<'de> for SeqDeserializer<I, E> |
885 | | where |
886 | | I: Iterator<Item = T>, |
887 | | T: IntoDeserializer<'de, E>, |
888 | | E: de::Error, |
889 | | { |
890 | | type Error = E; |
891 | | |
892 | 0 | fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> |
893 | 0 | where |
894 | 0 | V: de::Visitor<'de>, |
895 | 0 | { |
896 | 0 | let v = try!(visitor.visit_seq(&mut self)); |
897 | 0 | try!(self.end()); |
898 | 0 | Ok(v) |
899 | 0 | } |
900 | | |
901 | | forward_to_deserialize_any! { |
902 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
903 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
904 | | tuple_struct map struct enum identifier ignored_any |
905 | | } |
906 | | } |
907 | | |
908 | | impl<'de, I, T, E> de::SeqAccess<'de> for SeqDeserializer<I, E> |
909 | | where |
910 | | I: Iterator<Item = T>, |
911 | | T: IntoDeserializer<'de, E>, |
912 | | E: de::Error, |
913 | | { |
914 | | type Error = E; |
915 | | |
916 | 0 | fn next_element_seed<V>(&mut self, seed: V) -> Result<Option<V::Value>, Self::Error> |
917 | 0 | where |
918 | 0 | V: de::DeserializeSeed<'de>, |
919 | 0 | { |
920 | 0 | match self.iter.next() { |
921 | 0 | Some(value) => { |
922 | 0 | self.count += 1; |
923 | 0 | seed.deserialize(value.into_deserializer()).map(Some) |
924 | | } |
925 | 0 | None => Ok(None), |
926 | | } |
927 | 0 | } Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<serde::de::ignored_any::IgnoredAny>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<wasmtime_cache::config::CacheConfig as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::__DeserializeWith>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<wasmtime_cache::config::CacheConfig as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::__DeserializeWith>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<wasmtime_cache::config::CacheConfig as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::__DeserializeWith>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<core::option::Option<i32>>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<wasmtime_cache::config::CacheConfig as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::__DeserializeWith>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<wasmtime_cache::config::CacheConfig as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::__DeserializeWith>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<wasmtime_cache::config::CacheConfig as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::__DeserializeWith>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<wasmtime_cache::config::CacheConfig as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::__DeserializeWith>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<wasmtime_cache::config::CacheConfig as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::__DeserializeWith>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<core::option::Option<std::path::PathBuf>>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<wasmtime_cache::config::CacheConfig as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::__DeserializeWith>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<bool>> Unexecuted instantiation: <serde::de::value::SeqDeserializer<alloc::vec::into_iter::IntoIter<toml::de::Value>, toml::de::Error> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<toml::value::Value>> |
928 | | |
929 | 0 | fn size_hint(&self) -> Option<usize> { |
930 | 0 | size_hint::from_bounds(&self.iter) |
931 | 0 | } |
932 | | } |
933 | | |
934 | | struct ExpectedInSeq(usize); |
935 | | |
936 | | impl Expected for ExpectedInSeq { |
937 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
938 | 0 | if self.0 == 1 { |
939 | 0 | write!(formatter, "1 element in sequence") |
940 | | } else { |
941 | 0 | write!(formatter, "{} elements in sequence", self.0) |
942 | | } |
943 | 0 | } |
944 | | } |
945 | | |
946 | | impl<I, E> Debug for SeqDeserializer<I, E> |
947 | | where |
948 | | I: Debug, |
949 | | { |
950 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
951 | 0 | formatter |
952 | 0 | .debug_struct("SeqDeserializer") |
953 | 0 | .field("iter", &self.iter) |
954 | 0 | .field("count", &self.count) |
955 | 0 | .finish() |
956 | 0 | } |
957 | | } |
958 | | |
959 | | //////////////////////////////////////////////////////////////////////////////// |
960 | | |
961 | | #[cfg(any(feature = "std", feature = "alloc"))] |
962 | | impl<'de, T, E> IntoDeserializer<'de, E> for Vec<T> |
963 | | where |
964 | | T: IntoDeserializer<'de, E>, |
965 | | E: de::Error, |
966 | | { |
967 | | type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>; |
968 | | |
969 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
970 | 0 | SeqDeserializer::new(self.into_iter()) |
971 | 0 | } |
972 | | } |
973 | | |
974 | | #[cfg(any(feature = "std", feature = "alloc"))] |
975 | | impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet<T> |
976 | | where |
977 | | T: IntoDeserializer<'de, E> + Eq + Ord, |
978 | | E: de::Error, |
979 | | { |
980 | | type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>; |
981 | | |
982 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
983 | 0 | SeqDeserializer::new(self.into_iter()) |
984 | 0 | } |
985 | | } |
986 | | |
987 | | #[cfg(feature = "std")] |
988 | | impl<'de, T, S, E> IntoDeserializer<'de, E> for HashSet<T, S> |
989 | | where |
990 | | T: IntoDeserializer<'de, E> + Eq + Hash, |
991 | | S: BuildHasher, |
992 | | E: de::Error, |
993 | | { |
994 | | type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>; |
995 | | |
996 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
997 | 0 | SeqDeserializer::new(self.into_iter()) |
998 | 0 | } |
999 | | } |
1000 | | |
1001 | | //////////////////////////////////////////////////////////////////////////////// |
1002 | | |
1003 | | /// A deserializer holding a `SeqAccess`. |
1004 | 0 | #[derive(Clone, Debug)] |
1005 | | pub struct SeqAccessDeserializer<A> { |
1006 | | seq: A, |
1007 | | } |
1008 | | |
1009 | | impl<A> SeqAccessDeserializer<A> { |
1010 | | /// Construct a new `SeqAccessDeserializer<A>`. |
1011 | 0 | pub fn new(seq: A) -> Self { |
1012 | 0 | SeqAccessDeserializer { seq: seq } |
1013 | 0 | } |
1014 | | } |
1015 | | |
1016 | | impl<'de, A> de::Deserializer<'de> for SeqAccessDeserializer<A> |
1017 | | where |
1018 | | A: de::SeqAccess<'de>, |
1019 | | { |
1020 | | type Error = A::Error; |
1021 | | |
1022 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1023 | 0 | where |
1024 | 0 | V: de::Visitor<'de>, |
1025 | 0 | { |
1026 | 0 | visitor.visit_seq(self.seq) |
1027 | 0 | } |
1028 | | |
1029 | | forward_to_deserialize_any! { |
1030 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1031 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
1032 | | tuple_struct map struct enum identifier ignored_any |
1033 | | } |
1034 | | } |
1035 | | |
1036 | | //////////////////////////////////////////////////////////////////////////////// |
1037 | | |
1038 | | /// A deserializer that iterates over a map. |
1039 | | pub struct MapDeserializer<'de, I, E> |
1040 | | where |
1041 | | I: Iterator, |
1042 | | I::Item: private::Pair, |
1043 | | { |
1044 | | iter: iter::Fuse<I>, |
1045 | | value: Option<Second<I::Item>>, |
1046 | | count: usize, |
1047 | | lifetime: PhantomData<&'de ()>, |
1048 | | error: PhantomData<E>, |
1049 | | } |
1050 | | |
1051 | | impl<'de, I, E> MapDeserializer<'de, I, E> |
1052 | | where |
1053 | | I: Iterator, |
1054 | | I::Item: private::Pair, |
1055 | | { |
1056 | | /// Construct a new `MapDeserializer<I, E>`. |
1057 | 0 | pub fn new(iter: I) -> Self { |
1058 | 0 | MapDeserializer { |
1059 | 0 | iter: iter.fuse(), |
1060 | 0 | value: None, |
1061 | 0 | count: 0, |
1062 | 0 | lifetime: PhantomData, |
1063 | 0 | error: PhantomData, |
1064 | 0 | } |
1065 | 0 | } |
1066 | | } |
1067 | | |
1068 | | impl<'de, I, E> MapDeserializer<'de, I, E> |
1069 | | where |
1070 | | I: Iterator, |
1071 | | I::Item: private::Pair, |
1072 | | E: de::Error, |
1073 | | { |
1074 | | /// Check for remaining elements after passing a `MapDeserializer` to |
1075 | | /// `Visitor::visit_map`. |
1076 | 0 | pub fn end(self) -> Result<(), E> { |
1077 | 0 | let remaining = self.iter.count(); |
1078 | 0 | if remaining == 0 { |
1079 | 0 | Ok(()) |
1080 | | } else { |
1081 | | // First argument is the number of elements in the data, second |
1082 | | // argument is the number of elements expected by the Deserialize. |
1083 | 0 | Err(de::Error::invalid_length( |
1084 | 0 | self.count + remaining, |
1085 | 0 | &ExpectedInMap(self.count), |
1086 | 0 | )) |
1087 | | } |
1088 | 0 | } |
1089 | | } |
1090 | | |
1091 | | impl<'de, I, E> MapDeserializer<'de, I, E> |
1092 | | where |
1093 | | I: Iterator, |
1094 | | I::Item: private::Pair, |
1095 | | { |
1096 | 0 | fn next_pair(&mut self) -> Option<(First<I::Item>, Second<I::Item>)> { |
1097 | 0 | match self.iter.next() { |
1098 | 0 | Some(kv) => { |
1099 | 0 | self.count += 1; |
1100 | 0 | Some(private::Pair::split(kv)) |
1101 | | } |
1102 | 0 | None => None, |
1103 | | } |
1104 | 0 | } |
1105 | | } |
1106 | | |
1107 | | impl<'de, I, E> de::Deserializer<'de> for MapDeserializer<'de, I, E> |
1108 | | where |
1109 | | I: Iterator, |
1110 | | I::Item: private::Pair, |
1111 | | First<I::Item>: IntoDeserializer<'de, E>, |
1112 | | Second<I::Item>: IntoDeserializer<'de, E>, |
1113 | | E: de::Error, |
1114 | | { |
1115 | | type Error = E; |
1116 | | |
1117 | 0 | fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> |
1118 | 0 | where |
1119 | 0 | V: de::Visitor<'de>, |
1120 | 0 | { |
1121 | 0 | let value = try!(visitor.visit_map(&mut self)); |
1122 | 0 | try!(self.end()); |
1123 | 0 | Ok(value) |
1124 | 0 | } |
1125 | | |
1126 | 0 | fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> |
1127 | 0 | where |
1128 | 0 | V: de::Visitor<'de>, |
1129 | 0 | { |
1130 | 0 | let value = try!(visitor.visit_seq(&mut self)); |
1131 | 0 | try!(self.end()); |
1132 | 0 | Ok(value) |
1133 | 0 | } |
1134 | | |
1135 | 0 | fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
1136 | 0 | where |
1137 | 0 | V: de::Visitor<'de>, |
1138 | 0 | { |
1139 | 0 | let _ = len; |
1140 | 0 | self.deserialize_seq(visitor) |
1141 | 0 | } |
1142 | | |
1143 | | forward_to_deserialize_any! { |
1144 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1145 | | bytes byte_buf option unit unit_struct newtype_struct tuple_struct map |
1146 | | struct enum identifier ignored_any |
1147 | | } |
1148 | | } |
1149 | | |
1150 | | impl<'de, I, E> de::MapAccess<'de> for MapDeserializer<'de, I, E> |
1151 | | where |
1152 | | I: Iterator, |
1153 | | I::Item: private::Pair, |
1154 | | First<I::Item>: IntoDeserializer<'de, E>, |
1155 | | Second<I::Item>: IntoDeserializer<'de, E>, |
1156 | | E: de::Error, |
1157 | | { |
1158 | | type Error = E; |
1159 | | |
1160 | 0 | fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
1161 | 0 | where |
1162 | 0 | T: de::DeserializeSeed<'de>, |
1163 | 0 | { |
1164 | 0 | match self.next_pair() { |
1165 | 0 | Some((key, value)) => { |
1166 | 0 | self.value = Some(value); |
1167 | 0 | seed.deserialize(key.into_deserializer()).map(Some) |
1168 | | } |
1169 | 0 | None => Ok(None), |
1170 | | } |
1171 | 0 | } |
1172 | | |
1173 | 0 | fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> |
1174 | 0 | where |
1175 | 0 | T: de::DeserializeSeed<'de>, |
1176 | 0 | { |
1177 | 0 | let value = self.value.take(); |
1178 | 0 | // Panic because this indicates a bug in the program rather than an |
1179 | 0 | // expected failure. |
1180 | 0 | let value = value.expect("MapAccess::next_value called before next_key"); |
1181 | 0 | seed.deserialize(value.into_deserializer()) |
1182 | 0 | } |
1183 | | |
1184 | 0 | fn next_entry_seed<TK, TV>( |
1185 | 0 | &mut self, |
1186 | 0 | kseed: TK, |
1187 | 0 | vseed: TV, |
1188 | 0 | ) -> Result<Option<(TK::Value, TV::Value)>, Self::Error> |
1189 | 0 | where |
1190 | 0 | TK: de::DeserializeSeed<'de>, |
1191 | 0 | TV: de::DeserializeSeed<'de>, |
1192 | 0 | { |
1193 | 0 | match self.next_pair() { |
1194 | 0 | Some((key, value)) => { |
1195 | 0 | let key = try!(kseed.deserialize(key.into_deserializer())); |
1196 | 0 | let value = try!(vseed.deserialize(value.into_deserializer())); |
1197 | 0 | Ok(Some((key, value))) |
1198 | | } |
1199 | 0 | None => Ok(None), |
1200 | | } |
1201 | 0 | } |
1202 | | |
1203 | 0 | fn size_hint(&self) -> Option<usize> { |
1204 | 0 | size_hint::from_bounds(&self.iter) |
1205 | 0 | } |
1206 | | } |
1207 | | |
1208 | | impl<'de, I, E> de::SeqAccess<'de> for MapDeserializer<'de, I, E> |
1209 | | where |
1210 | | I: Iterator, |
1211 | | I::Item: private::Pair, |
1212 | | First<I::Item>: IntoDeserializer<'de, E>, |
1213 | | Second<I::Item>: IntoDeserializer<'de, E>, |
1214 | | E: de::Error, |
1215 | | { |
1216 | | type Error = E; |
1217 | | |
1218 | 0 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
1219 | 0 | where |
1220 | 0 | T: de::DeserializeSeed<'de>, |
1221 | 0 | { |
1222 | 0 | match self.next_pair() { |
1223 | 0 | Some((k, v)) => { |
1224 | 0 | let de = PairDeserializer(k, v, PhantomData); |
1225 | 0 | seed.deserialize(de).map(Some) |
1226 | | } |
1227 | 0 | None => Ok(None), |
1228 | | } |
1229 | 0 | } |
1230 | | |
1231 | 0 | fn size_hint(&self) -> Option<usize> { |
1232 | 0 | size_hint::from_bounds(&self.iter) |
1233 | 0 | } |
1234 | | } |
1235 | | |
1236 | | // Cannot #[derive(Clone)] because of the bound `Second<I::Item>: Clone`. |
1237 | | impl<'de, I, E> Clone for MapDeserializer<'de, I, E> |
1238 | | where |
1239 | | I: Iterator + Clone, |
1240 | | I::Item: private::Pair, |
1241 | | Second<I::Item>: Clone, |
1242 | | { |
1243 | 0 | fn clone(&self) -> Self { |
1244 | 0 | MapDeserializer { |
1245 | 0 | iter: self.iter.clone(), |
1246 | 0 | value: self.value.clone(), |
1247 | 0 | count: self.count, |
1248 | 0 | lifetime: self.lifetime, |
1249 | 0 | error: self.error, |
1250 | 0 | } |
1251 | 0 | } |
1252 | | } |
1253 | | |
1254 | | impl<'de, I, E> Debug for MapDeserializer<'de, I, E> |
1255 | | where |
1256 | | I: Iterator + Debug, |
1257 | | I::Item: private::Pair, |
1258 | | Second<I::Item>: Debug, |
1259 | | { |
1260 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
1261 | 0 | formatter |
1262 | 0 | .debug_struct("MapDeserializer") |
1263 | 0 | .field("iter", &self.iter) |
1264 | 0 | .field("value", &self.value) |
1265 | 0 | .field("count", &self.count) |
1266 | 0 | .finish() |
1267 | 0 | } |
1268 | | } |
1269 | | |
1270 | | // Used in the `impl SeqAccess for MapDeserializer` to visit the map as a |
1271 | | // sequence of pairs. |
1272 | | struct PairDeserializer<A, B, E>(A, B, PhantomData<E>); |
1273 | | |
1274 | | impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer<A, B, E> |
1275 | | where |
1276 | | A: IntoDeserializer<'de, E>, |
1277 | | B: IntoDeserializer<'de, E>, |
1278 | | E: de::Error, |
1279 | | { |
1280 | | type Error = E; |
1281 | | |
1282 | | forward_to_deserialize_any! { |
1283 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1284 | | bytes byte_buf option unit unit_struct newtype_struct tuple_struct map |
1285 | | struct enum identifier ignored_any |
1286 | | } |
1287 | | |
1288 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1289 | 0 | where |
1290 | 0 | V: de::Visitor<'de>, |
1291 | 0 | { |
1292 | 0 | self.deserialize_seq(visitor) |
1293 | 0 | } |
1294 | | |
1295 | 0 | fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1296 | 0 | where |
1297 | 0 | V: de::Visitor<'de>, |
1298 | 0 | { |
1299 | 0 | let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData); |
1300 | 0 | let pair = try!(visitor.visit_seq(&mut pair_visitor)); |
1301 | 0 | if pair_visitor.1.is_none() { |
1302 | 0 | Ok(pair) |
1303 | | } else { |
1304 | 0 | let remaining = pair_visitor.size_hint().unwrap(); |
1305 | 0 | // First argument is the number of elements in the data, second |
1306 | 0 | // argument is the number of elements expected by the Deserialize. |
1307 | 0 | Err(de::Error::invalid_length(2, &ExpectedInSeq(2 - remaining))) |
1308 | | } |
1309 | 0 | } |
1310 | | |
1311 | 0 | fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
1312 | 0 | where |
1313 | 0 | V: de::Visitor<'de>, |
1314 | 0 | { |
1315 | 0 | if len == 2 { |
1316 | 0 | self.deserialize_seq(visitor) |
1317 | | } else { |
1318 | | // First argument is the number of elements in the data, second |
1319 | | // argument is the number of elements expected by the Deserialize. |
1320 | 0 | Err(de::Error::invalid_length(2, &ExpectedInSeq(len))) |
1321 | | } |
1322 | 0 | } |
1323 | | } |
1324 | | |
1325 | | struct PairVisitor<A, B, E>(Option<A>, Option<B>, PhantomData<E>); |
1326 | | |
1327 | | impl<'de, A, B, E> de::SeqAccess<'de> for PairVisitor<A, B, E> |
1328 | | where |
1329 | | A: IntoDeserializer<'de, E>, |
1330 | | B: IntoDeserializer<'de, E>, |
1331 | | E: de::Error, |
1332 | | { |
1333 | | type Error = E; |
1334 | | |
1335 | | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
1336 | | where |
1337 | | T: de::DeserializeSeed<'de>, |
1338 | | { |
1339 | 0 | if let Some(k) = self.0.take() { |
1340 | 0 | seed.deserialize(k.into_deserializer()).map(Some) |
1341 | 0 | } else if let Some(v) = self.1.take() { |
1342 | 0 | seed.deserialize(v.into_deserializer()).map(Some) |
1343 | | } else { |
1344 | 0 | Ok(None) |
1345 | | } |
1346 | 0 | } |
1347 | | |
1348 | 0 | fn size_hint(&self) -> Option<usize> { |
1349 | 0 | if self.0.is_some() { |
1350 | 0 | Some(2) |
1351 | 0 | } else if self.1.is_some() { |
1352 | 0 | Some(1) |
1353 | | } else { |
1354 | 0 | Some(0) |
1355 | | } |
1356 | 0 | } |
1357 | | } |
1358 | | |
1359 | | struct ExpectedInMap(usize); |
1360 | | |
1361 | | impl Expected for ExpectedInMap { |
1362 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
1363 | 0 | if self.0 == 1 { |
1364 | 0 | write!(formatter, "1 element in map") |
1365 | | } else { |
1366 | 0 | write!(formatter, "{} elements in map", self.0) |
1367 | | } |
1368 | 0 | } |
1369 | | } |
1370 | | |
1371 | | //////////////////////////////////////////////////////////////////////////////// |
1372 | | |
1373 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1374 | | impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V> |
1375 | | where |
1376 | | K: IntoDeserializer<'de, E> + Eq + Ord, |
1377 | | V: IntoDeserializer<'de, E>, |
1378 | | E: de::Error, |
1379 | | { |
1380 | | type Deserializer = MapDeserializer<'de, <Self as IntoIterator>::IntoIter, E>; |
1381 | | |
1382 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
1383 | 0 | MapDeserializer::new(self.into_iter()) |
1384 | 0 | } |
1385 | | } |
1386 | | |
1387 | | #[cfg(feature = "std")] |
1388 | | impl<'de, K, V, S, E> IntoDeserializer<'de, E> for HashMap<K, V, S> |
1389 | | where |
1390 | | K: IntoDeserializer<'de, E> + Eq + Hash, |
1391 | | V: IntoDeserializer<'de, E>, |
1392 | | S: BuildHasher, |
1393 | | E: de::Error, |
1394 | | { |
1395 | | type Deserializer = MapDeserializer<'de, <Self as IntoIterator>::IntoIter, E>; |
1396 | | |
1397 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
1398 | 0 | MapDeserializer::new(self.into_iter()) |
1399 | 0 | } |
1400 | | } |
1401 | | |
1402 | | //////////////////////////////////////////////////////////////////////////////// |
1403 | | |
1404 | | /// A deserializer holding a `MapAccess`. |
1405 | 0 | #[derive(Clone, Debug)] |
1406 | | pub struct MapAccessDeserializer<A> { |
1407 | | map: A, |
1408 | | } |
1409 | | |
1410 | | impl<A> MapAccessDeserializer<A> { |
1411 | | /// Construct a new `MapAccessDeserializer<A>`. |
1412 | 0 | pub fn new(map: A) -> Self { |
1413 | 0 | MapAccessDeserializer { map: map } |
1414 | 0 | } |
1415 | | } |
1416 | | |
1417 | | impl<'de, A> de::Deserializer<'de> for MapAccessDeserializer<A> |
1418 | | where |
1419 | | A: de::MapAccess<'de>, |
1420 | | { |
1421 | | type Error = A::Error; |
1422 | | |
1423 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1424 | 0 | where |
1425 | 0 | V: de::Visitor<'de>, |
1426 | 0 | { |
1427 | 0 | visitor.visit_map(self.map) |
1428 | 0 | } |
1429 | | |
1430 | 0 | fn deserialize_enum<V>( |
1431 | 0 | self, |
1432 | 0 | _name: &str, |
1433 | 0 | _variants: &'static [&'static str], |
1434 | 0 | visitor: V, |
1435 | 0 | ) -> Result<V::Value, Self::Error> |
1436 | 0 | where |
1437 | 0 | V: de::Visitor<'de>, |
1438 | 0 | { |
1439 | 0 | visitor.visit_enum(self) |
1440 | 0 | } |
1441 | | |
1442 | | forward_to_deserialize_any! { |
1443 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1444 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
1445 | | tuple_struct map struct identifier ignored_any |
1446 | | } |
1447 | | } |
1448 | | |
1449 | | impl<'de, A> de::EnumAccess<'de> for MapAccessDeserializer<A> |
1450 | | where |
1451 | | A: de::MapAccess<'de>, |
1452 | | { |
1453 | | type Error = A::Error; |
1454 | | type Variant = private::MapAsEnum<A>; |
1455 | | |
1456 | | fn variant_seed<T>(mut self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
1457 | | where |
1458 | | T: de::DeserializeSeed<'de>, |
1459 | | { |
1460 | 0 | match self.map.next_key_seed(seed)? { |
1461 | 0 | Some(key) => Ok((key, private::map_as_enum(self.map))), |
1462 | 0 | None => Err(de::Error::invalid_type(de::Unexpected::Map, &"enum")), |
1463 | | } |
1464 | 0 | } |
1465 | | } |
1466 | | |
1467 | | //////////////////////////////////////////////////////////////////////////////// |
1468 | | |
1469 | | mod private { |
1470 | | use lib::*; |
1471 | | |
1472 | | use de::{self, DeserializeSeed, Deserializer, MapAccess, Unexpected, VariantAccess, Visitor}; |
1473 | | |
1474 | | pub struct UnitOnly<E> { |
1475 | | marker: PhantomData<E>, |
1476 | | } |
1477 | | |
1478 | 0 | pub fn unit_only<T, E>(t: T) -> (T, UnitOnly<E>) { |
1479 | 0 | ( |
1480 | 0 | t, |
1481 | 0 | UnitOnly { |
1482 | 0 | marker: PhantomData, |
1483 | 0 | }, |
1484 | 0 | ) |
1485 | 0 | } |
1486 | | |
1487 | | impl<'de, E> de::VariantAccess<'de> for UnitOnly<E> |
1488 | | where |
1489 | | E: de::Error, |
1490 | | { |
1491 | | type Error = E; |
1492 | | |
1493 | 0 | fn unit_variant(self) -> Result<(), Self::Error> { |
1494 | 0 | Ok(()) |
1495 | 0 | } |
1496 | | |
1497 | 0 | fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error> |
1498 | 0 | where |
1499 | 0 | T: de::DeserializeSeed<'de>, |
1500 | 0 | { |
1501 | 0 | Err(de::Error::invalid_type( |
1502 | 0 | Unexpected::UnitVariant, |
1503 | 0 | &"newtype variant", |
1504 | 0 | )) |
1505 | 0 | } |
1506 | | |
1507 | 0 | fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error> |
1508 | 0 | where |
1509 | 0 | V: de::Visitor<'de>, |
1510 | 0 | { |
1511 | 0 | Err(de::Error::invalid_type( |
1512 | 0 | Unexpected::UnitVariant, |
1513 | 0 | &"tuple variant", |
1514 | 0 | )) |
1515 | 0 | } |
1516 | | |
1517 | 0 | fn struct_variant<V>( |
1518 | 0 | self, |
1519 | 0 | _fields: &'static [&'static str], |
1520 | 0 | _visitor: V, |
1521 | 0 | ) -> Result<V::Value, Self::Error> |
1522 | 0 | where |
1523 | 0 | V: de::Visitor<'de>, |
1524 | 0 | { |
1525 | 0 | Err(de::Error::invalid_type( |
1526 | 0 | Unexpected::UnitVariant, |
1527 | 0 | &"struct variant", |
1528 | 0 | )) |
1529 | 0 | } |
1530 | | } |
1531 | | |
1532 | | pub struct MapAsEnum<A> { |
1533 | | map: A, |
1534 | | } |
1535 | | |
1536 | 0 | pub fn map_as_enum<A>(map: A) -> MapAsEnum<A> { |
1537 | 0 | MapAsEnum { map: map } |
1538 | 0 | } |
1539 | | |
1540 | | impl<'de, A> VariantAccess<'de> for MapAsEnum<A> |
1541 | | where |
1542 | | A: MapAccess<'de>, |
1543 | | { |
1544 | | type Error = A::Error; |
1545 | | |
1546 | 0 | fn unit_variant(mut self) -> Result<(), Self::Error> { |
1547 | 0 | self.map.next_value() |
1548 | 0 | } |
1549 | | |
1550 | 0 | fn newtype_variant_seed<T>(mut self, seed: T) -> Result<T::Value, Self::Error> |
1551 | 0 | where |
1552 | 0 | T: DeserializeSeed<'de>, |
1553 | 0 | { |
1554 | 0 | self.map.next_value_seed(seed) |
1555 | 0 | } |
1556 | | |
1557 | 0 | fn tuple_variant<V>(mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
1558 | 0 | where |
1559 | 0 | V: Visitor<'de>, |
1560 | 0 | { |
1561 | 0 | self.map.next_value_seed(SeedTupleVariant { |
1562 | 0 | len: len, |
1563 | 0 | visitor: visitor, |
1564 | 0 | }) |
1565 | 0 | } |
1566 | | |
1567 | 0 | fn struct_variant<V>( |
1568 | 0 | mut self, |
1569 | 0 | _fields: &'static [&'static str], |
1570 | 0 | visitor: V, |
1571 | 0 | ) -> Result<V::Value, Self::Error> |
1572 | 0 | where |
1573 | 0 | V: Visitor<'de>, |
1574 | 0 | { |
1575 | 0 | self.map |
1576 | 0 | .next_value_seed(SeedStructVariant { visitor: visitor }) |
1577 | 0 | } |
1578 | | } |
1579 | | |
1580 | | struct SeedTupleVariant<V> { |
1581 | | len: usize, |
1582 | | visitor: V, |
1583 | | } |
1584 | | |
1585 | | impl<'de, V> DeserializeSeed<'de> for SeedTupleVariant<V> |
1586 | | where |
1587 | | V: Visitor<'de>, |
1588 | | { |
1589 | | type Value = V::Value; |
1590 | | |
1591 | 0 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
1592 | 0 | where |
1593 | 0 | D: Deserializer<'de>, |
1594 | 0 | { |
1595 | 0 | deserializer.deserialize_tuple(self.len, self.visitor) |
1596 | 0 | } |
1597 | | } |
1598 | | |
1599 | | struct SeedStructVariant<V> { |
1600 | | visitor: V, |
1601 | | } |
1602 | | |
1603 | | impl<'de, V> DeserializeSeed<'de> for SeedStructVariant<V> |
1604 | | where |
1605 | | V: Visitor<'de>, |
1606 | | { |
1607 | | type Value = V::Value; |
1608 | | |
1609 | 0 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
1610 | 0 | where |
1611 | 0 | D: Deserializer<'de>, |
1612 | 0 | { |
1613 | 0 | deserializer.deserialize_map(self.visitor) |
1614 | 0 | } |
1615 | | } |
1616 | | |
1617 | | /// Avoid having to restate the generic types on `MapDeserializer`. The |
1618 | | /// `Iterator::Item` contains enough information to figure out K and V. |
1619 | | pub trait Pair { |
1620 | | type First; |
1621 | | type Second; |
1622 | | fn split(self) -> (Self::First, Self::Second); |
1623 | | } |
1624 | | |
1625 | | impl<A, B> Pair for (A, B) { |
1626 | | type First = A; |
1627 | | type Second = B; |
1628 | 0 | fn split(self) -> (A, B) { |
1629 | 0 | self |
1630 | 0 | } |
1631 | | } |
1632 | | |
1633 | | pub type First<T> = <T as Pair>::First; |
1634 | | pub type Second<T> = <T as Pair>::Second; |
1635 | | } |