Import 'serde' crate version 1.0.115

Bug: 163175424
Test: N/A
Change-Id: Ibac4c3a38612d12add0cc5feaae8e190992a0386
diff --git a/src/private/de.rs b/src/private/de.rs
new file mode 100644
index 0000000..bcb964a
--- /dev/null
+++ b/src/private/de.rs
@@ -0,0 +1,2954 @@
+use lib::*;
+
+use de::{Deserialize, DeserializeSeed, Deserializer, Error, IntoDeserializer, Visitor};
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+use de::{MapAccess, Unexpected};
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub use self::content::{
+    Content, ContentDeserializer, ContentRefDeserializer, EnumDeserializer,
+    InternallyTaggedUnitVisitor, TagContentOtherField, TagContentOtherFieldVisitor,
+    TagOrContentField, TagOrContentFieldVisitor, TaggedContentVisitor, UntaggedUnitVisitor,
+};
+
+/// If the missing field is of type `Option<T>` then treat is as `None`,
+/// otherwise it is an error.
+pub fn missing_field<'de, V, E>(field: &'static str) -> Result<V, E>
+where
+    V: Deserialize<'de>,
+    E: Error,
+{
+    struct MissingFieldDeserializer<E>(&'static str, PhantomData<E>);
+
+    impl<'de, E> Deserializer<'de> for MissingFieldDeserializer<E>
+    where
+        E: Error,
+    {
+        type Error = E;
+
+        fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, E>
+        where
+            V: Visitor<'de>,
+        {
+            Err(Error::missing_field(self.0))
+        }
+
+        fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E>
+        where
+            V: Visitor<'de>,
+        {
+            visitor.visit_none()
+        }
+
+        forward_to_deserialize_any! {
+            bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
+            bytes byte_buf unit unit_struct newtype_struct seq tuple
+            tuple_struct map struct enum identifier ignored_any
+        }
+    }
+
+    let deserializer = MissingFieldDeserializer(field, PhantomData);
+    Deserialize::deserialize(deserializer)
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub fn borrow_cow_str<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error>
+where
+    D: Deserializer<'de>,
+    R: From<Cow<'a, str>>,
+{
+    struct CowStrVisitor;
+
+    impl<'a> Visitor<'a> for CowStrVisitor {
+        type Value = Cow<'a, str>;
+
+        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+            formatter.write_str("a string")
+        }
+
+        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            Ok(Cow::Owned(v.to_owned()))
+        }
+
+        fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            Ok(Cow::Borrowed(v))
+        }
+
+        fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            Ok(Cow::Owned(v))
+        }
+
+        fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            match str::from_utf8(v) {
+                Ok(s) => Ok(Cow::Owned(s.to_owned())),
+                Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
+            }
+        }
+
+        fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            match str::from_utf8(v) {
+                Ok(s) => Ok(Cow::Borrowed(s)),
+                Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
+            }
+        }
+
+        fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            match String::from_utf8(v) {
+                Ok(s) => Ok(Cow::Owned(s)),
+                Err(e) => Err(Error::invalid_value(
+                    Unexpected::Bytes(&e.into_bytes()),
+                    &self,
+                )),
+            }
+        }
+    }
+
+    deserializer.deserialize_str(CowStrVisitor).map(From::from)
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub fn borrow_cow_bytes<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error>
+where
+    D: Deserializer<'de>,
+    R: From<Cow<'a, [u8]>>,
+{
+    struct CowBytesVisitor;
+
+    impl<'a> Visitor<'a> for CowBytesVisitor {
+        type Value = Cow<'a, [u8]>;
+
+        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+            formatter.write_str("a byte array")
+        }
+
+        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            Ok(Cow::Owned(v.as_bytes().to_vec()))
+        }
+
+        fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            Ok(Cow::Borrowed(v.as_bytes()))
+        }
+
+        fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            Ok(Cow::Owned(v.into_bytes()))
+        }
+
+        fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            Ok(Cow::Owned(v.to_vec()))
+        }
+
+        fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            Ok(Cow::Borrowed(v))
+        }
+
+        fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
+        where
+            E: Error,
+        {
+            Ok(Cow::Owned(v))
+        }
+    }
+
+    deserializer
+        .deserialize_bytes(CowBytesVisitor)
+        .map(From::from)
+}
+
+pub mod size_hint {
+    use lib::*;
+
+    pub fn from_bounds<I>(iter: &I) -> Option<usize>
+    where
+        I: Iterator,
+    {
+        helper(iter.size_hint())
+    }
+
+    #[inline]
+    pub fn cautious(hint: Option<usize>) -> usize {
+        cmp::min(hint.unwrap_or(0), 4096)
+    }
+
+    fn helper(bounds: (usize, Option<usize>)) -> Option<usize> {
+        match bounds {
+            (lower, Some(upper)) if lower == upper => Some(upper),
+            _ => None,
+        }
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+mod content {
+    // This module is private and nothing here should be used outside of
+    // generated code.
+    //
+    // We will iterate on the implementation for a few releases and only have to
+    // worry about backward compatibility for the `untagged` and `tag` attributes
+    // rather than for this entire mechanism.
+    //
+    // This issue is tracking making some of this stuff public:
+    // https://github.com/serde-rs/serde/issues/741
+
+    use lib::*;
+
+    use super::size_hint;
+    use de::{
+        self, Deserialize, DeserializeSeed, Deserializer, EnumAccess, Expected, IgnoredAny,
+        MapAccess, SeqAccess, Unexpected, Visitor,
+    };
+
+    /// Used from generated code to buffer the contents of the Deserializer when
+    /// deserializing untagged enums and internally tagged enums.
+    ///
+    /// Not public API. Use serde-value instead.
+    #[derive(Debug)]
+    pub enum Content<'de> {
+        Bool(bool),
+
+        U8(u8),
+        U16(u16),
+        U32(u32),
+        U64(u64),
+
+        I8(i8),
+        I16(i16),
+        I32(i32),
+        I64(i64),
+
+        F32(f32),
+        F64(f64),
+
+        Char(char),
+        String(String),
+        Str(&'de str),
+        ByteBuf(Vec<u8>),
+        Bytes(&'de [u8]),
+
+        None,
+        Some(Box<Content<'de>>),
+
+        Unit,
+        Newtype(Box<Content<'de>>),
+        Seq(Vec<Content<'de>>),
+        Map(Vec<(Content<'de>, Content<'de>)>),
+    }
+
+    impl<'de> Content<'de> {
+        pub fn as_str(&self) -> Option<&str> {
+            match *self {
+                Content::Str(x) => Some(x),
+                Content::String(ref x) => Some(x),
+                Content::Bytes(x) => str::from_utf8(x).ok(),
+                Content::ByteBuf(ref x) => str::from_utf8(x).ok(),
+                _ => None,
+            }
+        }
+
+        #[cold]
+        fn unexpected(&self) -> Unexpected {
+            match *self {
+                Content::Bool(b) => Unexpected::Bool(b),
+                Content::U8(n) => Unexpected::Unsigned(n as u64),
+                Content::U16(n) => Unexpected::Unsigned(n as u64),
+                Content::U32(n) => Unexpected::Unsigned(n as u64),
+                Content::U64(n) => Unexpected::Unsigned(n),
+                Content::I8(n) => Unexpected::Signed(n as i64),
+                Content::I16(n) => Unexpected::Signed(n as i64),
+                Content::I32(n) => Unexpected::Signed(n as i64),
+                Content::I64(n) => Unexpected::Signed(n),
+                Content::F32(f) => Unexpected::Float(f as f64),
+                Content::F64(f) => Unexpected::Float(f),
+                Content::Char(c) => Unexpected::Char(c),
+                Content::String(ref s) => Unexpected::Str(s),
+                Content::Str(s) => Unexpected::Str(s),
+                Content::ByteBuf(ref b) => Unexpected::Bytes(b),
+                Content::Bytes(b) => Unexpected::Bytes(b),
+                Content::None | Content::Some(_) => Unexpected::Option,
+                Content::Unit => Unexpected::Unit,
+                Content::Newtype(_) => Unexpected::NewtypeStruct,
+                Content::Seq(_) => Unexpected::Seq,
+                Content::Map(_) => Unexpected::Map,
+            }
+        }
+    }
+
+    impl<'de> Deserialize<'de> for Content<'de> {
+        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+        where
+            D: Deserializer<'de>,
+        {
+            // Untagged and internally tagged enums are only supported in
+            // self-describing formats.
+            let visitor = ContentVisitor { value: PhantomData };
+            deserializer.deserialize_any(visitor)
+        }
+    }
+
+    struct ContentVisitor<'de> {
+        value: PhantomData<Content<'de>>,
+    }
+
+    impl<'de> ContentVisitor<'de> {
+        fn new() -> Self {
+            ContentVisitor { value: PhantomData }
+        }
+    }
+
+    impl<'de> Visitor<'de> for ContentVisitor<'de> {
+        type Value = Content<'de>;
+
+        fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+            fmt.write_str("any value")
+        }
+
+        fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::Bool(value))
+        }
+
+        fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::I8(value))
+        }
+
+        fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::I16(value))
+        }
+
+        fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::I32(value))
+        }
+
+        fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::I64(value))
+        }
+
+        fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::U8(value))
+        }
+
+        fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::U16(value))
+        }
+
+        fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::U32(value))
+        }
+
+        fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::U64(value))
+        }
+
+        fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::F32(value))
+        }
+
+        fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::F64(value))
+        }
+
+        fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::Char(value))
+        }
+
+        fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::String(value.into()))
+        }
+
+        fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::Str(value))
+        }
+
+        fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::String(value))
+        }
+
+        fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::ByteBuf(value.into()))
+        }
+
+        fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::Bytes(value))
+        }
+
+        fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::ByteBuf(value))
+        }
+
+        fn visit_unit<F>(self) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::Unit)
+        }
+
+        fn visit_none<F>(self) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            Ok(Content::None)
+        }
+
+        fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+        where
+            D: Deserializer<'de>,
+        {
+            Deserialize::deserialize(deserializer).map(|v| Content::Some(Box::new(v)))
+        }
+
+        fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+        where
+            D: Deserializer<'de>,
+        {
+            Deserialize::deserialize(deserializer).map(|v| Content::Newtype(Box::new(v)))
+        }
+
+        fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
+        where
+            V: SeqAccess<'de>,
+        {
+            let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
+            while let Some(e) = try!(visitor.next_element()) {
+                vec.push(e);
+            }
+            Ok(Content::Seq(vec))
+        }
+
+        fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
+        where
+            V: MapAccess<'de>,
+        {
+            let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
+            while let Some(kv) = try!(visitor.next_entry()) {
+                vec.push(kv);
+            }
+            Ok(Content::Map(vec))
+        }
+
+        fn visit_enum<V>(self, _visitor: V) -> Result<Self::Value, V::Error>
+        where
+            V: EnumAccess<'de>,
+        {
+            Err(de::Error::custom(
+                "untagged and internally tagged enums do not support enum input",
+            ))
+        }
+    }
+
+    /// This is the type of the map keys in an internally tagged enum.
+    ///
+    /// Not public API.
+    pub enum TagOrContent<'de> {
+        Tag,
+        Content(Content<'de>),
+    }
+
+    struct TagOrContentVisitor<'de> {
+        name: &'static str,
+        value: PhantomData<TagOrContent<'de>>,
+    }
+
+    impl<'de> TagOrContentVisitor<'de> {
+        fn new(name: &'static str) -> Self {
+            TagOrContentVisitor {
+                name: name,
+                value: PhantomData,
+            }
+        }
+    }
+
+    impl<'de> DeserializeSeed<'de> for TagOrContentVisitor<'de> {
+        type Value = TagOrContent<'de>;
+
+        fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+        where
+            D: Deserializer<'de>,
+        {
+            // Internally tagged enums are only supported in self-describing
+            // formats.
+            deserializer.deserialize_any(self)
+        }
+    }
+
+    impl<'de> Visitor<'de> for TagOrContentVisitor<'de> {
+        type Value = TagOrContent<'de>;
+
+        fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+            write!(fmt, "a type tag `{}` or any other value", self.name)
+        }
+
+        fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_bool(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_i8(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_i16(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_i32(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_i64(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_u8(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_u16(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_u32(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_u64(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_f32(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_f64(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_char(value)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            if value == self.name {
+                Ok(TagOrContent::Tag)
+            } else {
+                ContentVisitor::new()
+                    .visit_str(value)
+                    .map(TagOrContent::Content)
+            }
+        }
+
+        fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            if value == self.name {
+                Ok(TagOrContent::Tag)
+            } else {
+                ContentVisitor::new()
+                    .visit_borrowed_str(value)
+                    .map(TagOrContent::Content)
+            }
+        }
+
+        fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            if value == self.name {
+                Ok(TagOrContent::Tag)
+            } else {
+                ContentVisitor::new()
+                    .visit_string(value)
+                    .map(TagOrContent::Content)
+            }
+        }
+
+        fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            if value == self.name.as_bytes() {
+                Ok(TagOrContent::Tag)
+            } else {
+                ContentVisitor::new()
+                    .visit_bytes(value)
+                    .map(TagOrContent::Content)
+            }
+        }
+
+        fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            if value == self.name.as_bytes() {
+                Ok(TagOrContent::Tag)
+            } else {
+                ContentVisitor::new()
+                    .visit_borrowed_bytes(value)
+                    .map(TagOrContent::Content)
+            }
+        }
+
+        fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            if value == self.name.as_bytes() {
+                Ok(TagOrContent::Tag)
+            } else {
+                ContentVisitor::new()
+                    .visit_byte_buf(value)
+                    .map(TagOrContent::Content)
+            }
+        }
+
+        fn visit_unit<F>(self) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_unit()
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_none<F>(self) -> Result<Self::Value, F>
+        where
+            F: de::Error,
+        {
+            ContentVisitor::new()
+                .visit_none()
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+        where
+            D: Deserializer<'de>,
+        {
+            ContentVisitor::new()
+                .visit_some(deserializer)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+        where
+            D: Deserializer<'de>,
+        {
+            ContentVisitor::new()
+                .visit_newtype_struct(deserializer)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
+        where
+            V: SeqAccess<'de>,
+        {
+            ContentVisitor::new()
+                .visit_seq(visitor)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
+        where
+            V: MapAccess<'de>,
+        {
+            ContentVisitor::new()
+                .visit_map(visitor)
+                .map(TagOrContent::Content)
+        }
+
+        fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
+        where
+            V: EnumAccess<'de>,
+        {
+            ContentVisitor::new()
+                .visit_enum(visitor)
+                .map(TagOrContent::Content)
+        }
+    }
+
+    /// Used by generated code to deserialize an internally tagged enum.
+    ///
+    /// Not public API.
+    pub struct TaggedContent<'de, T> {
+        pub tag: T,
+        pub content: Content<'de>,
+    }
+
+    /// Not public API.
+    pub struct TaggedContentVisitor<'de, T> {
+        tag_name: &'static str,
+        value: PhantomData<TaggedContent<'de, T>>,
+    }
+
+    impl<'de, T> TaggedContentVisitor<'de, T> {
+        /// Visitor for the content of an internally tagged enum with the given
+        /// tag name.
+        pub fn new(name: &'static str) -> Self {
+            TaggedContentVisitor {
+                tag_name: name,
+                value: PhantomData,
+            }
+        }
+    }
+
+    impl<'de, T> DeserializeSeed<'de> for TaggedContentVisitor<'de, T>
+    where
+        T: Deserialize<'de>,
+    {
+        type Value = TaggedContent<'de, T>;
+
+        fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+        where
+            D: Deserializer<'de>,
+        {
+            // Internally tagged enums are only supported in self-describing
+            // formats.
+            deserializer.deserialize_any(self)
+        }
+    }
+
+    impl<'de, T> Visitor<'de> for TaggedContentVisitor<'de, T>
+    where
+        T: Deserialize<'de>,
+    {
+        type Value = TaggedContent<'de, T>;
+
+        fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+            fmt.write_str("internally tagged enum")
+        }
+
+        fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error>
+        where
+            S: SeqAccess<'de>,
+        {
+            let tag = match try!(seq.next_element()) {
+                Some(tag) => tag,
+                None => {
+                    return Err(de::Error::missing_field(self.tag_name));
+                }
+            };
+            let rest = de::value::SeqAccessDeserializer::new(seq);
+            Ok(TaggedContent {
+                tag: tag,
+                content: try!(Content::deserialize(rest)),
+            })
+        }
+
+        fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
+        where
+            M: MapAccess<'de>,
+        {
+            let mut tag = None;
+            let mut vec = Vec::with_capacity(size_hint::cautious(map.size_hint()));
+            while let Some(k) = try!(map.next_key_seed(TagOrContentVisitor::new(self.tag_name))) {
+                match k {
+                    TagOrContent::Tag => {
+                        if tag.is_some() {
+                            return Err(de::Error::duplicate_field(self.tag_name));
+                        }
+                        tag = Some(try!(map.next_value()));
+                    }
+                    TagOrContent::Content(k) => {
+                        let v = try!(map.next_value());
+                        vec.push((k, v));
+                    }
+                }
+            }
+            match tag {
+                None => Err(de::Error::missing_field(self.tag_name)),
+                Some(tag) => Ok(TaggedContent {
+                    tag: tag,
+                    content: Content::Map(vec),
+                }),
+            }
+        }
+    }
+
+    /// Used by generated code to deserialize an adjacently tagged enum.
+    ///
+    /// Not public API.
+    pub enum TagOrContentField {
+        Tag,
+        Content,
+    }
+
+    /// Not public API.
+    pub struct TagOrContentFieldVisitor {
+        pub tag: &'static str,
+        pub content: &'static str,
+    }
+
+    impl<'de> DeserializeSeed<'de> for TagOrContentFieldVisitor {
+        type Value = TagOrContentField;
+
+        fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+        where
+            D: Deserializer<'de>,
+        {
+            deserializer.deserialize_str(self)
+        }
+    }
+
+    impl<'de> Visitor<'de> for TagOrContentFieldVisitor {
+        type Value = TagOrContentField;
+
+        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+            write!(formatter, "{:?} or {:?}", self.tag, self.content)
+        }
+
+        fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
+        where
+            E: de::Error,
+        {
+            if field == self.tag {
+                Ok(TagOrContentField::Tag)
+            } else if field == self.content {
+                Ok(TagOrContentField::Content)
+            } else {
+                Err(de::Error::invalid_value(Unexpected::Str(field), &self))
+            }
+        }
+    }
+
+    /// Used by generated code to deserialize an adjacently tagged enum when
+    /// ignoring unrelated fields is allowed.
+    ///
+    /// Not public API.
+    pub enum TagContentOtherField {
+        Tag,
+        Content,
+        Other,
+    }
+
+    /// Not public API.
+    pub struct TagContentOtherFieldVisitor {
+        pub tag: &'static str,
+        pub content: &'static str,
+    }
+
+    impl<'de> DeserializeSeed<'de> for TagContentOtherFieldVisitor {
+        type Value = TagContentOtherField;
+
+        fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+        where
+            D: Deserializer<'de>,
+        {
+            deserializer.deserialize_str(self)
+        }
+    }
+
+    impl<'de> Visitor<'de> for TagContentOtherFieldVisitor {
+        type Value = TagContentOtherField;
+
+        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+            write!(
+                formatter,
+                "{:?}, {:?}, or other ignored fields",
+                self.tag, self.content
+            )
+        }
+
+        fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
+        where
+            E: de::Error,
+        {
+            if field == self.tag {
+                Ok(TagContentOtherField::Tag)
+            } else if field == self.content {
+                Ok(TagContentOtherField::Content)
+            } else {
+                Ok(TagContentOtherField::Other)
+            }
+        }
+    }
+
+    /// Not public API
+    pub struct ContentDeserializer<'de, E> {
+        content: Content<'de>,
+        err: PhantomData<E>,
+    }
+
+    impl<'de, E> ContentDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        #[cold]
+        fn invalid_type(self, exp: &Expected) -> E {
+            de::Error::invalid_type(self.content.unexpected(), exp)
+        }
+
+        fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::U8(v) => visitor.visit_u8(v),
+                Content::U16(v) => visitor.visit_u16(v),
+                Content::U32(v) => visitor.visit_u32(v),
+                Content::U64(v) => visitor.visit_u64(v),
+                Content::I8(v) => visitor.visit_i8(v),
+                Content::I16(v) => visitor.visit_i16(v),
+                Content::I32(v) => visitor.visit_i32(v),
+                Content::I64(v) => visitor.visit_i64(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+    }
+
+    fn visit_content_seq<'de, V, E>(content: Vec<Content<'de>>, visitor: V) -> Result<V::Value, E>
+    where
+        V: Visitor<'de>,
+        E: de::Error,
+    {
+        let seq = content.into_iter().map(ContentDeserializer::new);
+        let mut seq_visitor = de::value::SeqDeserializer::new(seq);
+        let value = try!(visitor.visit_seq(&mut seq_visitor));
+        try!(seq_visitor.end());
+        Ok(value)
+    }
+
+    fn 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,
+    {
+        let map = content
+            .into_iter()
+            .map(|(k, v)| (ContentDeserializer::new(k), ContentDeserializer::new(v)));
+        let mut map_visitor = de::value::MapDeserializer::new(map);
+        let value = try!(visitor.visit_map(&mut map_visitor));
+        try!(map_visitor.end());
+        Ok(value)
+    }
+
+    /// Used when deserializing an internally tagged enum because the content
+    /// will be used exactly once.
+    impl<'de, E> Deserializer<'de> for ContentDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::Bool(v) => visitor.visit_bool(v),
+                Content::U8(v) => visitor.visit_u8(v),
+                Content::U16(v) => visitor.visit_u16(v),
+                Content::U32(v) => visitor.visit_u32(v),
+                Content::U64(v) => visitor.visit_u64(v),
+                Content::I8(v) => visitor.visit_i8(v),
+                Content::I16(v) => visitor.visit_i16(v),
+                Content::I32(v) => visitor.visit_i32(v),
+                Content::I64(v) => visitor.visit_i64(v),
+                Content::F32(v) => visitor.visit_f32(v),
+                Content::F64(v) => visitor.visit_f64(v),
+                Content::Char(v) => visitor.visit_char(v),
+                Content::String(v) => visitor.visit_string(v),
+                Content::Str(v) => visitor.visit_borrowed_str(v),
+                Content::ByteBuf(v) => visitor.visit_byte_buf(v),
+                Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
+                Content::Unit => visitor.visit_unit(),
+                Content::None => visitor.visit_none(),
+                Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
+                Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)),
+                Content::Seq(v) => visit_content_seq(v, visitor),
+                Content::Map(v) => visit_content_map(v, visitor),
+            }
+        }
+
+        fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::Bool(v) => visitor.visit_bool(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::F32(v) => visitor.visit_f32(v),
+                Content::F64(v) => visitor.visit_f64(v),
+                Content::U64(v) => visitor.visit_u64(v),
+                Content::I64(v) => visitor.visit_i64(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::F64(v) => visitor.visit_f64(v),
+                Content::U64(v) => visitor.visit_u64(v),
+                Content::I64(v) => visitor.visit_i64(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::Char(v) => visitor.visit_char(v),
+                Content::String(v) => visitor.visit_string(v),
+                Content::Str(v) => visitor.visit_borrowed_str(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_string(visitor)
+        }
+
+        fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::String(v) => visitor.visit_string(v),
+                Content::Str(v) => visitor.visit_borrowed_str(v),
+                Content::ByteBuf(v) => visitor.visit_byte_buf(v),
+                Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_byte_buf(visitor)
+        }
+
+        fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::String(v) => visitor.visit_string(v),
+                Content::Str(v) => visitor.visit_borrowed_str(v),
+                Content::ByteBuf(v) => visitor.visit_byte_buf(v),
+                Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
+                Content::Seq(v) => visit_content_seq(v, visitor),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::None => visitor.visit_none(),
+                Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
+                Content::Unit => visitor.visit_unit(),
+                _ => visitor.visit_some(self),
+            }
+        }
+
+        fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::Unit => visitor.visit_unit(),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_unit_struct<V>(
+            self,
+            _name: &'static str,
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                // As a special case, allow deserializing untagged newtype
+                // variant containing unit struct.
+                //
+                //     #[derive(Deserialize)]
+                //     struct Info;
+                //
+                //     #[derive(Deserialize)]
+                //     #[serde(tag = "topic")]
+                //     enum Message {
+                //         Info(Info),
+                //     }
+                //
+                // We want {"topic":"Info"} to deserialize even though
+                // ordinarily unit structs do not deserialize from empty map.
+                Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
+                _ => self.deserialize_any(visitor),
+            }
+        }
+
+        fn deserialize_newtype_struct<V>(
+            self,
+            _name: &str,
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)),
+                _ => visitor.visit_newtype_struct(self),
+            }
+        }
+
+        fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::Seq(v) => visit_content_seq(v, visitor),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_seq(visitor)
+        }
+
+        fn deserialize_tuple_struct<V>(
+            self,
+            _name: &'static str,
+            _len: usize,
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_seq(visitor)
+        }
+
+        fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::Map(v) => visit_content_map(v, visitor),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_struct<V>(
+            self,
+            _name: &'static str,
+            _fields: &'static [&'static str],
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::Seq(v) => visit_content_seq(v, visitor),
+                Content::Map(v) => visit_content_map(v, visitor),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_enum<V>(
+            self,
+            _name: &str,
+            _variants: &'static [&'static str],
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            let (variant, value) = match self.content {
+                Content::Map(value) => {
+                    let mut iter = value.into_iter();
+                    let (variant, value) = match iter.next() {
+                        Some(v) => v,
+                        None => {
+                            return Err(de::Error::invalid_value(
+                                de::Unexpected::Map,
+                                &"map with a single key",
+                            ));
+                        }
+                    };
+                    // enums are encoded in json as maps with a single key:value pair
+                    if iter.next().is_some() {
+                        return Err(de::Error::invalid_value(
+                            de::Unexpected::Map,
+                            &"map with a single key",
+                        ));
+                    }
+                    (variant, Some(value))
+                }
+                s @ Content::String(_) | s @ Content::Str(_) => (s, None),
+                other => {
+                    return Err(de::Error::invalid_type(
+                        other.unexpected(),
+                        &"string or map",
+                    ));
+                }
+            };
+
+            visitor.visit_enum(EnumDeserializer::new(variant, value))
+        }
+
+        fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match self.content {
+                Content::String(v) => visitor.visit_string(v),
+                Content::Str(v) => visitor.visit_borrowed_str(v),
+                Content::ByteBuf(v) => visitor.visit_byte_buf(v),
+                Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
+                Content::U8(v) => visitor.visit_u8(v),
+                Content::U64(v) => visitor.visit_u64(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            drop(self);
+            visitor.visit_unit()
+        }
+    }
+
+    impl<'de, E> ContentDeserializer<'de, E> {
+        /// private API, don't use
+        pub fn new(content: Content<'de>) -> Self {
+            ContentDeserializer {
+                content: content,
+                err: PhantomData,
+            }
+        }
+    }
+
+    pub struct EnumDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        variant: Content<'de>,
+        value: Option<Content<'de>>,
+        err: PhantomData<E>,
+    }
+
+    impl<'de, E> EnumDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        pub fn new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E> {
+            EnumDeserializer {
+                variant: variant,
+                value: value,
+                err: PhantomData,
+            }
+        }
+    }
+
+    impl<'de, E> de::EnumAccess<'de> for EnumDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+        type Variant = VariantDeserializer<'de, Self::Error>;
+
+        fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), E>
+        where
+            V: de::DeserializeSeed<'de>,
+        {
+            let visitor = VariantDeserializer {
+                value: self.value,
+                err: PhantomData,
+            };
+            seed.deserialize(ContentDeserializer::new(self.variant))
+                .map(|v| (v, visitor))
+        }
+    }
+
+    pub struct VariantDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        value: Option<Content<'de>>,
+        err: PhantomData<E>,
+    }
+
+    impl<'de, E> de::VariantAccess<'de> for VariantDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        fn unit_variant(self) -> Result<(), E> {
+            match self.value {
+                Some(value) => de::Deserialize::deserialize(ContentDeserializer::new(value)),
+                None => Ok(()),
+            }
+        }
+
+        fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E>
+        where
+            T: de::DeserializeSeed<'de>,
+        {
+            match self.value {
+                Some(value) => seed.deserialize(ContentDeserializer::new(value)),
+                None => Err(de::Error::invalid_type(
+                    de::Unexpected::UnitVariant,
+                    &"newtype variant",
+                )),
+            }
+        }
+
+        fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: de::Visitor<'de>,
+        {
+            match self.value {
+                Some(Content::Seq(v)) => {
+                    de::Deserializer::deserialize_any(SeqDeserializer::new(v), visitor)
+                }
+                Some(other) => Err(de::Error::invalid_type(
+                    other.unexpected(),
+                    &"tuple variant",
+                )),
+                None => Err(de::Error::invalid_type(
+                    de::Unexpected::UnitVariant,
+                    &"tuple variant",
+                )),
+            }
+        }
+
+        fn struct_variant<V>(
+            self,
+            _fields: &'static [&'static str],
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: de::Visitor<'de>,
+        {
+            match self.value {
+                Some(Content::Map(v)) => {
+                    de::Deserializer::deserialize_any(MapDeserializer::new(v), visitor)
+                }
+                Some(Content::Seq(v)) => {
+                    de::Deserializer::deserialize_any(SeqDeserializer::new(v), visitor)
+                }
+                Some(other) => Err(de::Error::invalid_type(
+                    other.unexpected(),
+                    &"struct variant",
+                )),
+                None => Err(de::Error::invalid_type(
+                    de::Unexpected::UnitVariant,
+                    &"struct variant",
+                )),
+            }
+        }
+    }
+
+    struct SeqDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        iter: <Vec<Content<'de>> as IntoIterator>::IntoIter,
+        err: PhantomData<E>,
+    }
+
+    impl<'de, E> SeqDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        fn new(vec: Vec<Content<'de>>) -> Self {
+            SeqDeserializer {
+                iter: vec.into_iter(),
+                err: PhantomData,
+            }
+        }
+    }
+
+    impl<'de, E> de::Deserializer<'de> for SeqDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        #[inline]
+        fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: de::Visitor<'de>,
+        {
+            let len = self.iter.len();
+            if len == 0 {
+                visitor.visit_unit()
+            } else {
+                let ret = try!(visitor.visit_seq(&mut self));
+                let remaining = self.iter.len();
+                if remaining == 0 {
+                    Ok(ret)
+                } else {
+                    Err(de::Error::invalid_length(len, &"fewer elements in array"))
+                }
+            }
+        }
+
+        forward_to_deserialize_any! {
+            bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
+            bytes byte_buf option unit unit_struct newtype_struct seq tuple
+            tuple_struct map struct enum identifier ignored_any
+        }
+    }
+
+    impl<'de, E> de::SeqAccess<'de> for SeqDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
+        where
+            T: de::DeserializeSeed<'de>,
+        {
+            match self.iter.next() {
+                Some(value) => seed.deserialize(ContentDeserializer::new(value)).map(Some),
+                None => Ok(None),
+            }
+        }
+
+        fn size_hint(&self) -> Option<usize> {
+            size_hint::from_bounds(&self.iter)
+        }
+    }
+
+    struct MapDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        iter: <Vec<(Content<'de>, Content<'de>)> as IntoIterator>::IntoIter,
+        value: Option<Content<'de>>,
+        err: PhantomData<E>,
+    }
+
+    impl<'de, E> MapDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        fn new(map: Vec<(Content<'de>, Content<'de>)>) -> Self {
+            MapDeserializer {
+                iter: map.into_iter(),
+                value: None,
+                err: PhantomData,
+            }
+        }
+    }
+
+    impl<'de, E> de::MapAccess<'de> for MapDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
+        where
+            T: de::DeserializeSeed<'de>,
+        {
+            match self.iter.next() {
+                Some((key, value)) => {
+                    self.value = Some(value);
+                    seed.deserialize(ContentDeserializer::new(key)).map(Some)
+                }
+                None => Ok(None),
+            }
+        }
+
+        fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
+        where
+            T: de::DeserializeSeed<'de>,
+        {
+            match self.value.take() {
+                Some(value) => seed.deserialize(ContentDeserializer::new(value)),
+                None => Err(de::Error::custom("value is missing")),
+            }
+        }
+
+        fn size_hint(&self) -> Option<usize> {
+            size_hint::from_bounds(&self.iter)
+        }
+    }
+
+    impl<'de, E> de::Deserializer<'de> for MapDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        #[inline]
+        fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: de::Visitor<'de>,
+        {
+            visitor.visit_map(self)
+        }
+
+        forward_to_deserialize_any! {
+            bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
+            bytes byte_buf option unit unit_struct newtype_struct seq tuple
+            tuple_struct map struct enum identifier ignored_any
+        }
+    }
+
+    /// Not public API.
+    pub struct ContentRefDeserializer<'a, 'de: 'a, E> {
+        content: &'a Content<'de>,
+        err: PhantomData<E>,
+    }
+
+    impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        #[cold]
+        fn invalid_type(self, exp: &Expected) -> E {
+            de::Error::invalid_type(self.content.unexpected(), exp)
+        }
+
+        fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::U8(v) => visitor.visit_u8(v),
+                Content::U16(v) => visitor.visit_u16(v),
+                Content::U32(v) => visitor.visit_u32(v),
+                Content::U64(v) => visitor.visit_u64(v),
+                Content::I8(v) => visitor.visit_i8(v),
+                Content::I16(v) => visitor.visit_i16(v),
+                Content::I32(v) => visitor.visit_i32(v),
+                Content::I64(v) => visitor.visit_i64(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+    }
+
+    fn 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,
+    {
+        let seq = content.iter().map(ContentRefDeserializer::new);
+        let mut seq_visitor = de::value::SeqDeserializer::new(seq);
+        let value = try!(visitor.visit_seq(&mut seq_visitor));
+        try!(seq_visitor.end());
+        Ok(value)
+    }
+
+    fn 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,
+    {
+        let map = content.iter().map(|&(ref k, ref v)| {
+            (
+                ContentRefDeserializer::new(k),
+                ContentRefDeserializer::new(v),
+            )
+        });
+        let mut map_visitor = de::value::MapDeserializer::new(map);
+        let value = try!(visitor.visit_map(&mut map_visitor));
+        try!(map_visitor.end());
+        Ok(value)
+    }
+
+    /// Used when deserializing an untagged enum because the content may need
+    /// to be used more than once.
+    impl<'de, 'a, E> Deserializer<'de> for ContentRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, E>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::Bool(v) => visitor.visit_bool(v),
+                Content::U8(v) => visitor.visit_u8(v),
+                Content::U16(v) => visitor.visit_u16(v),
+                Content::U32(v) => visitor.visit_u32(v),
+                Content::U64(v) => visitor.visit_u64(v),
+                Content::I8(v) => visitor.visit_i8(v),
+                Content::I16(v) => visitor.visit_i16(v),
+                Content::I32(v) => visitor.visit_i32(v),
+                Content::I64(v) => visitor.visit_i64(v),
+                Content::F32(v) => visitor.visit_f32(v),
+                Content::F64(v) => visitor.visit_f64(v),
+                Content::Char(v) => visitor.visit_char(v),
+                Content::String(ref v) => visitor.visit_str(v),
+                Content::Str(v) => visitor.visit_borrowed_str(v),
+                Content::ByteBuf(ref v) => visitor.visit_bytes(v),
+                Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
+                Content::Unit => visitor.visit_unit(),
+                Content::None => visitor.visit_none(),
+                Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
+                Content::Newtype(ref v) => {
+                    visitor.visit_newtype_struct(ContentRefDeserializer::new(v))
+                }
+                Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
+                Content::Map(ref v) => visit_content_map_ref(v, visitor),
+            }
+        }
+
+        fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::Bool(v) => visitor.visit_bool(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_integer(visitor)
+        }
+
+        fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::F32(v) => visitor.visit_f32(v),
+                Content::F64(v) => visitor.visit_f64(v),
+                Content::U64(v) => visitor.visit_u64(v),
+                Content::I64(v) => visitor.visit_i64(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::F64(v) => visitor.visit_f64(v),
+                Content::U64(v) => visitor.visit_u64(v),
+                Content::I64(v) => visitor.visit_i64(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::Char(v) => visitor.visit_char(v),
+                Content::String(ref v) => visitor.visit_str(v),
+                Content::Str(v) => visitor.visit_borrowed_str(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::String(ref v) => visitor.visit_str(v),
+                Content::Str(v) => visitor.visit_borrowed_str(v),
+                Content::ByteBuf(ref v) => visitor.visit_bytes(v),
+                Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_str(visitor)
+        }
+
+        fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::String(ref v) => visitor.visit_str(v),
+                Content::Str(v) => visitor.visit_borrowed_str(v),
+                Content::ByteBuf(ref v) => visitor.visit_bytes(v),
+                Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
+                Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_bytes(visitor)
+        }
+
+        fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::None => visitor.visit_none(),
+                Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
+                Content::Unit => visitor.visit_unit(),
+                _ => visitor.visit_some(self),
+            }
+        }
+
+        fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::Unit => visitor.visit_unit(),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_unit_struct<V>(
+            self,
+            _name: &'static str,
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_unit(visitor)
+        }
+
+        fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, E>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::Newtype(ref v) => {
+                    visitor.visit_newtype_struct(ContentRefDeserializer::new(v))
+                }
+                _ => visitor.visit_newtype_struct(self),
+            }
+        }
+
+        fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_seq(visitor)
+        }
+
+        fn deserialize_tuple_struct<V>(
+            self,
+            _name: &'static str,
+            _len: usize,
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            self.deserialize_seq(visitor)
+        }
+
+        fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::Map(ref v) => visit_content_map_ref(v, visitor),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_struct<V>(
+            self,
+            _name: &'static str,
+            _fields: &'static [&'static str],
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
+                Content::Map(ref v) => visit_content_map_ref(v, visitor),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_enum<V>(
+            self,
+            _name: &str,
+            _variants: &'static [&'static str],
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            let (variant, value) = match *self.content {
+                Content::Map(ref value) => {
+                    let mut iter = value.iter();
+                    let &(ref variant, ref value) = match iter.next() {
+                        Some(v) => v,
+                        None => {
+                            return Err(de::Error::invalid_value(
+                                de::Unexpected::Map,
+                                &"map with a single key",
+                            ));
+                        }
+                    };
+                    // enums are encoded in json as maps with a single key:value pair
+                    if iter.next().is_some() {
+                        return Err(de::Error::invalid_value(
+                            de::Unexpected::Map,
+                            &"map with a single key",
+                        ));
+                    }
+                    (variant, Some(value))
+                }
+                ref s @ Content::String(_) | ref s @ Content::Str(_) => (s, None),
+                ref other => {
+                    return Err(de::Error::invalid_type(
+                        other.unexpected(),
+                        &"string or map",
+                    ));
+                }
+            };
+
+            visitor.visit_enum(EnumRefDeserializer {
+                variant: variant,
+                value: value,
+                err: PhantomData,
+            })
+        }
+
+        fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            match *self.content {
+                Content::String(ref v) => visitor.visit_str(v),
+                Content::Str(v) => visitor.visit_borrowed_str(v),
+                Content::ByteBuf(ref v) => visitor.visit_bytes(v),
+                Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
+                Content::U8(v) => visitor.visit_u8(v),
+                Content::U64(v) => visitor.visit_u64(v),
+                _ => Err(self.invalid_type(&visitor)),
+            }
+        }
+
+        fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            visitor.visit_unit()
+        }
+    }
+
+    impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> {
+        /// private API, don't use
+        pub fn new(content: &'a Content<'de>) -> Self {
+            ContentRefDeserializer {
+                content: content,
+                err: PhantomData,
+            }
+        }
+    }
+
+    struct EnumRefDeserializer<'a, 'de: 'a, E>
+    where
+        E: de::Error,
+    {
+        variant: &'a Content<'de>,
+        value: Option<&'a Content<'de>>,
+        err: PhantomData<E>,
+    }
+
+    impl<'de, 'a, E> de::EnumAccess<'de> for EnumRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+        type Variant = VariantRefDeserializer<'a, 'de, Self::Error>;
+
+        fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
+        where
+            V: de::DeserializeSeed<'de>,
+        {
+            let visitor = VariantRefDeserializer {
+                value: self.value,
+                err: PhantomData,
+            };
+            seed.deserialize(ContentRefDeserializer::new(self.variant))
+                .map(|v| (v, visitor))
+        }
+    }
+
+    struct VariantRefDeserializer<'a, 'de: 'a, E>
+    where
+        E: de::Error,
+    {
+        value: Option<&'a Content<'de>>,
+        err: PhantomData<E>,
+    }
+
+    impl<'de, 'a, E> de::VariantAccess<'de> for VariantRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        fn unit_variant(self) -> Result<(), E> {
+            match self.value {
+                Some(value) => de::Deserialize::deserialize(ContentRefDeserializer::new(value)),
+                None => Ok(()),
+            }
+        }
+
+        fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E>
+        where
+            T: de::DeserializeSeed<'de>,
+        {
+            match self.value {
+                Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
+                None => Err(de::Error::invalid_type(
+                    de::Unexpected::UnitVariant,
+                    &"newtype variant",
+                )),
+            }
+        }
+
+        fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: de::Visitor<'de>,
+        {
+            match self.value {
+                Some(&Content::Seq(ref v)) => {
+                    de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor)
+                }
+                Some(other) => Err(de::Error::invalid_type(
+                    other.unexpected(),
+                    &"tuple variant",
+                )),
+                None => Err(de::Error::invalid_type(
+                    de::Unexpected::UnitVariant,
+                    &"tuple variant",
+                )),
+            }
+        }
+
+        fn struct_variant<V>(
+            self,
+            _fields: &'static [&'static str],
+            visitor: V,
+        ) -> Result<V::Value, Self::Error>
+        where
+            V: de::Visitor<'de>,
+        {
+            match self.value {
+                Some(&Content::Map(ref v)) => {
+                    de::Deserializer::deserialize_any(MapRefDeserializer::new(v), visitor)
+                }
+                Some(&Content::Seq(ref v)) => {
+                    de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor)
+                }
+                Some(other) => Err(de::Error::invalid_type(
+                    other.unexpected(),
+                    &"struct variant",
+                )),
+                None => Err(de::Error::invalid_type(
+                    de::Unexpected::UnitVariant,
+                    &"struct variant",
+                )),
+            }
+        }
+    }
+
+    struct SeqRefDeserializer<'a, 'de: 'a, E>
+    where
+        E: de::Error,
+    {
+        iter: <&'a [Content<'de>] as IntoIterator>::IntoIter,
+        err: PhantomData<E>,
+    }
+
+    impl<'a, 'de, E> SeqRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        fn new(slice: &'a [Content<'de>]) -> Self {
+            SeqRefDeserializer {
+                iter: slice.iter(),
+                err: PhantomData,
+            }
+        }
+    }
+
+    impl<'de, 'a, E> de::Deserializer<'de> for SeqRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        #[inline]
+        fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: de::Visitor<'de>,
+        {
+            let len = self.iter.len();
+            if len == 0 {
+                visitor.visit_unit()
+            } else {
+                let ret = try!(visitor.visit_seq(&mut self));
+                let remaining = self.iter.len();
+                if remaining == 0 {
+                    Ok(ret)
+                } else {
+                    Err(de::Error::invalid_length(len, &"fewer elements in array"))
+                }
+            }
+        }
+
+        forward_to_deserialize_any! {
+            bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
+            bytes byte_buf option unit unit_struct newtype_struct seq tuple
+            tuple_struct map struct enum identifier ignored_any
+        }
+    }
+
+    impl<'de, 'a, E> de::SeqAccess<'de> for SeqRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
+        where
+            T: de::DeserializeSeed<'de>,
+        {
+            match self.iter.next() {
+                Some(value) => seed
+                    .deserialize(ContentRefDeserializer::new(value))
+                    .map(Some),
+                None => Ok(None),
+            }
+        }
+
+        fn size_hint(&self) -> Option<usize> {
+            size_hint::from_bounds(&self.iter)
+        }
+    }
+
+    struct MapRefDeserializer<'a, 'de: 'a, E>
+    where
+        E: de::Error,
+    {
+        iter: <&'a [(Content<'de>, Content<'de>)] as IntoIterator>::IntoIter,
+        value: Option<&'a Content<'de>>,
+        err: PhantomData<E>,
+    }
+
+    impl<'a, 'de, E> MapRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        fn new(map: &'a [(Content<'de>, Content<'de>)]) -> Self {
+            MapRefDeserializer {
+                iter: map.iter(),
+                value: None,
+                err: PhantomData,
+            }
+        }
+    }
+
+    impl<'de, 'a, E> de::MapAccess<'de> for MapRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
+        where
+            T: de::DeserializeSeed<'de>,
+        {
+            match self.iter.next() {
+                Some(&(ref key, ref value)) => {
+                    self.value = Some(value);
+                    seed.deserialize(ContentRefDeserializer::new(key)).map(Some)
+                }
+                None => Ok(None),
+            }
+        }
+
+        fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
+        where
+            T: de::DeserializeSeed<'de>,
+        {
+            match self.value.take() {
+                Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
+                None => Err(de::Error::custom("value is missing")),
+            }
+        }
+
+        fn size_hint(&self) -> Option<usize> {
+            size_hint::from_bounds(&self.iter)
+        }
+    }
+
+    impl<'de, 'a, E> de::Deserializer<'de> for MapRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        type Error = E;
+
+        #[inline]
+        fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: de::Visitor<'de>,
+        {
+            visitor.visit_map(self)
+        }
+
+        forward_to_deserialize_any! {
+            bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
+            bytes byte_buf option unit unit_struct newtype_struct seq tuple
+            tuple_struct map struct enum identifier ignored_any
+        }
+    }
+
+    impl<'de, E> de::IntoDeserializer<'de, E> for ContentDeserializer<'de, E>
+    where
+        E: de::Error,
+    {
+        type Deserializer = Self;
+
+        fn into_deserializer(self) -> Self {
+            self
+        }
+    }
+
+    impl<'de, 'a, E> de::IntoDeserializer<'de, E> for ContentRefDeserializer<'a, 'de, E>
+    where
+        E: de::Error,
+    {
+        type Deserializer = Self;
+
+        fn into_deserializer(self) -> Self {
+            self
+        }
+    }
+
+    /// Visitor for deserializing an internally tagged unit variant.
+    ///
+    /// Not public API.
+    pub struct InternallyTaggedUnitVisitor<'a> {
+        type_name: &'a str,
+        variant_name: &'a str,
+    }
+
+    impl<'a> InternallyTaggedUnitVisitor<'a> {
+        /// Not public API.
+        pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
+            InternallyTaggedUnitVisitor {
+                type_name: type_name,
+                variant_name: variant_name,
+            }
+        }
+    }
+
+    impl<'de, 'a> Visitor<'de> for InternallyTaggedUnitVisitor<'a> {
+        type Value = ();
+
+        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+            write!(
+                formatter,
+                "unit variant {}::{}",
+                self.type_name, self.variant_name
+            )
+        }
+
+        fn visit_seq<S>(self, _: S) -> Result<(), S::Error>
+        where
+            S: SeqAccess<'de>,
+        {
+            Ok(())
+        }
+
+        fn visit_map<M>(self, mut access: M) -> Result<(), M::Error>
+        where
+            M: MapAccess<'de>,
+        {
+            while try!(access.next_entry::<IgnoredAny, IgnoredAny>()).is_some() {}
+            Ok(())
+        }
+    }
+
+    /// Visitor for deserializing an untagged unit variant.
+    ///
+    /// Not public API.
+    pub struct UntaggedUnitVisitor<'a> {
+        type_name: &'a str,
+        variant_name: &'a str,
+    }
+
+    impl<'a> UntaggedUnitVisitor<'a> {
+        /// Not public API.
+        pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
+            UntaggedUnitVisitor {
+                type_name: type_name,
+                variant_name: variant_name,
+            }
+        }
+    }
+
+    impl<'de, 'a> Visitor<'de> for UntaggedUnitVisitor<'a> {
+        type Value = ();
+
+        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+            write!(
+                formatter,
+                "unit variant {}::{}",
+                self.type_name, self.variant_name
+            )
+        }
+
+        fn visit_unit<E>(self) -> Result<(), E>
+        where
+            E: de::Error,
+        {
+            Ok(())
+        }
+
+        fn visit_none<E>(self) -> Result<(), E>
+        where
+            E: de::Error,
+        {
+            Ok(())
+        }
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Like `IntoDeserializer` but also implemented for `&[u8]`. This is used for
+// the newtype fallthrough case of `field_identifier`.
+//
+//    #[derive(Deserialize)]
+//    #[serde(field_identifier)]
+//    enum F {
+//        A,
+//        B,
+//        Other(String), // deserialized using IdentifierDeserializer
+//    }
+pub trait IdentifierDeserializer<'de, E: Error> {
+    type Deserializer: Deserializer<'de, Error = E>;
+
+    fn from(self) -> Self::Deserializer;
+}
+
+impl<'de, E> IdentifierDeserializer<'de, E> for u32
+where
+    E: Error,
+{
+    type Deserializer = <u32 as IntoDeserializer<'de, E>>::Deserializer;
+
+    fn from(self) -> Self::Deserializer {
+        self.into_deserializer()
+    }
+}
+
+pub struct StrDeserializer<'a, E> {
+    value: &'a str,
+    marker: PhantomData<E>,
+}
+
+impl<'a, E> IdentifierDeserializer<'a, E> for &'a str
+where
+    E: Error,
+{
+    type Deserializer = StrDeserializer<'a, E>;
+
+    fn from(self) -> Self::Deserializer {
+        StrDeserializer {
+            value: self,
+            marker: PhantomData,
+        }
+    }
+}
+
+impl<'de, 'a, E> Deserializer<'de> for StrDeserializer<'a, E>
+where
+    E: Error,
+{
+    type Error = E;
+
+    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+    where
+        V: Visitor<'de>,
+    {
+        visitor.visit_str(self.value)
+    }
+
+    forward_to_deserialize_any! {
+        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
+        bytes byte_buf option unit unit_struct newtype_struct seq tuple
+        tuple_struct map struct enum identifier ignored_any
+    }
+}
+
+pub struct BytesDeserializer<'a, E> {
+    value: &'a [u8],
+    marker: PhantomData<E>,
+}
+
+impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8]
+where
+    E: Error,
+{
+    type Deserializer = BytesDeserializer<'a, E>;
+
+    fn from(self) -> Self::Deserializer {
+        BytesDeserializer {
+            value: self,
+            marker: PhantomData,
+        }
+    }
+}
+
+impl<'de, 'a, E> Deserializer<'de> for BytesDeserializer<'a, E>
+where
+    E: Error,
+{
+    type Error = E;
+
+    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+    where
+        V: Visitor<'de>,
+    {
+        visitor.visit_bytes(self.value)
+    }
+
+    forward_to_deserialize_any! {
+        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
+        bytes byte_buf option unit unit_struct newtype_struct seq tuple
+        tuple_struct map struct enum identifier ignored_any
+    }
+}
+
+/// A DeserializeSeed helper for implementing deserialize_in_place Visitors.
+///
+/// Wraps a mutable reference and calls deserialize_in_place on it.
+pub struct InPlaceSeed<'a, T: 'a>(pub &'a mut T);
+
+impl<'a, 'de, T> DeserializeSeed<'de> for InPlaceSeed<'a, T>
+where
+    T: Deserialize<'de>,
+{
+    type Value = ();
+    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+    where
+        D: Deserializer<'de>,
+    {
+        T::deserialize_in_place(deserializer, self.0)
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub struct FlatMapDeserializer<'a, 'de: 'a, E>(
+    pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>,
+    pub PhantomData<E>,
+);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E>
+where
+    E: Error,
+{
+    fn deserialize_other<V>() -> Result<V, E> {
+        Err(Error::custom("can only flatten structs and maps"))
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+macro_rules! forward_to_deserialize_other {
+    ($($func:ident ( $($arg:ty),* ))*) => {
+        $(
+            fn $func<V>(self, $(_: $arg,)* _visitor: V) -> Result<V::Value, Self::Error>
+            where
+                V: Visitor<'de>,
+            {
+                Self::deserialize_other()
+            }
+        )*
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
+where
+    E: Error,
+{
+    type Error = E;
+
+    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+    where
+        V: Visitor<'de>,
+    {
+        visitor.visit_map(FlatInternallyTaggedAccess {
+            iter: self.0.iter_mut(),
+            pending: None,
+            _marker: PhantomData,
+        })
+    }
+
+    fn deserialize_enum<V>(
+        self,
+        name: &'static str,
+        variants: &'static [&'static str],
+        visitor: V,
+    ) -> Result<V::Value, Self::Error>
+    where
+        V: Visitor<'de>,
+    {
+        for item in self.0.iter_mut() {
+            // items in the vector are nulled out when used.  So we can only use
+            // an item if it's still filled in and if the field is one we care
+            // about.
+            let use_item = match *item {
+                None => false,
+                Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)),
+            };
+
+            if use_item {
+                let (key, value) = item.take().unwrap();
+                return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
+            }
+        }
+
+        Err(Error::custom(format_args!(
+            "no variant of enum {} found in flattened data",
+            name
+        )))
+    }
+
+    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+    where
+        V: Visitor<'de>,
+    {
+        visitor.visit_map(FlatMapAccess::new(self.0.iter()))
+    }
+
+    fn deserialize_struct<V>(
+        self,
+        _: &'static str,
+        fields: &'static [&'static str],
+        visitor: V,
+    ) -> Result<V::Value, Self::Error>
+    where
+        V: Visitor<'de>,
+    {
+        visitor.visit_map(FlatStructAccess::new(self.0.iter_mut(), fields))
+    }
+
+    fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
+    where
+        V: Visitor<'de>,
+    {
+        visitor.visit_newtype_struct(self)
+    }
+
+    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+    where
+        V: Visitor<'de>,
+    {
+        match visitor.__private_visit_untagged_option(self) {
+            Ok(value) => Ok(value),
+            Err(()) => Self::deserialize_other(),
+        }
+    }
+
+    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+    where
+        V: Visitor<'de>,
+    {
+        visitor.visit_unit()
+    }
+
+    forward_to_deserialize_other! {
+        deserialize_bool()
+        deserialize_i8()
+        deserialize_i16()
+        deserialize_i32()
+        deserialize_i64()
+        deserialize_u8()
+        deserialize_u16()
+        deserialize_u32()
+        deserialize_u64()
+        deserialize_f32()
+        deserialize_f64()
+        deserialize_char()
+        deserialize_str()
+        deserialize_string()
+        deserialize_bytes()
+        deserialize_byte_buf()
+        deserialize_unit_struct(&'static str)
+        deserialize_seq()
+        deserialize_tuple(usize)
+        deserialize_tuple_struct(&'static str, usize)
+        deserialize_identifier()
+        deserialize_ignored_any()
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub struct FlatMapAccess<'a, 'de: 'a, E> {
+    iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
+    pending_content: Option<&'a Content<'de>>,
+    _marker: PhantomData<E>,
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> {
+    fn new(
+        iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
+    ) -> FlatMapAccess<'a, 'de, E> {
+        FlatMapAccess {
+            iter: iter,
+            pending_content: None,
+            _marker: PhantomData,
+        }
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E>
+where
+    E: Error,
+{
+    type Error = E;
+
+    fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
+    where
+        T: DeserializeSeed<'de>,
+    {
+        while let Some(item) = self.iter.next() {
+            // Items in the vector are nulled out when used by a struct.
+            if let Some((ref key, ref content)) = *item {
+                self.pending_content = Some(content);
+                return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
+            }
+        }
+        Ok(None)
+    }
+
+    fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
+    where
+        T: DeserializeSeed<'de>,
+    {
+        match self.pending_content.take() {
+            Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
+            None => Err(Error::custom("value is missing")),
+        }
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub struct FlatStructAccess<'a, 'de: 'a, E> {
+    iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
+    pending_content: Option<Content<'de>>,
+    fields: &'static [&'static str],
+    _marker: PhantomData<E>,
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, 'de, E> FlatStructAccess<'a, 'de, E> {
+    fn new(
+        iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
+        fields: &'static [&'static str],
+    ) -> FlatStructAccess<'a, 'de, E> {
+        FlatStructAccess {
+            iter: iter,
+            pending_content: None,
+            fields: fields,
+            _marker: PhantomData,
+        }
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, 'de, E> MapAccess<'de> for FlatStructAccess<'a, 'de, E>
+where
+    E: Error,
+{
+    type Error = E;
+
+    fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
+    where
+        T: DeserializeSeed<'de>,
+    {
+        while let Some(item) = self.iter.next() {
+            // items in the vector are nulled out when used.  So we can only use
+            // an item if it's still filled in and if the field is one we care
+            // about.  In case we do not know which fields we want, we take them all.
+            let use_item = match *item {
+                None => false,
+                Some((ref c, _)) => c.as_str().map_or(false, |key| self.fields.contains(&key)),
+            };
+
+            if use_item {
+                let (key, content) = item.take().unwrap();
+                self.pending_content = Some(content);
+                return seed.deserialize(ContentDeserializer::new(key)).map(Some);
+            }
+        }
+        Ok(None)
+    }
+
+    fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
+    where
+        T: DeserializeSeed<'de>,
+    {
+        match self.pending_content.take() {
+            Some(value) => seed.deserialize(ContentDeserializer::new(value)),
+            None => Err(Error::custom("value is missing")),
+        }
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub struct FlatInternallyTaggedAccess<'a, 'de: 'a, E> {
+    iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
+    pending: Option<&'a Content<'de>>,
+    _marker: PhantomData<E>,
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, 'de, E> MapAccess<'de> for FlatInternallyTaggedAccess<'a, 'de, E>
+where
+    E: Error,
+{
+    type Error = E;
+
+    fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
+    where
+        T: DeserializeSeed<'de>,
+    {
+        while let Some(item) = self.iter.next() {
+            if let Some((ref key, ref content)) = *item {
+                // Do not take(), instead borrow this entry. The internally tagged
+                // enum does its own buffering so we can't tell whether this entry
+                // is going to be consumed. Borrowing here leaves the entry
+                // available for later flattened fields.
+                self.pending = Some(content);
+                return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
+            }
+        }
+        Ok(None)
+    }
+
+    fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
+    where
+        T: DeserializeSeed<'de>,
+    {
+        match self.pending.take() {
+            Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
+            None => panic!("value is missing"),
+        }
+    }
+}
diff --git a/src/private/macros.rs b/src/private/macros.rs
new file mode 100644
index 0000000..4f7054f
--- /dev/null
+++ b/src/private/macros.rs
@@ -0,0 +1,140 @@
+#[doc(hidden)]
+#[macro_export]
+macro_rules! __private_serialize {
+    () => {
+        trait Serialize {
+            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+            where
+                S: $crate::Serializer;
+        }
+    };
+}
+
+#[doc(hidden)]
+#[macro_export]
+macro_rules! __private_deserialize {
+    () => {
+        trait Deserialize<'de>: Sized {
+            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+            where
+                D: $crate::Deserializer<'de>;
+        }
+    };
+}
+
+/// Used only by Serde doc tests. Not public API.
+#[doc(hidden)]
+#[macro_export(local_inner_macros)]
+macro_rules! __serialize_unimplemented {
+    ($($func:ident)*) => {
+        $(
+            __serialize_unimplemented_helper!($func);
+        )*
+    };
+}
+
+#[doc(hidden)]
+#[macro_export]
+macro_rules! __serialize_unimplemented_method {
+    ($func:ident $(<$t:ident>)* ($($arg:ty),*) -> $ret:ident) => {
+        fn $func $(<$t: ?Sized + $crate::Serialize>)* (self $(, _: $arg)*) -> $crate::export::Result<Self::$ret, Self::Error> {
+            unimplemented!()
+        }
+    };
+}
+
+#[doc(hidden)]
+#[macro_export(local_inner_macros)]
+macro_rules! __serialize_unimplemented_helper {
+    (bool) => {
+        __serialize_unimplemented_method!(serialize_bool(bool) -> Ok);
+    };
+    (i8) => {
+        __serialize_unimplemented_method!(serialize_i8(i8) -> Ok);
+    };
+    (i16) => {
+        __serialize_unimplemented_method!(serialize_i16(i16) -> Ok);
+    };
+    (i32) => {
+        __serialize_unimplemented_method!(serialize_i32(i32) -> Ok);
+    };
+    (i64) => {
+        __serialize_unimplemented_method!(serialize_i64(i64) -> Ok);
+    };
+    (u8) => {
+        __serialize_unimplemented_method!(serialize_u8(u8) -> Ok);
+    };
+    (u16) => {
+        __serialize_unimplemented_method!(serialize_u16(u16) -> Ok);
+    };
+    (u32) => {
+        __serialize_unimplemented_method!(serialize_u32(u32) -> Ok);
+    };
+    (u64) => {
+        __serialize_unimplemented_method!(serialize_u64(u64) -> Ok);
+    };
+    (f32) => {
+        __serialize_unimplemented_method!(serialize_f32(f32) -> Ok);
+    };
+    (f64) => {
+        __serialize_unimplemented_method!(serialize_f64(f64) -> Ok);
+    };
+    (char) => {
+        __serialize_unimplemented_method!(serialize_char(char) -> Ok);
+    };
+    (str) => {
+        __serialize_unimplemented_method!(serialize_str(&str) -> Ok);
+    };
+    (bytes) => {
+        __serialize_unimplemented_method!(serialize_bytes(&[u8]) -> Ok);
+    };
+    (none) => {
+        __serialize_unimplemented_method!(serialize_none() -> Ok);
+    };
+    (some) => {
+        __serialize_unimplemented_method!(serialize_some<T>(&T) -> Ok);
+    };
+    (unit) => {
+        __serialize_unimplemented_method!(serialize_unit() -> Ok);
+    };
+    (unit_struct) => {
+        __serialize_unimplemented_method!(serialize_unit_struct(&str) -> Ok);
+    };
+    (unit_variant) => {
+        __serialize_unimplemented_method!(serialize_unit_variant(&str, u32, &str) -> Ok);
+    };
+    (newtype_struct) => {
+        __serialize_unimplemented_method!(serialize_newtype_struct<T>(&str, &T) -> Ok);
+    };
+    (newtype_variant) => {
+        __serialize_unimplemented_method!(serialize_newtype_variant<T>(&str, u32, &str, &T) -> Ok);
+    };
+    (seq) => {
+        type SerializeSeq = $crate::ser::Impossible<Self::Ok, Self::Error>;
+        __serialize_unimplemented_method!(serialize_seq(Option<usize>) -> SerializeSeq);
+    };
+    (tuple) => {
+        type SerializeTuple = $crate::ser::Impossible<Self::Ok, Self::Error>;
+        __serialize_unimplemented_method!(serialize_tuple(usize) -> SerializeTuple);
+    };
+    (tuple_struct) => {
+        type SerializeTupleStruct = $crate::ser::Impossible<Self::Ok, Self::Error>;
+        __serialize_unimplemented_method!(serialize_tuple_struct(&str, usize) -> SerializeTupleStruct);
+    };
+    (tuple_variant) => {
+        type SerializeTupleVariant = $crate::ser::Impossible<Self::Ok, Self::Error>;
+        __serialize_unimplemented_method!(serialize_tuple_variant(&str, u32, &str, usize) -> SerializeTupleVariant);
+    };
+    (map) => {
+        type SerializeMap = $crate::ser::Impossible<Self::Ok, Self::Error>;
+        __serialize_unimplemented_method!(serialize_map(Option<usize>) -> SerializeMap);
+    };
+    (struct) => {
+        type SerializeStruct = $crate::ser::Impossible<Self::Ok, Self::Error>;
+        __serialize_unimplemented_method!(serialize_struct(&str, usize) -> SerializeStruct);
+    };
+    (struct_variant) => {
+        type SerializeStructVariant = $crate::ser::Impossible<Self::Ok, Self::Error>;
+        __serialize_unimplemented_method!(serialize_struct_variant(&str, u32, &str, usize) -> SerializeStructVariant);
+    };
+}
diff --git a/src/private/mod.rs b/src/private/mod.rs
new file mode 100644
index 0000000..79e0a7d
--- /dev/null
+++ b/src/private/mod.rs
@@ -0,0 +1,4 @@
+mod macros;
+
+pub mod de;
+pub mod ser;
diff --git a/src/private/ser.rs b/src/private/ser.rs
new file mode 100644
index 0000000..eb8cfc9
--- /dev/null
+++ b/src/private/ser.rs
@@ -0,0 +1,1338 @@
+use lib::*;
+
+use ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer};
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+use self::content::{
+    Content, ContentSerializer, SerializeStructVariantAsMapValue, SerializeTupleVariantAsMapValue,
+};
+
+/// Used to check that serde(getter) attributes return the expected type.
+/// Not public API.
+pub fn constrain<T: ?Sized>(t: &T) -> &T {
+    t
+}
+
+/// Not public API.
+pub fn serialize_tagged_newtype<S, T>(
+    serializer: S,
+    type_ident: &'static str,
+    variant_ident: &'static str,
+    tag: &'static str,
+    variant_name: &'static str,
+    value: &T,
+) -> Result<S::Ok, S::Error>
+where
+    S: Serializer,
+    T: Serialize,
+{
+    value.serialize(TaggedSerializer {
+        type_ident: type_ident,
+        variant_ident: variant_ident,
+        tag: tag,
+        variant_name: variant_name,
+        delegate: serializer,
+    })
+}
+
+struct TaggedSerializer<S> {
+    type_ident: &'static str,
+    variant_ident: &'static str,
+    tag: &'static str,
+    variant_name: &'static str,
+    delegate: S,
+}
+
+enum Unsupported {
+    Boolean,
+    Integer,
+    Float,
+    Char,
+    String,
+    ByteArray,
+    Optional,
+    Unit,
+    #[cfg(any(feature = "std", feature = "alloc"))]
+    UnitStruct,
+    Sequence,
+    Tuple,
+    TupleStruct,
+    Enum,
+}
+
+impl Display for Unsupported {
+    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+        match *self {
+            Unsupported::Boolean => formatter.write_str("a boolean"),
+            Unsupported::Integer => formatter.write_str("an integer"),
+            Unsupported::Float => formatter.write_str("a float"),
+            Unsupported::Char => formatter.write_str("a char"),
+            Unsupported::String => formatter.write_str("a string"),
+            Unsupported::ByteArray => formatter.write_str("a byte array"),
+            Unsupported::Optional => formatter.write_str("an optional"),
+            Unsupported::Unit => formatter.write_str("unit"),
+            #[cfg(any(feature = "std", feature = "alloc"))]
+            Unsupported::UnitStruct => formatter.write_str("unit struct"),
+            Unsupported::Sequence => formatter.write_str("a sequence"),
+            Unsupported::Tuple => formatter.write_str("a tuple"),
+            Unsupported::TupleStruct => formatter.write_str("a tuple struct"),
+            Unsupported::Enum => formatter.write_str("an enum"),
+        }
+    }
+}
+
+impl<S> TaggedSerializer<S>
+where
+    S: Serializer,
+{
+    fn bad_type(self, what: Unsupported) -> S::Error {
+        ser::Error::custom(format_args!(
+            "cannot serialize tagged newtype variant {}::{} containing {}",
+            self.type_ident, self.variant_ident, what
+        ))
+    }
+}
+
+impl<S> Serializer for TaggedSerializer<S>
+where
+    S: Serializer,
+{
+    type Ok = S::Ok;
+    type Error = S::Error;
+
+    type SerializeSeq = Impossible<S::Ok, S::Error>;
+    type SerializeTuple = Impossible<S::Ok, S::Error>;
+    type SerializeTupleStruct = Impossible<S::Ok, S::Error>;
+    type SerializeMap = S::SerializeMap;
+    type SerializeStruct = S::SerializeStruct;
+
+    #[cfg(not(any(feature = "std", feature = "alloc")))]
+    type SerializeTupleVariant = Impossible<S::Ok, S::Error>;
+    #[cfg(any(feature = "std", feature = "alloc"))]
+    type SerializeTupleVariant = SerializeTupleVariantAsMapValue<S::SerializeMap>;
+
+    #[cfg(not(any(feature = "std", feature = "alloc")))]
+    type SerializeStructVariant = Impossible<S::Ok, S::Error>;
+    #[cfg(any(feature = "std", feature = "alloc"))]
+    type SerializeStructVariant = SerializeStructVariantAsMapValue<S::SerializeMap>;
+
+    fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Boolean))
+    }
+
+    fn serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Float))
+    }
+
+    fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Float))
+    }
+
+    fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Char))
+    }
+
+    fn serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::String))
+    }
+
+    fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::ByteArray))
+    }
+
+    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Optional))
+    }
+
+    fn serialize_some<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
+    where
+        T: Serialize,
+    {
+        Err(self.bad_type(Unsupported::Optional))
+    }
+
+    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
+        Err(self.bad_type(Unsupported::Unit))
+    }
+
+    fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
+        let mut map = try!(self.delegate.serialize_map(Some(1)));
+        try!(map.serialize_entry(self.tag, self.variant_name));
+        map.end()
+    }
+
+    fn serialize_unit_variant(
+        self,
+        _: &'static str,
+        _: u32,
+        inner_variant: &'static str,
+    ) -> Result<Self::Ok, Self::Error> {
+        let mut map = try!(self.delegate.serialize_map(Some(2)));
+        try!(map.serialize_entry(self.tag, self.variant_name));
+        try!(map.serialize_entry(inner_variant, &()));
+        map.end()
+    }
+
+    fn serialize_newtype_struct<T: ?Sized>(
+        self,
+        _: &'static str,
+        value: &T,
+    ) -> Result<Self::Ok, Self::Error>
+    where
+        T: Serialize,
+    {
+        value.serialize(self)
+    }
+
+    fn serialize_newtype_variant<T: ?Sized>(
+        self,
+        _: &'static str,
+        _: u32,
+        inner_variant: &'static str,
+        inner_value: &T,
+    ) -> Result<Self::Ok, Self::Error>
+    where
+        T: Serialize,
+    {
+        let mut map = try!(self.delegate.serialize_map(Some(2)));
+        try!(map.serialize_entry(self.tag, self.variant_name));
+        try!(map.serialize_entry(inner_variant, inner_value));
+        map.end()
+    }
+
+    fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
+        Err(self.bad_type(Unsupported::Sequence))
+    }
+
+    fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
+        Err(self.bad_type(Unsupported::Tuple))
+    }
+
+    fn serialize_tuple_struct(
+        self,
+        _: &'static str,
+        _: usize,
+    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
+        Err(self.bad_type(Unsupported::TupleStruct))
+    }
+
+    #[cfg(not(any(feature = "std", feature = "alloc")))]
+    fn serialize_tuple_variant(
+        self,
+        _: &'static str,
+        _: u32,
+        _: &'static str,
+        _: usize,
+    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
+        // Lack of push-based serialization means we need to buffer the content
+        // of the tuple variant, so it requires std.
+        Err(self.bad_type(Unsupported::Enum))
+    }
+
+    #[cfg(any(feature = "std", feature = "alloc"))]
+    fn serialize_tuple_variant(
+        self,
+        _: &'static str,
+        _: u32,
+        inner_variant: &'static str,
+        len: usize,
+    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
+        let mut map = try!(self.delegate.serialize_map(Some(2)));
+        try!(map.serialize_entry(self.tag, self.variant_name));
+        try!(map.serialize_key(inner_variant));
+        Ok(SerializeTupleVariantAsMapValue::new(
+            map,
+            inner_variant,
+            len,
+        ))
+    }
+
+    fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
+        let mut map = try!(self.delegate.serialize_map(len.map(|len| len + 1)));
+        try!(map.serialize_entry(self.tag, self.variant_name));
+        Ok(map)
+    }
+
+    fn serialize_struct(
+        self,
+        name: &'static str,
+        len: usize,
+    ) -> Result<Self::SerializeStruct, Self::Error> {
+        let mut state = try!(self.delegate.serialize_struct(name, len + 1));
+        try!(state.serialize_field(self.tag, self.variant_name));
+        Ok(state)
+    }
+
+    #[cfg(not(any(feature = "std", feature = "alloc")))]
+    fn serialize_struct_variant(
+        self,
+        _: &'static str,
+        _: u32,
+        _: &'static str,
+        _: usize,
+    ) -> Result<Self::SerializeStructVariant, Self::Error> {
+        // Lack of push-based serialization means we need to buffer the content
+        // of the struct variant, so it requires std.
+        Err(self.bad_type(Unsupported::Enum))
+    }
+
+    #[cfg(any(feature = "std", feature = "alloc"))]
+    fn serialize_struct_variant(
+        self,
+        _: &'static str,
+        _: u32,
+        inner_variant: &'static str,
+        len: usize,
+    ) -> Result<Self::SerializeStructVariant, Self::Error> {
+        let mut map = try!(self.delegate.serialize_map(Some(2)));
+        try!(map.serialize_entry(self.tag, self.variant_name));
+        try!(map.serialize_key(inner_variant));
+        Ok(SerializeStructVariantAsMapValue::new(
+            map,
+            inner_variant,
+            len,
+        ))
+    }
+
+    #[cfg(not(any(feature = "std", feature = "alloc")))]
+    fn collect_str<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
+    where
+        T: Display,
+    {
+        Err(self.bad_type(Unsupported::String))
+    }
+}
+
+/// Used only by Serde doc tests. Not public API.
+#[doc(hidden)]
+#[derive(Debug)]
+pub struct Error;
+
+impl ser::Error for Error {
+    fn custom<T>(_: T) -> Self
+    where
+        T: Display,
+    {
+        unimplemented!()
+    }
+}
+
+#[cfg(feature = "std")]
+impl error::Error for Error {
+    fn description(&self) -> &str {
+        unimplemented!()
+    }
+}
+
+impl Display for Error {
+    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
+        unimplemented!()
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+mod content {
+    use lib::*;
+
+    use ser::{self, Serialize, Serializer};
+
+    pub struct SerializeTupleVariantAsMapValue<M> {
+        map: M,
+        name: &'static str,
+        fields: Vec<Content>,
+    }
+
+    impl<M> SerializeTupleVariantAsMapValue<M> {
+        pub fn new(map: M, name: &'static str, len: usize) -> Self {
+            SerializeTupleVariantAsMapValue {
+                map: map,
+                name: name,
+                fields: Vec::with_capacity(len),
+            }
+        }
+    }
+
+    impl<M> ser::SerializeTupleVariant for SerializeTupleVariantAsMapValue<M>
+    where
+        M: ser::SerializeMap,
+    {
+        type Ok = M::Ok;
+        type Error = M::Error;
+
+        fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), M::Error>
+        where
+            T: Serialize,
+        {
+            let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
+            self.fields.push(value);
+            Ok(())
+        }
+
+        fn end(mut self) -> Result<M::Ok, M::Error> {
+            try!(self
+                .map
+                .serialize_value(&Content::TupleStruct(self.name, self.fields)));
+            self.map.end()
+        }
+    }
+
+    pub struct SerializeStructVariantAsMapValue<M> {
+        map: M,
+        name: &'static str,
+        fields: Vec<(&'static str, Content)>,
+    }
+
+    impl<M> SerializeStructVariantAsMapValue<M> {
+        pub fn new(map: M, name: &'static str, len: usize) -> Self {
+            SerializeStructVariantAsMapValue {
+                map: map,
+                name: name,
+                fields: Vec::with_capacity(len),
+            }
+        }
+    }
+
+    impl<M> ser::SerializeStructVariant for SerializeStructVariantAsMapValue<M>
+    where
+        M: ser::SerializeMap,
+    {
+        type Ok = M::Ok;
+        type Error = M::Error;
+
+        fn serialize_field<T: ?Sized>(
+            &mut self,
+            key: &'static str,
+            value: &T,
+        ) -> Result<(), M::Error>
+        where
+            T: Serialize,
+        {
+            let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
+            self.fields.push((key, value));
+            Ok(())
+        }
+
+        fn end(mut self) -> Result<M::Ok, M::Error> {
+            try!(self
+                .map
+                .serialize_value(&Content::Struct(self.name, self.fields)));
+            self.map.end()
+        }
+    }
+
+    #[derive(Debug)]
+    pub enum Content {
+        Bool(bool),
+
+        U8(u8),
+        U16(u16),
+        U32(u32),
+        U64(u64),
+
+        I8(i8),
+        I16(i16),
+        I32(i32),
+        I64(i64),
+
+        F32(f32),
+        F64(f64),
+
+        Char(char),
+        String(String),
+        Bytes(Vec<u8>),
+
+        None,
+        Some(Box<Content>),
+
+        Unit,
+        UnitStruct(&'static str),
+        UnitVariant(&'static str, u32, &'static str),
+        NewtypeStruct(&'static str, Box<Content>),
+        NewtypeVariant(&'static str, u32, &'static str, Box<Content>),
+
+        Seq(Vec<Content>),
+        Tuple(Vec<Content>),
+        TupleStruct(&'static str, Vec<Content>),
+        TupleVariant(&'static str, u32, &'static str, Vec<Content>),
+        Map(Vec<(Content, Content)>),
+        Struct(&'static str, Vec<(&'static str, Content)>),
+        StructVariant(
+            &'static str,
+            u32,
+            &'static str,
+            Vec<(&'static str, Content)>,
+        ),
+    }
+
+    impl Serialize for Content {
+        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+        where
+            S: Serializer,
+        {
+            match *self {
+                Content::Bool(b) => serializer.serialize_bool(b),
+                Content::U8(u) => serializer.serialize_u8(u),
+                Content::U16(u) => serializer.serialize_u16(u),
+                Content::U32(u) => serializer.serialize_u32(u),
+                Content::U64(u) => serializer.serialize_u64(u),
+                Content::I8(i) => serializer.serialize_i8(i),
+                Content::I16(i) => serializer.serialize_i16(i),
+                Content::I32(i) => serializer.serialize_i32(i),
+                Content::I64(i) => serializer.serialize_i64(i),
+                Content::F32(f) => serializer.serialize_f32(f),
+                Content::F64(f) => serializer.serialize_f64(f),
+                Content::Char(c) => serializer.serialize_char(c),
+                Content::String(ref s) => serializer.serialize_str(s),
+                Content::Bytes(ref b) => serializer.serialize_bytes(b),
+                Content::None => serializer.serialize_none(),
+                Content::Some(ref c) => serializer.serialize_some(&**c),
+                Content::Unit => serializer.serialize_unit(),
+                Content::UnitStruct(n) => serializer.serialize_unit_struct(n),
+                Content::UnitVariant(n, i, v) => serializer.serialize_unit_variant(n, i, v),
+                Content::NewtypeStruct(n, ref c) => serializer.serialize_newtype_struct(n, &**c),
+                Content::NewtypeVariant(n, i, v, ref c) => {
+                    serializer.serialize_newtype_variant(n, i, v, &**c)
+                }
+                Content::Seq(ref elements) => elements.serialize(serializer),
+                Content::Tuple(ref elements) => {
+                    use ser::SerializeTuple;
+                    let mut tuple = try!(serializer.serialize_tuple(elements.len()));
+                    for e in elements {
+                        try!(tuple.serialize_element(e));
+                    }
+                    tuple.end()
+                }
+                Content::TupleStruct(n, ref fields) => {
+                    use ser::SerializeTupleStruct;
+                    let mut ts = try!(serializer.serialize_tuple_struct(n, fields.len()));
+                    for f in fields {
+                        try!(ts.serialize_field(f));
+                    }
+                    ts.end()
+                }
+                Content::TupleVariant(n, i, v, ref fields) => {
+                    use ser::SerializeTupleVariant;
+                    let mut tv = try!(serializer.serialize_tuple_variant(n, i, v, fields.len()));
+                    for f in fields {
+                        try!(tv.serialize_field(f));
+                    }
+                    tv.end()
+                }
+                Content::Map(ref entries) => {
+                    use ser::SerializeMap;
+                    let mut map = try!(serializer.serialize_map(Some(entries.len())));
+                    for &(ref k, ref v) in entries {
+                        try!(map.serialize_entry(k, v));
+                    }
+                    map.end()
+                }
+                Content::Struct(n, ref fields) => {
+                    use ser::SerializeStruct;
+                    let mut s = try!(serializer.serialize_struct(n, fields.len()));
+                    for &(k, ref v) in fields {
+                        try!(s.serialize_field(k, v));
+                    }
+                    s.end()
+                }
+                Content::StructVariant(n, i, v, ref fields) => {
+                    use ser::SerializeStructVariant;
+                    let mut sv = try!(serializer.serialize_struct_variant(n, i, v, fields.len()));
+                    for &(k, ref v) in fields {
+                        try!(sv.serialize_field(k, v));
+                    }
+                    sv.end()
+                }
+            }
+        }
+    }
+
+    pub struct ContentSerializer<E> {
+        error: PhantomData<E>,
+    }
+
+    impl<E> ContentSerializer<E> {
+        pub fn new() -> Self {
+            ContentSerializer { error: PhantomData }
+        }
+    }
+
+    impl<E> Serializer for ContentSerializer<E>
+    where
+        E: ser::Error,
+    {
+        type Ok = Content;
+        type Error = E;
+
+        type SerializeSeq = SerializeSeq<E>;
+        type SerializeTuple = SerializeTuple<E>;
+        type SerializeTupleStruct = SerializeTupleStruct<E>;
+        type SerializeTupleVariant = SerializeTupleVariant<E>;
+        type SerializeMap = SerializeMap<E>;
+        type SerializeStruct = SerializeStruct<E>;
+        type SerializeStructVariant = SerializeStructVariant<E>;
+
+        fn serialize_bool(self, v: bool) -> Result<Content, E> {
+            Ok(Content::Bool(v))
+        }
+
+        fn serialize_i8(self, v: i8) -> Result<Content, E> {
+            Ok(Content::I8(v))
+        }
+
+        fn serialize_i16(self, v: i16) -> Result<Content, E> {
+            Ok(Content::I16(v))
+        }
+
+        fn serialize_i32(self, v: i32) -> Result<Content, E> {
+            Ok(Content::I32(v))
+        }
+
+        fn serialize_i64(self, v: i64) -> Result<Content, E> {
+            Ok(Content::I64(v))
+        }
+
+        fn serialize_u8(self, v: u8) -> Result<Content, E> {
+            Ok(Content::U8(v))
+        }
+
+        fn serialize_u16(self, v: u16) -> Result<Content, E> {
+            Ok(Content::U16(v))
+        }
+
+        fn serialize_u32(self, v: u32) -> Result<Content, E> {
+            Ok(Content::U32(v))
+        }
+
+        fn serialize_u64(self, v: u64) -> Result<Content, E> {
+            Ok(Content::U64(v))
+        }
+
+        fn serialize_f32(self, v: f32) -> Result<Content, E> {
+            Ok(Content::F32(v))
+        }
+
+        fn serialize_f64(self, v: f64) -> Result<Content, E> {
+            Ok(Content::F64(v))
+        }
+
+        fn serialize_char(self, v: char) -> Result<Content, E> {
+            Ok(Content::Char(v))
+        }
+
+        fn serialize_str(self, value: &str) -> Result<Content, E> {
+            Ok(Content::String(value.to_owned()))
+        }
+
+        fn serialize_bytes(self, value: &[u8]) -> Result<Content, E> {
+            Ok(Content::Bytes(value.to_owned()))
+        }
+
+        fn serialize_none(self) -> Result<Content, E> {
+            Ok(Content::None)
+        }
+
+        fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Content, E>
+        where
+            T: Serialize,
+        {
+            Ok(Content::Some(Box::new(try!(value.serialize(self)))))
+        }
+
+        fn serialize_unit(self) -> Result<Content, E> {
+            Ok(Content::Unit)
+        }
+
+        fn serialize_unit_struct(self, name: &'static str) -> Result<Content, E> {
+            Ok(Content::UnitStruct(name))
+        }
+
+        fn serialize_unit_variant(
+            self,
+            name: &'static str,
+            variant_index: u32,
+            variant: &'static str,
+        ) -> Result<Content, E> {
+            Ok(Content::UnitVariant(name, variant_index, variant))
+        }
+
+        fn serialize_newtype_struct<T: ?Sized>(
+            self,
+            name: &'static str,
+            value: &T,
+        ) -> Result<Content, E>
+        where
+            T: Serialize,
+        {
+            Ok(Content::NewtypeStruct(
+                name,
+                Box::new(try!(value.serialize(self))),
+            ))
+        }
+
+        fn serialize_newtype_variant<T: ?Sized>(
+            self,
+            name: &'static str,
+            variant_index: u32,
+            variant: &'static str,
+            value: &T,
+        ) -> Result<Content, E>
+        where
+            T: Serialize,
+        {
+            Ok(Content::NewtypeVariant(
+                name,
+                variant_index,
+                variant,
+                Box::new(try!(value.serialize(self))),
+            ))
+        }
+
+        fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, E> {
+            Ok(SerializeSeq {
+                elements: Vec::with_capacity(len.unwrap_or(0)),
+                error: PhantomData,
+            })
+        }
+
+        fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, E> {
+            Ok(SerializeTuple {
+                elements: Vec::with_capacity(len),
+                error: PhantomData,
+            })
+        }
+
+        fn serialize_tuple_struct(
+            self,
+            name: &'static str,
+            len: usize,
+        ) -> Result<Self::SerializeTupleStruct, E> {
+            Ok(SerializeTupleStruct {
+                name: name,
+                fields: Vec::with_capacity(len),
+                error: PhantomData,
+            })
+        }
+
+        fn serialize_tuple_variant(
+            self,
+            name: &'static str,
+            variant_index: u32,
+            variant: &'static str,
+            len: usize,
+        ) -> Result<Self::SerializeTupleVariant, E> {
+            Ok(SerializeTupleVariant {
+                name: name,
+                variant_index: variant_index,
+                variant: variant,
+                fields: Vec::with_capacity(len),
+                error: PhantomData,
+            })
+        }
+
+        fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, E> {
+            Ok(SerializeMap {
+                entries: Vec::with_capacity(len.unwrap_or(0)),
+                key: None,
+                error: PhantomData,
+            })
+        }
+
+        fn serialize_struct(
+            self,
+            name: &'static str,
+            len: usize,
+        ) -> Result<Self::SerializeStruct, E> {
+            Ok(SerializeStruct {
+                name: name,
+                fields: Vec::with_capacity(len),
+                error: PhantomData,
+            })
+        }
+
+        fn serialize_struct_variant(
+            self,
+            name: &'static str,
+            variant_index: u32,
+            variant: &'static str,
+            len: usize,
+        ) -> Result<Self::SerializeStructVariant, E> {
+            Ok(SerializeStructVariant {
+                name: name,
+                variant_index: variant_index,
+                variant: variant,
+                fields: Vec::with_capacity(len),
+                error: PhantomData,
+            })
+        }
+    }
+
+    pub struct SerializeSeq<E> {
+        elements: Vec<Content>,
+        error: PhantomData<E>,
+    }
+
+    impl<E> ser::SerializeSeq for SerializeSeq<E>
+    where
+        E: ser::Error,
+    {
+        type Ok = Content;
+        type Error = E;
+
+        fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
+        where
+            T: Serialize,
+        {
+            let value = try!(value.serialize(ContentSerializer::<E>::new()));
+            self.elements.push(value);
+            Ok(())
+        }
+
+        fn end(self) -> Result<Content, E> {
+            Ok(Content::Seq(self.elements))
+        }
+    }
+
+    pub struct SerializeTuple<E> {
+        elements: Vec<Content>,
+        error: PhantomData<E>,
+    }
+
+    impl<E> ser::SerializeTuple for SerializeTuple<E>
+    where
+        E: ser::Error,
+    {
+        type Ok = Content;
+        type Error = E;
+
+        fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
+        where
+            T: Serialize,
+        {
+            let value = try!(value.serialize(ContentSerializer::<E>::new()));
+            self.elements.push(value);
+            Ok(())
+        }
+
+        fn end(self) -> Result<Content, E> {
+            Ok(Content::Tuple(self.elements))
+        }
+    }
+
+    pub struct SerializeTupleStruct<E> {
+        name: &'static str,
+        fields: Vec<Content>,
+        error: PhantomData<E>,
+    }
+
+    impl<E> ser::SerializeTupleStruct for SerializeTupleStruct<E>
+    where
+        E: ser::Error,
+    {
+        type Ok = Content;
+        type Error = E;
+
+        fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
+        where
+            T: Serialize,
+        {
+            let value = try!(value.serialize(ContentSerializer::<E>::new()));
+            self.fields.push(value);
+            Ok(())
+        }
+
+        fn end(self) -> Result<Content, E> {
+            Ok(Content::TupleStruct(self.name, self.fields))
+        }
+    }
+
+    pub struct SerializeTupleVariant<E> {
+        name: &'static str,
+        variant_index: u32,
+        variant: &'static str,
+        fields: Vec<Content>,
+        error: PhantomData<E>,
+    }
+
+    impl<E> ser::SerializeTupleVariant for SerializeTupleVariant<E>
+    where
+        E: ser::Error,
+    {
+        type Ok = Content;
+        type Error = E;
+
+        fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
+        where
+            T: Serialize,
+        {
+            let value = try!(value.serialize(ContentSerializer::<E>::new()));
+            self.fields.push(value);
+            Ok(())
+        }
+
+        fn end(self) -> Result<Content, E> {
+            Ok(Content::TupleVariant(
+                self.name,
+                self.variant_index,
+                self.variant,
+                self.fields,
+            ))
+        }
+    }
+
+    pub struct SerializeMap<E> {
+        entries: Vec<(Content, Content)>,
+        key: Option<Content>,
+        error: PhantomData<E>,
+    }
+
+    impl<E> ser::SerializeMap for SerializeMap<E>
+    where
+        E: ser::Error,
+    {
+        type Ok = Content;
+        type Error = E;
+
+        fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), E>
+        where
+            T: Serialize,
+        {
+            let key = try!(key.serialize(ContentSerializer::<E>::new()));
+            self.key = Some(key);
+            Ok(())
+        }
+
+        fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
+        where
+            T: Serialize,
+        {
+            let key = self
+                .key
+                .take()
+                .expect("serialize_value called before serialize_key");
+            let value = try!(value.serialize(ContentSerializer::<E>::new()));
+            self.entries.push((key, value));
+            Ok(())
+        }
+
+        fn end(self) -> Result<Content, E> {
+            Ok(Content::Map(self.entries))
+        }
+
+        fn serialize_entry<K: ?Sized, V: ?Sized>(&mut self, key: &K, value: &V) -> Result<(), E>
+        where
+            K: Serialize,
+            V: Serialize,
+        {
+            let key = try!(key.serialize(ContentSerializer::<E>::new()));
+            let value = try!(value.serialize(ContentSerializer::<E>::new()));
+            self.entries.push((key, value));
+            Ok(())
+        }
+    }
+
+    pub struct SerializeStruct<E> {
+        name: &'static str,
+        fields: Vec<(&'static str, Content)>,
+        error: PhantomData<E>,
+    }
+
+    impl<E> ser::SerializeStruct for SerializeStruct<E>
+    where
+        E: ser::Error,
+    {
+        type Ok = Content;
+        type Error = E;
+
+        fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E>
+        where
+            T: Serialize,
+        {
+            let value = try!(value.serialize(ContentSerializer::<E>::new()));
+            self.fields.push((key, value));
+            Ok(())
+        }
+
+        fn end(self) -> Result<Content, E> {
+            Ok(Content::Struct(self.name, self.fields))
+        }
+    }
+
+    pub struct SerializeStructVariant<E> {
+        name: &'static str,
+        variant_index: u32,
+        variant: &'static str,
+        fields: Vec<(&'static str, Content)>,
+        error: PhantomData<E>,
+    }
+
+    impl<E> ser::SerializeStructVariant for SerializeStructVariant<E>
+    where
+        E: ser::Error,
+    {
+        type Ok = Content;
+        type Error = E;
+
+        fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E>
+        where
+            T: Serialize,
+        {
+            let value = try!(value.serialize(ContentSerializer::<E>::new()));
+            self.fields.push((key, value));
+            Ok(())
+        }
+
+        fn end(self) -> Result<Content, E> {
+            Ok(Content::StructVariant(
+                self.name,
+                self.variant_index,
+                self.variant,
+                self.fields,
+            ))
+        }
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub struct FlatMapSerializer<'a, M: 'a>(pub &'a mut M);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, M> FlatMapSerializer<'a, M>
+where
+    M: SerializeMap + 'a,
+{
+    fn bad_type(what: Unsupported) -> M::Error {
+        ser::Error::custom(format_args!(
+            "can only flatten structs and maps (got {})",
+            what
+        ))
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, M> Serializer for FlatMapSerializer<'a, M>
+where
+    M: SerializeMap + 'a,
+{
+    type Ok = ();
+    type Error = M::Error;
+
+    type SerializeSeq = Impossible<Self::Ok, M::Error>;
+    type SerializeTuple = Impossible<Self::Ok, M::Error>;
+    type SerializeTupleStruct = Impossible<Self::Ok, M::Error>;
+    type SerializeMap = FlatMapSerializeMap<'a, M>;
+    type SerializeStruct = FlatMapSerializeStruct<'a, M>;
+    type SerializeTupleVariant = Impossible<Self::Ok, M::Error>;
+    type SerializeStructVariant = FlatMapSerializeStructVariantAsMapValue<'a, M>;
+
+    fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Boolean))
+    }
+
+    fn serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Integer))
+    }
+
+    fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Float))
+    }
+
+    fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Float))
+    }
+
+    fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Char))
+    }
+
+    fn serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::String))
+    }
+
+    fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::ByteArray))
+    }
+
+    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
+        Ok(())
+    }
+
+    fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
+    where
+        T: Serialize,
+    {
+        value.serialize(self)
+    }
+
+    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
+        Ok(())
+    }
+
+    fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::UnitStruct))
+    }
+
+    fn serialize_unit_variant(
+        self,
+        _: &'static str,
+        _: u32,
+        _: &'static str,
+    ) -> Result<Self::Ok, Self::Error> {
+        Err(Self::bad_type(Unsupported::Enum))
+    }
+
+    fn serialize_newtype_struct<T: ?Sized>(
+        self,
+        _: &'static str,
+        value: &T,
+    ) -> Result<Self::Ok, Self::Error>
+    where
+        T: Serialize,
+    {
+        value.serialize(self)
+    }
+
+    fn serialize_newtype_variant<T: ?Sized>(
+        self,
+        _: &'static str,
+        _: u32,
+        variant: &'static str,
+        value: &T,
+    ) -> Result<Self::Ok, Self::Error>
+    where
+        T: Serialize,
+    {
+        try!(self.0.serialize_key(variant));
+        self.0.serialize_value(value)
+    }
+
+    fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
+        Err(Self::bad_type(Unsupported::Sequence))
+    }
+
+    fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
+        Err(Self::bad_type(Unsupported::Tuple))
+    }
+
+    fn serialize_tuple_struct(
+        self,
+        _: &'static str,
+        _: usize,
+    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
+        Err(Self::bad_type(Unsupported::TupleStruct))
+    }
+
+    fn serialize_tuple_variant(
+        self,
+        _: &'static str,
+        _: u32,
+        _: &'static str,
+        _: usize,
+    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
+        Err(Self::bad_type(Unsupported::Enum))
+    }
+
+    fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
+        Ok(FlatMapSerializeMap(self.0))
+    }
+
+    fn serialize_struct(
+        self,
+        _: &'static str,
+        _: usize,
+    ) -> Result<Self::SerializeStruct, Self::Error> {
+        Ok(FlatMapSerializeStruct(self.0))
+    }
+
+    fn serialize_struct_variant(
+        self,
+        _: &'static str,
+        _: u32,
+        inner_variant: &'static str,
+        _: usize,
+    ) -> Result<Self::SerializeStructVariant, Self::Error> {
+        try!(self.0.serialize_key(inner_variant));
+        Ok(FlatMapSerializeStructVariantAsMapValue::new(
+            self.0,
+            inner_variant,
+        ))
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub struct FlatMapSerializeMap<'a, M: 'a>(&'a mut M);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, M> ser::SerializeMap for FlatMapSerializeMap<'a, M>
+where
+    M: SerializeMap + 'a,
+{
+    type Ok = ();
+    type Error = M::Error;
+
+    fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
+    where
+        T: Serialize,
+    {
+        self.0.serialize_key(key)
+    }
+
+    fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
+    where
+        T: Serialize,
+    {
+        self.0.serialize_value(value)
+    }
+
+    fn serialize_entry<K: ?Sized, V: ?Sized>(
+        &mut self,
+        key: &K,
+        value: &V,
+    ) -> Result<(), Self::Error>
+    where
+        K: Serialize,
+        V: Serialize,
+    {
+        self.0.serialize_entry(key, value)
+    }
+
+    fn end(self) -> Result<(), Self::Error> {
+        Ok(())
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub struct FlatMapSerializeStruct<'a, M: 'a>(&'a mut M);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, M> ser::SerializeStruct for FlatMapSerializeStruct<'a, M>
+where
+    M: SerializeMap + 'a,
+{
+    type Ok = ();
+    type Error = M::Error;
+
+    fn serialize_field<T: ?Sized>(
+        &mut self,
+        key: &'static str,
+        value: &T,
+    ) -> Result<(), Self::Error>
+    where
+        T: Serialize,
+    {
+        self.0.serialize_entry(key, value)
+    }
+
+    fn end(self) -> Result<(), Self::Error> {
+        Ok(())
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub struct FlatMapSerializeStructVariantAsMapValue<'a, M: 'a> {
+    map: &'a mut M,
+    name: &'static str,
+    fields: Vec<(&'static str, Content)>,
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, M> FlatMapSerializeStructVariantAsMapValue<'a, M>
+where
+    M: SerializeMap + 'a,
+{
+    fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> {
+        FlatMapSerializeStructVariantAsMapValue {
+            map: map,
+            name: name,
+            fields: Vec::new(),
+        }
+    }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapValue<'a, M>
+where
+    M: SerializeMap + 'a,
+{
+    type Ok = ();
+    type Error = M::Error;
+
+    fn serialize_field<T: ?Sized>(
+        &mut self,
+        key: &'static str,
+        value: &T,
+    ) -> Result<(), Self::Error>
+    where
+        T: Serialize,
+    {
+        let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
+        self.fields.push((key, value));
+        Ok(())
+    }
+
+    fn end(self) -> Result<(), Self::Error> {
+        try!(self
+            .map
+            .serialize_value(&Content::Struct(self.name, self.fields)));
+        Ok(())
+    }
+}