• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use lib::*;
2 
3 use de::value::{BorrowedBytesDeserializer, BytesDeserializer};
4 use de::{Deserialize, Deserializer, Error, IntoDeserializer, Visitor};
5 
6 #[cfg(any(feature = "std", feature = "alloc"))]
7 use de::{DeserializeSeed, MapAccess, Unexpected};
8 
9 #[cfg(any(feature = "std", feature = "alloc"))]
10 pub use self::content::{
11     Content, ContentDeserializer, ContentRefDeserializer, EnumDeserializer,
12     InternallyTaggedUnitVisitor, TagContentOtherField, TagContentOtherFieldVisitor,
13     TagOrContentField, TagOrContentFieldVisitor, TaggedContentVisitor, UntaggedUnitVisitor,
14 };
15 
16 pub use seed::InPlaceSeed;
17 
18 /// If the missing field is of type `Option<T>` then treat is as `None`,
19 /// otherwise it is an error.
missing_field<'de, V, E>(field: &'static str) -> Result<V, E> where V: Deserialize<'de>, E: Error,20 pub fn missing_field<'de, V, E>(field: &'static str) -> Result<V, E>
21 where
22     V: Deserialize<'de>,
23     E: Error,
24 {
25     struct MissingFieldDeserializer<E>(&'static str, PhantomData<E>);
26 
27     impl<'de, E> Deserializer<'de> for MissingFieldDeserializer<E>
28     where
29         E: Error,
30     {
31         type Error = E;
32 
33         fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, E>
34         where
35             V: Visitor<'de>,
36         {
37             Err(Error::missing_field(self.0))
38         }
39 
40         fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E>
41         where
42             V: Visitor<'de>,
43         {
44             visitor.visit_none()
45         }
46 
47         forward_to_deserialize_any! {
48             bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
49             bytes byte_buf unit unit_struct newtype_struct seq tuple
50             tuple_struct map struct enum identifier ignored_any
51         }
52     }
53 
54     let deserializer = MissingFieldDeserializer(field, PhantomData);
55     Deserialize::deserialize(deserializer)
56 }
57 
58 #[cfg(any(feature = "std", feature = "alloc"))]
borrow_cow_str<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error> where D: Deserializer<'de>, R: From<Cow<'a, str>>,59 pub fn borrow_cow_str<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error>
60 where
61     D: Deserializer<'de>,
62     R: From<Cow<'a, str>>,
63 {
64     struct CowStrVisitor;
65 
66     impl<'a> Visitor<'a> for CowStrVisitor {
67         type Value = Cow<'a, str>;
68 
69         fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
70             formatter.write_str("a string")
71         }
72 
73         fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
74         where
75             E: Error,
76         {
77             Ok(Cow::Owned(v.to_owned()))
78         }
79 
80         fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
81         where
82             E: Error,
83         {
84             Ok(Cow::Borrowed(v))
85         }
86 
87         fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
88         where
89             E: Error,
90         {
91             Ok(Cow::Owned(v))
92         }
93 
94         fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
95         where
96             E: Error,
97         {
98             match str::from_utf8(v) {
99                 Ok(s) => Ok(Cow::Owned(s.to_owned())),
100                 Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
101             }
102         }
103 
104         fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
105         where
106             E: Error,
107         {
108             match str::from_utf8(v) {
109                 Ok(s) => Ok(Cow::Borrowed(s)),
110                 Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
111             }
112         }
113 
114         fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
115         where
116             E: Error,
117         {
118             match String::from_utf8(v) {
119                 Ok(s) => Ok(Cow::Owned(s)),
120                 Err(e) => Err(Error::invalid_value(
121                     Unexpected::Bytes(&e.into_bytes()),
122                     &self,
123                 )),
124             }
125         }
126     }
127 
128     deserializer.deserialize_str(CowStrVisitor).map(From::from)
129 }
130 
131 #[cfg(any(feature = "std", feature = "alloc"))]
borrow_cow_bytes<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error> where D: Deserializer<'de>, R: From<Cow<'a, [u8]>>,132 pub fn borrow_cow_bytes<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error>
133 where
134     D: Deserializer<'de>,
135     R: From<Cow<'a, [u8]>>,
136 {
137     struct CowBytesVisitor;
138 
139     impl<'a> Visitor<'a> for CowBytesVisitor {
140         type Value = Cow<'a, [u8]>;
141 
142         fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
143             formatter.write_str("a byte array")
144         }
145 
146         fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
147         where
148             E: Error,
149         {
150             Ok(Cow::Owned(v.as_bytes().to_vec()))
151         }
152 
153         fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
154         where
155             E: Error,
156         {
157             Ok(Cow::Borrowed(v.as_bytes()))
158         }
159 
160         fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
161         where
162             E: Error,
163         {
164             Ok(Cow::Owned(v.into_bytes()))
165         }
166 
167         fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
168         where
169             E: Error,
170         {
171             Ok(Cow::Owned(v.to_vec()))
172         }
173 
174         fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
175         where
176             E: Error,
177         {
178             Ok(Cow::Borrowed(v))
179         }
180 
181         fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
182         where
183             E: Error,
184         {
185             Ok(Cow::Owned(v))
186         }
187     }
188 
189     deserializer
190         .deserialize_bytes(CowBytesVisitor)
191         .map(From::from)
192 }
193 
194 #[cfg(any(feature = "std", feature = "alloc"))]
195 mod content {
196     // This module is private and nothing here should be used outside of
197     // generated code.
198     //
199     // We will iterate on the implementation for a few releases and only have to
200     // worry about backward compatibility for the `untagged` and `tag` attributes
201     // rather than for this entire mechanism.
202     //
203     // This issue is tracking making some of this stuff public:
204     // https://github.com/serde-rs/serde/issues/741
205 
206     use lib::*;
207 
208     use __private::size_hint;
209     use actually_private;
210     use de::{
211         self, Deserialize, DeserializeSeed, Deserializer, EnumAccess, Expected, IgnoredAny,
212         MapAccess, SeqAccess, Unexpected, Visitor,
213     };
214 
215     /// Used from generated code to buffer the contents of the Deserializer when
216     /// deserializing untagged enums and internally tagged enums.
217     ///
218     /// Not public API. Use serde-value instead.
219     #[derive(Debug, Clone)]
220     pub enum Content<'de> {
221         Bool(bool),
222 
223         U8(u8),
224         U16(u16),
225         U32(u32),
226         U64(u64),
227 
228         I8(i8),
229         I16(i16),
230         I32(i32),
231         I64(i64),
232 
233         F32(f32),
234         F64(f64),
235 
236         Char(char),
237         String(String),
238         Str(&'de str),
239         ByteBuf(Vec<u8>),
240         Bytes(&'de [u8]),
241 
242         None,
243         Some(Box<Content<'de>>),
244 
245         Unit,
246         Newtype(Box<Content<'de>>),
247         Seq(Vec<Content<'de>>),
248         Map(Vec<(Content<'de>, Content<'de>)>),
249     }
250 
251     impl<'de> Content<'de> {
as_str(&self) -> Option<&str>252         pub fn as_str(&self) -> Option<&str> {
253             match *self {
254                 Content::Str(x) => Some(x),
255                 Content::String(ref x) => Some(x),
256                 Content::Bytes(x) => str::from_utf8(x).ok(),
257                 Content::ByteBuf(ref x) => str::from_utf8(x).ok(),
258                 _ => None,
259             }
260         }
261 
262         #[cold]
unexpected(&self) -> Unexpected263         fn unexpected(&self) -> Unexpected {
264             match *self {
265                 Content::Bool(b) => Unexpected::Bool(b),
266                 Content::U8(n) => Unexpected::Unsigned(n as u64),
267                 Content::U16(n) => Unexpected::Unsigned(n as u64),
268                 Content::U32(n) => Unexpected::Unsigned(n as u64),
269                 Content::U64(n) => Unexpected::Unsigned(n),
270                 Content::I8(n) => Unexpected::Signed(n as i64),
271                 Content::I16(n) => Unexpected::Signed(n as i64),
272                 Content::I32(n) => Unexpected::Signed(n as i64),
273                 Content::I64(n) => Unexpected::Signed(n),
274                 Content::F32(f) => Unexpected::Float(f as f64),
275                 Content::F64(f) => Unexpected::Float(f),
276                 Content::Char(c) => Unexpected::Char(c),
277                 Content::String(ref s) => Unexpected::Str(s),
278                 Content::Str(s) => Unexpected::Str(s),
279                 Content::ByteBuf(ref b) => Unexpected::Bytes(b),
280                 Content::Bytes(b) => Unexpected::Bytes(b),
281                 Content::None | Content::Some(_) => Unexpected::Option,
282                 Content::Unit => Unexpected::Unit,
283                 Content::Newtype(_) => Unexpected::NewtypeStruct,
284                 Content::Seq(_) => Unexpected::Seq,
285                 Content::Map(_) => Unexpected::Map,
286             }
287         }
288     }
289 
290     impl<'de> Deserialize<'de> for Content<'de> {
deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>,291         fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
292         where
293             D: Deserializer<'de>,
294         {
295             // Untagged and internally tagged enums are only supported in
296             // self-describing formats.
297             let visitor = ContentVisitor { value: PhantomData };
298             deserializer.__deserialize_content(actually_private::T, visitor)
299         }
300     }
301 
302     struct ContentVisitor<'de> {
303         value: PhantomData<Content<'de>>,
304     }
305 
306     impl<'de> ContentVisitor<'de> {
new() -> Self307         fn new() -> Self {
308             ContentVisitor { value: PhantomData }
309         }
310     }
311 
312     impl<'de> Visitor<'de> for ContentVisitor<'de> {
313         type Value = Content<'de>;
314 
expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result315         fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
316             fmt.write_str("any value")
317         }
318 
visit_bool<F>(self, value: bool) -> Result<Self::Value, F> where F: de::Error,319         fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
320         where
321             F: de::Error,
322         {
323             Ok(Content::Bool(value))
324         }
325 
visit_i8<F>(self, value: i8) -> Result<Self::Value, F> where F: de::Error,326         fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
327         where
328             F: de::Error,
329         {
330             Ok(Content::I8(value))
331         }
332 
visit_i16<F>(self, value: i16) -> Result<Self::Value, F> where F: de::Error,333         fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
334         where
335             F: de::Error,
336         {
337             Ok(Content::I16(value))
338         }
339 
visit_i32<F>(self, value: i32) -> Result<Self::Value, F> where F: de::Error,340         fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
341         where
342             F: de::Error,
343         {
344             Ok(Content::I32(value))
345         }
346 
visit_i64<F>(self, value: i64) -> Result<Self::Value, F> where F: de::Error,347         fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
348         where
349             F: de::Error,
350         {
351             Ok(Content::I64(value))
352         }
353 
visit_u8<F>(self, value: u8) -> Result<Self::Value, F> where F: de::Error,354         fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
355         where
356             F: de::Error,
357         {
358             Ok(Content::U8(value))
359         }
360 
visit_u16<F>(self, value: u16) -> Result<Self::Value, F> where F: de::Error,361         fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
362         where
363             F: de::Error,
364         {
365             Ok(Content::U16(value))
366         }
367 
visit_u32<F>(self, value: u32) -> Result<Self::Value, F> where F: de::Error,368         fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
369         where
370             F: de::Error,
371         {
372             Ok(Content::U32(value))
373         }
374 
visit_u64<F>(self, value: u64) -> Result<Self::Value, F> where F: de::Error,375         fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
376         where
377             F: de::Error,
378         {
379             Ok(Content::U64(value))
380         }
381 
visit_f32<F>(self, value: f32) -> Result<Self::Value, F> where F: de::Error,382         fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
383         where
384             F: de::Error,
385         {
386             Ok(Content::F32(value))
387         }
388 
visit_f64<F>(self, value: f64) -> Result<Self::Value, F> where F: de::Error,389         fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
390         where
391             F: de::Error,
392         {
393             Ok(Content::F64(value))
394         }
395 
visit_char<F>(self, value: char) -> Result<Self::Value, F> where F: de::Error,396         fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
397         where
398             F: de::Error,
399         {
400             Ok(Content::Char(value))
401         }
402 
visit_str<F>(self, value: &str) -> Result<Self::Value, F> where F: de::Error,403         fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
404         where
405             F: de::Error,
406         {
407             Ok(Content::String(value.into()))
408         }
409 
visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F> where F: de::Error,410         fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
411         where
412             F: de::Error,
413         {
414             Ok(Content::Str(value))
415         }
416 
visit_string<F>(self, value: String) -> Result<Self::Value, F> where F: de::Error,417         fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
418         where
419             F: de::Error,
420         {
421             Ok(Content::String(value))
422         }
423 
visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F> where F: de::Error,424         fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F>
425         where
426             F: de::Error,
427         {
428             Ok(Content::ByteBuf(value.into()))
429         }
430 
visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F> where F: de::Error,431         fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
432         where
433             F: de::Error,
434         {
435             Ok(Content::Bytes(value))
436         }
437 
visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F> where F: de::Error,438         fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
439         where
440             F: de::Error,
441         {
442             Ok(Content::ByteBuf(value))
443         }
444 
visit_unit<F>(self) -> Result<Self::Value, F> where F: de::Error,445         fn visit_unit<F>(self) -> Result<Self::Value, F>
446         where
447             F: de::Error,
448         {
449             Ok(Content::Unit)
450         }
451 
visit_none<F>(self) -> Result<Self::Value, F> where F: de::Error,452         fn visit_none<F>(self) -> Result<Self::Value, F>
453         where
454             F: de::Error,
455         {
456             Ok(Content::None)
457         }
458 
visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,459         fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
460         where
461             D: Deserializer<'de>,
462         {
463             Deserialize::deserialize(deserializer).map(|v| Content::Some(Box::new(v)))
464         }
465 
visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,466         fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
467         where
468             D: Deserializer<'de>,
469         {
470             Deserialize::deserialize(deserializer).map(|v| Content::Newtype(Box::new(v)))
471         }
472 
visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> where V: SeqAccess<'de>,473         fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
474         where
475             V: SeqAccess<'de>,
476         {
477             let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
478             while let Some(e) = try!(visitor.next_element()) {
479                 vec.push(e);
480             }
481             Ok(Content::Seq(vec))
482         }
483 
visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> where V: MapAccess<'de>,484         fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
485         where
486             V: MapAccess<'de>,
487         {
488             let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
489             while let Some(kv) = try!(visitor.next_entry()) {
490                 vec.push(kv);
491             }
492             Ok(Content::Map(vec))
493         }
494 
visit_enum<V>(self, _visitor: V) -> Result<Self::Value, V::Error> where V: EnumAccess<'de>,495         fn visit_enum<V>(self, _visitor: V) -> Result<Self::Value, V::Error>
496         where
497             V: EnumAccess<'de>,
498         {
499             Err(de::Error::custom(
500                 "untagged and internally tagged enums do not support enum input",
501             ))
502         }
503     }
504 
505     /// This is the type of the map keys in an internally tagged enum.
506     ///
507     /// Not public API.
508     pub enum TagOrContent<'de> {
509         Tag,
510         Content(Content<'de>),
511     }
512 
513     struct TagOrContentVisitor<'de> {
514         name: &'static str,
515         value: PhantomData<TagOrContent<'de>>,
516     }
517 
518     impl<'de> TagOrContentVisitor<'de> {
new(name: &'static str) -> Self519         fn new(name: &'static str) -> Self {
520             TagOrContentVisitor {
521                 name: name,
522                 value: PhantomData,
523             }
524         }
525     }
526 
527     impl<'de> DeserializeSeed<'de> for TagOrContentVisitor<'de> {
528         type Value = TagOrContent<'de>;
529 
deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,530         fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
531         where
532             D: Deserializer<'de>,
533         {
534             // Internally tagged enums are only supported in self-describing
535             // formats.
536             deserializer.deserialize_any(self)
537         }
538     }
539 
540     impl<'de> Visitor<'de> for TagOrContentVisitor<'de> {
541         type Value = TagOrContent<'de>;
542 
expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result543         fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
544             write!(fmt, "a type tag `{}` or any other value", self.name)
545         }
546 
visit_bool<F>(self, value: bool) -> Result<Self::Value, F> where F: de::Error,547         fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
548         where
549             F: de::Error,
550         {
551             ContentVisitor::new()
552                 .visit_bool(value)
553                 .map(TagOrContent::Content)
554         }
555 
visit_i8<F>(self, value: i8) -> Result<Self::Value, F> where F: de::Error,556         fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
557         where
558             F: de::Error,
559         {
560             ContentVisitor::new()
561                 .visit_i8(value)
562                 .map(TagOrContent::Content)
563         }
564 
visit_i16<F>(self, value: i16) -> Result<Self::Value, F> where F: de::Error,565         fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
566         where
567             F: de::Error,
568         {
569             ContentVisitor::new()
570                 .visit_i16(value)
571                 .map(TagOrContent::Content)
572         }
573 
visit_i32<F>(self, value: i32) -> Result<Self::Value, F> where F: de::Error,574         fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
575         where
576             F: de::Error,
577         {
578             ContentVisitor::new()
579                 .visit_i32(value)
580                 .map(TagOrContent::Content)
581         }
582 
visit_i64<F>(self, value: i64) -> Result<Self::Value, F> where F: de::Error,583         fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
584         where
585             F: de::Error,
586         {
587             ContentVisitor::new()
588                 .visit_i64(value)
589                 .map(TagOrContent::Content)
590         }
591 
visit_u8<F>(self, value: u8) -> Result<Self::Value, F> where F: de::Error,592         fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
593         where
594             F: de::Error,
595         {
596             ContentVisitor::new()
597                 .visit_u8(value)
598                 .map(TagOrContent::Content)
599         }
600 
visit_u16<F>(self, value: u16) -> Result<Self::Value, F> where F: de::Error,601         fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
602         where
603             F: de::Error,
604         {
605             ContentVisitor::new()
606                 .visit_u16(value)
607                 .map(TagOrContent::Content)
608         }
609 
visit_u32<F>(self, value: u32) -> Result<Self::Value, F> where F: de::Error,610         fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
611         where
612             F: de::Error,
613         {
614             ContentVisitor::new()
615                 .visit_u32(value)
616                 .map(TagOrContent::Content)
617         }
618 
visit_u64<F>(self, value: u64) -> Result<Self::Value, F> where F: de::Error,619         fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
620         where
621             F: de::Error,
622         {
623             ContentVisitor::new()
624                 .visit_u64(value)
625                 .map(TagOrContent::Content)
626         }
627 
visit_f32<F>(self, value: f32) -> Result<Self::Value, F> where F: de::Error,628         fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
629         where
630             F: de::Error,
631         {
632             ContentVisitor::new()
633                 .visit_f32(value)
634                 .map(TagOrContent::Content)
635         }
636 
visit_f64<F>(self, value: f64) -> Result<Self::Value, F> where F: de::Error,637         fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
638         where
639             F: de::Error,
640         {
641             ContentVisitor::new()
642                 .visit_f64(value)
643                 .map(TagOrContent::Content)
644         }
645 
visit_char<F>(self, value: char) -> Result<Self::Value, F> where F: de::Error,646         fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
647         where
648             F: de::Error,
649         {
650             ContentVisitor::new()
651                 .visit_char(value)
652                 .map(TagOrContent::Content)
653         }
654 
visit_str<F>(self, value: &str) -> Result<Self::Value, F> where F: de::Error,655         fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
656         where
657             F: de::Error,
658         {
659             if value == self.name {
660                 Ok(TagOrContent::Tag)
661             } else {
662                 ContentVisitor::new()
663                     .visit_str(value)
664                     .map(TagOrContent::Content)
665             }
666         }
667 
visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F> where F: de::Error,668         fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
669         where
670             F: de::Error,
671         {
672             if value == self.name {
673                 Ok(TagOrContent::Tag)
674             } else {
675                 ContentVisitor::new()
676                     .visit_borrowed_str(value)
677                     .map(TagOrContent::Content)
678             }
679         }
680 
visit_string<F>(self, value: String) -> Result<Self::Value, F> where F: de::Error,681         fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
682         where
683             F: de::Error,
684         {
685             if value == self.name {
686                 Ok(TagOrContent::Tag)
687             } else {
688                 ContentVisitor::new()
689                     .visit_string(value)
690                     .map(TagOrContent::Content)
691             }
692         }
693 
visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F> where F: de::Error,694         fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F>
695         where
696             F: de::Error,
697         {
698             if value == self.name.as_bytes() {
699                 Ok(TagOrContent::Tag)
700             } else {
701                 ContentVisitor::new()
702                     .visit_bytes(value)
703                     .map(TagOrContent::Content)
704             }
705         }
706 
visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F> where F: de::Error,707         fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
708         where
709             F: de::Error,
710         {
711             if value == self.name.as_bytes() {
712                 Ok(TagOrContent::Tag)
713             } else {
714                 ContentVisitor::new()
715                     .visit_borrowed_bytes(value)
716                     .map(TagOrContent::Content)
717             }
718         }
719 
visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F> where F: de::Error,720         fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
721         where
722             F: de::Error,
723         {
724             if value == self.name.as_bytes() {
725                 Ok(TagOrContent::Tag)
726             } else {
727                 ContentVisitor::new()
728                     .visit_byte_buf(value)
729                     .map(TagOrContent::Content)
730             }
731         }
732 
visit_unit<F>(self) -> Result<Self::Value, F> where F: de::Error,733         fn visit_unit<F>(self) -> Result<Self::Value, F>
734         where
735             F: de::Error,
736         {
737             ContentVisitor::new()
738                 .visit_unit()
739                 .map(TagOrContent::Content)
740         }
741 
visit_none<F>(self) -> Result<Self::Value, F> where F: de::Error,742         fn visit_none<F>(self) -> Result<Self::Value, F>
743         where
744             F: de::Error,
745         {
746             ContentVisitor::new()
747                 .visit_none()
748                 .map(TagOrContent::Content)
749         }
750 
visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,751         fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
752         where
753             D: Deserializer<'de>,
754         {
755             ContentVisitor::new()
756                 .visit_some(deserializer)
757                 .map(TagOrContent::Content)
758         }
759 
visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,760         fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
761         where
762             D: Deserializer<'de>,
763         {
764             ContentVisitor::new()
765                 .visit_newtype_struct(deserializer)
766                 .map(TagOrContent::Content)
767         }
768 
visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error> where V: SeqAccess<'de>,769         fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
770         where
771             V: SeqAccess<'de>,
772         {
773             ContentVisitor::new()
774                 .visit_seq(visitor)
775                 .map(TagOrContent::Content)
776         }
777 
visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error> where V: MapAccess<'de>,778         fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
779         where
780             V: MapAccess<'de>,
781         {
782             ContentVisitor::new()
783                 .visit_map(visitor)
784                 .map(TagOrContent::Content)
785         }
786 
visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error> where V: EnumAccess<'de>,787         fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
788         where
789             V: EnumAccess<'de>,
790         {
791             ContentVisitor::new()
792                 .visit_enum(visitor)
793                 .map(TagOrContent::Content)
794         }
795     }
796 
797     /// Used by generated code to deserialize an internally tagged enum.
798     ///
799     /// Not public API.
800     pub struct TaggedContent<'de, T> {
801         pub tag: T,
802         pub content: Content<'de>,
803     }
804 
805     /// Not public API.
806     pub struct TaggedContentVisitor<'de, T> {
807         tag_name: &'static str,
808         expecting: &'static str,
809         value: PhantomData<TaggedContent<'de, T>>,
810     }
811 
812     impl<'de, T> TaggedContentVisitor<'de, T> {
813         /// Visitor for the content of an internally tagged enum with the given
814         /// tag name.
new(name: &'static str, expecting: &'static str) -> Self815         pub fn new(name: &'static str, expecting: &'static str) -> Self {
816             TaggedContentVisitor {
817                 tag_name: name,
818                 expecting: expecting,
819                 value: PhantomData,
820             }
821         }
822     }
823 
824     impl<'de, T> DeserializeSeed<'de> for TaggedContentVisitor<'de, T>
825     where
826         T: Deserialize<'de>,
827     {
828         type Value = TaggedContent<'de, T>;
829 
deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,830         fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
831         where
832             D: Deserializer<'de>,
833         {
834             // Internally tagged enums are only supported in self-describing
835             // formats.
836             deserializer.deserialize_any(self)
837         }
838     }
839 
840     impl<'de, T> Visitor<'de> for TaggedContentVisitor<'de, T>
841     where
842         T: Deserialize<'de>,
843     {
844         type Value = TaggedContent<'de, T>;
845 
expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result846         fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
847             fmt.write_str(self.expecting)
848         }
849 
visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error> where S: SeqAccess<'de>,850         fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error>
851         where
852             S: SeqAccess<'de>,
853         {
854             let tag = match try!(seq.next_element()) {
855                 Some(tag) => tag,
856                 None => {
857                     return Err(de::Error::missing_field(self.tag_name));
858                 }
859             };
860             let rest = de::value::SeqAccessDeserializer::new(seq);
861             Ok(TaggedContent {
862                 tag: tag,
863                 content: try!(Content::deserialize(rest)),
864             })
865         }
866 
visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> where M: MapAccess<'de>,867         fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
868         where
869             M: MapAccess<'de>,
870         {
871             let mut tag = None;
872             let mut vec = Vec::with_capacity(size_hint::cautious(map.size_hint()));
873             while let Some(k) = try!(map.next_key_seed(TagOrContentVisitor::new(self.tag_name))) {
874                 match k {
875                     TagOrContent::Tag => {
876                         if tag.is_some() {
877                             return Err(de::Error::duplicate_field(self.tag_name));
878                         }
879                         tag = Some(try!(map.next_value()));
880                     }
881                     TagOrContent::Content(k) => {
882                         let v = try!(map.next_value());
883                         vec.push((k, v));
884                     }
885                 }
886             }
887             match tag {
888                 None => Err(de::Error::missing_field(self.tag_name)),
889                 Some(tag) => Ok(TaggedContent {
890                     tag: tag,
891                     content: Content::Map(vec),
892                 }),
893             }
894         }
895     }
896 
897     /// Used by generated code to deserialize an adjacently tagged enum.
898     ///
899     /// Not public API.
900     pub enum TagOrContentField {
901         Tag,
902         Content,
903     }
904 
905     /// Not public API.
906     pub struct TagOrContentFieldVisitor {
907         pub tag: &'static str,
908         pub content: &'static str,
909     }
910 
911     impl<'de> DeserializeSeed<'de> for TagOrContentFieldVisitor {
912         type Value = TagOrContentField;
913 
deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,914         fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
915         where
916             D: Deserializer<'de>,
917         {
918             deserializer.deserialize_str(self)
919         }
920     }
921 
922     impl<'de> Visitor<'de> for TagOrContentFieldVisitor {
923         type Value = TagOrContentField;
924 
expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result925         fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
926             write!(formatter, "{:?} or {:?}", self.tag, self.content)
927         }
928 
visit_str<E>(self, field: &str) -> Result<Self::Value, E> where E: de::Error,929         fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
930         where
931             E: de::Error,
932         {
933             if field == self.tag {
934                 Ok(TagOrContentField::Tag)
935             } else if field == self.content {
936                 Ok(TagOrContentField::Content)
937             } else {
938                 Err(de::Error::invalid_value(Unexpected::Str(field), &self))
939             }
940         }
941     }
942 
943     /// Used by generated code to deserialize an adjacently tagged enum when
944     /// ignoring unrelated fields is allowed.
945     ///
946     /// Not public API.
947     pub enum TagContentOtherField {
948         Tag,
949         Content,
950         Other,
951     }
952 
953     /// Not public API.
954     pub struct TagContentOtherFieldVisitor {
955         pub tag: &'static str,
956         pub content: &'static str,
957     }
958 
959     impl<'de> DeserializeSeed<'de> for TagContentOtherFieldVisitor {
960         type Value = TagContentOtherField;
961 
deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,962         fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
963         where
964             D: Deserializer<'de>,
965         {
966             deserializer.deserialize_str(self)
967         }
968     }
969 
970     impl<'de> Visitor<'de> for TagContentOtherFieldVisitor {
971         type Value = TagContentOtherField;
972 
expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result973         fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
974             write!(
975                 formatter,
976                 "{:?}, {:?}, or other ignored fields",
977                 self.tag, self.content
978             )
979         }
980 
visit_str<E>(self, field: &str) -> Result<Self::Value, E> where E: de::Error,981         fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
982         where
983             E: de::Error,
984         {
985             if field == self.tag {
986                 Ok(TagContentOtherField::Tag)
987             } else if field == self.content {
988                 Ok(TagContentOtherField::Content)
989             } else {
990                 Ok(TagContentOtherField::Other)
991             }
992         }
993     }
994 
995     /// Not public API
996     pub struct ContentDeserializer<'de, E> {
997         content: Content<'de>,
998         err: PhantomData<E>,
999     }
1000 
1001     impl<'de, E> ContentDeserializer<'de, E>
1002     where
1003         E: de::Error,
1004     {
1005         #[cold]
invalid_type(self, exp: &Expected) -> E1006         fn invalid_type(self, exp: &Expected) -> E {
1007             de::Error::invalid_type(self.content.unexpected(), exp)
1008         }
1009 
deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E> where V: Visitor<'de>,1010         fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E>
1011         where
1012             V: Visitor<'de>,
1013         {
1014             match self.content {
1015                 Content::U8(v) => visitor.visit_u8(v),
1016                 Content::U16(v) => visitor.visit_u16(v),
1017                 Content::U32(v) => visitor.visit_u32(v),
1018                 Content::U64(v) => visitor.visit_u64(v),
1019                 Content::I8(v) => visitor.visit_i8(v),
1020                 Content::I16(v) => visitor.visit_i16(v),
1021                 Content::I32(v) => visitor.visit_i32(v),
1022                 Content::I64(v) => visitor.visit_i64(v),
1023                 _ => Err(self.invalid_type(&visitor)),
1024             }
1025         }
1026 
deserialize_float<V>(self, visitor: V) -> Result<V::Value, E> where V: Visitor<'de>,1027         fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E>
1028         where
1029             V: Visitor<'de>,
1030         {
1031             match self.content {
1032                 Content::F32(v) => visitor.visit_f32(v),
1033                 Content::F64(v) => visitor.visit_f64(v),
1034                 Content::U8(v) => visitor.visit_u8(v),
1035                 Content::U16(v) => visitor.visit_u16(v),
1036                 Content::U32(v) => visitor.visit_u32(v),
1037                 Content::U64(v) => visitor.visit_u64(v),
1038                 Content::I8(v) => visitor.visit_i8(v),
1039                 Content::I16(v) => visitor.visit_i16(v),
1040                 Content::I32(v) => visitor.visit_i32(v),
1041                 Content::I64(v) => visitor.visit_i64(v),
1042                 _ => Err(self.invalid_type(&visitor)),
1043             }
1044         }
1045     }
1046 
visit_content_seq<'de, V, E>(content: Vec<Content<'de>>, visitor: V) -> Result<V::Value, E> where V: Visitor<'de>, E: de::Error,1047     fn visit_content_seq<'de, V, E>(content: Vec<Content<'de>>, visitor: V) -> Result<V::Value, E>
1048     where
1049         V: Visitor<'de>,
1050         E: de::Error,
1051     {
1052         let seq = content.into_iter().map(ContentDeserializer::new);
1053         let mut seq_visitor = de::value::SeqDeserializer::new(seq);
1054         let value = try!(visitor.visit_seq(&mut seq_visitor));
1055         try!(seq_visitor.end());
1056         Ok(value)
1057     }
1058 
visit_content_map<'de, V, E>( content: Vec<(Content<'de>, Content<'de>)>, visitor: V, ) -> Result<V::Value, E> where V: Visitor<'de>, E: de::Error,1059     fn visit_content_map<'de, V, E>(
1060         content: Vec<(Content<'de>, Content<'de>)>,
1061         visitor: V,
1062     ) -> Result<V::Value, E>
1063     where
1064         V: Visitor<'de>,
1065         E: de::Error,
1066     {
1067         let map = content
1068             .into_iter()
1069             .map(|(k, v)| (ContentDeserializer::new(k), ContentDeserializer::new(v)));
1070         let mut map_visitor = de::value::MapDeserializer::new(map);
1071         let value = try!(visitor.visit_map(&mut map_visitor));
1072         try!(map_visitor.end());
1073         Ok(value)
1074     }
1075 
1076     /// Used when deserializing an internally tagged enum because the content
1077     /// will be used exactly once.
1078     impl<'de, E> Deserializer<'de> for ContentDeserializer<'de, E>
1079     where
1080         E: de::Error,
1081     {
1082         type Error = E;
1083 
deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1084         fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1085         where
1086             V: Visitor<'de>,
1087         {
1088             match self.content {
1089                 Content::Bool(v) => visitor.visit_bool(v),
1090                 Content::U8(v) => visitor.visit_u8(v),
1091                 Content::U16(v) => visitor.visit_u16(v),
1092                 Content::U32(v) => visitor.visit_u32(v),
1093                 Content::U64(v) => visitor.visit_u64(v),
1094                 Content::I8(v) => visitor.visit_i8(v),
1095                 Content::I16(v) => visitor.visit_i16(v),
1096                 Content::I32(v) => visitor.visit_i32(v),
1097                 Content::I64(v) => visitor.visit_i64(v),
1098                 Content::F32(v) => visitor.visit_f32(v),
1099                 Content::F64(v) => visitor.visit_f64(v),
1100                 Content::Char(v) => visitor.visit_char(v),
1101                 Content::String(v) => visitor.visit_string(v),
1102                 Content::Str(v) => visitor.visit_borrowed_str(v),
1103                 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
1104                 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1105                 Content::Unit => visitor.visit_unit(),
1106                 Content::None => visitor.visit_none(),
1107                 Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
1108                 Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)),
1109                 Content::Seq(v) => visit_content_seq(v, visitor),
1110                 Content::Map(v) => visit_content_map(v, visitor),
1111             }
1112         }
1113 
deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1114         fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1115         where
1116             V: Visitor<'de>,
1117         {
1118             match self.content {
1119                 Content::Bool(v) => visitor.visit_bool(v),
1120                 _ => Err(self.invalid_type(&visitor)),
1121             }
1122         }
1123 
deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1124         fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1125         where
1126             V: Visitor<'de>,
1127         {
1128             self.deserialize_integer(visitor)
1129         }
1130 
deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1131         fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1132         where
1133             V: Visitor<'de>,
1134         {
1135             self.deserialize_integer(visitor)
1136         }
1137 
deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1138         fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1139         where
1140             V: Visitor<'de>,
1141         {
1142             self.deserialize_integer(visitor)
1143         }
1144 
deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1145         fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1146         where
1147             V: Visitor<'de>,
1148         {
1149             self.deserialize_integer(visitor)
1150         }
1151 
deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1152         fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1153         where
1154             V: Visitor<'de>,
1155         {
1156             self.deserialize_integer(visitor)
1157         }
1158 
deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1159         fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1160         where
1161             V: Visitor<'de>,
1162         {
1163             self.deserialize_integer(visitor)
1164         }
1165 
deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1166         fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1167         where
1168             V: Visitor<'de>,
1169         {
1170             self.deserialize_integer(visitor)
1171         }
1172 
deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1173         fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1174         where
1175             V: Visitor<'de>,
1176         {
1177             self.deserialize_integer(visitor)
1178         }
1179 
deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1180         fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1181         where
1182             V: Visitor<'de>,
1183         {
1184             self.deserialize_float(visitor)
1185         }
1186 
deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1187         fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1188         where
1189             V: Visitor<'de>,
1190         {
1191             self.deserialize_float(visitor)
1192         }
1193 
deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1194         fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1195         where
1196             V: Visitor<'de>,
1197         {
1198             match self.content {
1199                 Content::Char(v) => visitor.visit_char(v),
1200                 Content::String(v) => visitor.visit_string(v),
1201                 Content::Str(v) => visitor.visit_borrowed_str(v),
1202                 _ => Err(self.invalid_type(&visitor)),
1203             }
1204         }
1205 
deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1206         fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1207         where
1208             V: Visitor<'de>,
1209         {
1210             self.deserialize_string(visitor)
1211         }
1212 
deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1213         fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1214         where
1215             V: Visitor<'de>,
1216         {
1217             match self.content {
1218                 Content::String(v) => visitor.visit_string(v),
1219                 Content::Str(v) => visitor.visit_borrowed_str(v),
1220                 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
1221                 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1222                 _ => Err(self.invalid_type(&visitor)),
1223             }
1224         }
1225 
deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1226         fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1227         where
1228             V: Visitor<'de>,
1229         {
1230             self.deserialize_byte_buf(visitor)
1231         }
1232 
deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1233         fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1234         where
1235             V: Visitor<'de>,
1236         {
1237             match self.content {
1238                 Content::String(v) => visitor.visit_string(v),
1239                 Content::Str(v) => visitor.visit_borrowed_str(v),
1240                 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
1241                 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1242                 Content::Seq(v) => visit_content_seq(v, visitor),
1243                 _ => Err(self.invalid_type(&visitor)),
1244             }
1245         }
1246 
deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1247         fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1248         where
1249             V: Visitor<'de>,
1250         {
1251             match self.content {
1252                 Content::None => visitor.visit_none(),
1253                 Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
1254                 Content::Unit => visitor.visit_unit(),
1255                 _ => visitor.visit_some(self),
1256             }
1257         }
1258 
deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1259         fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1260         where
1261             V: Visitor<'de>,
1262         {
1263             match self.content {
1264                 Content::Unit => visitor.visit_unit(),
1265 
1266                 // Allow deserializing newtype variant containing unit.
1267                 //
1268                 //     #[derive(Deserialize)]
1269                 //     #[serde(tag = "result")]
1270                 //     enum Response<T> {
1271                 //         Success(T),
1272                 //     }
1273                 //
1274                 // We want {"result":"Success"} to deserialize into Response<()>.
1275                 Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
1276                 _ => Err(self.invalid_type(&visitor)),
1277             }
1278         }
1279 
deserialize_unit_struct<V>( self, _name: &'static str, visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1280         fn deserialize_unit_struct<V>(
1281             self,
1282             _name: &'static str,
1283             visitor: V,
1284         ) -> Result<V::Value, Self::Error>
1285         where
1286             V: Visitor<'de>,
1287         {
1288             match self.content {
1289                 // As a special case, allow deserializing untagged newtype
1290                 // variant containing unit struct.
1291                 //
1292                 //     #[derive(Deserialize)]
1293                 //     struct Info;
1294                 //
1295                 //     #[derive(Deserialize)]
1296                 //     #[serde(tag = "topic")]
1297                 //     enum Message {
1298                 //         Info(Info),
1299                 //     }
1300                 //
1301                 // We want {"topic":"Info"} to deserialize even though
1302                 // ordinarily unit structs do not deserialize from empty map/seq.
1303                 Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
1304                 Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(),
1305                 _ => self.deserialize_any(visitor),
1306             }
1307         }
1308 
deserialize_newtype_struct<V>( self, _name: &str, visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1309         fn deserialize_newtype_struct<V>(
1310             self,
1311             _name: &str,
1312             visitor: V,
1313         ) -> Result<V::Value, Self::Error>
1314         where
1315             V: Visitor<'de>,
1316         {
1317             match self.content {
1318                 Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)),
1319                 _ => visitor.visit_newtype_struct(self),
1320             }
1321         }
1322 
deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1323         fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1324         where
1325             V: Visitor<'de>,
1326         {
1327             match self.content {
1328                 Content::Seq(v) => visit_content_seq(v, visitor),
1329                 _ => Err(self.invalid_type(&visitor)),
1330             }
1331         }
1332 
deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1333         fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
1334         where
1335             V: Visitor<'de>,
1336         {
1337             self.deserialize_seq(visitor)
1338         }
1339 
deserialize_tuple_struct<V>( self, _name: &'static str, _len: usize, visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1340         fn deserialize_tuple_struct<V>(
1341             self,
1342             _name: &'static str,
1343             _len: usize,
1344             visitor: V,
1345         ) -> Result<V::Value, Self::Error>
1346         where
1347             V: Visitor<'de>,
1348         {
1349             self.deserialize_seq(visitor)
1350         }
1351 
deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1352         fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1353         where
1354             V: Visitor<'de>,
1355         {
1356             match self.content {
1357                 Content::Map(v) => visit_content_map(v, visitor),
1358                 _ => Err(self.invalid_type(&visitor)),
1359             }
1360         }
1361 
deserialize_struct<V>( self, _name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1362         fn deserialize_struct<V>(
1363             self,
1364             _name: &'static str,
1365             _fields: &'static [&'static str],
1366             visitor: V,
1367         ) -> Result<V::Value, Self::Error>
1368         where
1369             V: Visitor<'de>,
1370         {
1371             match self.content {
1372                 Content::Seq(v) => visit_content_seq(v, visitor),
1373                 Content::Map(v) => visit_content_map(v, visitor),
1374                 _ => Err(self.invalid_type(&visitor)),
1375             }
1376         }
1377 
deserialize_enum<V>( self, _name: &str, _variants: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1378         fn deserialize_enum<V>(
1379             self,
1380             _name: &str,
1381             _variants: &'static [&'static str],
1382             visitor: V,
1383         ) -> Result<V::Value, Self::Error>
1384         where
1385             V: Visitor<'de>,
1386         {
1387             let (variant, value) = match self.content {
1388                 Content::Map(value) => {
1389                     let mut iter = value.into_iter();
1390                     let (variant, value) = match iter.next() {
1391                         Some(v) => v,
1392                         None => {
1393                             return Err(de::Error::invalid_value(
1394                                 de::Unexpected::Map,
1395                                 &"map with a single key",
1396                             ));
1397                         }
1398                     };
1399                     // enums are encoded in json as maps with a single key:value pair
1400                     if iter.next().is_some() {
1401                         return Err(de::Error::invalid_value(
1402                             de::Unexpected::Map,
1403                             &"map with a single key",
1404                         ));
1405                     }
1406                     (variant, Some(value))
1407                 }
1408                 s @ Content::String(_) | s @ Content::Str(_) => (s, None),
1409                 other => {
1410                     return Err(de::Error::invalid_type(
1411                         other.unexpected(),
1412                         &"string or map",
1413                     ));
1414                 }
1415             };
1416 
1417             visitor.visit_enum(EnumDeserializer::new(variant, value))
1418         }
1419 
deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1420         fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1421         where
1422             V: Visitor<'de>,
1423         {
1424             match self.content {
1425                 Content::String(v) => visitor.visit_string(v),
1426                 Content::Str(v) => visitor.visit_borrowed_str(v),
1427                 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
1428                 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1429                 Content::U8(v) => visitor.visit_u8(v),
1430                 Content::U64(v) => visitor.visit_u64(v),
1431                 _ => Err(self.invalid_type(&visitor)),
1432             }
1433         }
1434 
deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1435         fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1436         where
1437             V: Visitor<'de>,
1438         {
1439             drop(self);
1440             visitor.visit_unit()
1441         }
1442 
__deserialize_content<V>( self, _: actually_private::T, visitor: V, ) -> Result<Content<'de>, Self::Error> where V: Visitor<'de, Value = Content<'de>>,1443         fn __deserialize_content<V>(
1444             self,
1445             _: actually_private::T,
1446             visitor: V,
1447         ) -> Result<Content<'de>, Self::Error>
1448         where
1449             V: Visitor<'de, Value = Content<'de>>,
1450         {
1451             let _ = visitor;
1452             Ok(self.content)
1453         }
1454     }
1455 
1456     impl<'de, E> ContentDeserializer<'de, E> {
1457         /// private API, don't use
new(content: Content<'de>) -> Self1458         pub fn new(content: Content<'de>) -> Self {
1459             ContentDeserializer {
1460                 content: content,
1461                 err: PhantomData,
1462             }
1463         }
1464     }
1465 
1466     pub struct EnumDeserializer<'de, E>
1467     where
1468         E: de::Error,
1469     {
1470         variant: Content<'de>,
1471         value: Option<Content<'de>>,
1472         err: PhantomData<E>,
1473     }
1474 
1475     impl<'de, E> EnumDeserializer<'de, E>
1476     where
1477         E: de::Error,
1478     {
new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E>1479         pub fn new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E> {
1480             EnumDeserializer {
1481                 variant: variant,
1482                 value: value,
1483                 err: PhantomData,
1484             }
1485         }
1486     }
1487 
1488     impl<'de, E> de::EnumAccess<'de> for EnumDeserializer<'de, E>
1489     where
1490         E: de::Error,
1491     {
1492         type Error = E;
1493         type Variant = VariantDeserializer<'de, Self::Error>;
1494 
variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), E> where V: de::DeserializeSeed<'de>,1495         fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), E>
1496         where
1497             V: de::DeserializeSeed<'de>,
1498         {
1499             let visitor = VariantDeserializer {
1500                 value: self.value,
1501                 err: PhantomData,
1502             };
1503             seed.deserialize(ContentDeserializer::new(self.variant))
1504                 .map(|v| (v, visitor))
1505         }
1506     }
1507 
1508     pub struct VariantDeserializer<'de, E>
1509     where
1510         E: de::Error,
1511     {
1512         value: Option<Content<'de>>,
1513         err: PhantomData<E>,
1514     }
1515 
1516     impl<'de, E> de::VariantAccess<'de> for VariantDeserializer<'de, E>
1517     where
1518         E: de::Error,
1519     {
1520         type Error = E;
1521 
unit_variant(self) -> Result<(), E>1522         fn unit_variant(self) -> Result<(), E> {
1523             match self.value {
1524                 Some(value) => de::Deserialize::deserialize(ContentDeserializer::new(value)),
1525                 None => Ok(()),
1526             }
1527         }
1528 
newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E> where T: de::DeserializeSeed<'de>,1529         fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E>
1530         where
1531             T: de::DeserializeSeed<'de>,
1532         {
1533             match self.value {
1534                 Some(value) => seed.deserialize(ContentDeserializer::new(value)),
1535                 None => Err(de::Error::invalid_type(
1536                     de::Unexpected::UnitVariant,
1537                     &"newtype variant",
1538                 )),
1539             }
1540         }
1541 
tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error> where V: de::Visitor<'de>,1542         fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
1543         where
1544             V: de::Visitor<'de>,
1545         {
1546             match self.value {
1547                 Some(Content::Seq(v)) => {
1548                     de::Deserializer::deserialize_any(SeqDeserializer::new(v), visitor)
1549                 }
1550                 Some(other) => Err(de::Error::invalid_type(
1551                     other.unexpected(),
1552                     &"tuple variant",
1553                 )),
1554                 None => Err(de::Error::invalid_type(
1555                     de::Unexpected::UnitVariant,
1556                     &"tuple variant",
1557                 )),
1558             }
1559         }
1560 
struct_variant<V>( self, _fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error> where V: de::Visitor<'de>,1561         fn struct_variant<V>(
1562             self,
1563             _fields: &'static [&'static str],
1564             visitor: V,
1565         ) -> Result<V::Value, Self::Error>
1566         where
1567             V: de::Visitor<'de>,
1568         {
1569             match self.value {
1570                 Some(Content::Map(v)) => {
1571                     de::Deserializer::deserialize_any(MapDeserializer::new(v), visitor)
1572                 }
1573                 Some(Content::Seq(v)) => {
1574                     de::Deserializer::deserialize_any(SeqDeserializer::new(v), visitor)
1575                 }
1576                 Some(other) => Err(de::Error::invalid_type(
1577                     other.unexpected(),
1578                     &"struct variant",
1579                 )),
1580                 None => Err(de::Error::invalid_type(
1581                     de::Unexpected::UnitVariant,
1582                     &"struct variant",
1583                 )),
1584             }
1585         }
1586     }
1587 
1588     struct SeqDeserializer<'de, E>
1589     where
1590         E: de::Error,
1591     {
1592         iter: <Vec<Content<'de>> as IntoIterator>::IntoIter,
1593         err: PhantomData<E>,
1594     }
1595 
1596     impl<'de, E> SeqDeserializer<'de, E>
1597     where
1598         E: de::Error,
1599     {
new(vec: Vec<Content<'de>>) -> Self1600         fn new(vec: Vec<Content<'de>>) -> Self {
1601             SeqDeserializer {
1602                 iter: vec.into_iter(),
1603                 err: PhantomData,
1604             }
1605         }
1606     }
1607 
1608     impl<'de, E> de::Deserializer<'de> for SeqDeserializer<'de, E>
1609     where
1610         E: de::Error,
1611     {
1612         type Error = E;
1613 
1614         #[inline]
deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> where V: de::Visitor<'de>,1615         fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
1616         where
1617             V: de::Visitor<'de>,
1618         {
1619             let len = self.iter.len();
1620             if len == 0 {
1621                 visitor.visit_unit()
1622             } else {
1623                 let ret = try!(visitor.visit_seq(&mut self));
1624                 let remaining = self.iter.len();
1625                 if remaining == 0 {
1626                     Ok(ret)
1627                 } else {
1628                     Err(de::Error::invalid_length(len, &"fewer elements in array"))
1629                 }
1630             }
1631         }
1632 
1633         forward_to_deserialize_any! {
1634             bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
1635             bytes byte_buf option unit unit_struct newtype_struct seq tuple
1636             tuple_struct map struct enum identifier ignored_any
1637         }
1638     }
1639 
1640     impl<'de, E> de::SeqAccess<'de> for SeqDeserializer<'de, E>
1641     where
1642         E: de::Error,
1643     {
1644         type Error = E;
1645 
next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> where T: de::DeserializeSeed<'de>,1646         fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
1647         where
1648             T: de::DeserializeSeed<'de>,
1649         {
1650             match self.iter.next() {
1651                 Some(value) => seed.deserialize(ContentDeserializer::new(value)).map(Some),
1652                 None => Ok(None),
1653             }
1654         }
1655 
size_hint(&self) -> Option<usize>1656         fn size_hint(&self) -> Option<usize> {
1657             size_hint::from_bounds(&self.iter)
1658         }
1659     }
1660 
1661     struct MapDeserializer<'de, E>
1662     where
1663         E: de::Error,
1664     {
1665         iter: <Vec<(Content<'de>, Content<'de>)> as IntoIterator>::IntoIter,
1666         value: Option<Content<'de>>,
1667         err: PhantomData<E>,
1668     }
1669 
1670     impl<'de, E> MapDeserializer<'de, E>
1671     where
1672         E: de::Error,
1673     {
new(map: Vec<(Content<'de>, Content<'de>)>) -> Self1674         fn new(map: Vec<(Content<'de>, Content<'de>)>) -> Self {
1675             MapDeserializer {
1676                 iter: map.into_iter(),
1677                 value: None,
1678                 err: PhantomData,
1679             }
1680         }
1681     }
1682 
1683     impl<'de, E> de::MapAccess<'de> for MapDeserializer<'de, E>
1684     where
1685         E: de::Error,
1686     {
1687         type Error = E;
1688 
next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> where T: de::DeserializeSeed<'de>,1689         fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
1690         where
1691             T: de::DeserializeSeed<'de>,
1692         {
1693             match self.iter.next() {
1694                 Some((key, value)) => {
1695                     self.value = Some(value);
1696                     seed.deserialize(ContentDeserializer::new(key)).map(Some)
1697                 }
1698                 None => Ok(None),
1699             }
1700         }
1701 
next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> where T: de::DeserializeSeed<'de>,1702         fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
1703         where
1704             T: de::DeserializeSeed<'de>,
1705         {
1706             match self.value.take() {
1707                 Some(value) => seed.deserialize(ContentDeserializer::new(value)),
1708                 None => Err(de::Error::custom("value is missing")),
1709             }
1710         }
1711 
size_hint(&self) -> Option<usize>1712         fn size_hint(&self) -> Option<usize> {
1713             size_hint::from_bounds(&self.iter)
1714         }
1715     }
1716 
1717     impl<'de, E> de::Deserializer<'de> for MapDeserializer<'de, E>
1718     where
1719         E: de::Error,
1720     {
1721         type Error = E;
1722 
1723         #[inline]
deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: de::Visitor<'de>,1724         fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1725         where
1726             V: de::Visitor<'de>,
1727         {
1728             visitor.visit_map(self)
1729         }
1730 
1731         forward_to_deserialize_any! {
1732             bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
1733             bytes byte_buf option unit unit_struct newtype_struct seq tuple
1734             tuple_struct map struct enum identifier ignored_any
1735         }
1736     }
1737 
1738     /// Not public API.
1739     pub struct ContentRefDeserializer<'a, 'de: 'a, E> {
1740         content: &'a Content<'de>,
1741         err: PhantomData<E>,
1742     }
1743 
1744     impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E>
1745     where
1746         E: de::Error,
1747     {
1748         #[cold]
invalid_type(self, exp: &Expected) -> E1749         fn invalid_type(self, exp: &Expected) -> E {
1750             de::Error::invalid_type(self.content.unexpected(), exp)
1751         }
1752 
deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E> where V: Visitor<'de>,1753         fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E>
1754         where
1755             V: Visitor<'de>,
1756         {
1757             match *self.content {
1758                 Content::U8(v) => visitor.visit_u8(v),
1759                 Content::U16(v) => visitor.visit_u16(v),
1760                 Content::U32(v) => visitor.visit_u32(v),
1761                 Content::U64(v) => visitor.visit_u64(v),
1762                 Content::I8(v) => visitor.visit_i8(v),
1763                 Content::I16(v) => visitor.visit_i16(v),
1764                 Content::I32(v) => visitor.visit_i32(v),
1765                 Content::I64(v) => visitor.visit_i64(v),
1766                 _ => Err(self.invalid_type(&visitor)),
1767             }
1768         }
1769 
deserialize_float<V>(self, visitor: V) -> Result<V::Value, E> where V: Visitor<'de>,1770         fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E>
1771         where
1772             V: Visitor<'de>,
1773         {
1774             match *self.content {
1775                 Content::F32(v) => visitor.visit_f32(v),
1776                 Content::F64(v) => visitor.visit_f64(v),
1777                 Content::U8(v) => visitor.visit_u8(v),
1778                 Content::U16(v) => visitor.visit_u16(v),
1779                 Content::U32(v) => visitor.visit_u32(v),
1780                 Content::U64(v) => visitor.visit_u64(v),
1781                 Content::I8(v) => visitor.visit_i8(v),
1782                 Content::I16(v) => visitor.visit_i16(v),
1783                 Content::I32(v) => visitor.visit_i32(v),
1784                 Content::I64(v) => visitor.visit_i64(v),
1785                 _ => Err(self.invalid_type(&visitor)),
1786             }
1787         }
1788     }
1789 
visit_content_seq_ref<'a, 'de, V, E>( content: &'a [Content<'de>], visitor: V, ) -> Result<V::Value, E> where V: Visitor<'de>, E: de::Error,1790     fn visit_content_seq_ref<'a, 'de, V, E>(
1791         content: &'a [Content<'de>],
1792         visitor: V,
1793     ) -> Result<V::Value, E>
1794     where
1795         V: Visitor<'de>,
1796         E: de::Error,
1797     {
1798         let seq = content.iter().map(ContentRefDeserializer::new);
1799         let mut seq_visitor = de::value::SeqDeserializer::new(seq);
1800         let value = try!(visitor.visit_seq(&mut seq_visitor));
1801         try!(seq_visitor.end());
1802         Ok(value)
1803     }
1804 
visit_content_map_ref<'a, 'de, V, E>( content: &'a [(Content<'de>, Content<'de>)], visitor: V, ) -> Result<V::Value, E> where V: Visitor<'de>, E: de::Error,1805     fn visit_content_map_ref<'a, 'de, V, E>(
1806         content: &'a [(Content<'de>, Content<'de>)],
1807         visitor: V,
1808     ) -> Result<V::Value, E>
1809     where
1810         V: Visitor<'de>,
1811         E: de::Error,
1812     {
1813         let map = content.iter().map(|(k, v)| {
1814             (
1815                 ContentRefDeserializer::new(k),
1816                 ContentRefDeserializer::new(v),
1817             )
1818         });
1819         let mut map_visitor = de::value::MapDeserializer::new(map);
1820         let value = try!(visitor.visit_map(&mut map_visitor));
1821         try!(map_visitor.end());
1822         Ok(value)
1823     }
1824 
1825     /// Used when deserializing an untagged enum because the content may need
1826     /// to be used more than once.
1827     impl<'de, 'a, E> Deserializer<'de> for ContentRefDeserializer<'a, 'de, E>
1828     where
1829         E: de::Error,
1830     {
1831         type Error = E;
1832 
deserialize_any<V>(self, visitor: V) -> Result<V::Value, E> where V: Visitor<'de>,1833         fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, E>
1834         where
1835             V: Visitor<'de>,
1836         {
1837             match *self.content {
1838                 Content::Bool(v) => visitor.visit_bool(v),
1839                 Content::U8(v) => visitor.visit_u8(v),
1840                 Content::U16(v) => visitor.visit_u16(v),
1841                 Content::U32(v) => visitor.visit_u32(v),
1842                 Content::U64(v) => visitor.visit_u64(v),
1843                 Content::I8(v) => visitor.visit_i8(v),
1844                 Content::I16(v) => visitor.visit_i16(v),
1845                 Content::I32(v) => visitor.visit_i32(v),
1846                 Content::I64(v) => visitor.visit_i64(v),
1847                 Content::F32(v) => visitor.visit_f32(v),
1848                 Content::F64(v) => visitor.visit_f64(v),
1849                 Content::Char(v) => visitor.visit_char(v),
1850                 Content::String(ref v) => visitor.visit_str(v),
1851                 Content::Str(v) => visitor.visit_borrowed_str(v),
1852                 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1853                 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1854                 Content::Unit => visitor.visit_unit(),
1855                 Content::None => visitor.visit_none(),
1856                 Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
1857                 Content::Newtype(ref v) => {
1858                     visitor.visit_newtype_struct(ContentRefDeserializer::new(v))
1859                 }
1860                 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
1861                 Content::Map(ref v) => visit_content_map_ref(v, visitor),
1862             }
1863         }
1864 
deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1865         fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1866         where
1867             V: Visitor<'de>,
1868         {
1869             match *self.content {
1870                 Content::Bool(v) => visitor.visit_bool(v),
1871                 _ => Err(self.invalid_type(&visitor)),
1872             }
1873         }
1874 
deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1875         fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1876         where
1877             V: Visitor<'de>,
1878         {
1879             self.deserialize_integer(visitor)
1880         }
1881 
deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1882         fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1883         where
1884             V: Visitor<'de>,
1885         {
1886             self.deserialize_integer(visitor)
1887         }
1888 
deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1889         fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1890         where
1891             V: Visitor<'de>,
1892         {
1893             self.deserialize_integer(visitor)
1894         }
1895 
deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1896         fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1897         where
1898             V: Visitor<'de>,
1899         {
1900             self.deserialize_integer(visitor)
1901         }
1902 
deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1903         fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1904         where
1905             V: Visitor<'de>,
1906         {
1907             self.deserialize_integer(visitor)
1908         }
1909 
deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1910         fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1911         where
1912             V: Visitor<'de>,
1913         {
1914             self.deserialize_integer(visitor)
1915         }
1916 
deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1917         fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1918         where
1919             V: Visitor<'de>,
1920         {
1921             self.deserialize_integer(visitor)
1922         }
1923 
deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1924         fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1925         where
1926             V: Visitor<'de>,
1927         {
1928             self.deserialize_integer(visitor)
1929         }
1930 
deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1931         fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1932         where
1933             V: Visitor<'de>,
1934         {
1935             self.deserialize_float(visitor)
1936         }
1937 
deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1938         fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1939         where
1940             V: Visitor<'de>,
1941         {
1942             self.deserialize_float(visitor)
1943         }
1944 
deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1945         fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1946         where
1947             V: Visitor<'de>,
1948         {
1949             match *self.content {
1950                 Content::Char(v) => visitor.visit_char(v),
1951                 Content::String(ref v) => visitor.visit_str(v),
1952                 Content::Str(v) => visitor.visit_borrowed_str(v),
1953                 _ => Err(self.invalid_type(&visitor)),
1954             }
1955         }
1956 
deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1957         fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1958         where
1959             V: Visitor<'de>,
1960         {
1961             match *self.content {
1962                 Content::String(ref v) => visitor.visit_str(v),
1963                 Content::Str(v) => visitor.visit_borrowed_str(v),
1964                 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1965                 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1966                 _ => Err(self.invalid_type(&visitor)),
1967             }
1968         }
1969 
deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1970         fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1971         where
1972             V: Visitor<'de>,
1973         {
1974             self.deserialize_str(visitor)
1975         }
1976 
deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1977         fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1978         where
1979             V: Visitor<'de>,
1980         {
1981             match *self.content {
1982                 Content::String(ref v) => visitor.visit_str(v),
1983                 Content::Str(v) => visitor.visit_borrowed_str(v),
1984                 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1985                 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1986                 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
1987                 _ => Err(self.invalid_type(&visitor)),
1988             }
1989         }
1990 
deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,1991         fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1992         where
1993             V: Visitor<'de>,
1994         {
1995             self.deserialize_bytes(visitor)
1996         }
1997 
deserialize_option<V>(self, visitor: V) -> Result<V::Value, E> where V: Visitor<'de>,1998         fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E>
1999         where
2000             V: Visitor<'de>,
2001         {
2002             match *self.content {
2003                 Content::None => visitor.visit_none(),
2004                 Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
2005                 Content::Unit => visitor.visit_unit(),
2006                 _ => visitor.visit_some(self),
2007             }
2008         }
2009 
deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2010         fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2011         where
2012             V: Visitor<'de>,
2013         {
2014             match *self.content {
2015                 Content::Unit => visitor.visit_unit(),
2016                 _ => Err(self.invalid_type(&visitor)),
2017             }
2018         }
2019 
deserialize_unit_struct<V>( self, _name: &'static str, visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2020         fn deserialize_unit_struct<V>(
2021             self,
2022             _name: &'static str,
2023             visitor: V,
2024         ) -> Result<V::Value, Self::Error>
2025         where
2026             V: Visitor<'de>,
2027         {
2028             self.deserialize_unit(visitor)
2029         }
2030 
deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, E> where V: Visitor<'de>,2031         fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, E>
2032         where
2033             V: Visitor<'de>,
2034         {
2035             match *self.content {
2036                 Content::Newtype(ref v) => {
2037                     visitor.visit_newtype_struct(ContentRefDeserializer::new(v))
2038                 }
2039                 _ => visitor.visit_newtype_struct(self),
2040             }
2041         }
2042 
deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2043         fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2044         where
2045             V: Visitor<'de>,
2046         {
2047             match *self.content {
2048                 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
2049                 _ => Err(self.invalid_type(&visitor)),
2050             }
2051         }
2052 
deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2053         fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
2054         where
2055             V: Visitor<'de>,
2056         {
2057             self.deserialize_seq(visitor)
2058         }
2059 
deserialize_tuple_struct<V>( self, _name: &'static str, _len: usize, visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2060         fn deserialize_tuple_struct<V>(
2061             self,
2062             _name: &'static str,
2063             _len: usize,
2064             visitor: V,
2065         ) -> Result<V::Value, Self::Error>
2066         where
2067             V: Visitor<'de>,
2068         {
2069             self.deserialize_seq(visitor)
2070         }
2071 
deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2072         fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2073         where
2074             V: Visitor<'de>,
2075         {
2076             match *self.content {
2077                 Content::Map(ref v) => visit_content_map_ref(v, visitor),
2078                 _ => Err(self.invalid_type(&visitor)),
2079             }
2080         }
2081 
deserialize_struct<V>( self, _name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2082         fn deserialize_struct<V>(
2083             self,
2084             _name: &'static str,
2085             _fields: &'static [&'static str],
2086             visitor: V,
2087         ) -> Result<V::Value, Self::Error>
2088         where
2089             V: Visitor<'de>,
2090         {
2091             match *self.content {
2092                 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
2093                 Content::Map(ref v) => visit_content_map_ref(v, visitor),
2094                 _ => Err(self.invalid_type(&visitor)),
2095             }
2096         }
2097 
deserialize_enum<V>( self, _name: &str, _variants: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2098         fn deserialize_enum<V>(
2099             self,
2100             _name: &str,
2101             _variants: &'static [&'static str],
2102             visitor: V,
2103         ) -> Result<V::Value, Self::Error>
2104         where
2105             V: Visitor<'de>,
2106         {
2107             let (variant, value) = match *self.content {
2108                 Content::Map(ref value) => {
2109                     let mut iter = value.iter();
2110                     let (variant, value) = match iter.next() {
2111                         Some(v) => v,
2112                         None => {
2113                             return Err(de::Error::invalid_value(
2114                                 de::Unexpected::Map,
2115                                 &"map with a single key",
2116                             ));
2117                         }
2118                     };
2119                     // enums are encoded in json as maps with a single key:value pair
2120                     if iter.next().is_some() {
2121                         return Err(de::Error::invalid_value(
2122                             de::Unexpected::Map,
2123                             &"map with a single key",
2124                         ));
2125                     }
2126                     (variant, Some(value))
2127                 }
2128                 ref s @ Content::String(_) | ref s @ Content::Str(_) => (s, None),
2129                 ref other => {
2130                     return Err(de::Error::invalid_type(
2131                         other.unexpected(),
2132                         &"string or map",
2133                     ));
2134                 }
2135             };
2136 
2137             visitor.visit_enum(EnumRefDeserializer {
2138                 variant: variant,
2139                 value: value,
2140                 err: PhantomData,
2141             })
2142         }
2143 
deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2144         fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2145         where
2146             V: Visitor<'de>,
2147         {
2148             match *self.content {
2149                 Content::String(ref v) => visitor.visit_str(v),
2150                 Content::Str(v) => visitor.visit_borrowed_str(v),
2151                 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
2152                 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
2153                 Content::U8(v) => visitor.visit_u8(v),
2154                 Content::U64(v) => visitor.visit_u64(v),
2155                 _ => Err(self.invalid_type(&visitor)),
2156             }
2157         }
2158 
deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2159         fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2160         where
2161             V: Visitor<'de>,
2162         {
2163             visitor.visit_unit()
2164         }
2165 
__deserialize_content<V>( self, _: actually_private::T, visitor: V, ) -> Result<Content<'de>, Self::Error> where V: Visitor<'de, Value = Content<'de>>,2166         fn __deserialize_content<V>(
2167             self,
2168             _: actually_private::T,
2169             visitor: V,
2170         ) -> Result<Content<'de>, Self::Error>
2171         where
2172             V: Visitor<'de, Value = Content<'de>>,
2173         {
2174             let _ = visitor;
2175             Ok(self.content.clone())
2176         }
2177     }
2178 
2179     impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> {
2180         /// private API, don't use
new(content: &'a Content<'de>) -> Self2181         pub fn new(content: &'a Content<'de>) -> Self {
2182             ContentRefDeserializer {
2183                 content: content,
2184                 err: PhantomData,
2185             }
2186         }
2187     }
2188 
2189     struct EnumRefDeserializer<'a, 'de: 'a, E>
2190     where
2191         E: de::Error,
2192     {
2193         variant: &'a Content<'de>,
2194         value: Option<&'a Content<'de>>,
2195         err: PhantomData<E>,
2196     }
2197 
2198     impl<'de, 'a, E> de::EnumAccess<'de> for EnumRefDeserializer<'a, 'de, E>
2199     where
2200         E: de::Error,
2201     {
2202         type Error = E;
2203         type Variant = VariantRefDeserializer<'a, 'de, Self::Error>;
2204 
variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> where V: de::DeserializeSeed<'de>,2205         fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
2206         where
2207             V: de::DeserializeSeed<'de>,
2208         {
2209             let visitor = VariantRefDeserializer {
2210                 value: self.value,
2211                 err: PhantomData,
2212             };
2213             seed.deserialize(ContentRefDeserializer::new(self.variant))
2214                 .map(|v| (v, visitor))
2215         }
2216     }
2217 
2218     struct VariantRefDeserializer<'a, 'de: 'a, E>
2219     where
2220         E: de::Error,
2221     {
2222         value: Option<&'a Content<'de>>,
2223         err: PhantomData<E>,
2224     }
2225 
2226     impl<'de, 'a, E> de::VariantAccess<'de> for VariantRefDeserializer<'a, 'de, E>
2227     where
2228         E: de::Error,
2229     {
2230         type Error = E;
2231 
unit_variant(self) -> Result<(), E>2232         fn unit_variant(self) -> Result<(), E> {
2233             match self.value {
2234                 Some(value) => de::Deserialize::deserialize(ContentRefDeserializer::new(value)),
2235                 None => Ok(()),
2236             }
2237         }
2238 
newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E> where T: de::DeserializeSeed<'de>,2239         fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E>
2240         where
2241             T: de::DeserializeSeed<'de>,
2242         {
2243             match self.value {
2244                 Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
2245                 None => Err(de::Error::invalid_type(
2246                     de::Unexpected::UnitVariant,
2247                     &"newtype variant",
2248                 )),
2249             }
2250         }
2251 
tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error> where V: de::Visitor<'de>,2252         fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
2253         where
2254             V: de::Visitor<'de>,
2255         {
2256             match self.value {
2257                 Some(Content::Seq(v)) => {
2258                     de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor)
2259                 }
2260                 Some(other) => Err(de::Error::invalid_type(
2261                     other.unexpected(),
2262                     &"tuple variant",
2263                 )),
2264                 None => Err(de::Error::invalid_type(
2265                     de::Unexpected::UnitVariant,
2266                     &"tuple variant",
2267                 )),
2268             }
2269         }
2270 
struct_variant<V>( self, _fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error> where V: de::Visitor<'de>,2271         fn struct_variant<V>(
2272             self,
2273             _fields: &'static [&'static str],
2274             visitor: V,
2275         ) -> Result<V::Value, Self::Error>
2276         where
2277             V: de::Visitor<'de>,
2278         {
2279             match self.value {
2280                 Some(Content::Map(v)) => {
2281                     de::Deserializer::deserialize_any(MapRefDeserializer::new(v), visitor)
2282                 }
2283                 Some(Content::Seq(v)) => {
2284                     de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor)
2285                 }
2286                 Some(other) => Err(de::Error::invalid_type(
2287                     other.unexpected(),
2288                     &"struct variant",
2289                 )),
2290                 None => Err(de::Error::invalid_type(
2291                     de::Unexpected::UnitVariant,
2292                     &"struct variant",
2293                 )),
2294             }
2295         }
2296     }
2297 
2298     struct SeqRefDeserializer<'a, 'de: 'a, E>
2299     where
2300         E: de::Error,
2301     {
2302         iter: <&'a [Content<'de>] as IntoIterator>::IntoIter,
2303         err: PhantomData<E>,
2304     }
2305 
2306     impl<'a, 'de, E> SeqRefDeserializer<'a, 'de, E>
2307     where
2308         E: de::Error,
2309     {
new(slice: &'a [Content<'de>]) -> Self2310         fn new(slice: &'a [Content<'de>]) -> Self {
2311             SeqRefDeserializer {
2312                 iter: slice.iter(),
2313                 err: PhantomData,
2314             }
2315         }
2316     }
2317 
2318     impl<'de, 'a, E> de::Deserializer<'de> for SeqRefDeserializer<'a, 'de, E>
2319     where
2320         E: de::Error,
2321     {
2322         type Error = E;
2323 
2324         #[inline]
deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> where V: de::Visitor<'de>,2325         fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
2326         where
2327             V: de::Visitor<'de>,
2328         {
2329             let len = self.iter.len();
2330             if len == 0 {
2331                 visitor.visit_unit()
2332             } else {
2333                 let ret = try!(visitor.visit_seq(&mut self));
2334                 let remaining = self.iter.len();
2335                 if remaining == 0 {
2336                     Ok(ret)
2337                 } else {
2338                     Err(de::Error::invalid_length(len, &"fewer elements in array"))
2339                 }
2340             }
2341         }
2342 
2343         forward_to_deserialize_any! {
2344             bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
2345             bytes byte_buf option unit unit_struct newtype_struct seq tuple
2346             tuple_struct map struct enum identifier ignored_any
2347         }
2348     }
2349 
2350     impl<'de, 'a, E> de::SeqAccess<'de> for SeqRefDeserializer<'a, 'de, E>
2351     where
2352         E: de::Error,
2353     {
2354         type Error = E;
2355 
next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> where T: de::DeserializeSeed<'de>,2356         fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
2357         where
2358             T: de::DeserializeSeed<'de>,
2359         {
2360             match self.iter.next() {
2361                 Some(value) => seed
2362                     .deserialize(ContentRefDeserializer::new(value))
2363                     .map(Some),
2364                 None => Ok(None),
2365             }
2366         }
2367 
size_hint(&self) -> Option<usize>2368         fn size_hint(&self) -> Option<usize> {
2369             size_hint::from_bounds(&self.iter)
2370         }
2371     }
2372 
2373     struct MapRefDeserializer<'a, 'de: 'a, E>
2374     where
2375         E: de::Error,
2376     {
2377         iter: <&'a [(Content<'de>, Content<'de>)] as IntoIterator>::IntoIter,
2378         value: Option<&'a Content<'de>>,
2379         err: PhantomData<E>,
2380     }
2381 
2382     impl<'a, 'de, E> MapRefDeserializer<'a, 'de, E>
2383     where
2384         E: de::Error,
2385     {
new(map: &'a [(Content<'de>, Content<'de>)]) -> Self2386         fn new(map: &'a [(Content<'de>, Content<'de>)]) -> Self {
2387             MapRefDeserializer {
2388                 iter: map.iter(),
2389                 value: None,
2390                 err: PhantomData,
2391             }
2392         }
2393     }
2394 
2395     impl<'de, 'a, E> de::MapAccess<'de> for MapRefDeserializer<'a, 'de, E>
2396     where
2397         E: de::Error,
2398     {
2399         type Error = E;
2400 
next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> where T: de::DeserializeSeed<'de>,2401         fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
2402         where
2403             T: de::DeserializeSeed<'de>,
2404         {
2405             match self.iter.next() {
2406                 Some((key, value)) => {
2407                     self.value = Some(value);
2408                     seed.deserialize(ContentRefDeserializer::new(key)).map(Some)
2409                 }
2410                 None => Ok(None),
2411             }
2412         }
2413 
next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> where T: de::DeserializeSeed<'de>,2414         fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
2415         where
2416             T: de::DeserializeSeed<'de>,
2417         {
2418             match self.value.take() {
2419                 Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
2420                 None => Err(de::Error::custom("value is missing")),
2421             }
2422         }
2423 
size_hint(&self) -> Option<usize>2424         fn size_hint(&self) -> Option<usize> {
2425             size_hint::from_bounds(&self.iter)
2426         }
2427     }
2428 
2429     impl<'de, 'a, E> de::Deserializer<'de> for MapRefDeserializer<'a, 'de, E>
2430     where
2431         E: de::Error,
2432     {
2433         type Error = E;
2434 
2435         #[inline]
deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: de::Visitor<'de>,2436         fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2437         where
2438             V: de::Visitor<'de>,
2439         {
2440             visitor.visit_map(self)
2441         }
2442 
2443         forward_to_deserialize_any! {
2444             bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
2445             bytes byte_buf option unit unit_struct newtype_struct seq tuple
2446             tuple_struct map struct enum identifier ignored_any
2447         }
2448     }
2449 
2450     impl<'de, E> de::IntoDeserializer<'de, E> for ContentDeserializer<'de, E>
2451     where
2452         E: de::Error,
2453     {
2454         type Deserializer = Self;
2455 
into_deserializer(self) -> Self2456         fn into_deserializer(self) -> Self {
2457             self
2458         }
2459     }
2460 
2461     impl<'de, 'a, E> de::IntoDeserializer<'de, E> for ContentRefDeserializer<'a, 'de, E>
2462     where
2463         E: de::Error,
2464     {
2465         type Deserializer = Self;
2466 
into_deserializer(self) -> Self2467         fn into_deserializer(self) -> Self {
2468             self
2469         }
2470     }
2471 
2472     /// Visitor for deserializing an internally tagged unit variant.
2473     ///
2474     /// Not public API.
2475     pub struct InternallyTaggedUnitVisitor<'a> {
2476         type_name: &'a str,
2477         variant_name: &'a str,
2478     }
2479 
2480     impl<'a> InternallyTaggedUnitVisitor<'a> {
2481         /// Not public API.
new(type_name: &'a str, variant_name: &'a str) -> Self2482         pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
2483             InternallyTaggedUnitVisitor {
2484                 type_name: type_name,
2485                 variant_name: variant_name,
2486             }
2487         }
2488     }
2489 
2490     impl<'de, 'a> Visitor<'de> for InternallyTaggedUnitVisitor<'a> {
2491         type Value = ();
2492 
expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result2493         fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
2494             write!(
2495                 formatter,
2496                 "unit variant {}::{}",
2497                 self.type_name, self.variant_name
2498             )
2499         }
2500 
visit_seq<S>(self, _: S) -> Result<(), S::Error> where S: SeqAccess<'de>,2501         fn visit_seq<S>(self, _: S) -> Result<(), S::Error>
2502         where
2503             S: SeqAccess<'de>,
2504         {
2505             Ok(())
2506         }
2507 
visit_map<M>(self, mut access: M) -> Result<(), M::Error> where M: MapAccess<'de>,2508         fn visit_map<M>(self, mut access: M) -> Result<(), M::Error>
2509         where
2510             M: MapAccess<'de>,
2511         {
2512             while try!(access.next_entry::<IgnoredAny, IgnoredAny>()).is_some() {}
2513             Ok(())
2514         }
2515     }
2516 
2517     /// Visitor for deserializing an untagged unit variant.
2518     ///
2519     /// Not public API.
2520     pub struct UntaggedUnitVisitor<'a> {
2521         type_name: &'a str,
2522         variant_name: &'a str,
2523     }
2524 
2525     impl<'a> UntaggedUnitVisitor<'a> {
2526         /// Not public API.
new(type_name: &'a str, variant_name: &'a str) -> Self2527         pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
2528             UntaggedUnitVisitor {
2529                 type_name: type_name,
2530                 variant_name: variant_name,
2531             }
2532         }
2533     }
2534 
2535     impl<'de, 'a> Visitor<'de> for UntaggedUnitVisitor<'a> {
2536         type Value = ();
2537 
expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result2538         fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
2539             write!(
2540                 formatter,
2541                 "unit variant {}::{}",
2542                 self.type_name, self.variant_name
2543             )
2544         }
2545 
visit_unit<E>(self) -> Result<(), E> where E: de::Error,2546         fn visit_unit<E>(self) -> Result<(), E>
2547         where
2548             E: de::Error,
2549         {
2550             Ok(())
2551         }
2552 
visit_none<E>(self) -> Result<(), E> where E: de::Error,2553         fn visit_none<E>(self) -> Result<(), E>
2554         where
2555             E: de::Error,
2556         {
2557             Ok(())
2558         }
2559     }
2560 }
2561 
2562 ////////////////////////////////////////////////////////////////////////////////
2563 
2564 // Like `IntoDeserializer` but also implemented for `&[u8]`. This is used for
2565 // the newtype fallthrough case of `field_identifier`.
2566 //
2567 //    #[derive(Deserialize)]
2568 //    #[serde(field_identifier)]
2569 //    enum F {
2570 //        A,
2571 //        B,
2572 //        Other(String), // deserialized using IdentifierDeserializer
2573 //    }
2574 pub trait IdentifierDeserializer<'de, E: Error> {
2575     type Deserializer: Deserializer<'de, Error = E>;
2576 
from(self) -> Self::Deserializer2577     fn from(self) -> Self::Deserializer;
2578 }
2579 
2580 pub struct Borrowed<'de, T: 'de + ?Sized>(pub &'de T);
2581 
2582 impl<'de, E> IdentifierDeserializer<'de, E> for u64
2583 where
2584     E: Error,
2585 {
2586     type Deserializer = <u64 as IntoDeserializer<'de, E>>::Deserializer;
2587 
from(self) -> Self::Deserializer2588     fn from(self) -> Self::Deserializer {
2589         self.into_deserializer()
2590     }
2591 }
2592 
2593 pub struct StrDeserializer<'a, E> {
2594     value: &'a str,
2595     marker: PhantomData<E>,
2596 }
2597 
2598 impl<'de, 'a, E> Deserializer<'de> for StrDeserializer<'a, E>
2599 where
2600     E: Error,
2601 {
2602     type Error = E;
2603 
deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2604     fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2605     where
2606         V: Visitor<'de>,
2607     {
2608         visitor.visit_str(self.value)
2609     }
2610 
2611     forward_to_deserialize_any! {
2612         bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
2613         bytes byte_buf option unit unit_struct newtype_struct seq tuple
2614         tuple_struct map struct enum identifier ignored_any
2615     }
2616 }
2617 
2618 pub struct BorrowedStrDeserializer<'de, E> {
2619     value: &'de str,
2620     marker: PhantomData<E>,
2621 }
2622 
2623 impl<'de, E> Deserializer<'de> for BorrowedStrDeserializer<'de, E>
2624 where
2625     E: Error,
2626 {
2627     type Error = E;
2628 
deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2629     fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2630     where
2631         V: Visitor<'de>,
2632     {
2633         visitor.visit_borrowed_str(self.value)
2634     }
2635 
2636     forward_to_deserialize_any! {
2637         bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
2638         bytes byte_buf option unit unit_struct newtype_struct seq tuple
2639         tuple_struct map struct enum identifier ignored_any
2640     }
2641 }
2642 
2643 impl<'a, E> IdentifierDeserializer<'a, E> for &'a str
2644 where
2645     E: Error,
2646 {
2647     type Deserializer = StrDeserializer<'a, E>;
2648 
from(self) -> Self::Deserializer2649     fn from(self) -> Self::Deserializer {
2650         StrDeserializer {
2651             value: self,
2652             marker: PhantomData,
2653         }
2654     }
2655 }
2656 
2657 impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, str>
2658 where
2659     E: Error,
2660 {
2661     type Deserializer = BorrowedStrDeserializer<'de, E>;
2662 
from(self) -> Self::Deserializer2663     fn from(self) -> Self::Deserializer {
2664         BorrowedStrDeserializer {
2665             value: self.0,
2666             marker: PhantomData,
2667         }
2668     }
2669 }
2670 
2671 impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8]
2672 where
2673     E: Error,
2674 {
2675     type Deserializer = BytesDeserializer<'a, E>;
2676 
from(self) -> Self::Deserializer2677     fn from(self) -> Self::Deserializer {
2678         BytesDeserializer::new(self)
2679     }
2680 }
2681 
2682 impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, [u8]>
2683 where
2684     E: Error,
2685 {
2686     type Deserializer = BorrowedBytesDeserializer<'de, E>;
2687 
from(self) -> Self::Deserializer2688     fn from(self) -> Self::Deserializer {
2689         BorrowedBytesDeserializer::new(self.0)
2690     }
2691 }
2692 
2693 #[cfg(any(feature = "std", feature = "alloc"))]
2694 pub struct FlatMapDeserializer<'a, 'de: 'a, E>(
2695     pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>,
2696     pub PhantomData<E>,
2697 );
2698 
2699 #[cfg(any(feature = "std", feature = "alloc"))]
2700 impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E>
2701 where
2702     E: Error,
2703 {
deserialize_other<V>() -> Result<V, E>2704     fn deserialize_other<V>() -> Result<V, E> {
2705         Err(Error::custom("can only flatten structs and maps"))
2706     }
2707 }
2708 
2709 #[cfg(any(feature = "std", feature = "alloc"))]
2710 macro_rules! forward_to_deserialize_other {
2711     ($($func:ident ($($arg:ty),*))*) => {
2712         $(
2713             fn $func<V>(self, $(_: $arg,)* _visitor: V) -> Result<V::Value, Self::Error>
2714             where
2715                 V: Visitor<'de>,
2716             {
2717                 Self::deserialize_other()
2718             }
2719         )*
2720     }
2721 }
2722 
2723 #[cfg(any(feature = "std", feature = "alloc"))]
2724 impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
2725 where
2726     E: Error,
2727 {
2728     type Error = E;
2729 
deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2730     fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2731     where
2732         V: Visitor<'de>,
2733     {
2734         visitor.visit_map(FlatInternallyTaggedAccess {
2735             iter: self.0.iter_mut(),
2736             pending: None,
2737             _marker: PhantomData,
2738         })
2739     }
2740 
deserialize_enum<V>( self, name: &'static str, variants: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2741     fn deserialize_enum<V>(
2742         self,
2743         name: &'static str,
2744         variants: &'static [&'static str],
2745         visitor: V,
2746     ) -> Result<V::Value, Self::Error>
2747     where
2748         V: Visitor<'de>,
2749     {
2750         for item in self.0.iter_mut() {
2751             // items in the vector are nulled out when used.  So we can only use
2752             // an item if it's still filled in and if the field is one we care
2753             // about.
2754             let use_item = match *item {
2755                 None => false,
2756                 Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)),
2757             };
2758 
2759             if use_item {
2760                 let (key, value) = item.take().unwrap();
2761                 return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
2762             }
2763         }
2764 
2765         Err(Error::custom(format_args!(
2766             "no variant of enum {} found in flattened data",
2767             name
2768         )))
2769     }
2770 
deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2771     fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2772     where
2773         V: Visitor<'de>,
2774     {
2775         visitor.visit_map(FlatMapAccess::new(self.0.iter()))
2776     }
2777 
deserialize_struct<V>( self, _: &'static str, fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2778     fn deserialize_struct<V>(
2779         self,
2780         _: &'static str,
2781         fields: &'static [&'static str],
2782         visitor: V,
2783     ) -> Result<V::Value, Self::Error>
2784     where
2785         V: Visitor<'de>,
2786     {
2787         visitor.visit_map(FlatStructAccess::new(self.0.iter_mut(), fields))
2788     }
2789 
deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2790     fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
2791     where
2792         V: Visitor<'de>,
2793     {
2794         visitor.visit_newtype_struct(self)
2795     }
2796 
deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2797     fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2798     where
2799         V: Visitor<'de>,
2800     {
2801         match visitor.__private_visit_untagged_option(self) {
2802             Ok(value) => Ok(value),
2803             Err(()) => Self::deserialize_other(),
2804         }
2805     }
2806 
deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,2807     fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2808     where
2809         V: Visitor<'de>,
2810     {
2811         visitor.visit_unit()
2812     }
2813 
2814     forward_to_deserialize_other! {
2815         deserialize_bool()
2816         deserialize_i8()
2817         deserialize_i16()
2818         deserialize_i32()
2819         deserialize_i64()
2820         deserialize_u8()
2821         deserialize_u16()
2822         deserialize_u32()
2823         deserialize_u64()
2824         deserialize_f32()
2825         deserialize_f64()
2826         deserialize_char()
2827         deserialize_str()
2828         deserialize_string()
2829         deserialize_bytes()
2830         deserialize_byte_buf()
2831         deserialize_unit_struct(&'static str)
2832         deserialize_seq()
2833         deserialize_tuple(usize)
2834         deserialize_tuple_struct(&'static str, usize)
2835         deserialize_identifier()
2836         deserialize_ignored_any()
2837     }
2838 }
2839 
2840 #[cfg(any(feature = "std", feature = "alloc"))]
2841 pub struct FlatMapAccess<'a, 'de: 'a, E> {
2842     iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
2843     pending_content: Option<&'a Content<'de>>,
2844     _marker: PhantomData<E>,
2845 }
2846 
2847 #[cfg(any(feature = "std", feature = "alloc"))]
2848 impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> {
new( iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>, ) -> FlatMapAccess<'a, 'de, E>2849     fn new(
2850         iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
2851     ) -> FlatMapAccess<'a, 'de, E> {
2852         FlatMapAccess {
2853             iter: iter,
2854             pending_content: None,
2855             _marker: PhantomData,
2856         }
2857     }
2858 }
2859 
2860 #[cfg(any(feature = "std", feature = "alloc"))]
2861 impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E>
2862 where
2863     E: Error,
2864 {
2865     type Error = E;
2866 
next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> where T: DeserializeSeed<'de>,2867     fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
2868     where
2869         T: DeserializeSeed<'de>,
2870     {
2871         for item in &mut self.iter {
2872             // Items in the vector are nulled out when used by a struct.
2873             if let Some((ref key, ref content)) = *item {
2874                 self.pending_content = Some(content);
2875                 return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
2876             }
2877         }
2878         Ok(None)
2879     }
2880 
next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> where T: DeserializeSeed<'de>,2881     fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
2882     where
2883         T: DeserializeSeed<'de>,
2884     {
2885         match self.pending_content.take() {
2886             Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
2887             None => Err(Error::custom("value is missing")),
2888         }
2889     }
2890 }
2891 
2892 #[cfg(any(feature = "std", feature = "alloc"))]
2893 pub struct FlatStructAccess<'a, 'de: 'a, E> {
2894     iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
2895     pending_content: Option<Content<'de>>,
2896     fields: &'static [&'static str],
2897     _marker: PhantomData<E>,
2898 }
2899 
2900 #[cfg(any(feature = "std", feature = "alloc"))]
2901 impl<'a, 'de, E> FlatStructAccess<'a, 'de, E> {
new( iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>, fields: &'static [&'static str], ) -> FlatStructAccess<'a, 'de, E>2902     fn new(
2903         iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
2904         fields: &'static [&'static str],
2905     ) -> FlatStructAccess<'a, 'de, E> {
2906         FlatStructAccess {
2907             iter: iter,
2908             pending_content: None,
2909             fields: fields,
2910             _marker: PhantomData,
2911         }
2912     }
2913 }
2914 
2915 #[cfg(any(feature = "std", feature = "alloc"))]
2916 impl<'a, 'de, E> MapAccess<'de> for FlatStructAccess<'a, 'de, E>
2917 where
2918     E: Error,
2919 {
2920     type Error = E;
2921 
next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> where T: DeserializeSeed<'de>,2922     fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
2923     where
2924         T: DeserializeSeed<'de>,
2925     {
2926         while let Some(item) = self.iter.next() {
2927             // items in the vector are nulled out when used.  So we can only use
2928             // an item if it's still filled in and if the field is one we care
2929             // about.  In case we do not know which fields we want, we take them all.
2930             let use_item = match *item {
2931                 None => false,
2932                 Some((ref c, _)) => c.as_str().map_or(false, |key| self.fields.contains(&key)),
2933             };
2934 
2935             if use_item {
2936                 let (key, content) = item.take().unwrap();
2937                 self.pending_content = Some(content);
2938                 return seed.deserialize(ContentDeserializer::new(key)).map(Some);
2939             }
2940         }
2941         Ok(None)
2942     }
2943 
next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> where T: DeserializeSeed<'de>,2944     fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
2945     where
2946         T: DeserializeSeed<'de>,
2947     {
2948         match self.pending_content.take() {
2949             Some(value) => seed.deserialize(ContentDeserializer::new(value)),
2950             None => Err(Error::custom("value is missing")),
2951         }
2952     }
2953 }
2954 
2955 #[cfg(any(feature = "std", feature = "alloc"))]
2956 pub struct FlatInternallyTaggedAccess<'a, 'de: 'a, E> {
2957     iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
2958     pending: Option<&'a Content<'de>>,
2959     _marker: PhantomData<E>,
2960 }
2961 
2962 #[cfg(any(feature = "std", feature = "alloc"))]
2963 impl<'a, 'de, E> MapAccess<'de> for FlatInternallyTaggedAccess<'a, 'de, E>
2964 where
2965     E: Error,
2966 {
2967     type Error = E;
2968 
next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> where T: DeserializeSeed<'de>,2969     fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
2970     where
2971         T: DeserializeSeed<'de>,
2972     {
2973         for item in &mut self.iter {
2974             if let Some((ref key, ref content)) = *item {
2975                 // Do not take(), instead borrow this entry. The internally tagged
2976                 // enum does its own buffering so we can't tell whether this entry
2977                 // is going to be consumed. Borrowing here leaves the entry
2978                 // available for later flattened fields.
2979                 self.pending = Some(content);
2980                 return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
2981             }
2982         }
2983         Ok(None)
2984     }
2985 
next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> where T: DeserializeSeed<'de>,2986     fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
2987     where
2988         T: DeserializeSeed<'de>,
2989     {
2990         match self.pending.take() {
2991             Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
2992             None => panic!("value is missing"),
2993         }
2994     }
2995 }
2996