summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--third_party/rust/serde/src/de/format.rs30
-rw-r--r--third_party/rust/serde/src/de/ignored_any.rs243
-rw-r--r--third_party/rust/serde/src/de/impls.rs2733
-rw-r--r--third_party/rust/serde/src/de/mod.rs2299
-rw-r--r--third_party/rust/serde/src/de/seed.rs19
-rw-r--r--third_party/rust/serde/src/de/utf8.rs46
-rw-r--r--third_party/rust/serde/src/de/value.rs1714
-rw-r--r--third_party/rust/serde/src/integer128.rs82
-rw-r--r--third_party/rust/serde/src/lib.rs334
-rw-r--r--third_party/rust/serde/src/macros.rs236
-rw-r--r--third_party/rust/serde/src/private/de.rs2995
-rw-r--r--third_party/rust/serde/src/private/doc.rs159
-rw-r--r--third_party/rust/serde/src/private/mod.rs50
-rw-r--r--third_party/rust/serde/src/private/ser.rs1310
-rw-r--r--third_party/rust/serde/src/private/size_hint.rs21
-rw-r--r--third_party/rust/serde/src/ser/fmt.rs174
-rw-r--r--third_party/rust/serde/src/ser/impls.rs983
-rw-r--r--third_party/rust/serde/src/ser/impossible.rs216
-rw-r--r--third_party/rust/serde/src/ser/mod.rs1988
-rw-r--r--third_party/rust/serde/src/std_error.rs48
20 files changed, 15680 insertions, 0 deletions
diff --git a/third_party/rust/serde/src/de/format.rs b/third_party/rust/serde/src/de/format.rs
new file mode 100644
index 0000000000..f14580b8d1
--- /dev/null
+++ b/third_party/rust/serde/src/de/format.rs
@@ -0,0 +1,30 @@
+use lib::fmt::{self, Write};
+use lib::str;
+
+pub(super) struct Buf<'a> {
+ bytes: &'a mut [u8],
+ offset: usize,
+}
+
+impl<'a> Buf<'a> {
+ pub fn new(bytes: &'a mut [u8]) -> Self {
+ Buf { bytes, offset: 0 }
+ }
+
+ pub fn as_str(&self) -> &str {
+ let slice = &self.bytes[..self.offset];
+ unsafe { str::from_utf8_unchecked(slice) }
+ }
+}
+
+impl<'a> Write for Buf<'a> {
+ fn write_str(&mut self, s: &str) -> fmt::Result {
+ if self.offset + s.len() > self.bytes.len() {
+ Err(fmt::Error)
+ } else {
+ self.bytes[self.offset..self.offset + s.len()].copy_from_slice(s.as_bytes());
+ self.offset += s.len();
+ Ok(())
+ }
+ }
+}
diff --git a/third_party/rust/serde/src/de/ignored_any.rs b/third_party/rust/serde/src/de/ignored_any.rs
new file mode 100644
index 0000000000..9ed438e797
--- /dev/null
+++ b/third_party/rust/serde/src/de/ignored_any.rs
@@ -0,0 +1,243 @@
+use lib::*;
+
+use de::{
+ Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor,
+};
+
+/// An efficient way of discarding data from a deserializer.
+///
+/// Think of this like `serde_json::Value` in that it can be deserialized from
+/// any type, except that it does not store any information about the data that
+/// gets deserialized.
+///
+/// ```edition2018
+/// use std::fmt;
+/// use std::marker::PhantomData;
+///
+/// use serde::de::{
+/// self, Deserialize, DeserializeSeed, Deserializer, IgnoredAny, SeqAccess, Visitor,
+/// };
+///
+/// /// A seed that can be used to deserialize only the `n`th element of a sequence
+/// /// while efficiently discarding elements of any type before or after index `n`.
+/// ///
+/// /// For example to deserialize only the element at index 3:
+/// ///
+/// /// ```
+/// /// NthElement::new(3).deserialize(deserializer)
+/// /// ```
+/// pub struct NthElement<T> {
+/// n: usize,
+/// marker: PhantomData<T>,
+/// }
+///
+/// impl<T> NthElement<T> {
+/// pub fn new(n: usize) -> Self {
+/// NthElement {
+/// n: n,
+/// marker: PhantomData,
+/// }
+/// }
+/// }
+///
+/// impl<'de, T> Visitor<'de> for NthElement<T>
+/// where
+/// T: Deserialize<'de>,
+/// {
+/// type Value = T;
+///
+/// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+/// write!(
+/// formatter,
+/// "a sequence in which we care about element {}",
+/// self.n
+/// )
+/// }
+///
+/// fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+/// where
+/// A: SeqAccess<'de>,
+/// {
+/// // Skip over the first `n` elements.
+/// for i in 0..self.n {
+/// // It is an error if the sequence ends before we get to element `n`.
+/// if seq.next_element::<IgnoredAny>()?.is_none() {
+/// return Err(de::Error::invalid_length(i, &self));
+/// }
+/// }
+///
+/// // Deserialize the one we care about.
+/// let nth = match seq.next_element()? {
+/// Some(nth) => nth,
+/// None => {
+/// return Err(de::Error::invalid_length(self.n, &self));
+/// }
+/// };
+///
+/// // Skip over any remaining elements in the sequence after `n`.
+/// while let Some(IgnoredAny) = seq.next_element()? {
+/// // ignore
+/// }
+///
+/// Ok(nth)
+/// }
+/// }
+///
+/// impl<'de, T> DeserializeSeed<'de> for NthElement<T>
+/// where
+/// T: Deserialize<'de>,
+/// {
+/// type Value = T;
+///
+/// fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+/// where
+/// D: Deserializer<'de>,
+/// {
+/// deserializer.deserialize_seq(self)
+/// }
+/// }
+///
+/// # fn example<'de, D>(deserializer: D) -> Result<(), D::Error>
+/// # where
+/// # D: Deserializer<'de>,
+/// # {
+/// // Deserialize only the sequence element at index 3 from this deserializer.
+/// // The element at index 3 is required to be a string. Elements before and
+/// // after index 3 are allowed to be of any type.
+/// let s: String = NthElement::new(3).deserialize(deserializer)?;
+/// # Ok(())
+/// # }
+/// ```
+#[derive(Copy, Clone, Debug, Default)]
+pub struct IgnoredAny;
+
+impl<'de> Visitor<'de> for IgnoredAny {
+ type Value = IgnoredAny;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("anything at all")
+ }
+
+ #[inline]
+ fn visit_bool<E>(self, x: bool) -> Result<Self::Value, E> {
+ let _ = x;
+ Ok(IgnoredAny)
+ }
+
+ #[inline]
+ fn visit_i64<E>(self, x: i64) -> Result<Self::Value, E> {
+ let _ = x;
+ Ok(IgnoredAny)
+ }
+
+ serde_if_integer128! {
+ #[inline]
+ fn visit_i128<E>(self, x: i128) -> Result<Self::Value, E> {
+ let _ = x;
+ Ok(IgnoredAny)
+ }
+ }
+
+ #[inline]
+ fn visit_u64<E>(self, x: u64) -> Result<Self::Value, E> {
+ let _ = x;
+ Ok(IgnoredAny)
+ }
+
+ serde_if_integer128! {
+ #[inline]
+ fn visit_u128<E>(self, x: u128) -> Result<Self::Value, E> {
+ let _ = x;
+ Ok(IgnoredAny)
+ }
+ }
+
+ #[inline]
+ fn visit_f64<E>(self, x: f64) -> Result<Self::Value, E> {
+ let _ = x;
+ Ok(IgnoredAny)
+ }
+
+ #[inline]
+ fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ let _ = s;
+ Ok(IgnoredAny)
+ }
+
+ #[inline]
+ fn visit_none<E>(self) -> Result<Self::Value, E> {
+ Ok(IgnoredAny)
+ }
+
+ #[inline]
+ fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ IgnoredAny::deserialize(deserializer)
+ }
+
+ #[inline]
+ fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ IgnoredAny::deserialize(deserializer)
+ }
+
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Self::Value, E> {
+ Ok(IgnoredAny)
+ }
+
+ #[inline]
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ while let Some(IgnoredAny) = try!(seq.next_element()) {
+ // Gobble
+ }
+ Ok(IgnoredAny)
+ }
+
+ #[inline]
+ fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
+ where
+ A: MapAccess<'de>,
+ {
+ while let Some((IgnoredAny, IgnoredAny)) = try!(map.next_entry()) {
+ // Gobble
+ }
+ Ok(IgnoredAny)
+ }
+
+ #[inline]
+ fn visit_bytes<E>(self, bytes: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ let _ = bytes;
+ Ok(IgnoredAny)
+ }
+
+ fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ try!(data.variant::<IgnoredAny>()).1.newtype_variant()
+ }
+}
+
+impl<'de> Deserialize<'de> for IgnoredAny {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_ignored_any(IgnoredAny)
+ }
+}
diff --git a/third_party/rust/serde/src/de/impls.rs b/third_party/rust/serde/src/de/impls.rs
new file mode 100644
index 0000000000..a257d9e2f4
--- /dev/null
+++ b/third_party/rust/serde/src/de/impls.rs
@@ -0,0 +1,2733 @@
+use lib::*;
+
+use de::{
+ Deserialize, Deserializer, EnumAccess, Error, SeqAccess, Unexpected, VariantAccess, Visitor,
+};
+
+#[cfg(any(feature = "std", feature = "alloc", not(no_core_duration)))]
+use de::MapAccess;
+
+use seed::InPlaceSeed;
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+use __private::size_hint;
+
+////////////////////////////////////////////////////////////////////////////////
+
+struct UnitVisitor;
+
+impl<'de> Visitor<'de> for UnitVisitor {
+ type Value = ();
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("unit")
+ }
+
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(())
+ }
+}
+
+impl<'de> Deserialize<'de> for () {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_unit(UnitVisitor)
+ }
+}
+
+#[cfg(feature = "unstable")]
+impl<'de> Deserialize<'de> for ! {
+ fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Err(Error::custom("cannot deserialize `!`"))
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+struct BoolVisitor;
+
+impl<'de> Visitor<'de> for BoolVisitor {
+ type Value = bool;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a boolean")
+ }
+
+ fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(v)
+ }
+}
+
+impl<'de> Deserialize<'de> for bool {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_bool(BoolVisitor)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! impl_deserialize_num {
+ ($primitive:ident, $nonzero:ident $(cfg($($cfg:tt)*))*, $deserialize:ident $($method:ident!($($val:ident : $visit:ident)*);)*) => {
+ impl_deserialize_num!($primitive, $deserialize $($method!($($val : $visit)*);)*);
+
+ #[cfg(all(not(no_num_nonzero), $($($cfg)*)*))]
+ impl<'de> Deserialize<'de> for num::$nonzero {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct NonZeroVisitor;
+
+ impl<'de> Visitor<'de> for NonZeroVisitor {
+ type Value = num::$nonzero;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(concat!("a nonzero ", stringify!($primitive)))
+ }
+
+ $($($method!(nonzero $primitive $val : $visit);)*)*
+ }
+
+ deserializer.$deserialize(NonZeroVisitor)
+ }
+ }
+ };
+
+ ($primitive:ident, $deserialize:ident $($method:ident!($($val:ident : $visit:ident)*);)*) => {
+ impl<'de> Deserialize<'de> for $primitive {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct PrimitiveVisitor;
+
+ impl<'de> Visitor<'de> for PrimitiveVisitor {
+ type Value = $primitive;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(stringify!($primitive))
+ }
+
+ $($($method!($val : $visit);)*)*
+ }
+
+ deserializer.$deserialize(PrimitiveVisitor)
+ }
+ }
+ };
+}
+
+macro_rules! num_self {
+ ($ty:ident : $visit:ident) => {
+ #[inline]
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(v)
+ }
+ };
+
+ (nonzero $primitive:ident $ty:ident : $visit:ident) => {
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ if let Some(nonzero) = Self::Value::new(v) {
+ Ok(nonzero)
+ } else {
+ Err(Error::invalid_value(Unexpected::Unsigned(0), &self))
+ }
+ }
+ };
+}
+
+macro_rules! num_as_self {
+ ($ty:ident : $visit:ident) => {
+ #[inline]
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(v as Self::Value)
+ }
+ };
+
+ (nonzero $primitive:ident $ty:ident : $visit:ident) => {
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ if let Some(nonzero) = Self::Value::new(v as $primitive) {
+ Ok(nonzero)
+ } else {
+ Err(Error::invalid_value(Unexpected::Unsigned(0), &self))
+ }
+ }
+ };
+}
+
+macro_rules! int_to_int {
+ ($ty:ident : $visit:ident) => {
+ #[inline]
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ if Self::Value::min_value() as i64 <= v as i64
+ && v as i64 <= Self::Value::max_value() as i64
+ {
+ Ok(v as Self::Value)
+ } else {
+ Err(Error::invalid_value(Unexpected::Signed(v as i64), &self))
+ }
+ }
+ };
+
+ (nonzero $primitive:ident $ty:ident : $visit:ident) => {
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ if $primitive::min_value() as i64 <= v as i64
+ && v as i64 <= $primitive::max_value() as i64
+ {
+ if let Some(nonzero) = Self::Value::new(v as $primitive) {
+ return Ok(nonzero);
+ }
+ }
+ Err(Error::invalid_value(Unexpected::Signed(v as i64), &self))
+ }
+ };
+}
+
+macro_rules! int_to_uint {
+ ($ty:ident : $visit:ident) => {
+ #[inline]
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ if 0 <= v && v as u64 <= Self::Value::max_value() as u64 {
+ Ok(v as Self::Value)
+ } else {
+ Err(Error::invalid_value(Unexpected::Signed(v as i64), &self))
+ }
+ }
+ };
+
+ (nonzero $primitive:ident $ty:ident : $visit:ident) => {
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ if 0 < v && v as u64 <= $primitive::max_value() as u64 {
+ if let Some(nonzero) = Self::Value::new(v as $primitive) {
+ return Ok(nonzero);
+ }
+ }
+ Err(Error::invalid_value(Unexpected::Signed(v as i64), &self))
+ }
+ };
+}
+
+macro_rules! uint_to_self {
+ ($ty:ident : $visit:ident) => {
+ #[inline]
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ if v as u64 <= Self::Value::max_value() as u64 {
+ Ok(v as Self::Value)
+ } else {
+ Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self))
+ }
+ }
+ };
+
+ (nonzero $primitive:ident $ty:ident : $visit:ident) => {
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ if v as u64 <= $primitive::max_value() as u64 {
+ if let Some(nonzero) = Self::Value::new(v as $primitive) {
+ return Ok(nonzero);
+ }
+ }
+ Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self))
+ }
+ };
+}
+
+impl_deserialize_num! {
+ i8, NonZeroI8 cfg(not(no_num_nonzero_signed)), deserialize_i8
+ num_self!(i8:visit_i8);
+ int_to_int!(i16:visit_i16 i32:visit_i32 i64:visit_i64);
+ uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
+}
+
+impl_deserialize_num! {
+ i16, NonZeroI16 cfg(not(no_num_nonzero_signed)), deserialize_i16
+ num_self!(i16:visit_i16);
+ num_as_self!(i8:visit_i8);
+ int_to_int!(i32:visit_i32 i64:visit_i64);
+ uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
+}
+
+impl_deserialize_num! {
+ i32, NonZeroI32 cfg(not(no_num_nonzero_signed)), deserialize_i32
+ num_self!(i32:visit_i32);
+ num_as_self!(i8:visit_i8 i16:visit_i16);
+ int_to_int!(i64:visit_i64);
+ uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
+}
+
+impl_deserialize_num! {
+ i64, NonZeroI64 cfg(not(no_num_nonzero_signed)), deserialize_i64
+ num_self!(i64:visit_i64);
+ num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32);
+ uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
+}
+
+impl_deserialize_num! {
+ isize, NonZeroIsize cfg(not(no_num_nonzero_signed)), deserialize_i64
+ num_as_self!(i8:visit_i8 i16:visit_i16);
+ int_to_int!(i32:visit_i32 i64:visit_i64);
+ uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
+}
+
+impl_deserialize_num! {
+ u8, NonZeroU8, deserialize_u8
+ num_self!(u8:visit_u8);
+ int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
+ uint_to_self!(u16:visit_u16 u32:visit_u32 u64:visit_u64);
+}
+
+impl_deserialize_num! {
+ u16, NonZeroU16, deserialize_u16
+ num_self!(u16:visit_u16);
+ num_as_self!(u8:visit_u8);
+ int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
+ uint_to_self!(u32:visit_u32 u64:visit_u64);
+}
+
+impl_deserialize_num! {
+ u32, NonZeroU32, deserialize_u32
+ num_self!(u32:visit_u32);
+ num_as_self!(u8:visit_u8 u16:visit_u16);
+ int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
+ uint_to_self!(u64:visit_u64);
+}
+
+impl_deserialize_num! {
+ u64, NonZeroU64, deserialize_u64
+ num_self!(u64:visit_u64);
+ num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32);
+ int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
+}
+
+impl_deserialize_num! {
+ usize, NonZeroUsize, deserialize_u64
+ num_as_self!(u8:visit_u8 u16:visit_u16);
+ int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
+ uint_to_self!(u32:visit_u32 u64:visit_u64);
+}
+
+impl_deserialize_num! {
+ f32, deserialize_f32
+ num_self!(f32:visit_f32);
+ num_as_self!(f64:visit_f64);
+ num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
+ num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
+}
+
+impl_deserialize_num! {
+ f64, deserialize_f64
+ num_self!(f64:visit_f64);
+ num_as_self!(f32:visit_f32);
+ num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
+ num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
+}
+
+serde_if_integer128! {
+ macro_rules! num_128 {
+ ($ty:ident : $visit:ident) => {
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ if v as i128 >= Self::Value::min_value() as i128
+ && v as u128 <= Self::Value::max_value() as u128
+ {
+ Ok(v as Self::Value)
+ } else {
+ Err(Error::invalid_value(
+ Unexpected::Other(stringify!($ty)),
+ &self,
+ ))
+ }
+ }
+ };
+
+ (nonzero $primitive:ident $ty:ident : $visit:ident) => {
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ if v as i128 >= $primitive::min_value() as i128
+ && v as u128 <= $primitive::max_value() as u128
+ {
+ if let Some(nonzero) = Self::Value::new(v as $primitive) {
+ Ok(nonzero)
+ } else {
+ Err(Error::invalid_value(Unexpected::Unsigned(0), &self))
+ }
+ } else {
+ Err(Error::invalid_value(
+ Unexpected::Other(stringify!($ty)),
+ &self,
+ ))
+ }
+ }
+ };
+ }
+
+ impl_deserialize_num! {
+ i128, NonZeroI128 cfg(not(no_num_nonzero_signed)), deserialize_i128
+ num_self!(i128:visit_i128);
+ num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
+ num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
+ num_128!(u128:visit_u128);
+ }
+
+ impl_deserialize_num! {
+ u128, NonZeroU128, deserialize_u128
+ num_self!(u128:visit_u128);
+ num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
+ int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
+ num_128!(i128:visit_i128);
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+struct CharVisitor;
+
+impl<'de> Visitor<'de> for CharVisitor {
+ type Value = char;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a character")
+ }
+
+ #[inline]
+ fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(v)
+ }
+
+ #[inline]
+ fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ let mut iter = v.chars();
+ match (iter.next(), iter.next()) {
+ (Some(c), None) => Ok(c),
+ _ => Err(Error::invalid_value(Unexpected::Str(v), &self)),
+ }
+ }
+}
+
+impl<'de> Deserialize<'de> for char {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_char(CharVisitor)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+struct StringVisitor;
+#[cfg(any(feature = "std", feature = "alloc"))]
+struct StringInPlaceVisitor<'a>(&'a mut String);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de> Visitor<'de> for StringVisitor {
+ type Value = String;
+
+ 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(v.to_owned())
+ }
+
+ fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(v)
+ }
+
+ fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match str::from_utf8(v) {
+ Ok(s) => Ok(s.to_owned()),
+ 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(s),
+ Err(e) => Err(Error::invalid_value(
+ Unexpected::Bytes(&e.into_bytes()),
+ &self,
+ )),
+ }
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, 'de> Visitor<'de> for StringInPlaceVisitor<'a> {
+ type Value = ();
+
+ 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,
+ {
+ self.0.clear();
+ self.0.push_str(v);
+ Ok(())
+ }
+
+ fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ *self.0 = v;
+ Ok(())
+ }
+
+ fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match str::from_utf8(v) {
+ Ok(s) => {
+ self.0.clear();
+ self.0.push_str(s);
+ Ok(())
+ }
+ 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) => {
+ *self.0 = s;
+ Ok(())
+ }
+ Err(e) => Err(Error::invalid_value(
+ Unexpected::Bytes(&e.into_bytes()),
+ &self,
+ )),
+ }
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de> Deserialize<'de> for String {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_string(StringVisitor)
+ }
+
+ fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_string(StringInPlaceVisitor(place))
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+struct StrVisitor;
+
+impl<'a> Visitor<'a> for StrVisitor {
+ type Value = &'a str;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a borrowed string")
+ }
+
+ fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(v) // so easy
+ }
+
+ fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ str::from_utf8(v).map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
+ }
+}
+
+impl<'de: 'a, 'a> Deserialize<'de> for &'a str {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_str(StrVisitor)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+struct BytesVisitor;
+
+impl<'a> Visitor<'a> for BytesVisitor {
+ type Value = &'a [u8];
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a borrowed byte array")
+ }
+
+ fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(v)
+ }
+
+ fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(v.as_bytes())
+ }
+}
+
+impl<'de: 'a, 'a> Deserialize<'de> for &'a [u8] {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_bytes(BytesVisitor)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "std")]
+struct CStringVisitor;
+
+#[cfg(feature = "std")]
+impl<'de> Visitor<'de> for CStringVisitor {
+ type Value = CString;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("byte array")
+ }
+
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ let len = size_hint::cautious(seq.size_hint());
+ let mut values = Vec::with_capacity(len);
+
+ while let Some(value) = try!(seq.next_element()) {
+ values.push(value);
+ }
+
+ CString::new(values).map_err(Error::custom)
+ }
+
+ fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ CString::new(v).map_err(Error::custom)
+ }
+
+ fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ CString::new(v).map_err(Error::custom)
+ }
+
+ fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ CString::new(v).map_err(Error::custom)
+ }
+
+ fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ CString::new(v).map_err(Error::custom)
+ }
+}
+
+#[cfg(feature = "std")]
+impl<'de> Deserialize<'de> for CString {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_byte_buf(CStringVisitor)
+ }
+}
+
+macro_rules! forwarded_impl {
+ (
+ $(#[doc = $doc:tt])*
+ ($($id:ident),*), $ty:ty, $func:expr
+ ) => {
+ $(#[doc = $doc])*
+ impl<'de $(, $id : Deserialize<'de>,)*> Deserialize<'de> for $ty {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Deserialize::deserialize(deserializer).map($func)
+ }
+ }
+ }
+}
+
+#[cfg(all(feature = "std", not(no_de_boxed_c_str)))]
+forwarded_impl!((), Box<CStr>, CString::into_boxed_c_str);
+
+#[cfg(not(no_core_reverse))]
+forwarded_impl!((T), Reverse<T>, Reverse);
+
+////////////////////////////////////////////////////////////////////////////////
+
+struct OptionVisitor<T> {
+ marker: PhantomData<T>,
+}
+
+impl<'de, T> Visitor<'de> for OptionVisitor<T>
+where
+ T: Deserialize<'de>,
+{
+ type Value = Option<T>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("option")
+ }
+
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(None)
+ }
+
+ #[inline]
+ fn visit_none<E>(self) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(None)
+ }
+
+ #[inline]
+ fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ T::deserialize(deserializer).map(Some)
+ }
+
+ fn __private_visit_untagged_option<D>(self, deserializer: D) -> Result<Self::Value, ()>
+ where
+ D: Deserializer<'de>,
+ {
+ Ok(T::deserialize(deserializer).ok())
+ }
+}
+
+impl<'de, T> Deserialize<'de> for Option<T>
+where
+ T: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_option(OptionVisitor {
+ marker: PhantomData,
+ })
+ }
+
+ // The Some variant's repr is opaque, so we can't play cute tricks with its
+ // tag to have deserialize_in_place build the content in place unconditionally.
+ //
+ // FIXME: investigate whether branching on the old value being Some to
+ // deserialize_in_place the value is profitable (probably data-dependent?)
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+struct PhantomDataVisitor<T: ?Sized> {
+ marker: PhantomData<T>,
+}
+
+impl<'de, T: ?Sized> Visitor<'de> for PhantomDataVisitor<T> {
+ type Value = PhantomData<T>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("unit")
+ }
+
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(PhantomData)
+ }
+}
+
+impl<'de, T: ?Sized> Deserialize<'de> for PhantomData<T> {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let visitor = PhantomDataVisitor {
+ marker: PhantomData,
+ };
+ deserializer.deserialize_unit_struct("PhantomData", visitor)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+macro_rules! seq_impl {
+ (
+ $ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
+ $access:ident,
+ $clear:expr,
+ $with_capacity:expr,
+ $reserve:expr,
+ $insert:expr
+ ) => {
+ impl<'de, T $(, $typaram)*> Deserialize<'de> for $ty<T $(, $typaram)*>
+ where
+ T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*,
+ $($typaram: $bound1 $(+ $bound2)*,)*
+ {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct SeqVisitor<T $(, $typaram)*> {
+ marker: PhantomData<$ty<T $(, $typaram)*>>,
+ }
+
+ impl<'de, T $(, $typaram)*> Visitor<'de> for SeqVisitor<T $(, $typaram)*>
+ where
+ T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*,
+ $($typaram: $bound1 $(+ $bound2)*,)*
+ {
+ type Value = $ty<T $(, $typaram)*>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a sequence")
+ }
+
+ #[inline]
+ fn visit_seq<A>(self, mut $access: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ let mut values = $with_capacity;
+
+ while let Some(value) = try!($access.next_element()) {
+ $insert(&mut values, value);
+ }
+
+ Ok(values)
+ }
+ }
+
+ let visitor = SeqVisitor { marker: PhantomData };
+ deserializer.deserialize_seq(visitor)
+ }
+
+ fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct SeqInPlaceVisitor<'a, T: 'a $(, $typaram: 'a)*>(&'a mut $ty<T $(, $typaram)*>);
+
+ impl<'a, 'de, T $(, $typaram)*> Visitor<'de> for SeqInPlaceVisitor<'a, T $(, $typaram)*>
+ where
+ T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*,
+ $($typaram: $bound1 $(+ $bound2)*,)*
+ {
+ type Value = ();
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a sequence")
+ }
+
+ #[inline]
+ fn visit_seq<A>(mut self, mut $access: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ $clear(&mut self.0);
+ $reserve(&mut self.0, size_hint::cautious($access.size_hint()));
+
+ // FIXME: try to overwrite old values here? (Vec, VecDeque, LinkedList)
+ while let Some(value) = try!($access.next_element()) {
+ $insert(&mut self.0, value);
+ }
+
+ Ok(())
+ }
+ }
+
+ deserializer.deserialize_seq(SeqInPlaceVisitor(place))
+ }
+ }
+ }
+}
+
+// Dummy impl of reserve
+#[cfg(any(feature = "std", feature = "alloc"))]
+fn nop_reserve<T>(_seq: T, _n: usize) {}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+seq_impl!(
+ BinaryHeap<T: Ord>,
+ seq,
+ BinaryHeap::clear,
+ BinaryHeap::with_capacity(size_hint::cautious(seq.size_hint())),
+ BinaryHeap::reserve,
+ BinaryHeap::push
+);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+seq_impl!(
+ BTreeSet<T: Eq + Ord>,
+ seq,
+ BTreeSet::clear,
+ BTreeSet::new(),
+ nop_reserve,
+ BTreeSet::insert
+);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+seq_impl!(
+ LinkedList<T>,
+ seq,
+ LinkedList::clear,
+ LinkedList::new(),
+ nop_reserve,
+ LinkedList::push_back
+);
+
+#[cfg(feature = "std")]
+seq_impl!(
+ HashSet<T: Eq + Hash, S: BuildHasher + Default>,
+ seq,
+ HashSet::clear,
+ HashSet::with_capacity_and_hasher(size_hint::cautious(seq.size_hint()), S::default()),
+ HashSet::reserve,
+ HashSet::insert);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+seq_impl!(
+ VecDeque<T>,
+ seq,
+ VecDeque::clear,
+ VecDeque::with_capacity(size_hint::cautious(seq.size_hint())),
+ VecDeque::reserve,
+ VecDeque::push_back
+);
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, T> Deserialize<'de> for Vec<T>
+where
+ T: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct VecVisitor<T> {
+ marker: PhantomData<T>,
+ }
+
+ impl<'de, T> Visitor<'de> for VecVisitor<T>
+ where
+ T: Deserialize<'de>,
+ {
+ type Value = Vec<T>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a sequence")
+ }
+
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ let mut values = Vec::with_capacity(size_hint::cautious(seq.size_hint()));
+
+ while let Some(value) = try!(seq.next_element()) {
+ values.push(value);
+ }
+
+ Ok(values)
+ }
+ }
+
+ let visitor = VecVisitor {
+ marker: PhantomData,
+ };
+ deserializer.deserialize_seq(visitor)
+ }
+
+ fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct VecInPlaceVisitor<'a, T: 'a>(&'a mut Vec<T>);
+
+ impl<'a, 'de, T> Visitor<'de> for VecInPlaceVisitor<'a, T>
+ where
+ T: Deserialize<'de>,
+ {
+ type Value = ();
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a sequence")
+ }
+
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ let hint = size_hint::cautious(seq.size_hint());
+ if let Some(additional) = hint.checked_sub(self.0.len()) {
+ self.0.reserve(additional);
+ }
+
+ for i in 0..self.0.len() {
+ let next = {
+ let next_place = InPlaceSeed(&mut self.0[i]);
+ try!(seq.next_element_seed(next_place))
+ };
+ if next.is_none() {
+ self.0.truncate(i);
+ return Ok(());
+ }
+ }
+
+ while let Some(value) = try!(seq.next_element()) {
+ self.0.push(value);
+ }
+
+ Ok(())
+ }
+ }
+
+ deserializer.deserialize_seq(VecInPlaceVisitor(place))
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+struct ArrayVisitor<A> {
+ marker: PhantomData<A>,
+}
+struct ArrayInPlaceVisitor<'a, A: 'a>(&'a mut A);
+
+impl<A> ArrayVisitor<A> {
+ fn new() -> Self {
+ ArrayVisitor {
+ marker: PhantomData,
+ }
+ }
+}
+
+impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 0]> {
+ type Value = [T; 0];
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("an empty array")
+ }
+
+ #[inline]
+ fn visit_seq<A>(self, _: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ Ok([])
+ }
+}
+
+// Does not require T: Deserialize<'de>.
+impl<'de, T> Deserialize<'de> for [T; 0] {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_tuple(0, ArrayVisitor::<[T; 0]>::new())
+ }
+}
+
+macro_rules! array_impls {
+ ($($len:expr => ($($n:tt)+))+) => {
+ $(
+ impl<'de, T> Visitor<'de> for ArrayVisitor<[T; $len]>
+ where
+ T: Deserialize<'de>,
+ {
+ type Value = [T; $len];
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(concat!("an array of length ", $len))
+ }
+
+ #[inline]
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ Ok([$(
+ match try!(seq.next_element()) {
+ Some(val) => val,
+ None => return Err(Error::invalid_length($n, &self)),
+ }
+ ),+])
+ }
+ }
+
+ impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; $len]>
+ where
+ T: Deserialize<'de>,
+ {
+ type Value = ();
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(concat!("an array of length ", $len))
+ }
+
+ #[inline]
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ let mut fail_idx = None;
+ for (idx, dest) in self.0[..].iter_mut().enumerate() {
+ if try!(seq.next_element_seed(InPlaceSeed(dest))).is_none() {
+ fail_idx = Some(idx);
+ break;
+ }
+ }
+ if let Some(idx) = fail_idx {
+ return Err(Error::invalid_length(idx, &self));
+ }
+ Ok(())
+ }
+ }
+
+ impl<'de, T> Deserialize<'de> for [T; $len]
+ where
+ T: Deserialize<'de>,
+ {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_tuple($len, ArrayVisitor::<[T; $len]>::new())
+ }
+
+ fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_tuple($len, ArrayInPlaceVisitor(place))
+ }
+ }
+ )+
+ }
+}
+
+array_impls! {
+ 1 => (0)
+ 2 => (0 1)
+ 3 => (0 1 2)
+ 4 => (0 1 2 3)
+ 5 => (0 1 2 3 4)
+ 6 => (0 1 2 3 4 5)
+ 7 => (0 1 2 3 4 5 6)
+ 8 => (0 1 2 3 4 5 6 7)
+ 9 => (0 1 2 3 4 5 6 7 8)
+ 10 => (0 1 2 3 4 5 6 7 8 9)
+ 11 => (0 1 2 3 4 5 6 7 8 9 10)
+ 12 => (0 1 2 3 4 5 6 7 8 9 10 11)
+ 13 => (0 1 2 3 4 5 6 7 8 9 10 11 12)
+ 14 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13)
+ 15 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14)
+ 16 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
+ 17 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)
+ 18 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17)
+ 19 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18)
+ 20 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)
+ 21 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
+ 22 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21)
+ 23 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22)
+ 24 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23)
+ 25 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24)
+ 26 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25)
+ 27 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26)
+ 28 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27)
+ 29 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28)
+ 30 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29)
+ 31 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30)
+ 32 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31)
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! tuple_impls {
+ ($($len:tt => ($($n:tt $name:ident)+))+) => {
+ $(
+ impl<'de, $($name: Deserialize<'de>),+> Deserialize<'de> for ($($name,)+) {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct TupleVisitor<$($name,)+> {
+ marker: PhantomData<($($name,)+)>,
+ }
+
+ impl<'de, $($name: Deserialize<'de>),+> Visitor<'de> for TupleVisitor<$($name,)+> {
+ type Value = ($($name,)+);
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(concat!("a tuple of size ", $len))
+ }
+
+ #[inline]
+ #[allow(non_snake_case)]
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ $(
+ let $name = match try!(seq.next_element()) {
+ Some(value) => value,
+ None => return Err(Error::invalid_length($n, &self)),
+ };
+ )+
+
+ Ok(($($name,)+))
+ }
+ }
+
+ deserializer.deserialize_tuple($len, TupleVisitor { marker: PhantomData })
+ }
+
+ #[inline]
+ fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct TupleInPlaceVisitor<'a, $($name: 'a,)+>(&'a mut ($($name,)+));
+
+ impl<'a, 'de, $($name: Deserialize<'de>),+> Visitor<'de> for TupleInPlaceVisitor<'a, $($name,)+> {
+ type Value = ();
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(concat!("a tuple of size ", $len))
+ }
+
+ #[inline]
+ #[allow(non_snake_case)]
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ $(
+ if try!(seq.next_element_seed(InPlaceSeed(&mut (self.0).$n))).is_none() {
+ return Err(Error::invalid_length($n, &self));
+ }
+ )+
+
+ Ok(())
+ }
+ }
+
+ deserializer.deserialize_tuple($len, TupleInPlaceVisitor(place))
+ }
+ }
+ )+
+ }
+}
+
+tuple_impls! {
+ 1 => (0 T0)
+ 2 => (0 T0 1 T1)
+ 3 => (0 T0 1 T1 2 T2)
+ 4 => (0 T0 1 T1 2 T2 3 T3)
+ 5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
+ 6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
+ 7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
+ 8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
+ 9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
+ 10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
+ 11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
+ 12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
+ 13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
+ 14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
+ 15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
+ 16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+macro_rules! map_impl {
+ (
+ $ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
+ $access:ident,
+ $with_capacity:expr
+ ) => {
+ impl<'de, K, V $(, $typaram)*> Deserialize<'de> for $ty<K, V $(, $typaram)*>
+ where
+ K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*,
+ V: Deserialize<'de>,
+ $($typaram: $bound1 $(+ $bound2)*),*
+ {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct MapVisitor<K, V $(, $typaram)*> {
+ marker: PhantomData<$ty<K, V $(, $typaram)*>>,
+ }
+
+ impl<'de, K, V $(, $typaram)*> Visitor<'de> for MapVisitor<K, V $(, $typaram)*>
+ where
+ K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*,
+ V: Deserialize<'de>,
+ $($typaram: $bound1 $(+ $bound2)*),*
+ {
+ type Value = $ty<K, V $(, $typaram)*>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a map")
+ }
+
+ #[inline]
+ fn visit_map<A>(self, mut $access: A) -> Result<Self::Value, A::Error>
+ where
+ A: MapAccess<'de>,
+ {
+ let mut values = $with_capacity;
+
+ while let Some((key, value)) = try!($access.next_entry()) {
+ values.insert(key, value);
+ }
+
+ Ok(values)
+ }
+ }
+
+ let visitor = MapVisitor { marker: PhantomData };
+ deserializer.deserialize_map(visitor)
+ }
+ }
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+map_impl!(
+ BTreeMap<K: Ord, V>,
+ map,
+ BTreeMap::new());
+
+#[cfg(feature = "std")]
+map_impl!(
+ HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
+ map,
+ HashMap::with_capacity_and_hasher(size_hint::cautious(map.size_hint()), S::default()));
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "std")]
+macro_rules! parse_ip_impl {
+ ($expecting:tt $ty:ty; $size:tt) => {
+ impl<'de> Deserialize<'de> for $ty {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ if deserializer.is_human_readable() {
+ deserializer.deserialize_str(FromStrVisitor::new($expecting))
+ } else {
+ <[u8; $size]>::deserialize(deserializer).map(<$ty>::from)
+ }
+ }
+ }
+ };
+}
+
+#[cfg(feature = "std")]
+macro_rules! variant_identifier {
+ (
+ $name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*)
+ $expecting_message:expr,
+ $variants_name:ident
+ ) => {
+ enum $name_kind {
+ $($variant),*
+ }
+
+ static $variants_name: &'static [&'static str] = &[$(stringify!($variant)),*];
+
+ impl<'de> Deserialize<'de> for $name_kind {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct KindVisitor;
+
+ impl<'de> Visitor<'de> for KindVisitor {
+ type Value = $name_kind;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str($expecting_message)
+ }
+
+ fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ $(
+ $index => Ok($name_kind :: $variant),
+ )*
+ _ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self),),
+ }
+ }
+
+ fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ $(
+ stringify!($variant) => Ok($name_kind :: $variant),
+ )*
+ _ => Err(Error::unknown_variant(value, $variants_name)),
+ }
+ }
+
+ fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ $(
+ $bytes => Ok($name_kind :: $variant),
+ )*
+ _ => {
+ match str::from_utf8(value) {
+ Ok(value) => Err(Error::unknown_variant(value, $variants_name)),
+ Err(_) => Err(Error::invalid_value(Unexpected::Bytes(value), &self)),
+ }
+ }
+ }
+ }
+ }
+
+ deserializer.deserialize_identifier(KindVisitor)
+ }
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+macro_rules! deserialize_enum {
+ (
+ $name:ident $name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*)
+ $expecting_message:expr,
+ $deserializer:expr
+ ) => {
+ variant_identifier! {
+ $name_kind ($($variant; $bytes; $index),*)
+ $expecting_message,
+ VARIANTS
+ }
+
+ struct EnumVisitor;
+ impl<'de> Visitor<'de> for EnumVisitor {
+ type Value = $name;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(concat!("a ", stringify!($name)))
+ }
+
+
+ fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ match try!(data.variant()) {
+ $(
+ ($name_kind :: $variant, v) => v.newtype_variant().map($name :: $variant),
+ )*
+ }
+ }
+ }
+ $deserializer.deserialize_enum(stringify!($name), VARIANTS, EnumVisitor)
+ }
+}
+
+#[cfg(feature = "std")]
+impl<'de> Deserialize<'de> for net::IpAddr {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ if deserializer.is_human_readable() {
+ deserializer.deserialize_str(FromStrVisitor::new("IP address"))
+ } else {
+ use lib::net::IpAddr;
+ deserialize_enum! {
+ IpAddr IpAddrKind (V4; b"V4"; 0, V6; b"V6"; 1)
+ "`V4` or `V6`",
+ deserializer
+ }
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+parse_ip_impl!("IPv4 address" net::Ipv4Addr; 4);
+
+#[cfg(feature = "std")]
+parse_ip_impl!("IPv6 address" net::Ipv6Addr; 16);
+
+#[cfg(feature = "std")]
+macro_rules! parse_socket_impl {
+ ($expecting:tt $ty:ty, $new:expr) => {
+ impl<'de> Deserialize<'de> for $ty {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ if deserializer.is_human_readable() {
+ deserializer.deserialize_str(FromStrVisitor::new($expecting))
+ } else {
+ <(_, u16)>::deserialize(deserializer).map(|(ip, port)| $new(ip, port))
+ }
+ }
+ }
+ };
+}
+
+#[cfg(feature = "std")]
+impl<'de> Deserialize<'de> for net::SocketAddr {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ if deserializer.is_human_readable() {
+ deserializer.deserialize_str(FromStrVisitor::new("socket address"))
+ } else {
+ use lib::net::SocketAddr;
+ deserialize_enum! {
+ SocketAddr SocketAddrKind (V4; b"V4"; 0, V6; b"V6"; 1)
+ "`V4` or `V6`",
+ deserializer
+ }
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+parse_socket_impl!("IPv4 socket address" net::SocketAddrV4, net::SocketAddrV4::new);
+
+#[cfg(feature = "std")]
+parse_socket_impl!("IPv6 socket address" net::SocketAddrV6, |ip, port| net::SocketAddrV6::new(
+ ip, port, 0, 0
+));
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "std")]
+struct PathVisitor;
+
+#[cfg(feature = "std")]
+impl<'a> Visitor<'a> for PathVisitor {
+ type Value = &'a Path;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a borrowed path")
+ }
+
+ fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(v.as_ref())
+ }
+
+ fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ str::from_utf8(v)
+ .map(AsRef::as_ref)
+ .map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
+ }
+}
+
+#[cfg(feature = "std")]
+impl<'de: 'a, 'a> Deserialize<'de> for &'a Path {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_str(PathVisitor)
+ }
+}
+
+#[cfg(feature = "std")]
+struct PathBufVisitor;
+
+#[cfg(feature = "std")]
+impl<'de> Visitor<'de> for PathBufVisitor {
+ type Value = PathBuf;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("path string")
+ }
+
+ fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(From::from(v))
+ }
+
+ fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Ok(From::from(v))
+ }
+
+ fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ str::from_utf8(v)
+ .map(From::from)
+ .map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
+ }
+
+ fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ String::from_utf8(v)
+ .map(From::from)
+ .map_err(|e| Error::invalid_value(Unexpected::Bytes(&e.into_bytes()), &self))
+ }
+}
+
+#[cfg(feature = "std")]
+impl<'de> Deserialize<'de> for PathBuf {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_string(PathBufVisitor)
+ }
+}
+
+#[cfg(all(feature = "std", not(no_de_boxed_path)))]
+forwarded_impl!((), Box<Path>, PathBuf::into_boxed_path);
+
+////////////////////////////////////////////////////////////////////////////////
+
+// If this were outside of the serde crate, it would just use:
+//
+// #[derive(Deserialize)]
+// #[serde(variant_identifier)]
+#[cfg(all(feature = "std", any(unix, windows)))]
+variant_identifier! {
+ OsStringKind (Unix; b"Unix"; 0, Windows; b"Windows"; 1)
+ "`Unix` or `Windows`",
+ OSSTR_VARIANTS
+}
+
+#[cfg(all(feature = "std", any(unix, windows)))]
+struct OsStringVisitor;
+
+#[cfg(all(feature = "std", any(unix, windows)))]
+impl<'de> Visitor<'de> for OsStringVisitor {
+ type Value = OsString;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("os string")
+ }
+
+ #[cfg(unix)]
+ fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ use std::os::unix::ffi::OsStringExt;
+
+ match try!(data.variant()) {
+ (OsStringKind::Unix, v) => v.newtype_variant().map(OsString::from_vec),
+ (OsStringKind::Windows, _) => Err(Error::custom(
+ "cannot deserialize Windows OS string on Unix",
+ )),
+ }
+ }
+
+ #[cfg(windows)]
+ fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ use std::os::windows::ffi::OsStringExt;
+
+ match try!(data.variant()) {
+ (OsStringKind::Windows, v) => v
+ .newtype_variant::<Vec<u16>>()
+ .map(|vec| OsString::from_wide(&vec)),
+ (OsStringKind::Unix, _) => Err(Error::custom(
+ "cannot deserialize Unix OS string on Windows",
+ )),
+ }
+ }
+}
+
+#[cfg(all(feature = "std", any(unix, windows)))]
+impl<'de> Deserialize<'de> for OsString {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_enum("OsString", OSSTR_VARIANTS, OsStringVisitor)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+forwarded_impl!((T), Box<T>, Box::new);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+forwarded_impl!((T), Box<[T]>, Vec::into_boxed_slice);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+forwarded_impl!((), Box<str>, String::into_boxed_str);
+
+#[cfg(all(no_de_rc_dst, feature = "rc", any(feature = "std", feature = "alloc")))]
+forwarded_impl! {
+ /// This impl requires the [`"rc"`] Cargo feature of Serde.
+ ///
+ /// Deserializing a data structure containing `Arc` will not attempt to
+ /// deduplicate `Arc` references to the same data. Every deserialized `Arc`
+ /// will end up with a strong count of 1.
+ ///
+ /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
+ (T), Arc<T>, Arc::new
+}
+
+#[cfg(all(no_de_rc_dst, feature = "rc", any(feature = "std", feature = "alloc")))]
+forwarded_impl! {
+ /// This impl requires the [`"rc"`] Cargo feature of Serde.
+ ///
+ /// Deserializing a data structure containing `Rc` will not attempt to
+ /// deduplicate `Rc` references to the same data. Every deserialized `Rc`
+ /// will end up with a strong count of 1.
+ ///
+ /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
+ (T), Rc<T>, Rc::new
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T>
+where
+ T: ToOwned,
+ T::Owned: Deserialize<'de>,
+{
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ T::Owned::deserialize(deserializer).map(Cow::Owned)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// This impl requires the [`"rc"`] Cargo feature of Serde. The resulting
+/// `Weak<T>` has a reference count of 0 and cannot be upgraded.
+///
+/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
+#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
+impl<'de, T: ?Sized> Deserialize<'de> for RcWeak<T>
+where
+ T: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ try!(Option::<T>::deserialize(deserializer));
+ Ok(RcWeak::new())
+ }
+}
+
+/// This impl requires the [`"rc"`] Cargo feature of Serde. The resulting
+/// `Weak<T>` has a reference count of 0 and cannot be upgraded.
+///
+/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
+#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
+impl<'de, T: ?Sized> Deserialize<'de> for ArcWeak<T>
+where
+ T: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ try!(Option::<T>::deserialize(deserializer));
+ Ok(ArcWeak::new())
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(all(
+ not(no_de_rc_dst),
+ feature = "rc",
+ any(feature = "std", feature = "alloc")
+))]
+macro_rules! box_forwarded_impl {
+ (
+ $(#[doc = $doc:tt])*
+ $t:ident
+ ) => {
+ $(#[doc = $doc])*
+ impl<'de, T: ?Sized> Deserialize<'de> for $t<T>
+ where
+ Box<T>: Deserialize<'de>,
+ {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Box::deserialize(deserializer).map(Into::into)
+ }
+ }
+ };
+}
+
+#[cfg(all(
+ not(no_de_rc_dst),
+ feature = "rc",
+ any(feature = "std", feature = "alloc")
+))]
+box_forwarded_impl! {
+ /// This impl requires the [`"rc"`] Cargo feature of Serde.
+ ///
+ /// Deserializing a data structure containing `Rc` will not attempt to
+ /// deduplicate `Rc` references to the same data. Every deserialized `Rc`
+ /// will end up with a strong count of 1.
+ ///
+ /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
+ Rc
+}
+
+#[cfg(all(
+ not(no_de_rc_dst),
+ feature = "rc",
+ any(feature = "std", feature = "alloc")
+))]
+box_forwarded_impl! {
+ /// This impl requires the [`"rc"`] Cargo feature of Serde.
+ ///
+ /// Deserializing a data structure containing `Arc` will not attempt to
+ /// deduplicate `Arc` references to the same data. Every deserialized `Arc`
+ /// will end up with a strong count of 1.
+ ///
+ /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
+ Arc
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<'de, T> Deserialize<'de> for Cell<T>
+where
+ T: Deserialize<'de> + Copy,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ T::deserialize(deserializer).map(Cell::new)
+ }
+}
+
+forwarded_impl!((T), RefCell<T>, RefCell::new);
+
+#[cfg(feature = "std")]
+forwarded_impl!((T), Mutex<T>, Mutex::new);
+
+#[cfg(feature = "std")]
+forwarded_impl!((T), RwLock<T>, RwLock::new);
+
+////////////////////////////////////////////////////////////////////////////////
+
+// This is a cleaned-up version of the impl generated by:
+//
+// #[derive(Deserialize)]
+// #[serde(deny_unknown_fields)]
+// struct Duration {
+// secs: u64,
+// nanos: u32,
+// }
+#[cfg(any(feature = "std", not(no_core_duration)))]
+impl<'de> Deserialize<'de> for Duration {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ // If this were outside of the serde crate, it would just use:
+ //
+ // #[derive(Deserialize)]
+ // #[serde(field_identifier, rename_all = "lowercase")]
+ enum Field {
+ Secs,
+ Nanos,
+ }
+
+ impl<'de> Deserialize<'de> for Field {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct FieldVisitor;
+
+ impl<'de> Visitor<'de> for FieldVisitor {
+ type Value = Field;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("`secs` or `nanos`")
+ }
+
+ fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ "secs" => Ok(Field::Secs),
+ "nanos" => Ok(Field::Nanos),
+ _ => Err(Error::unknown_field(value, FIELDS)),
+ }
+ }
+
+ fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ b"secs" => Ok(Field::Secs),
+ b"nanos" => Ok(Field::Nanos),
+ _ => {
+ let value = ::__private::from_utf8_lossy(value);
+ Err(Error::unknown_field(&*value, FIELDS))
+ }
+ }
+ }
+ }
+
+ deserializer.deserialize_identifier(FieldVisitor)
+ }
+ }
+
+ fn check_overflow<E>(secs: u64, nanos: u32) -> Result<(), E>
+ where
+ E: Error,
+ {
+ static NANOS_PER_SEC: u32 = 1_000_000_000;
+ match secs.checked_add((nanos / NANOS_PER_SEC) as u64) {
+ Some(_) => Ok(()),
+ None => Err(E::custom("overflow deserializing Duration")),
+ }
+ }
+
+ struct DurationVisitor;
+
+ impl<'de> Visitor<'de> for DurationVisitor {
+ type Value = Duration;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("struct Duration")
+ }
+
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ let secs: u64 = match try!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(0, &self));
+ }
+ };
+ let nanos: u32 = match try!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(1, &self));
+ }
+ };
+ try!(check_overflow(secs, nanos));
+ Ok(Duration::new(secs, nanos))
+ }
+
+ fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
+ where
+ A: MapAccess<'de>,
+ {
+ let mut secs: Option<u64> = None;
+ let mut nanos: Option<u32> = None;
+ while let Some(key) = try!(map.next_key()) {
+ match key {
+ Field::Secs => {
+ if secs.is_some() {
+ return Err(<A::Error as Error>::duplicate_field("secs"));
+ }
+ secs = Some(try!(map.next_value()));
+ }
+ Field::Nanos => {
+ if nanos.is_some() {
+ return Err(<A::Error as Error>::duplicate_field("nanos"));
+ }
+ nanos = Some(try!(map.next_value()));
+ }
+ }
+ }
+ let secs = match secs {
+ Some(secs) => secs,
+ None => return Err(<A::Error as Error>::missing_field("secs")),
+ };
+ let nanos = match nanos {
+ Some(nanos) => nanos,
+ None => return Err(<A::Error as Error>::missing_field("nanos")),
+ };
+ try!(check_overflow(secs, nanos));
+ Ok(Duration::new(secs, nanos))
+ }
+ }
+
+ const FIELDS: &'static [&'static str] = &["secs", "nanos"];
+ deserializer.deserialize_struct("Duration", FIELDS, DurationVisitor)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "std")]
+impl<'de> Deserialize<'de> for SystemTime {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ // Reuse duration
+ enum Field {
+ Secs,
+ Nanos,
+ }
+
+ impl<'de> Deserialize<'de> for Field {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct FieldVisitor;
+
+ impl<'de> Visitor<'de> for FieldVisitor {
+ type Value = Field;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("`secs_since_epoch` or `nanos_since_epoch`")
+ }
+
+ fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ "secs_since_epoch" => Ok(Field::Secs),
+ "nanos_since_epoch" => Ok(Field::Nanos),
+ _ => Err(Error::unknown_field(value, FIELDS)),
+ }
+ }
+
+ fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ b"secs_since_epoch" => Ok(Field::Secs),
+ b"nanos_since_epoch" => Ok(Field::Nanos),
+ _ => {
+ let value = String::from_utf8_lossy(value);
+ Err(Error::unknown_field(&value, FIELDS))
+ }
+ }
+ }
+ }
+
+ deserializer.deserialize_identifier(FieldVisitor)
+ }
+ }
+
+ fn check_overflow<E>(secs: u64, nanos: u32) -> Result<(), E>
+ where
+ E: Error,
+ {
+ static NANOS_PER_SEC: u32 = 1_000_000_000;
+ match secs.checked_add((nanos / NANOS_PER_SEC) as u64) {
+ Some(_) => Ok(()),
+ None => Err(E::custom("overflow deserializing SystemTime epoch offset")),
+ }
+ }
+
+ struct DurationVisitor;
+
+ impl<'de> Visitor<'de> for DurationVisitor {
+ type Value = Duration;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("struct SystemTime")
+ }
+
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ let secs: u64 = match try!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(0, &self));
+ }
+ };
+ let nanos: u32 = match try!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(1, &self));
+ }
+ };
+ try!(check_overflow(secs, nanos));
+ Ok(Duration::new(secs, nanos))
+ }
+
+ fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
+ where
+ A: MapAccess<'de>,
+ {
+ let mut secs: Option<u64> = None;
+ let mut nanos: Option<u32> = None;
+ while let Some(key) = try!(map.next_key()) {
+ match key {
+ Field::Secs => {
+ if secs.is_some() {
+ return Err(<A::Error as Error>::duplicate_field(
+ "secs_since_epoch",
+ ));
+ }
+ secs = Some(try!(map.next_value()));
+ }
+ Field::Nanos => {
+ if nanos.is_some() {
+ return Err(<A::Error as Error>::duplicate_field(
+ "nanos_since_epoch",
+ ));
+ }
+ nanos = Some(try!(map.next_value()));
+ }
+ }
+ }
+ let secs = match secs {
+ Some(secs) => secs,
+ None => return Err(<A::Error as Error>::missing_field("secs_since_epoch")),
+ };
+ let nanos = match nanos {
+ Some(nanos) => nanos,
+ None => return Err(<A::Error as Error>::missing_field("nanos_since_epoch")),
+ };
+ try!(check_overflow(secs, nanos));
+ Ok(Duration::new(secs, nanos))
+ }
+ }
+
+ const FIELDS: &'static [&'static str] = &["secs_since_epoch", "nanos_since_epoch"];
+ let duration = try!(deserializer.deserialize_struct("SystemTime", FIELDS, DurationVisitor));
+ #[cfg(not(no_systemtime_checked_add))]
+ let ret = UNIX_EPOCH
+ .checked_add(duration)
+ .ok_or_else(|| D::Error::custom("overflow deserializing SystemTime"));
+ #[cfg(no_systemtime_checked_add)]
+ let ret = Ok(UNIX_EPOCH + duration);
+ ret
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Similar to:
+//
+// #[derive(Deserialize)]
+// #[serde(deny_unknown_fields)]
+// struct Range {
+// start: u64,
+// end: u32,
+// }
+impl<'de, Idx> Deserialize<'de> for Range<Idx>
+where
+ Idx: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let (start, end) = try!(deserializer.deserialize_struct(
+ "Range",
+ range::FIELDS,
+ range::RangeVisitor {
+ expecting: "struct Range",
+ phantom: PhantomData,
+ },
+ ));
+ Ok(start..end)
+ }
+}
+
+#[cfg(not(no_range_inclusive))]
+impl<'de, Idx> Deserialize<'de> for RangeInclusive<Idx>
+where
+ Idx: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let (start, end) = try!(deserializer.deserialize_struct(
+ "RangeInclusive",
+ range::FIELDS,
+ range::RangeVisitor {
+ expecting: "struct RangeInclusive",
+ phantom: PhantomData,
+ },
+ ));
+ Ok(RangeInclusive::new(start, end))
+ }
+}
+
+mod range {
+ use lib::*;
+
+ use de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
+
+ pub const FIELDS: &'static [&'static str] = &["start", "end"];
+
+ // If this were outside of the serde crate, it would just use:
+ //
+ // #[derive(Deserialize)]
+ // #[serde(field_identifier, rename_all = "lowercase")]
+ enum Field {
+ Start,
+ End,
+ }
+
+ impl<'de> Deserialize<'de> for Field {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct FieldVisitor;
+
+ impl<'de> Visitor<'de> for FieldVisitor {
+ type Value = Field;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("`start` or `end`")
+ }
+
+ fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ "start" => Ok(Field::Start),
+ "end" => Ok(Field::End),
+ _ => Err(Error::unknown_field(value, FIELDS)),
+ }
+ }
+
+ fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ b"start" => Ok(Field::Start),
+ b"end" => Ok(Field::End),
+ _ => {
+ let value = ::__private::from_utf8_lossy(value);
+ Err(Error::unknown_field(&*value, FIELDS))
+ }
+ }
+ }
+ }
+
+ deserializer.deserialize_identifier(FieldVisitor)
+ }
+ }
+
+ pub struct RangeVisitor<Idx> {
+ pub expecting: &'static str,
+ pub phantom: PhantomData<Idx>,
+ }
+
+ impl<'de, Idx> Visitor<'de> for RangeVisitor<Idx>
+ where
+ Idx: Deserialize<'de>,
+ {
+ type Value = (Idx, Idx);
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(self.expecting)
+ }
+
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ let start: Idx = match try!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(0, &self));
+ }
+ };
+ let end: Idx = match try!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(1, &self));
+ }
+ };
+ Ok((start, end))
+ }
+
+ fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
+ where
+ A: MapAccess<'de>,
+ {
+ let mut start: Option<Idx> = None;
+ let mut end: Option<Idx> = None;
+ while let Some(key) = try!(map.next_key()) {
+ match key {
+ Field::Start => {
+ if start.is_some() {
+ return Err(<A::Error as Error>::duplicate_field("start"));
+ }
+ start = Some(try!(map.next_value()));
+ }
+ Field::End => {
+ if end.is_some() {
+ return Err(<A::Error as Error>::duplicate_field("end"));
+ }
+ end = Some(try!(map.next_value()));
+ }
+ }
+ }
+ let start = match start {
+ Some(start) => start,
+ None => return Err(<A::Error as Error>::missing_field("start")),
+ };
+ let end = match end {
+ Some(end) => end,
+ None => return Err(<A::Error as Error>::missing_field("end")),
+ };
+ Ok((start, end))
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(not(no_ops_bound), all(feature = "std", not(no_collections_bound))))]
+impl<'de, T> Deserialize<'de> for Bound<T>
+where
+ T: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ enum Field {
+ Unbounded,
+ Included,
+ Excluded,
+ }
+
+ impl<'de> Deserialize<'de> for Field {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct FieldVisitor;
+
+ impl<'de> Visitor<'de> for FieldVisitor {
+ type Value = Field;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("`Unbounded`, `Included` or `Excluded`")
+ }
+
+ fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ 0 => Ok(Field::Unbounded),
+ 1 => Ok(Field::Included),
+ 2 => Ok(Field::Excluded),
+ _ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)),
+ }
+ }
+
+ fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ "Unbounded" => Ok(Field::Unbounded),
+ "Included" => Ok(Field::Included),
+ "Excluded" => Ok(Field::Excluded),
+ _ => Err(Error::unknown_variant(value, VARIANTS)),
+ }
+ }
+
+ fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ b"Unbounded" => Ok(Field::Unbounded),
+ b"Included" => Ok(Field::Included),
+ b"Excluded" => Ok(Field::Excluded),
+ _ => match str::from_utf8(value) {
+ Ok(value) => Err(Error::unknown_variant(value, VARIANTS)),
+ Err(_) => {
+ Err(Error::invalid_value(Unexpected::Bytes(value), &self))
+ }
+ },
+ }
+ }
+ }
+
+ deserializer.deserialize_identifier(FieldVisitor)
+ }
+ }
+
+ struct BoundVisitor<T>(PhantomData<Bound<T>>);
+
+ impl<'de, T> Visitor<'de> for BoundVisitor<T>
+ where
+ T: Deserialize<'de>,
+ {
+ type Value = Bound<T>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("enum Bound")
+ }
+
+ fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ match try!(data.variant()) {
+ (Field::Unbounded, v) => v.unit_variant().map(|()| Bound::Unbounded),
+ (Field::Included, v) => v.newtype_variant().map(Bound::Included),
+ (Field::Excluded, v) => v.newtype_variant().map(Bound::Excluded),
+ }
+ }
+ }
+
+ const VARIANTS: &'static [&'static str] = &["Unbounded", "Included", "Excluded"];
+
+ deserializer.deserialize_enum("Bound", VARIANTS, BoundVisitor(PhantomData))
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<'de, T, E> Deserialize<'de> for Result<T, E>
+where
+ T: Deserialize<'de>,
+ E: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ // If this were outside of the serde crate, it would just use:
+ //
+ // #[derive(Deserialize)]
+ // #[serde(variant_identifier)]
+ enum Field {
+ Ok,
+ Err,
+ }
+
+ impl<'de> Deserialize<'de> for Field {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct FieldVisitor;
+
+ impl<'de> Visitor<'de> for FieldVisitor {
+ type Value = Field;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("`Ok` or `Err`")
+ }
+
+ fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ 0 => Ok(Field::Ok),
+ 1 => Ok(Field::Err),
+ _ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)),
+ }
+ }
+
+ fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ "Ok" => Ok(Field::Ok),
+ "Err" => Ok(Field::Err),
+ _ => Err(Error::unknown_variant(value, VARIANTS)),
+ }
+ }
+
+ fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ b"Ok" => Ok(Field::Ok),
+ b"Err" => Ok(Field::Err),
+ _ => match str::from_utf8(value) {
+ Ok(value) => Err(Error::unknown_variant(value, VARIANTS)),
+ Err(_) => {
+ Err(Error::invalid_value(Unexpected::Bytes(value), &self))
+ }
+ },
+ }
+ }
+ }
+
+ deserializer.deserialize_identifier(FieldVisitor)
+ }
+ }
+
+ struct ResultVisitor<T, E>(PhantomData<Result<T, E>>);
+
+ impl<'de, T, E> Visitor<'de> for ResultVisitor<T, E>
+ where
+ T: Deserialize<'de>,
+ E: Deserialize<'de>,
+ {
+ type Value = Result<T, E>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("enum Result")
+ }
+
+ fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ match try!(data.variant()) {
+ (Field::Ok, v) => v.newtype_variant().map(Ok),
+ (Field::Err, v) => v.newtype_variant().map(Err),
+ }
+ }
+ }
+
+ const VARIANTS: &'static [&'static str] = &["Ok", "Err"];
+
+ deserializer.deserialize_enum("Result", VARIANTS, ResultVisitor(PhantomData))
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<'de, T> Deserialize<'de> for Wrapping<T>
+where
+ T: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Deserialize::deserialize(deserializer).map(Wrapping)
+ }
+}
+
+#[cfg(all(feature = "std", not(no_std_atomic)))]
+macro_rules! atomic_impl {
+ ($($ty:ident $size:expr)*) => {
+ $(
+ #[cfg(any(no_target_has_atomic, target_has_atomic = $size))]
+ impl<'de> Deserialize<'de> for $ty {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ Deserialize::deserialize(deserializer).map(Self::new)
+ }
+ }
+ )*
+ };
+}
+
+#[cfg(all(feature = "std", not(no_std_atomic)))]
+atomic_impl! {
+ AtomicBool "8"
+ AtomicI8 "8"
+ AtomicI16 "16"
+ AtomicI32 "32"
+ AtomicIsize "ptr"
+ AtomicU8 "8"
+ AtomicU16 "16"
+ AtomicU32 "32"
+ AtomicUsize "ptr"
+}
+
+#[cfg(all(feature = "std", not(no_std_atomic64)))]
+atomic_impl! {
+ AtomicI64 "64"
+ AtomicU64 "64"
+}
+
+#[cfg(feature = "std")]
+struct FromStrVisitor<T> {
+ expecting: &'static str,
+ ty: PhantomData<T>,
+}
+
+#[cfg(feature = "std")]
+impl<T> FromStrVisitor<T> {
+ fn new(expecting: &'static str) -> Self {
+ FromStrVisitor {
+ expecting: expecting,
+ ty: PhantomData,
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl<'de, T> Visitor<'de> for FromStrVisitor<T>
+where
+ T: str::FromStr,
+ T::Err: fmt::Display,
+{
+ type Value = T;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(self.expecting)
+ }
+
+ fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ s.parse().map_err(Error::custom)
+ }
+}
diff --git a/third_party/rust/serde/src/de/mod.rs b/third_party/rust/serde/src/de/mod.rs
new file mode 100644
index 0000000000..ca29ec610b
--- /dev/null
+++ b/third_party/rust/serde/src/de/mod.rs
@@ -0,0 +1,2299 @@
+//! Generic data structure deserialization framework.
+//!
+//! The two most important traits in this module are [`Deserialize`] and
+//! [`Deserializer`].
+//!
+//! - **A type that implements `Deserialize` is a data structure** that can be
+//! deserialized from any data format supported by Serde, and conversely
+//! - **A type that implements `Deserializer` is a data format** that can
+//! deserialize any data structure supported by Serde.
+//!
+//! # The Deserialize trait
+//!
+//! Serde provides [`Deserialize`] implementations for many Rust primitive and
+//! standard library types. The complete list is below. All of these can be
+//! deserialized using Serde out of the box.
+//!
+//! Additionally, Serde provides a procedural macro called [`serde_derive`] to
+//! automatically generate [`Deserialize`] implementations for structs and enums
+//! in your program. See the [derive section of the manual] for how to use this.
+//!
+//! In rare cases it may be necessary to implement [`Deserialize`] manually for
+//! some type in your program. See the [Implementing `Deserialize`] section of
+//! the manual for more about this.
+//!
+//! Third-party crates may provide [`Deserialize`] implementations for types
+//! that they expose. For example the [`linked-hash-map`] crate provides a
+//! [`LinkedHashMap<K, V>`] type that is deserializable by Serde because the
+//! crate provides an implementation of [`Deserialize`] for it.
+//!
+//! # The Deserializer trait
+//!
+//! [`Deserializer`] implementations are provided by third-party crates, for
+//! example [`serde_json`], [`serde_yaml`] and [`postcard`].
+//!
+//! A partial list of well-maintained formats is given on the [Serde
+//! website][data formats].
+//!
+//! # Implementations of Deserialize provided by Serde
+//!
+//! This is a slightly different set of types than what is supported for
+//! serialization. Some types can be serialized by Serde but not deserialized.
+//! One example is `OsStr`.
+//!
+//! - **Primitive types**:
+//! - bool
+//! - i8, i16, i32, i64, i128, isize
+//! - u8, u16, u32, u64, u128, usize
+//! - f32, f64
+//! - char
+//! - **Compound types**:
+//! - \[T; 0\] through \[T; 32\]
+//! - tuples up to size 16
+//! - **Common standard library types**:
+//! - String
+//! - Option\<T\>
+//! - Result\<T, E\>
+//! - PhantomData\<T\>
+//! - **Wrapper types**:
+//! - Box\<T\>
+//! - Box\<\[T\]\>
+//! - Box\<str\>
+//! - Cow\<'a, T\>
+//! - Cell\<T\>
+//! - RefCell\<T\>
+//! - Mutex\<T\>
+//! - RwLock\<T\>
+//! - Rc\<T\>&emsp;*(if* features = ["rc"] *is enabled)*
+//! - Arc\<T\>&emsp;*(if* features = ["rc"] *is enabled)*
+//! - **Collection types**:
+//! - BTreeMap\<K, V\>
+//! - BTreeSet\<T\>
+//! - BinaryHeap\<T\>
+//! - HashMap\<K, V, H\>
+//! - HashSet\<T, H\>
+//! - LinkedList\<T\>
+//! - VecDeque\<T\>
+//! - Vec\<T\>
+//! - **Zero-copy types**:
+//! - &str
+//! - &\[u8\]
+//! - **FFI types**:
+//! - CString
+//! - Box\<CStr\>
+//! - OsString
+//! - **Miscellaneous standard library types**:
+//! - Duration
+//! - SystemTime
+//! - Path
+//! - PathBuf
+//! - Range\<T\>
+//! - RangeInclusive\<T\>
+//! - Bound\<T\>
+//! - num::NonZero*
+//! - `!` *(unstable)*
+//! - **Net types**:
+//! - IpAddr
+//! - Ipv4Addr
+//! - Ipv6Addr
+//! - SocketAddr
+//! - SocketAddrV4
+//! - SocketAddrV6
+//!
+//! [Implementing `Deserialize`]: https://serde.rs/impl-deserialize.html
+//! [`Deserialize`]: ../trait.Deserialize.html
+//! [`Deserializer`]: ../trait.Deserializer.html
+//! [`LinkedHashMap<K, V>`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html
+//! [`postcard`]: https://github.com/jamesmunns/postcard
+//! [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
+//! [`serde_derive`]: https://crates.io/crates/serde_derive
+//! [`serde_json`]: https://github.com/serde-rs/json
+//! [`serde_yaml`]: https://github.com/dtolnay/serde-yaml
+//! [derive section of the manual]: https://serde.rs/derive.html
+//! [data formats]: https://serde.rs/#data-formats
+
+use lib::*;
+
+////////////////////////////////////////////////////////////////////////////////
+
+pub mod value;
+
+#[cfg(not(no_integer128))]
+mod format;
+mod ignored_any;
+mod impls;
+mod utf8;
+
+pub use self::ignored_any::IgnoredAny;
+
+#[cfg(feature = "std")]
+#[doc(no_inline)]
+pub use std::error::Error as StdError;
+#[cfg(not(feature = "std"))]
+#[doc(no_inline)]
+pub use std_error::Error as StdError;
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! declare_error_trait {
+ (Error: Sized $(+ $($supertrait:ident)::+)*) => {
+ /// The `Error` trait allows `Deserialize` implementations to create descriptive
+ /// error messages belonging to the `Deserializer` against which they are
+ /// currently running.
+ ///
+ /// Every `Deserializer` declares an `Error` type that encompasses both
+ /// general-purpose deserialization errors as well as errors specific to the
+ /// particular deserialization format. For example the `Error` type of
+ /// `serde_json` can represent errors like an invalid JSON escape sequence or an
+ /// unterminated string literal, in addition to the error cases that are part of
+ /// this trait.
+ ///
+ /// Most deserializers should only need to provide the `Error::custom` method
+ /// and inherit the default behavior for the other methods.
+ ///
+ /// # Example implementation
+ ///
+ /// The [example data format] presented on the website shows an error
+ /// type appropriate for a basic JSON data format.
+ ///
+ /// [example data format]: https://serde.rs/data-format.html
+ pub trait Error: Sized $(+ $($supertrait)::+)* {
+ /// Raised when there is general error when deserializing a type.
+ ///
+ /// The message should not be capitalized and should not end with a period.
+ ///
+ /// ```edition2018
+ /// # use std::str::FromStr;
+ /// #
+ /// # struct IpAddr;
+ /// #
+ /// # impl FromStr for IpAddr {
+ /// # type Err = String;
+ /// #
+ /// # fn from_str(_: &str) -> Result<Self, String> {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// use serde::de::{self, Deserialize, Deserializer};
+ ///
+ /// impl<'de> Deserialize<'de> for IpAddr {
+ /// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ /// where
+ /// D: Deserializer<'de>,
+ /// {
+ /// let s = String::deserialize(deserializer)?;
+ /// s.parse().map_err(de::Error::custom)
+ /// }
+ /// }
+ /// ```
+ fn custom<T>(msg: T) -> Self
+ where
+ T: Display;
+
+ /// Raised when a `Deserialize` receives a type different from what it was
+ /// expecting.
+ ///
+ /// The `unexp` argument provides information about what type was received.
+ /// This is the type that was present in the input file or other source data
+ /// of the Deserializer.
+ ///
+ /// The `exp` argument provides information about what type was being
+ /// expected. This is the type that is written in the program.
+ ///
+ /// For example if we try to deserialize a String out of a JSON file
+ /// containing an integer, the unexpected type is the integer and the
+ /// expected type is the string.
+ #[cold]
+ fn invalid_type(unexp: Unexpected, exp: &Expected) -> Self {
+ Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
+ }
+
+ /// Raised when a `Deserialize` receives a value of the right type but that
+ /// is wrong for some other reason.
+ ///
+ /// The `unexp` argument provides information about what value was received.
+ /// This is the value that was present in the input file or other source
+ /// data of the Deserializer.
+ ///
+ /// The `exp` argument provides information about what value was being
+ /// expected. This is the type that is written in the program.
+ ///
+ /// For example if we try to deserialize a String out of some binary data
+ /// that is not valid UTF-8, the unexpected value is the bytes and the
+ /// expected value is a string.
+ #[cold]
+ fn invalid_value(unexp: Unexpected, exp: &Expected) -> Self {
+ Error::custom(format_args!("invalid value: {}, expected {}", unexp, exp))
+ }
+
+ /// Raised when deserializing a sequence or map and the input data contains
+ /// too many or too few elements.
+ ///
+ /// The `len` argument is the number of elements encountered. The sequence
+ /// or map may have expected more arguments or fewer arguments.
+ ///
+ /// The `exp` argument provides information about what data was being
+ /// expected. For example `exp` might say that a tuple of size 6 was
+ /// expected.
+ #[cold]
+ fn invalid_length(len: usize, exp: &Expected) -> Self {
+ Error::custom(format_args!("invalid length {}, expected {}", len, exp))
+ }
+
+ /// Raised when a `Deserialize` enum type received a variant with an
+ /// unrecognized name.
+ #[cold]
+ fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self {
+ if expected.is_empty() {
+ Error::custom(format_args!(
+ "unknown variant `{}`, there are no variants",
+ variant
+ ))
+ } else {
+ Error::custom(format_args!(
+ "unknown variant `{}`, expected {}",
+ variant,
+ OneOf { names: expected }
+ ))
+ }
+ }
+
+ /// Raised when a `Deserialize` struct type received a field with an
+ /// unrecognized name.
+ #[cold]
+ fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self {
+ if expected.is_empty() {
+ Error::custom(format_args!(
+ "unknown field `{}`, there are no fields",
+ field
+ ))
+ } else {
+ Error::custom(format_args!(
+ "unknown field `{}`, expected {}",
+ field,
+ OneOf { names: expected }
+ ))
+ }
+ }
+
+ /// Raised when a `Deserialize` struct type expected to receive a required
+ /// field with a particular name but that field was not present in the
+ /// input.
+ #[cold]
+ fn missing_field(field: &'static str) -> Self {
+ Error::custom(format_args!("missing field `{}`", field))
+ }
+
+ /// Raised when a `Deserialize` struct type received more than one of the
+ /// same field.
+ #[cold]
+ fn duplicate_field(field: &'static str) -> Self {
+ Error::custom(format_args!("duplicate field `{}`", field))
+ }
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+declare_error_trait!(Error: Sized + StdError);
+
+#[cfg(not(feature = "std"))]
+declare_error_trait!(Error: Sized + Debug + Display);
+
+/// `Unexpected` represents an unexpected invocation of any one of the `Visitor`
+/// trait methods.
+///
+/// This is used as an argument to the `invalid_type`, `invalid_value`, and
+/// `invalid_length` methods of the `Error` trait to build error messages.
+///
+/// ```edition2018
+/// # use std::fmt;
+/// #
+/// # use serde::de::{self, Unexpected, Visitor};
+/// #
+/// # struct Example;
+/// #
+/// # impl<'de> Visitor<'de> for Example {
+/// # type Value = ();
+/// #
+/// # fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+/// # write!(formatter, "definitely not a boolean")
+/// # }
+/// #
+/// fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
+/// where
+/// E: de::Error,
+/// {
+/// Err(de::Error::invalid_type(Unexpected::Bool(v), &self))
+/// }
+/// # }
+/// ```
+#[derive(Copy, Clone, PartialEq, Debug)]
+pub enum Unexpected<'a> {
+ /// The input contained a boolean value that was not expected.
+ Bool(bool),
+
+ /// The input contained an unsigned integer `u8`, `u16`, `u32` or `u64` that
+ /// was not expected.
+ Unsigned(u64),
+
+ /// The input contained a signed integer `i8`, `i16`, `i32` or `i64` that
+ /// was not expected.
+ Signed(i64),
+
+ /// The input contained a floating point `f32` or `f64` that was not
+ /// expected.
+ Float(f64),
+
+ /// The input contained a `char` that was not expected.
+ Char(char),
+
+ /// The input contained a `&str` or `String` that was not expected.
+ Str(&'a str),
+
+ /// The input contained a `&[u8]` or `Vec<u8>` that was not expected.
+ Bytes(&'a [u8]),
+
+ /// The input contained a unit `()` that was not expected.
+ Unit,
+
+ /// The input contained an `Option<T>` that was not expected.
+ Option,
+
+ /// The input contained a newtype struct that was not expected.
+ NewtypeStruct,
+
+ /// The input contained a sequence that was not expected.
+ Seq,
+
+ /// The input contained a map that was not expected.
+ Map,
+
+ /// The input contained an enum that was not expected.
+ Enum,
+
+ /// The input contained a unit variant that was not expected.
+ UnitVariant,
+
+ /// The input contained a newtype variant that was not expected.
+ NewtypeVariant,
+
+ /// The input contained a tuple variant that was not expected.
+ TupleVariant,
+
+ /// The input contained a struct variant that was not expected.
+ StructVariant,
+
+ /// A message stating what uncategorized thing the input contained that was
+ /// not expected.
+ ///
+ /// The message should be a noun or noun phrase, not capitalized and without
+ /// a period. An example message is "unoriginal superhero".
+ Other(&'a str),
+}
+
+impl<'a> fmt::Display for Unexpected<'a> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ use self::Unexpected::*;
+ match *self {
+ Bool(b) => write!(formatter, "boolean `{}`", b),
+ Unsigned(i) => write!(formatter, "integer `{}`", i),
+ Signed(i) => write!(formatter, "integer `{}`", i),
+ Float(f) => write!(formatter, "floating point `{}`", f),
+ Char(c) => write!(formatter, "character `{}`", c),
+ Str(s) => write!(formatter, "string {:?}", s),
+ Bytes(_) => write!(formatter, "byte array"),
+ Unit => write!(formatter, "unit value"),
+ Option => write!(formatter, "Option value"),
+ NewtypeStruct => write!(formatter, "newtype struct"),
+ Seq => write!(formatter, "sequence"),
+ Map => write!(formatter, "map"),
+ Enum => write!(formatter, "enum"),
+ UnitVariant => write!(formatter, "unit variant"),
+ NewtypeVariant => write!(formatter, "newtype variant"),
+ TupleVariant => write!(formatter, "tuple variant"),
+ StructVariant => write!(formatter, "struct variant"),
+ Other(other) => formatter.write_str(other),
+ }
+ }
+}
+
+/// `Expected` represents an explanation of what data a `Visitor` was expecting
+/// to receive.
+///
+/// This is used as an argument to the `invalid_type`, `invalid_value`, and
+/// `invalid_length` methods of the `Error` trait to build error messages. The
+/// message should be a noun or noun phrase that completes the sentence "This
+/// Visitor expects to receive ...", for example the message could be "an
+/// integer between 0 and 64". The message should not be capitalized and should
+/// not end with a period.
+///
+/// Within the context of a `Visitor` implementation, the `Visitor` itself
+/// (`&self`) is an implementation of this trait.
+///
+/// ```edition2018
+/// # use std::fmt;
+/// #
+/// # use serde::de::{self, Unexpected, Visitor};
+/// #
+/// # struct Example;
+/// #
+/// # impl<'de> Visitor<'de> for Example {
+/// # type Value = ();
+/// #
+/// # fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+/// # write!(formatter, "definitely not a boolean")
+/// # }
+/// #
+/// fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
+/// where
+/// E: de::Error,
+/// {
+/// Err(de::Error::invalid_type(Unexpected::Bool(v), &self))
+/// }
+/// # }
+/// ```
+///
+/// Outside of a `Visitor`, `&"..."` can be used.
+///
+/// ```edition2018
+/// # use serde::de::{self, Unexpected};
+/// #
+/// # fn example<E>() -> Result<(), E>
+/// # where
+/// # E: de::Error,
+/// # {
+/// # let v = true;
+/// return Err(de::Error::invalid_type(Unexpected::Bool(v), &"a negative integer"));
+/// # }
+/// ```
+pub trait Expected {
+ /// Format an explanation of what data was being expected. Same signature as
+ /// the `Display` and `Debug` traits.
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result;
+}
+
+impl<'de, T> Expected for T
+where
+ T: Visitor<'de>,
+{
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ self.expecting(formatter)
+ }
+}
+
+impl<'a> Expected for &'a str {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(self)
+ }
+}
+
+impl<'a> Display for Expected + 'a {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ Expected::fmt(self, formatter)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A **data structure** that can be deserialized from any data format supported
+/// by Serde.
+///
+/// Serde provides `Deserialize` implementations for many Rust primitive and
+/// standard library types. The complete list is [here][crate::de]. All of these
+/// can be deserialized using Serde out of the box.
+///
+/// Additionally, Serde provides a procedural macro called `serde_derive` to
+/// automatically generate `Deserialize` implementations for structs and enums
+/// in your program. See the [derive section of the manual][derive] for how to
+/// use this.
+///
+/// In rare cases it may be necessary to implement `Deserialize` manually for
+/// some type in your program. See the [Implementing
+/// `Deserialize`][impl-deserialize] section of the manual for more about this.
+///
+/// Third-party crates may provide `Deserialize` implementations for types that
+/// they expose. For example the `linked-hash-map` crate provides a
+/// `LinkedHashMap<K, V>` type that is deserializable by Serde because the crate
+/// provides an implementation of `Deserialize` for it.
+///
+/// [derive]: https://serde.rs/derive.html
+/// [impl-deserialize]: https://serde.rs/impl-deserialize.html
+///
+/// # Lifetime
+///
+/// The `'de` lifetime of this trait is the lifetime of data that may be
+/// borrowed by `Self` when deserialized. See the page [Understanding
+/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
+///
+/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
+pub trait Deserialize<'de>: Sized {
+ /// Deserialize this value from the given Serde deserializer.
+ ///
+ /// See the [Implementing `Deserialize`][impl-deserialize] section of the
+ /// manual for more information about how to implement this method.
+ ///
+ /// [impl-deserialize]: https://serde.rs/impl-deserialize.html
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>;
+
+ /// Deserializes a value into `self` from the given Deserializer.
+ ///
+ /// The purpose of this method is to allow the deserializer to reuse
+ /// resources and avoid copies. As such, if this method returns an error,
+ /// `self` will be in an indeterminate state where some parts of the struct
+ /// have been overwritten. Although whatever state that is will be
+ /// memory-safe.
+ ///
+ /// This is generally useful when repeatedly deserializing values that
+ /// are processed one at a time, where the value of `self` doesn't matter
+ /// when the next deserialization occurs.
+ ///
+ /// If you manually implement this, your recursive deserializations should
+ /// use `deserialize_in_place`.
+ ///
+ /// This method is stable and an official public API, but hidden from the
+ /// documentation because it is almost never what newbies are looking for.
+ /// Showing it in rustdoc would cause it to be featured more prominently
+ /// than it deserves.
+ #[doc(hidden)]
+ fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ // Default implementation just delegates to `deserialize` impl.
+ *place = try!(Deserialize::deserialize(deserializer));
+ Ok(())
+ }
+}
+
+/// A data structure that can be deserialized without borrowing any data from
+/// the deserializer.
+///
+/// This is primarily useful for trait bounds on functions. For example a
+/// `from_str` function may be able to deserialize a data structure that borrows
+/// from the input string, but a `from_reader` function may only deserialize
+/// owned data.
+///
+/// ```edition2018
+/// # use serde::de::{Deserialize, DeserializeOwned};
+/// # use std::io::{Read, Result};
+/// #
+/// # trait Ignore {
+/// fn from_str<'a, T>(s: &'a str) -> Result<T>
+/// where
+/// T: Deserialize<'a>;
+///
+/// fn from_reader<R, T>(rdr: R) -> Result<T>
+/// where
+/// R: Read,
+/// T: DeserializeOwned;
+/// # }
+/// ```
+///
+/// # Lifetime
+///
+/// The relationship between `Deserialize` and `DeserializeOwned` in trait
+/// bounds is explained in more detail on the page [Understanding deserializer
+/// lifetimes].
+///
+/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
+pub trait DeserializeOwned: for<'de> Deserialize<'de> {}
+impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
+
+/// `DeserializeSeed` is the stateful form of the `Deserialize` trait. If you
+/// ever find yourself looking for a way to pass data into a `Deserialize` impl,
+/// this trait is the way to do it.
+///
+/// As one example of stateful deserialization consider deserializing a JSON
+/// array into an existing buffer. Using the `Deserialize` trait we could
+/// deserialize a JSON array into a `Vec<T>` but it would be a freshly allocated
+/// `Vec<T>`; there is no way for `Deserialize` to reuse a previously allocated
+/// buffer. Using `DeserializeSeed` instead makes this possible as in the
+/// example code below.
+///
+/// The canonical API for stateless deserialization looks like this:
+///
+/// ```edition2018
+/// # use serde::Deserialize;
+/// #
+/// # enum Error {}
+/// #
+/// fn func<'de, T: Deserialize<'de>>() -> Result<T, Error>
+/// # {
+/// # unimplemented!()
+/// # }
+/// ```
+///
+/// Adjusting an API like this to support stateful deserialization is a matter
+/// of accepting a seed as input:
+///
+/// ```edition2018
+/// # use serde::de::DeserializeSeed;
+/// #
+/// # enum Error {}
+/// #
+/// fn func_seed<'de, T: DeserializeSeed<'de>>(seed: T) -> Result<T::Value, Error>
+/// # {
+/// # let _ = seed;
+/// # unimplemented!()
+/// # }
+/// ```
+///
+/// In practice the majority of deserialization is stateless. An API expecting a
+/// seed can be appeased by passing `std::marker::PhantomData` as a seed in the
+/// case of stateless deserialization.
+///
+/// # Lifetime
+///
+/// The `'de` lifetime of this trait is the lifetime of data that may be
+/// borrowed by `Self::Value` when deserialized. See the page [Understanding
+/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
+///
+/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
+///
+/// # Example
+///
+/// Suppose we have JSON that looks like `[[1, 2], [3, 4, 5], [6]]` and we need
+/// to deserialize it into a flat representation like `vec![1, 2, 3, 4, 5, 6]`.
+/// Allocating a brand new `Vec<T>` for each subarray would be slow. Instead we
+/// would like to allocate a single `Vec<T>` and then deserialize each subarray
+/// into it. This requires stateful deserialization using the `DeserializeSeed`
+/// trait.
+///
+/// ```edition2018
+/// use std::fmt;
+/// use std::marker::PhantomData;
+///
+/// use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
+///
+/// // A DeserializeSeed implementation that uses stateful deserialization to
+/// // append array elements onto the end of an existing vector. The preexisting
+/// // state ("seed") in this case is the Vec<T>. The `deserialize` method of
+/// // `ExtendVec` will be traversing the inner arrays of the JSON input and
+/// // appending each integer into the existing Vec.
+/// struct ExtendVec<'a, T: 'a>(&'a mut Vec<T>);
+///
+/// impl<'de, 'a, T> DeserializeSeed<'de> for ExtendVec<'a, T>
+/// where
+/// T: Deserialize<'de>,
+/// {
+/// // The return type of the `deserialize` method. This implementation
+/// // appends onto an existing vector but does not create any new data
+/// // structure, so the return type is ().
+/// type Value = ();
+///
+/// fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+/// where
+/// D: Deserializer<'de>,
+/// {
+/// // Visitor implementation that will walk an inner array of the JSON
+/// // input.
+/// struct ExtendVecVisitor<'a, T: 'a>(&'a mut Vec<T>);
+///
+/// impl<'de, 'a, T> Visitor<'de> for ExtendVecVisitor<'a, T>
+/// where
+/// T: Deserialize<'de>,
+/// {
+/// type Value = ();
+///
+/// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+/// write!(formatter, "an array of integers")
+/// }
+///
+/// fn visit_seq<A>(self, mut seq: A) -> Result<(), A::Error>
+/// where
+/// A: SeqAccess<'de>,
+/// {
+/// // Decrease the number of reallocations if there are many elements
+/// if let Some(size_hint) = seq.size_hint() {
+/// self.0.reserve(size_hint);
+/// }
+///
+/// // Visit each element in the inner array and push it onto
+/// // the existing vector.
+/// while let Some(elem) = seq.next_element()? {
+/// self.0.push(elem);
+/// }
+/// Ok(())
+/// }
+/// }
+///
+/// deserializer.deserialize_seq(ExtendVecVisitor(self.0))
+/// }
+/// }
+///
+/// // Visitor implementation that will walk the outer array of the JSON input.
+/// struct FlattenedVecVisitor<T>(PhantomData<T>);
+///
+/// impl<'de, T> Visitor<'de> for FlattenedVecVisitor<T>
+/// where
+/// T: Deserialize<'de>,
+/// {
+/// // This Visitor constructs a single Vec<T> to hold the flattened
+/// // contents of the inner arrays.
+/// type Value = Vec<T>;
+///
+/// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+/// write!(formatter, "an array of arrays")
+/// }
+///
+/// fn visit_seq<A>(self, mut seq: A) -> Result<Vec<T>, A::Error>
+/// where
+/// A: SeqAccess<'de>,
+/// {
+/// // Create a single Vec to hold the flattened contents.
+/// let mut vec = Vec::new();
+///
+/// // Each iteration through this loop is one inner array.
+/// while let Some(()) = seq.next_element_seed(ExtendVec(&mut vec))? {
+/// // Nothing to do; inner array has been appended into `vec`.
+/// }
+///
+/// // Return the finished vec.
+/// Ok(vec)
+/// }
+/// }
+///
+/// # fn example<'de, D>(deserializer: D) -> Result<(), D::Error>
+/// # where
+/// # D: Deserializer<'de>,
+/// # {
+/// let visitor = FlattenedVecVisitor(PhantomData);
+/// let flattened: Vec<u64> = deserializer.deserialize_seq(visitor)?;
+/// # Ok(())
+/// # }
+/// ```
+pub trait DeserializeSeed<'de>: Sized {
+ /// The type produced by using this seed.
+ type Value;
+
+ /// Equivalent to the more common `Deserialize::deserialize` method, except
+ /// with some initial piece of data (the seed) passed in.
+ fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>;
+}
+
+impl<'de, T> DeserializeSeed<'de> for PhantomData<T>
+where
+ T: Deserialize<'de>,
+{
+ type Value = T;
+
+ #[inline]
+ fn deserialize<D>(self, deserializer: D) -> Result<T, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ T::deserialize(deserializer)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A **data format** that can deserialize any data structure supported by
+/// Serde.
+///
+/// The role of this trait is to define the deserialization half of the [Serde
+/// data model], which is a way to categorize every Rust data type into one of
+/// 29 possible types. Each method of the `Deserializer` trait corresponds to one
+/// of the types of the data model.
+///
+/// Implementations of `Deserialize` map themselves into this data model by
+/// passing to the `Deserializer` a `Visitor` implementation that can receive
+/// these various types.
+///
+/// The types that make up the Serde data model are:
+///
+/// - **14 primitive types**
+/// - bool
+/// - i8, i16, i32, i64, i128
+/// - u8, u16, u32, u64, u128
+/// - f32, f64
+/// - char
+/// - **string**
+/// - UTF-8 bytes with a length and no null terminator.
+/// - When serializing, all strings are handled equally. When deserializing,
+/// there are three flavors of strings: transient, owned, and borrowed.
+/// - **byte array** - \[u8\]
+/// - Similar to strings, during deserialization byte arrays can be
+/// transient, owned, or borrowed.
+/// - **option**
+/// - Either none or some value.
+/// - **unit**
+/// - The type of `()` in Rust. It represents an anonymous value containing
+/// no data.
+/// - **unit_struct**
+/// - For example `struct Unit` or `PhantomData<T>`. It represents a named
+/// value containing no data.
+/// - **unit_variant**
+/// - For example the `E::A` and `E::B` in `enum E { A, B }`.
+/// - **newtype_struct**
+/// - For example `struct Millimeters(u8)`.
+/// - **newtype_variant**
+/// - For example the `E::N` in `enum E { N(u8) }`.
+/// - **seq**
+/// - A variably sized heterogeneous sequence of values, for example `Vec<T>`
+/// or `HashSet<T>`. When serializing, the length may or may not be known
+/// before iterating through all the data. When deserializing, the length
+/// is determined by looking at the serialized data.
+/// - **tuple**
+/// - A statically sized heterogeneous sequence of values for which the
+/// length will be known at deserialization time without looking at the
+/// serialized data, for example `(u8,)` or `(String, u64, Vec<T>)` or
+/// `[u64; 10]`.
+/// - **tuple_struct**
+/// - A named tuple, for example `struct Rgb(u8, u8, u8)`.
+/// - **tuple_variant**
+/// - For example the `E::T` in `enum E { T(u8, u8) }`.
+/// - **map**
+/// - A heterogeneous key-value pairing, for example `BTreeMap<K, V>`.
+/// - **struct**
+/// - A heterogeneous key-value pairing in which the keys are strings and
+/// will be known at deserialization time without looking at the serialized
+/// data, for example `struct S { r: u8, g: u8, b: u8 }`.
+/// - **struct_variant**
+/// - For example the `E::S` in `enum E { S { r: u8, g: u8, b: u8 } }`.
+///
+/// The `Deserializer` trait supports two entry point styles which enables
+/// different kinds of deserialization.
+///
+/// 1. The `deserialize_any` method. Self-describing data formats like JSON are
+/// able to look at the serialized data and tell what it represents. For
+/// example the JSON deserializer may see an opening curly brace (`{`) and
+/// know that it is seeing a map. If the data format supports
+/// `Deserializer::deserialize_any`, it will drive the Visitor using whatever
+/// type it sees in the input. JSON uses this approach when deserializing
+/// `serde_json::Value` which is an enum that can represent any JSON
+/// document. Without knowing what is in a JSON document, we can deserialize
+/// it to `serde_json::Value` by going through
+/// `Deserializer::deserialize_any`.
+///
+/// 2. The various `deserialize_*` methods. Non-self-describing formats like
+/// Postcard need to be told what is in the input in order to deserialize it.
+/// The `deserialize_*` methods are hints to the deserializer for how to
+/// interpret the next piece of input. Non-self-describing formats are not
+/// able to deserialize something like `serde_json::Value` which relies on
+/// `Deserializer::deserialize_any`.
+///
+/// When implementing `Deserialize`, you should avoid relying on
+/// `Deserializer::deserialize_any` unless you need to be told by the
+/// Deserializer what type is in the input. Know that relying on
+/// `Deserializer::deserialize_any` means your data type will be able to
+/// deserialize from self-describing formats only, ruling out Postcard and many
+/// others.
+///
+/// [Serde data model]: https://serde.rs/data-model.html
+///
+/// # Lifetime
+///
+/// The `'de` lifetime of this trait is the lifetime of data that may be
+/// borrowed from the input when deserializing. See the page [Understanding
+/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
+///
+/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website contains example code for
+/// a basic JSON `Deserializer`.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait Deserializer<'de>: Sized {
+ /// The error type that can be returned if some error occurs during
+ /// deserialization.
+ type Error: Error;
+
+ /// Require the `Deserializer` to figure out how to drive the visitor based
+ /// on what data type is in the input.
+ ///
+ /// When implementing `Deserialize`, you should avoid relying on
+ /// `Deserializer::deserialize_any` unless you need to be told by the
+ /// Deserializer what type is in the input. Know that relying on
+ /// `Deserializer::deserialize_any` means your data type will be able to
+ /// deserialize from self-describing formats only, ruling out Postcard and
+ /// many others.
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a `bool` value.
+ fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting an `i8` value.
+ fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting an `i16` value.
+ fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting an `i32` value.
+ fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting an `i64` value.
+ fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ serde_if_integer128! {
+ /// Hint that the `Deserialize` type is expecting an `i128` value.
+ ///
+ /// This method is available only on Rust compiler versions >=1.26. The
+ /// default behavior unconditionally returns an error.
+ fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>
+ {
+ let _ = visitor;
+ Err(Error::custom("i128 is not supported"))
+ }
+ }
+
+ /// Hint that the `Deserialize` type is expecting a `u8` value.
+ fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a `u16` value.
+ fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a `u32` value.
+ fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a `u64` value.
+ fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ serde_if_integer128! {
+ /// Hint that the `Deserialize` type is expecting an `u128` value.
+ ///
+ /// This method is available only on Rust compiler versions >=1.26. The
+ /// default behavior unconditionally returns an error.
+ fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>
+ {
+ let _ = visitor;
+ Err(Error::custom("u128 is not supported"))
+ }
+ }
+
+ /// Hint that the `Deserialize` type is expecting a `f32` value.
+ fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a `f64` value.
+ fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a `char` value.
+ fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a string value and does
+ /// not benefit from taking ownership of buffered data owned by the
+ /// `Deserializer`.
+ ///
+ /// If the `Visitor` would benefit from taking ownership of `String` data,
+ /// indicate this to the `Deserializer` by using `deserialize_string`
+ /// instead.
+ fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a string value and would
+ /// benefit from taking ownership of buffered data owned by the
+ /// `Deserializer`.
+ ///
+ /// If the `Visitor` would not benefit from taking ownership of `String`
+ /// data, indicate that to the `Deserializer` by using `deserialize_str`
+ /// instead.
+ fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a byte array and does not
+ /// benefit from taking ownership of buffered data owned by the
+ /// `Deserializer`.
+ ///
+ /// If the `Visitor` would benefit from taking ownership of `Vec<u8>` data,
+ /// indicate this to the `Deserializer` by using `deserialize_byte_buf`
+ /// instead.
+ fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a byte array and would
+ /// benefit from taking ownership of buffered data owned by the
+ /// `Deserializer`.
+ ///
+ /// If the `Visitor` would not benefit from taking ownership of `Vec<u8>`
+ /// data, indicate that to the `Deserializer` by using `deserialize_bytes`
+ /// instead.
+ fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting an optional value.
+ ///
+ /// This allows deserializers that encode an optional value as a nullable
+ /// value to convert the null value into `None` and a regular value into
+ /// `Some(value)`.
+ fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a unit value.
+ fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a unit struct with a
+ /// particular name.
+ fn deserialize_unit_struct<V>(
+ self,
+ name: &'static str,
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a newtype struct with a
+ /// particular name.
+ fn deserialize_newtype_struct<V>(
+ self,
+ name: &'static str,
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a sequence of values.
+ fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a sequence of values and
+ /// knows how many values there are without looking at the serialized data.
+ fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a tuple struct with a
+ /// particular name and number of fields.
+ fn deserialize_tuple_struct<V>(
+ self,
+ name: &'static str,
+ len: usize,
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a map of key-value pairs.
+ fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting a struct with a particular
+ /// name and fields.
+ fn deserialize_struct<V>(
+ self,
+ name: &'static str,
+ fields: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting an enum value with a
+ /// particular name and possible variants.
+ fn deserialize_enum<V>(
+ self,
+ name: &'static str,
+ variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type is expecting the name of a struct
+ /// field or the discriminant of an enum variant.
+ fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Hint that the `Deserialize` type needs to deserialize a value whose type
+ /// doesn't matter because it is ignored.
+ ///
+ /// Deserializers for non-self-describing formats may not support this mode.
+ fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Determine whether `Deserialize` implementations should expect to
+ /// deserialize their human-readable form.
+ ///
+ /// Some types have a human-readable form that may be somewhat expensive to
+ /// construct, as well as a binary form that is compact and efficient.
+ /// Generally text-based formats like JSON and YAML will prefer to use the
+ /// human-readable one and binary formats like Postcard will prefer the
+ /// compact one.
+ ///
+ /// ```edition2018
+ /// # use std::ops::Add;
+ /// # use std::str::FromStr;
+ /// #
+ /// # struct Timestamp;
+ /// #
+ /// # impl Timestamp {
+ /// # const EPOCH: Timestamp = Timestamp;
+ /// # }
+ /// #
+ /// # impl FromStr for Timestamp {
+ /// # type Err = String;
+ /// # fn from_str(_: &str) -> Result<Self, Self::Err> {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// # struct Duration;
+ /// #
+ /// # impl Duration {
+ /// # fn seconds(_: u64) -> Self { unimplemented!() }
+ /// # }
+ /// #
+ /// # impl Add<Duration> for Timestamp {
+ /// # type Output = Timestamp;
+ /// # fn add(self, _: Duration) -> Self::Output {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// use serde::de::{self, Deserialize, Deserializer};
+ ///
+ /// impl<'de> Deserialize<'de> for Timestamp {
+ /// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ /// where
+ /// D: Deserializer<'de>,
+ /// {
+ /// if deserializer.is_human_readable() {
+ /// // Deserialize from a human-readable string like "2015-05-15T17:01:00Z".
+ /// let s = String::deserialize(deserializer)?;
+ /// Timestamp::from_str(&s).map_err(de::Error::custom)
+ /// } else {
+ /// // Deserialize from a compact binary representation, seconds since
+ /// // the Unix epoch.
+ /// let n = u64::deserialize(deserializer)?;
+ /// Ok(Timestamp::EPOCH + Duration::seconds(n))
+ /// }
+ /// }
+ /// }
+ /// ```
+ ///
+ /// The default implementation of this method returns `true`. Data formats
+ /// may override this to `false` to request a compact form for types that
+ /// support one. Note that modifying this method to change a format from
+ /// human-readable to compact or vice versa should be regarded as a breaking
+ /// change, as a value serialized in human-readable mode is not required to
+ /// deserialize from the same data in compact mode.
+ #[inline]
+ fn is_human_readable(&self) -> bool {
+ true
+ }
+
+ // Not public API.
+ #[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))]
+ #[doc(hidden)]
+ fn __deserialize_content<V>(
+ self,
+ _: ::actually_private::T,
+ visitor: V,
+ ) -> Result<::private::de::Content<'de>, Self::Error>
+ where
+ V: Visitor<'de, Value = ::private::de::Content<'de>>,
+ {
+ self.deserialize_any(visitor)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// This trait represents a visitor that walks through a deserializer.
+///
+/// # Lifetime
+///
+/// The `'de` lifetime of this trait is the requirement for lifetime of data
+/// that may be borrowed by `Self::Value`. See the page [Understanding
+/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
+///
+/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
+///
+/// # Example
+///
+/// ```edition2018
+/// # use std::fmt;
+/// #
+/// # use serde::de::{self, Unexpected, Visitor};
+/// #
+/// /// A visitor that deserializes a long string - a string containing at least
+/// /// some minimum number of bytes.
+/// struct LongString {
+/// min: usize,
+/// }
+///
+/// impl<'de> Visitor<'de> for LongString {
+/// type Value = String;
+///
+/// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+/// write!(formatter, "a string containing at least {} bytes", self.min)
+/// }
+///
+/// fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
+/// where
+/// E: de::Error,
+/// {
+/// if s.len() >= self.min {
+/// Ok(s.to_owned())
+/// } else {
+/// Err(de::Error::invalid_value(Unexpected::Str(s), &self))
+/// }
+/// }
+/// }
+/// ```
+pub trait Visitor<'de>: Sized {
+ /// The value produced by this visitor.
+ type Value;
+
+ /// Format a message stating what data this Visitor expects to receive.
+ ///
+ /// This is used in error messages. The message should complete the sentence
+ /// "This Visitor expects to receive ...", for example the message could be
+ /// "an integer between 0 and 64". The message should not be capitalized and
+ /// should not end with a period.
+ ///
+ /// ```edition2018
+ /// # use std::fmt;
+ /// #
+ /// # struct S {
+ /// # max: usize,
+ /// # }
+ /// #
+ /// # impl<'de> serde::de::Visitor<'de> for S {
+ /// # type Value = ();
+ /// #
+ /// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ /// write!(formatter, "an integer between 0 and {}", self.max)
+ /// }
+ /// # }
+ /// ```
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result;
+
+ /// The input contains a boolean.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Err(Error::invalid_type(Unexpected::Bool(v), &self))
+ }
+
+ /// The input contains an `i8`.
+ ///
+ /// The default implementation forwards to [`visit_i64`].
+ ///
+ /// [`visit_i64`]: #method.visit_i64
+ fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_i64(v as i64)
+ }
+
+ /// The input contains an `i16`.
+ ///
+ /// The default implementation forwards to [`visit_i64`].
+ ///
+ /// [`visit_i64`]: #method.visit_i64
+ fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_i64(v as i64)
+ }
+
+ /// The input contains an `i32`.
+ ///
+ /// The default implementation forwards to [`visit_i64`].
+ ///
+ /// [`visit_i64`]: #method.visit_i64
+ fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_i64(v as i64)
+ }
+
+ /// The input contains an `i64`.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Err(Error::invalid_type(Unexpected::Signed(v), &self))
+ }
+
+ serde_if_integer128! {
+ /// The input contains a `i128`.
+ ///
+ /// This method is available only on Rust compiler versions >=1.26. The
+ /// default implementation fails with a type error.
+ fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ let mut buf = [0u8; 58];
+ let mut writer = format::Buf::new(&mut buf);
+ fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as i128", v)).unwrap();
+ Err(Error::invalid_type(Unexpected::Other(writer.as_str()), &self))
+ }
+ }
+
+ /// The input contains a `u8`.
+ ///
+ /// The default implementation forwards to [`visit_u64`].
+ ///
+ /// [`visit_u64`]: #method.visit_u64
+ fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_u64(v as u64)
+ }
+
+ /// The input contains a `u16`.
+ ///
+ /// The default implementation forwards to [`visit_u64`].
+ ///
+ /// [`visit_u64`]: #method.visit_u64
+ fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_u64(v as u64)
+ }
+
+ /// The input contains a `u32`.
+ ///
+ /// The default implementation forwards to [`visit_u64`].
+ ///
+ /// [`visit_u64`]: #method.visit_u64
+ fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_u64(v as u64)
+ }
+
+ /// The input contains a `u64`.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Err(Error::invalid_type(Unexpected::Unsigned(v), &self))
+ }
+
+ serde_if_integer128! {
+ /// The input contains a `u128`.
+ ///
+ /// This method is available only on Rust compiler versions >=1.26. The
+ /// default implementation fails with a type error.
+ fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ let mut buf = [0u8; 57];
+ let mut writer = format::Buf::new(&mut buf);
+ fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as u128", v)).unwrap();
+ Err(Error::invalid_type(Unexpected::Other(writer.as_str()), &self))
+ }
+ }
+
+ /// The input contains an `f32`.
+ ///
+ /// The default implementation forwards to [`visit_f64`].
+ ///
+ /// [`visit_f64`]: #method.visit_f64
+ fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_f64(v as f64)
+ }
+
+ /// The input contains an `f64`.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Err(Error::invalid_type(Unexpected::Float(v), &self))
+ }
+
+ /// The input contains a `char`.
+ ///
+ /// The default implementation forwards to [`visit_str`] as a one-character
+ /// string.
+ ///
+ /// [`visit_str`]: #method.visit_str
+ #[inline]
+ fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_str(utf8::encode(v).as_str())
+ }
+
+ /// The input contains a string. The lifetime of the string is ephemeral and
+ /// it may be destroyed after this method returns.
+ ///
+ /// This method allows the `Deserializer` to avoid a copy by retaining
+ /// ownership of any buffered data. `Deserialize` implementations that do
+ /// not benefit from taking ownership of `String` data should indicate that
+ /// to the deserializer by using `Deserializer::deserialize_str` rather than
+ /// `Deserializer::deserialize_string`.
+ ///
+ /// It is never correct to implement `visit_string` without implementing
+ /// `visit_str`. Implement neither, both, or just `visit_str`.
+ fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Err(Error::invalid_type(Unexpected::Str(v), &self))
+ }
+
+ /// The input contains a string that lives at least as long as the
+ /// `Deserializer`.
+ ///
+ /// This enables zero-copy deserialization of strings in some formats. For
+ /// example JSON input containing the JSON string `"borrowed"` can be
+ /// deserialized with zero copying into a `&'a str` as long as the input
+ /// data outlives `'a`.
+ ///
+ /// The default implementation forwards to `visit_str`.
+ #[inline]
+ fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_str(v)
+ }
+
+ /// The input contains a string and ownership of the string is being given
+ /// to the `Visitor`.
+ ///
+ /// This method allows the `Visitor` to avoid a copy by taking ownership of
+ /// a string created by the `Deserializer`. `Deserialize` implementations
+ /// that benefit from taking ownership of `String` data should indicate that
+ /// to the deserializer by using `Deserializer::deserialize_string` rather
+ /// than `Deserializer::deserialize_str`, although not every deserializer
+ /// will honor such a request.
+ ///
+ /// It is never correct to implement `visit_string` without implementing
+ /// `visit_str`. Implement neither, both, or just `visit_str`.
+ ///
+ /// The default implementation forwards to `visit_str` and then drops the
+ /// `String`.
+ #[inline]
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_str(&v)
+ }
+
+ /// The input contains a byte array. The lifetime of the byte array is
+ /// ephemeral and it may be destroyed after this method returns.
+ ///
+ /// This method allows the `Deserializer` to avoid a copy by retaining
+ /// ownership of any buffered data. `Deserialize` implementations that do
+ /// not benefit from taking ownership of `Vec<u8>` data should indicate that
+ /// to the deserializer by using `Deserializer::deserialize_bytes` rather
+ /// than `Deserializer::deserialize_byte_buf`.
+ ///
+ /// It is never correct to implement `visit_byte_buf` without implementing
+ /// `visit_bytes`. Implement neither, both, or just `visit_bytes`.
+ fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ let _ = v;
+ Err(Error::invalid_type(Unexpected::Bytes(v), &self))
+ }
+
+ /// The input contains a byte array that lives at least as long as the
+ /// `Deserializer`.
+ ///
+ /// This enables zero-copy deserialization of bytes in some formats. For
+ /// example Postcard data containing bytes can be deserialized with zero
+ /// copying into a `&'a [u8]` as long as the input data outlives `'a`.
+ ///
+ /// The default implementation forwards to `visit_bytes`.
+ #[inline]
+ fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_bytes(v)
+ }
+
+ /// The input contains a byte array and ownership of the byte array is being
+ /// given to the `Visitor`.
+ ///
+ /// This method allows the `Visitor` to avoid a copy by taking ownership of
+ /// a byte buffer created by the `Deserializer`. `Deserialize`
+ /// implementations that benefit from taking ownership of `Vec<u8>` data
+ /// should indicate that to the deserializer by using
+ /// `Deserializer::deserialize_byte_buf` rather than
+ /// `Deserializer::deserialize_bytes`, although not every deserializer will
+ /// honor such a request.
+ ///
+ /// It is never correct to implement `visit_byte_buf` without implementing
+ /// `visit_bytes`. Implement neither, both, or just `visit_bytes`.
+ ///
+ /// The default implementation forwards to `visit_bytes` and then drops the
+ /// `Vec<u8>`.
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ self.visit_bytes(&v)
+ }
+
+ /// The input contains an optional that is absent.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_none<E>(self) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Err(Error::invalid_type(Unexpected::Option, &self))
+ }
+
+ /// The input contains an optional that is present.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let _ = deserializer;
+ Err(Error::invalid_type(Unexpected::Option, &self))
+ }
+
+ /// The input contains a unit `()`.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ Err(Error::invalid_type(Unexpected::Unit, &self))
+ }
+
+ /// The input contains a newtype struct.
+ ///
+ /// The content of the newtype struct may be read from the given
+ /// `Deserializer`.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let _ = deserializer;
+ Err(Error::invalid_type(Unexpected::NewtypeStruct, &self))
+ }
+
+ /// The input contains a sequence of elements.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: SeqAccess<'de>,
+ {
+ let _ = seq;
+ Err(Error::invalid_type(Unexpected::Seq, &self))
+ }
+
+ /// The input contains a key-value map.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
+ where
+ A: MapAccess<'de>,
+ {
+ let _ = map;
+ Err(Error::invalid_type(Unexpected::Map, &self))
+ }
+
+ /// The input contains an enum.
+ ///
+ /// The default implementation fails with a type error.
+ fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ let _ = data;
+ Err(Error::invalid_type(Unexpected::Enum, &self))
+ }
+
+ // Used when deserializing a flattened Option field. Not public API.
+ #[doc(hidden)]
+ fn __private_visit_untagged_option<D>(self, _: D) -> Result<Self::Value, ()>
+ where
+ D: Deserializer<'de>,
+ {
+ Err(())
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// Provides a `Visitor` access to each element of a sequence in the input.
+///
+/// This is a trait that a `Deserializer` passes to a `Visitor` implementation,
+/// which deserializes each item in a sequence.
+///
+/// # Lifetime
+///
+/// The `'de` lifetime of this trait is the lifetime of data that may be
+/// borrowed by deserialized sequence elements. See the page [Understanding
+/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
+///
+/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `SeqAccess` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait SeqAccess<'de> {
+ /// The error type that can be returned if some error occurs during
+ /// deserialization.
+ type Error: Error;
+
+ /// This returns `Ok(Some(value))` for the next value in the sequence, or
+ /// `Ok(None)` if there are no more remaining items.
+ ///
+ /// `Deserialize` implementations should typically use
+ /// `SeqAccess::next_element` instead.
+ fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
+ where
+ T: DeserializeSeed<'de>;
+
+ /// This returns `Ok(Some(value))` for the next value in the sequence, or
+ /// `Ok(None)` if there are no more remaining items.
+ ///
+ /// This method exists as a convenience for `Deserialize` implementations.
+ /// `SeqAccess` implementations should not override the default behavior.
+ #[inline]
+ fn next_element<T>(&mut self) -> Result<Option<T>, Self::Error>
+ where
+ T: Deserialize<'de>,
+ {
+ self.next_element_seed(PhantomData)
+ }
+
+ /// Returns the number of elements remaining in the sequence, if known.
+ #[inline]
+ fn size_hint(&self) -> Option<usize> {
+ None
+ }
+}
+
+impl<'de, 'a, A: ?Sized> SeqAccess<'de> for &'a mut A
+where
+ A: SeqAccess<'de>,
+{
+ type Error = A::Error;
+
+ #[inline]
+ fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ (**self).next_element_seed(seed)
+ }
+
+ #[inline]
+ fn next_element<T>(&mut self) -> Result<Option<T>, Self::Error>
+ where
+ T: Deserialize<'de>,
+ {
+ (**self).next_element()
+ }
+
+ #[inline]
+ fn size_hint(&self) -> Option<usize> {
+ (**self).size_hint()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// Provides a `Visitor` access to each entry of a map in the input.
+///
+/// This is a trait that a `Deserializer` passes to a `Visitor` implementation.
+///
+/// # Lifetime
+///
+/// The `'de` lifetime of this trait is the lifetime of data that may be
+/// borrowed by deserialized map entries. See the page [Understanding
+/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
+///
+/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `MapAccess` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait MapAccess<'de> {
+ /// The error type that can be returned if some error occurs during
+ /// deserialization.
+ type Error: Error;
+
+ /// This returns `Ok(Some(key))` for the next key in the map, or `Ok(None)`
+ /// if there are no more remaining entries.
+ ///
+ /// `Deserialize` implementations should typically use
+ /// `MapAccess::next_key` or `MapAccess::next_entry` instead.
+ fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
+ where
+ K: DeserializeSeed<'de>;
+
+ /// This returns a `Ok(value)` for the next value in the map.
+ ///
+ /// `Deserialize` implementations should typically use
+ /// `MapAccess::next_value` instead.
+ ///
+ /// # Panics
+ ///
+ /// Calling `next_value_seed` before `next_key_seed` is incorrect and is
+ /// allowed to panic or return bogus results.
+ fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
+ where
+ V: DeserializeSeed<'de>;
+
+ /// This returns `Ok(Some((key, value)))` for the next (key-value) pair in
+ /// the map, or `Ok(None)` if there are no more remaining items.
+ ///
+ /// `MapAccess` implementations should override the default behavior if a
+ /// more efficient implementation is possible.
+ ///
+ /// `Deserialize` implementations should typically use
+ /// `MapAccess::next_entry` instead.
+ #[inline]
+ fn next_entry_seed<K, V>(
+ &mut self,
+ kseed: K,
+ vseed: V,
+ ) -> Result<Option<(K::Value, V::Value)>, Self::Error>
+ where
+ K: DeserializeSeed<'de>,
+ V: DeserializeSeed<'de>,
+ {
+ match try!(self.next_key_seed(kseed)) {
+ Some(key) => {
+ let value = try!(self.next_value_seed(vseed));
+ Ok(Some((key, value)))
+ }
+ None => Ok(None),
+ }
+ }
+
+ /// This returns `Ok(Some(key))` for the next key in the map, or `Ok(None)`
+ /// if there are no more remaining entries.
+ ///
+ /// This method exists as a convenience for `Deserialize` implementations.
+ /// `MapAccess` implementations should not override the default behavior.
+ #[inline]
+ fn next_key<K>(&mut self) -> Result<Option<K>, Self::Error>
+ where
+ K: Deserialize<'de>,
+ {
+ self.next_key_seed(PhantomData)
+ }
+
+ /// This returns a `Ok(value)` for the next value in the map.
+ ///
+ /// This method exists as a convenience for `Deserialize` implementations.
+ /// `MapAccess` implementations should not override the default behavior.
+ ///
+ /// # Panics
+ ///
+ /// Calling `next_value` before `next_key` is incorrect and is allowed to
+ /// panic or return bogus results.
+ #[inline]
+ fn next_value<V>(&mut self) -> Result<V, Self::Error>
+ where
+ V: Deserialize<'de>,
+ {
+ self.next_value_seed(PhantomData)
+ }
+
+ /// This returns `Ok(Some((key, value)))` for the next (key-value) pair in
+ /// the map, or `Ok(None)` if there are no more remaining items.
+ ///
+ /// This method exists as a convenience for `Deserialize` implementations.
+ /// `MapAccess` implementations should not override the default behavior.
+ #[inline]
+ fn next_entry<K, V>(&mut self) -> Result<Option<(K, V)>, Self::Error>
+ where
+ K: Deserialize<'de>,
+ V: Deserialize<'de>,
+ {
+ self.next_entry_seed(PhantomData, PhantomData)
+ }
+
+ /// Returns the number of entries remaining in the map, if known.
+ #[inline]
+ fn size_hint(&self) -> Option<usize> {
+ None
+ }
+}
+
+impl<'de, 'a, A: ?Sized> MapAccess<'de> for &'a mut A
+where
+ A: MapAccess<'de>,
+{
+ type Error = A::Error;
+
+ #[inline]
+ fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
+ where
+ K: DeserializeSeed<'de>,
+ {
+ (**self).next_key_seed(seed)
+ }
+
+ #[inline]
+ fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
+ where
+ V: DeserializeSeed<'de>,
+ {
+ (**self).next_value_seed(seed)
+ }
+
+ #[inline]
+ fn next_entry_seed<K, V>(
+ &mut self,
+ kseed: K,
+ vseed: V,
+ ) -> Result<Option<(K::Value, V::Value)>, Self::Error>
+ where
+ K: DeserializeSeed<'de>,
+ V: DeserializeSeed<'de>,
+ {
+ (**self).next_entry_seed(kseed, vseed)
+ }
+
+ #[inline]
+ fn next_entry<K, V>(&mut self) -> Result<Option<(K, V)>, Self::Error>
+ where
+ K: Deserialize<'de>,
+ V: Deserialize<'de>,
+ {
+ (**self).next_entry()
+ }
+
+ #[inline]
+ fn next_key<K>(&mut self) -> Result<Option<K>, Self::Error>
+ where
+ K: Deserialize<'de>,
+ {
+ (**self).next_key()
+ }
+
+ #[inline]
+ fn next_value<V>(&mut self) -> Result<V, Self::Error>
+ where
+ V: Deserialize<'de>,
+ {
+ (**self).next_value()
+ }
+
+ #[inline]
+ fn size_hint(&self) -> Option<usize> {
+ (**self).size_hint()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// Provides a `Visitor` access to the data of an enum in the input.
+///
+/// `EnumAccess` is created by the `Deserializer` and passed to the
+/// `Visitor` in order to identify which variant of an enum to deserialize.
+///
+/// # Lifetime
+///
+/// The `'de` lifetime of this trait is the lifetime of data that may be
+/// borrowed by the deserialized enum variant. See the page [Understanding
+/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
+///
+/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `EnumAccess` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait EnumAccess<'de>: Sized {
+ /// The error type that can be returned if some error occurs during
+ /// deserialization.
+ type Error: Error;
+ /// The `Visitor` that will be used to deserialize the content of the enum
+ /// variant.
+ type Variant: VariantAccess<'de, Error = Self::Error>;
+
+ /// `variant` is called to identify which variant to deserialize.
+ ///
+ /// `Deserialize` implementations should typically use `EnumAccess::variant`
+ /// instead.
+ fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
+ where
+ V: DeserializeSeed<'de>;
+
+ /// `variant` is called to identify which variant to deserialize.
+ ///
+ /// This method exists as a convenience for `Deserialize` implementations.
+ /// `EnumAccess` implementations should not override the default behavior.
+ #[inline]
+ fn variant<V>(self) -> Result<(V, Self::Variant), Self::Error>
+ where
+ V: Deserialize<'de>,
+ {
+ self.variant_seed(PhantomData)
+ }
+}
+
+/// `VariantAccess` is a visitor that is created by the `Deserializer` and
+/// passed to the `Deserialize` to deserialize the content of a particular enum
+/// variant.
+///
+/// # Lifetime
+///
+/// The `'de` lifetime of this trait is the lifetime of data that may be
+/// borrowed by the deserialized enum variant. See the page [Understanding
+/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
+///
+/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `VariantAccess` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait VariantAccess<'de>: Sized {
+ /// The error type that can be returned if some error occurs during
+ /// deserialization. Must match the error type of our `EnumAccess`.
+ type Error: Error;
+
+ /// Called when deserializing a variant with no values.
+ ///
+ /// If the data contains a different type of variant, the following
+ /// `invalid_type` error should be constructed:
+ ///
+ /// ```edition2018
+ /// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
+ /// #
+ /// # struct X;
+ /// #
+ /// # impl<'de> VariantAccess<'de> for X {
+ /// # type Error = value::Error;
+ /// #
+ /// fn unit_variant(self) -> Result<(), Self::Error> {
+ /// // What the data actually contained; suppose it is a tuple variant.
+ /// let unexp = Unexpected::TupleVariant;
+ /// Err(de::Error::invalid_type(unexp, &"unit variant"))
+ /// }
+ /// #
+ /// # fn newtype_variant_seed<T>(self, _: T) -> Result<T::Value, Self::Error>
+ /// # where
+ /// # T: DeserializeSeed<'de>,
+ /// # { unimplemented!() }
+ /// #
+ /// # fn tuple_variant<V>(self, _: usize, _: V) -> Result<V::Value, Self::Error>
+ /// # where
+ /// # V: Visitor<'de>,
+ /// # { unimplemented!() }
+ /// #
+ /// # fn struct_variant<V>(self, _: &[&str], _: V) -> Result<V::Value, Self::Error>
+ /// # where
+ /// # V: Visitor<'de>,
+ /// # { unimplemented!() }
+ /// # }
+ /// ```
+ fn unit_variant(self) -> Result<(), Self::Error>;
+
+ /// Called when deserializing a variant with a single value.
+ ///
+ /// `Deserialize` implementations should typically use
+ /// `VariantAccess::newtype_variant` instead.
+ ///
+ /// If the data contains a different type of variant, the following
+ /// `invalid_type` error should be constructed:
+ ///
+ /// ```edition2018
+ /// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
+ /// #
+ /// # struct X;
+ /// #
+ /// # impl<'de> VariantAccess<'de> for X {
+ /// # type Error = value::Error;
+ /// #
+ /// # fn unit_variant(self) -> Result<(), Self::Error> {
+ /// # unimplemented!()
+ /// # }
+ /// #
+ /// fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
+ /// where
+ /// T: DeserializeSeed<'de>,
+ /// {
+ /// // What the data actually contained; suppose it is a unit variant.
+ /// let unexp = Unexpected::UnitVariant;
+ /// Err(de::Error::invalid_type(unexp, &"newtype variant"))
+ /// }
+ /// #
+ /// # fn tuple_variant<V>(self, _: usize, _: V) -> Result<V::Value, Self::Error>
+ /// # where
+ /// # V: Visitor<'de>,
+ /// # { unimplemented!() }
+ /// #
+ /// # fn struct_variant<V>(self, _: &[&str], _: V) -> Result<V::Value, Self::Error>
+ /// # where
+ /// # V: Visitor<'de>,
+ /// # { unimplemented!() }
+ /// # }
+ /// ```
+ fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
+ where
+ T: DeserializeSeed<'de>;
+
+ /// Called when deserializing a variant with a single value.
+ ///
+ /// This method exists as a convenience for `Deserialize` implementations.
+ /// `VariantAccess` implementations should not override the default
+ /// behavior.
+ #[inline]
+ fn newtype_variant<T>(self) -> Result<T, Self::Error>
+ where
+ T: Deserialize<'de>,
+ {
+ self.newtype_variant_seed(PhantomData)
+ }
+
+ /// Called when deserializing a tuple-like variant.
+ ///
+ /// The `len` is the number of fields expected in the tuple variant.
+ ///
+ /// If the data contains a different type of variant, the following
+ /// `invalid_type` error should be constructed:
+ ///
+ /// ```edition2018
+ /// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
+ /// #
+ /// # struct X;
+ /// #
+ /// # impl<'de> VariantAccess<'de> for X {
+ /// # type Error = value::Error;
+ /// #
+ /// # fn unit_variant(self) -> Result<(), Self::Error> {
+ /// # unimplemented!()
+ /// # }
+ /// #
+ /// # fn newtype_variant_seed<T>(self, _: T) -> Result<T::Value, Self::Error>
+ /// # where
+ /// # T: DeserializeSeed<'de>,
+ /// # { unimplemented!() }
+ /// #
+ /// fn tuple_variant<V>(
+ /// self,
+ /// _len: usize,
+ /// _visitor: V,
+ /// ) -> Result<V::Value, Self::Error>
+ /// where
+ /// V: Visitor<'de>,
+ /// {
+ /// // What the data actually contained; suppose it is a unit variant.
+ /// let unexp = Unexpected::UnitVariant;
+ /// Err(de::Error::invalid_type(unexp, &"tuple variant"))
+ /// }
+ /// #
+ /// # fn struct_variant<V>(self, _: &[&str], _: V) -> Result<V::Value, Self::Error>
+ /// # where
+ /// # V: Visitor<'de>,
+ /// # { unimplemented!() }
+ /// # }
+ /// ```
+ fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+
+ /// Called when deserializing a struct-like variant.
+ ///
+ /// The `fields` are the names of the fields of the struct variant.
+ ///
+ /// If the data contains a different type of variant, the following
+ /// `invalid_type` error should be constructed:
+ ///
+ /// ```edition2018
+ /// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
+ /// #
+ /// # struct X;
+ /// #
+ /// # impl<'de> VariantAccess<'de> for X {
+ /// # type Error = value::Error;
+ /// #
+ /// # fn unit_variant(self) -> Result<(), Self::Error> {
+ /// # unimplemented!()
+ /// # }
+ /// #
+ /// # fn newtype_variant_seed<T>(self, _: T) -> Result<T::Value, Self::Error>
+ /// # where
+ /// # T: DeserializeSeed<'de>,
+ /// # { unimplemented!() }
+ /// #
+ /// # fn tuple_variant<V>(self, _: usize, _: V) -> Result<V::Value, Self::Error>
+ /// # where
+ /// # V: Visitor<'de>,
+ /// # { unimplemented!() }
+ /// #
+ /// fn struct_variant<V>(
+ /// self,
+ /// _fields: &'static [&'static str],
+ /// _visitor: V,
+ /// ) -> Result<V::Value, Self::Error>
+ /// where
+ /// V: Visitor<'de>,
+ /// {
+ /// // What the data actually contained; suppose it is a unit variant.
+ /// let unexp = Unexpected::UnitVariant;
+ /// Err(de::Error::invalid_type(unexp, &"struct variant"))
+ /// }
+ /// # }
+ /// ```
+ fn struct_variant<V>(
+ self,
+ fields: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// Converts an existing value into a `Deserializer` from which other values can
+/// be deserialized.
+///
+/// # Lifetime
+///
+/// The `'de` lifetime of this trait is the lifetime of data that may be
+/// borrowed from the resulting `Deserializer`. See the page [Understanding
+/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
+///
+/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
+///
+/// # Example
+///
+/// ```edition2018
+/// use std::str::FromStr;
+/// use serde::Deserialize;
+/// use serde::de::{value, IntoDeserializer};
+///
+/// #[derive(Deserialize)]
+/// enum Setting {
+/// On,
+/// Off,
+/// }
+///
+/// impl FromStr for Setting {
+/// type Err = value::Error;
+///
+/// fn from_str(s: &str) -> Result<Self, Self::Err> {
+/// Self::deserialize(s.into_deserializer())
+/// }
+/// }
+/// ```
+pub trait IntoDeserializer<'de, E: Error = value::Error> {
+ /// The type of the deserializer being converted into.
+ type Deserializer: Deserializer<'de, Error = E>;
+
+ /// Convert this value into a deserializer.
+ fn into_deserializer(self) -> Self::Deserializer;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// Used in error messages.
+///
+/// - expected `a`
+/// - expected `a` or `b`
+/// - expected one of `a`, `b`, `c`
+///
+/// The slice of names must not be empty.
+struct OneOf {
+ names: &'static [&'static str],
+}
+
+impl Display for OneOf {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ match self.names.len() {
+ 0 => panic!(), // special case elsewhere
+ 1 => write!(formatter, "`{}`", self.names[0]),
+ 2 => write!(formatter, "`{}` or `{}`", self.names[0], self.names[1]),
+ _ => {
+ try!(write!(formatter, "one of "));
+ for (i, alt) in self.names.iter().enumerate() {
+ if i > 0 {
+ try!(write!(formatter, ", "));
+ }
+ try!(write!(formatter, "`{}`", alt));
+ }
+ Ok(())
+ }
+ }
+ }
+}
diff --git a/third_party/rust/serde/src/de/seed.rs b/third_party/rust/serde/src/de/seed.rs
new file mode 100644
index 0000000000..13b7ea461b
--- /dev/null
+++ b/third_party/rust/serde/src/de/seed.rs
@@ -0,0 +1,19 @@
+use de::{Deserialize, DeserializeSeed, Deserializer};
+
+/// 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)
+ }
+}
diff --git a/third_party/rust/serde/src/de/utf8.rs b/third_party/rust/serde/src/de/utf8.rs
new file mode 100644
index 0000000000..576fd03cfa
--- /dev/null
+++ b/third_party/rust/serde/src/de/utf8.rs
@@ -0,0 +1,46 @@
+use lib::*;
+
+const TAG_CONT: u8 = 0b1000_0000;
+const TAG_TWO_B: u8 = 0b1100_0000;
+const TAG_THREE_B: u8 = 0b1110_0000;
+const TAG_FOUR_B: u8 = 0b1111_0000;
+const MAX_ONE_B: u32 = 0x80;
+const MAX_TWO_B: u32 = 0x800;
+const MAX_THREE_B: u32 = 0x10000;
+
+#[inline]
+pub fn encode(c: char) -> Encode {
+ let code = c as u32;
+ let mut buf = [0; 4];
+ let pos = if code < MAX_ONE_B {
+ buf[3] = code as u8;
+ 3
+ } else if code < MAX_TWO_B {
+ buf[2] = (code >> 6 & 0x1F) as u8 | TAG_TWO_B;
+ buf[3] = (code & 0x3F) as u8 | TAG_CONT;
+ 2
+ } else if code < MAX_THREE_B {
+ buf[1] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
+ buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
+ buf[3] = (code & 0x3F) as u8 | TAG_CONT;
+ 1
+ } else {
+ buf[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
+ buf[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT;
+ buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
+ buf[3] = (code & 0x3F) as u8 | TAG_CONT;
+ 0
+ };
+ Encode { buf: buf, pos: pos }
+}
+
+pub struct Encode {
+ buf: [u8; 4],
+ pos: usize,
+}
+
+impl Encode {
+ pub fn as_str(&self) -> &str {
+ str::from_utf8(&self.buf[self.pos..]).unwrap()
+ }
+}
diff --git a/third_party/rust/serde/src/de/value.rs b/third_party/rust/serde/src/de/value.rs
new file mode 100644
index 0000000000..5d88862159
--- /dev/null
+++ b/third_party/rust/serde/src/de/value.rs
@@ -0,0 +1,1714 @@
+//! Building blocks for deserializing basic values using the `IntoDeserializer`
+//! trait.
+//!
+//! ```edition2018
+//! use std::str::FromStr;
+//! use serde::Deserialize;
+//! use serde::de::{value, IntoDeserializer};
+//!
+//! #[derive(Deserialize)]
+//! enum Setting {
+//! On,
+//! Off,
+//! }
+//!
+//! impl FromStr for Setting {
+//! type Err = value::Error;
+//!
+//! fn from_str(s: &str) -> Result<Self, Self::Err> {
+//! Self::deserialize(s.into_deserializer())
+//! }
+//! }
+//! ```
+
+use lib::*;
+
+use self::private::{First, Second};
+use __private::size_hint;
+use de::{self, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor};
+use ser;
+
+////////////////////////////////////////////////////////////////////////////////
+
+// For structs that contain a PhantomData. We do not want the trait
+// bound `E: Clone` inferred by derive(Clone).
+macro_rules! impl_copy_clone {
+ ($ty:ident $(<$lifetime:tt>)*) => {
+ impl<$($lifetime,)* E> Copy for $ty<$($lifetime,)* E> {}
+
+ impl<$($lifetime,)* E> Clone for $ty<$($lifetime,)* E> {
+ fn clone(&self) -> Self {
+ *self
+ }
+ }
+ };
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A minimal representation of all possible errors that can occur using the
+/// `IntoDeserializer` trait.
+#[derive(Clone, PartialEq)]
+pub struct Error {
+ err: ErrorImpl,
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+type ErrorImpl = Box<str>;
+#[cfg(not(any(feature = "std", feature = "alloc")))]
+type ErrorImpl = ();
+
+impl de::Error for Error {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cold]
+ fn custom<T>(msg: T) -> Self
+ where
+ T: Display,
+ {
+ Error {
+ err: msg.to_string().into_boxed_str(),
+ }
+ }
+
+ #[cfg(not(any(feature = "std", feature = "alloc")))]
+ #[cold]
+ fn custom<T>(msg: T) -> Self
+ where
+ T: Display,
+ {
+ let _ = msg;
+ Error { err: () }
+ }
+}
+
+impl ser::Error for Error {
+ #[cold]
+ fn custom<T>(msg: T) -> Self
+ where
+ T: Display,
+ {
+ de::Error::custom(msg)
+ }
+}
+
+impl Display for Error {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(&self.err)
+ }
+
+ #[cfg(not(any(feature = "std", feature = "alloc")))]
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("Serde deserialization error")
+ }
+}
+
+impl Debug for Error {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ let mut debug = formatter.debug_tuple("Error");
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ debug.field(&self.err);
+ debug.finish()
+ }
+}
+
+#[cfg(feature = "std")]
+impl error::Error for Error {
+ fn description(&self) -> &str {
+ &self.err
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<'de, E> IntoDeserializer<'de, E> for ()
+where
+ E: de::Error,
+{
+ type Deserializer = UnitDeserializer<E>;
+
+ fn into_deserializer(self) -> UnitDeserializer<E> {
+ UnitDeserializer::new()
+ }
+}
+
+/// A deserializer holding a `()`.
+pub struct UnitDeserializer<E> {
+ marker: PhantomData<E>,
+}
+
+impl_copy_clone!(UnitDeserializer);
+
+impl<E> UnitDeserializer<E> {
+ #[allow(missing_docs)]
+ pub fn new() -> Self {
+ UnitDeserializer {
+ marker: PhantomData,
+ }
+ }
+}
+
+impl<'de, E> de::Deserializer<'de> for UnitDeserializer<E>
+where
+ E: de::Error,
+{
+ type Error = E;
+
+ 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
+ }
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_unit()
+ }
+
+ fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_none()
+ }
+}
+
+impl<E> Debug for UnitDeserializer<E> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.debug_struct("UnitDeserializer").finish()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A deserializer that cannot be instantiated.
+#[cfg(feature = "unstable")]
+pub struct NeverDeserializer<E> {
+ never: !,
+ marker: PhantomData<E>,
+}
+
+#[cfg(feature = "unstable")]
+impl<'de, E> IntoDeserializer<'de, E> for !
+where
+ E: de::Error,
+{
+ type Deserializer = NeverDeserializer<E>;
+
+ fn into_deserializer(self) -> Self::Deserializer {
+ self
+ }
+}
+
+#[cfg(feature = "unstable")]
+impl<'de, E> de::Deserializer<'de> for NeverDeserializer<E>
+where
+ E: de::Error,
+{
+ type Error = E;
+
+ fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.never
+ }
+
+ 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
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! primitive_deserializer {
+ ($ty:ty, $doc:tt, $name:ident, $method:ident $($cast:tt)*) => {
+ #[doc = "A deserializer holding"]
+ #[doc = $doc]
+ pub struct $name<E> {
+ value: $ty,
+ marker: PhantomData<E>
+ }
+
+ impl_copy_clone!($name);
+
+ impl<'de, E> IntoDeserializer<'de, E> for $ty
+ where
+ E: de::Error,
+ {
+ type Deserializer = $name<E>;
+
+ fn into_deserializer(self) -> $name<E> {
+ $name::new(self)
+ }
+ }
+
+ impl<E> $name<E> {
+ #[allow(missing_docs)]
+ pub fn new(value: $ty) -> Self {
+ $name {
+ value: value,
+ marker: PhantomData,
+ }
+ }
+ }
+
+ impl<'de, E> de::Deserializer<'de> for $name<E>
+ where
+ E: de::Error,
+ {
+ type Error = E;
+
+ 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
+ }
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.$method(self.value $($cast)*)
+ }
+ }
+
+ impl<E> Debug for $name<E> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_struct(stringify!($name))
+ .field("value", &self.value)
+ .finish()
+ }
+ }
+ }
+}
+
+primitive_deserializer!(bool, "a `bool`.", BoolDeserializer, visit_bool);
+primitive_deserializer!(i8, "an `i8`.", I8Deserializer, visit_i8);
+primitive_deserializer!(i16, "an `i16`.", I16Deserializer, visit_i16);
+primitive_deserializer!(i32, "an `i32`.", I32Deserializer, visit_i32);
+primitive_deserializer!(i64, "an `i64`.", I64Deserializer, visit_i64);
+primitive_deserializer!(isize, "an `isize`.", IsizeDeserializer, visit_i64 as i64);
+primitive_deserializer!(u8, "a `u8`.", U8Deserializer, visit_u8);
+primitive_deserializer!(u16, "a `u16`.", U16Deserializer, visit_u16);
+primitive_deserializer!(u64, "a `u64`.", U64Deserializer, visit_u64);
+primitive_deserializer!(usize, "a `usize`.", UsizeDeserializer, visit_u64 as u64);
+primitive_deserializer!(f32, "an `f32`.", F32Deserializer, visit_f32);
+primitive_deserializer!(f64, "an `f64`.", F64Deserializer, visit_f64);
+primitive_deserializer!(char, "a `char`.", CharDeserializer, visit_char);
+
+serde_if_integer128! {
+ primitive_deserializer!(i128, "an `i128`.", I128Deserializer, visit_i128);
+ primitive_deserializer!(u128, "a `u128`.", U128Deserializer, visit_u128);
+}
+
+/// A deserializer holding a `u32`.
+pub struct U32Deserializer<E> {
+ value: u32,
+ marker: PhantomData<E>,
+}
+
+impl_copy_clone!(U32Deserializer);
+
+impl<'de, E> IntoDeserializer<'de, E> for u32
+where
+ E: de::Error,
+{
+ type Deserializer = U32Deserializer<E>;
+
+ fn into_deserializer(self) -> U32Deserializer<E> {
+ U32Deserializer::new(self)
+ }
+}
+
+impl<E> U32Deserializer<E> {
+ #[allow(missing_docs)]
+ pub fn new(value: u32) -> Self {
+ U32Deserializer {
+ value: value,
+ marker: PhantomData,
+ }
+ }
+}
+
+impl<'de, E> de::Deserializer<'de> for U32Deserializer<E>
+where
+ E: de::Error,
+{
+ type Error = E;
+
+ 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 identifier ignored_any
+ }
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_u32(self.value)
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ name: &str,
+ variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let _ = name;
+ let _ = variants;
+ visitor.visit_enum(self)
+ }
+}
+
+impl<'de, E> de::EnumAccess<'de> for U32Deserializer<E>
+where
+ E: de::Error,
+{
+ type Error = E;
+ type Variant = private::UnitOnly<E>;
+
+ fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ seed.deserialize(self).map(private::unit_only)
+ }
+}
+
+impl<E> Debug for U32Deserializer<E> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_struct("U32Deserializer")
+ .field("value", &self.value)
+ .finish()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A deserializer holding a `&str`.
+pub struct StrDeserializer<'a, E> {
+ value: &'a str,
+ marker: PhantomData<E>,
+}
+
+impl_copy_clone!(StrDeserializer<'de>);
+
+impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a str
+where
+ E: de::Error,
+{
+ type Deserializer = StrDeserializer<'a, E>;
+
+ fn into_deserializer(self) -> StrDeserializer<'a, E> {
+ StrDeserializer::new(self)
+ }
+}
+
+impl<'a, E> StrDeserializer<'a, E> {
+ #[allow(missing_docs)]
+ pub fn new(value: &'a str) -> Self {
+ StrDeserializer {
+ value: value,
+ marker: PhantomData,
+ }
+ }
+}
+
+impl<'de, 'a, E> de::Deserializer<'de> for StrDeserializer<'a, E>
+where
+ E: de::Error,
+{
+ type Error = E;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_str(self.value)
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ name: &str,
+ variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let _ = name;
+ let _ = variants;
+ visitor.visit_enum(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 identifier ignored_any
+ }
+}
+
+impl<'de, 'a, E> de::EnumAccess<'de> for StrDeserializer<'a, E>
+where
+ E: de::Error,
+{
+ type Error = E;
+ type Variant = private::UnitOnly<E>;
+
+ fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ seed.deserialize(self).map(private::unit_only)
+ }
+}
+
+impl<'a, E> Debug for StrDeserializer<'a, E> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_struct("StrDeserializer")
+ .field("value", &self.value)
+ .finish()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A deserializer holding a `&str` with a lifetime tied to another
+/// deserializer.
+pub struct BorrowedStrDeserializer<'de, E> {
+ value: &'de str,
+ marker: PhantomData<E>,
+}
+
+impl_copy_clone!(BorrowedStrDeserializer<'de>);
+
+impl<'de, E> BorrowedStrDeserializer<'de, E> {
+ /// Create a new borrowed deserializer from the given string.
+ pub fn new(value: &'de str) -> BorrowedStrDeserializer<'de, E> {
+ BorrowedStrDeserializer {
+ value: value,
+ marker: PhantomData,
+ }
+ }
+}
+
+impl<'de, E> de::Deserializer<'de> for BorrowedStrDeserializer<'de, E>
+where
+ E: de::Error,
+{
+ type Error = E;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_borrowed_str(self.value)
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ name: &str,
+ variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let _ = name;
+ let _ = variants;
+ visitor.visit_enum(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 identifier ignored_any
+ }
+}
+
+impl<'de, E> de::EnumAccess<'de> for BorrowedStrDeserializer<'de, E>
+where
+ E: de::Error,
+{
+ type Error = E;
+ type Variant = private::UnitOnly<E>;
+
+ fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ seed.deserialize(self).map(private::unit_only)
+ }
+}
+
+impl<'de, E> Debug for BorrowedStrDeserializer<'de, E> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_struct("BorrowedStrDeserializer")
+ .field("value", &self.value)
+ .finish()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A deserializer holding a `String`.
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub struct StringDeserializer<E> {
+ value: String,
+ marker: PhantomData<E>,
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<E> Clone for StringDeserializer<E> {
+ fn clone(&self) -> Self {
+ StringDeserializer {
+ value: self.value.clone(),
+ marker: PhantomData,
+ }
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, E> IntoDeserializer<'de, E> for String
+where
+ E: de::Error,
+{
+ type Deserializer = StringDeserializer<E>;
+
+ fn into_deserializer(self) -> StringDeserializer<E> {
+ StringDeserializer::new(self)
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<E> StringDeserializer<E> {
+ #[allow(missing_docs)]
+ pub fn new(value: String) -> Self {
+ StringDeserializer {
+ value: value,
+ marker: PhantomData,
+ }
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, E> de::Deserializer<'de> for StringDeserializer<E>
+where
+ E: de::Error,
+{
+ type Error = E;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_string(self.value)
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ name: &str,
+ variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let _ = name;
+ let _ = variants;
+ visitor.visit_enum(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 identifier ignored_any
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, E> de::EnumAccess<'de> for StringDeserializer<E>
+where
+ E: de::Error,
+{
+ type Error = E;
+ type Variant = private::UnitOnly<E>;
+
+ fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ seed.deserialize(self).map(private::unit_only)
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<E> Debug for StringDeserializer<E> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_struct("StringDeserializer")
+ .field("value", &self.value)
+ .finish()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A deserializer holding a `Cow<str>`.
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub struct CowStrDeserializer<'a, E> {
+ value: Cow<'a, str>,
+ marker: PhantomData<E>,
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, E> Clone for CowStrDeserializer<'a, E> {
+ fn clone(&self) -> Self {
+ CowStrDeserializer {
+ value: self.value.clone(),
+ marker: PhantomData,
+ }
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str>
+where
+ E: de::Error,
+{
+ type Deserializer = CowStrDeserializer<'a, E>;
+
+ fn into_deserializer(self) -> CowStrDeserializer<'a, E> {
+ CowStrDeserializer::new(self)
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, E> CowStrDeserializer<'a, E> {
+ #[allow(missing_docs)]
+ pub fn new(value: Cow<'a, str>) -> Self {
+ CowStrDeserializer {
+ value: value,
+ marker: PhantomData,
+ }
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E>
+where
+ E: de::Error,
+{
+ type Error = E;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ match self.value {
+ Cow::Borrowed(string) => visitor.visit_str(string),
+ Cow::Owned(string) => visitor.visit_string(string),
+ }
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ name: &str,
+ variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let _ = name;
+ let _ = variants;
+ visitor.visit_enum(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 identifier ignored_any
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, 'a, E> de::EnumAccess<'de> for CowStrDeserializer<'a, E>
+where
+ E: de::Error,
+{
+ type Error = E;
+ type Variant = private::UnitOnly<E>;
+
+ fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ seed.deserialize(self).map(private::unit_only)
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, E> Debug for CowStrDeserializer<'a, E> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_struct("CowStrDeserializer")
+ .field("value", &self.value)
+ .finish()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A deserializer holding a `&[u8]`. Always calls [`Visitor::visit_bytes`].
+pub struct BytesDeserializer<'a, E> {
+ value: &'a [u8],
+ marker: PhantomData<E>,
+}
+
+impl<'a, E> BytesDeserializer<'a, E> {
+ /// Create a new deserializer from the given bytes.
+ pub fn new(value: &'a [u8]) -> Self {
+ BytesDeserializer {
+ value: value,
+ marker: PhantomData,
+ }
+ }
+}
+
+impl_copy_clone!(BytesDeserializer<'a>);
+
+impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a [u8]
+where
+ E: de::Error,
+{
+ type Deserializer = BytesDeserializer<'a, E>;
+
+ fn into_deserializer(self) -> BytesDeserializer<'a, E> {
+ BytesDeserializer::new(self)
+ }
+}
+
+impl<'de, 'a, E> Deserializer<'de> for BytesDeserializer<'a, E>
+where
+ E: de::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
+ }
+}
+
+impl<'a, E> Debug for BytesDeserializer<'a, E> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_struct("BytesDeserializer")
+ .field("value", &self.value)
+ .finish()
+ }
+}
+
+/// A deserializer holding a `&[u8]` with a lifetime tied to another
+/// deserializer. Always calls [`Visitor::visit_borrowed_bytes`].
+pub struct BorrowedBytesDeserializer<'de, E> {
+ value: &'de [u8],
+ marker: PhantomData<E>,
+}
+
+impl<'de, E> BorrowedBytesDeserializer<'de, E> {
+ /// Create a new borrowed deserializer from the given borrowed bytes.
+ pub fn new(value: &'de [u8]) -> Self {
+ BorrowedBytesDeserializer {
+ value: value,
+ marker: PhantomData,
+ }
+ }
+}
+
+impl_copy_clone!(BorrowedBytesDeserializer<'de>);
+
+impl<'de, E> Deserializer<'de> for BorrowedBytesDeserializer<'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>,
+ {
+ visitor.visit_borrowed_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
+ }
+}
+
+impl<'de, E> Debug for BorrowedBytesDeserializer<'de, E> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_struct("BorrowedBytesDeserializer")
+ .field("value", &self.value)
+ .finish()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A deserializer that iterates over a sequence.
+#[derive(Clone)]
+pub struct SeqDeserializer<I, E> {
+ iter: iter::Fuse<I>,
+ count: usize,
+ marker: PhantomData<E>,
+}
+
+impl<I, E> SeqDeserializer<I, E>
+where
+ I: Iterator,
+{
+ /// Construct a new `SeqDeserializer<I, E>`.
+ pub fn new(iter: I) -> Self {
+ SeqDeserializer {
+ iter: iter.fuse(),
+ count: 0,
+ marker: PhantomData,
+ }
+ }
+}
+
+impl<I, E> SeqDeserializer<I, E>
+where
+ I: Iterator,
+ E: de::Error,
+{
+ /// Check for remaining elements after passing a `SeqDeserializer` to
+ /// `Visitor::visit_seq`.
+ pub fn end(self) -> Result<(), E> {
+ let remaining = self.iter.count();
+ if remaining == 0 {
+ Ok(())
+ } else {
+ // First argument is the number of elements in the data, second
+ // argument is the number of elements expected by the Deserialize.
+ Err(de::Error::invalid_length(
+ self.count + remaining,
+ &ExpectedInSeq(self.count),
+ ))
+ }
+ }
+}
+
+impl<'de, I, T, E> de::Deserializer<'de> for SeqDeserializer<I, E>
+where
+ I: Iterator<Item = T>,
+ T: IntoDeserializer<'de, E>,
+ E: de::Error,
+{
+ type Error = E;
+
+ fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let v = try!(visitor.visit_seq(&mut self));
+ try!(self.end());
+ Ok(v)
+ }
+
+ 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, I, T, E> de::SeqAccess<'de> for SeqDeserializer<I, E>
+where
+ I: Iterator<Item = T>,
+ T: IntoDeserializer<'de, E>,
+ E: de::Error,
+{
+ type Error = E;
+
+ fn next_element_seed<V>(&mut self, seed: V) -> Result<Option<V::Value>, Self::Error>
+ where
+ V: de::DeserializeSeed<'de>,
+ {
+ match self.iter.next() {
+ Some(value) => {
+ self.count += 1;
+ seed.deserialize(value.into_deserializer()).map(Some)
+ }
+ None => Ok(None),
+ }
+ }
+
+ fn size_hint(&self) -> Option<usize> {
+ size_hint::from_bounds(&self.iter)
+ }
+}
+
+struct ExpectedInSeq(usize);
+
+impl Expected for ExpectedInSeq {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ if self.0 == 1 {
+ write!(formatter, "1 element in sequence")
+ } else {
+ write!(formatter, "{} elements in sequence", self.0)
+ }
+ }
+}
+
+impl<I, E> Debug for SeqDeserializer<I, E>
+where
+ I: Debug,
+{
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_struct("SeqDeserializer")
+ .field("iter", &self.iter)
+ .field("count", &self.count)
+ .finish()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, T, E> IntoDeserializer<'de, E> for Vec<T>
+where
+ T: IntoDeserializer<'de, E>,
+ E: de::Error,
+{
+ type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>;
+
+ fn into_deserializer(self) -> Self::Deserializer {
+ SeqDeserializer::new(self.into_iter())
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet<T>
+where
+ T: IntoDeserializer<'de, E> + Eq + Ord,
+ E: de::Error,
+{
+ type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>;
+
+ fn into_deserializer(self) -> Self::Deserializer {
+ SeqDeserializer::new(self.into_iter())
+ }
+}
+
+#[cfg(feature = "std")]
+impl<'de, T, S, E> IntoDeserializer<'de, E> for HashSet<T, S>
+where
+ T: IntoDeserializer<'de, E> + Eq + Hash,
+ S: BuildHasher,
+ E: de::Error,
+{
+ type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>;
+
+ fn into_deserializer(self) -> Self::Deserializer {
+ SeqDeserializer::new(self.into_iter())
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A deserializer holding a `SeqAccess`.
+#[derive(Clone, Debug)]
+pub struct SeqAccessDeserializer<A> {
+ seq: A,
+}
+
+impl<A> SeqAccessDeserializer<A> {
+ /// Construct a new `SeqAccessDeserializer<A>`.
+ pub fn new(seq: A) -> Self {
+ SeqAccessDeserializer { seq: seq }
+ }
+}
+
+impl<'de, A> de::Deserializer<'de> for SeqAccessDeserializer<A>
+where
+ A: de::SeqAccess<'de>,
+{
+ type Error = A::Error;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_seq(self.seq)
+ }
+
+ 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 deserializer that iterates over a map.
+pub struct MapDeserializer<'de, I, E>
+where
+ I: Iterator,
+ I::Item: private::Pair,
+{
+ iter: iter::Fuse<I>,
+ value: Option<Second<I::Item>>,
+ count: usize,
+ lifetime: PhantomData<&'de ()>,
+ error: PhantomData<E>,
+}
+
+impl<'de, I, E> MapDeserializer<'de, I, E>
+where
+ I: Iterator,
+ I::Item: private::Pair,
+{
+ /// Construct a new `MapDeserializer<I, E>`.
+ pub fn new(iter: I) -> Self {
+ MapDeserializer {
+ iter: iter.fuse(),
+ value: None,
+ count: 0,
+ lifetime: PhantomData,
+ error: PhantomData,
+ }
+ }
+}
+
+impl<'de, I, E> MapDeserializer<'de, I, E>
+where
+ I: Iterator,
+ I::Item: private::Pair,
+ E: de::Error,
+{
+ /// Check for remaining elements after passing a `MapDeserializer` to
+ /// `Visitor::visit_map`.
+ pub fn end(self) -> Result<(), E> {
+ let remaining = self.iter.count();
+ if remaining == 0 {
+ Ok(())
+ } else {
+ // First argument is the number of elements in the data, second
+ // argument is the number of elements expected by the Deserialize.
+ Err(de::Error::invalid_length(
+ self.count + remaining,
+ &ExpectedInMap(self.count),
+ ))
+ }
+ }
+}
+
+impl<'de, I, E> MapDeserializer<'de, I, E>
+where
+ I: Iterator,
+ I::Item: private::Pair,
+{
+ fn next_pair(&mut self) -> Option<(First<I::Item>, Second<I::Item>)> {
+ match self.iter.next() {
+ Some(kv) => {
+ self.count += 1;
+ Some(private::Pair::split(kv))
+ }
+ None => None,
+ }
+ }
+}
+
+impl<'de, I, E> de::Deserializer<'de> for MapDeserializer<'de, I, E>
+where
+ I: Iterator,
+ I::Item: private::Pair,
+ First<I::Item>: IntoDeserializer<'de, E>,
+ Second<I::Item>: IntoDeserializer<'de, E>,
+ E: de::Error,
+{
+ type Error = E;
+
+ fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let value = try!(visitor.visit_map(&mut self));
+ try!(self.end());
+ Ok(value)
+ }
+
+ fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let value = try!(visitor.visit_seq(&mut self));
+ try!(self.end());
+ Ok(value)
+ }
+
+ fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let _ = len;
+ self.deserialize_seq(visitor)
+ }
+
+ 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 tuple_struct map
+ struct enum identifier ignored_any
+ }
+}
+
+impl<'de, I, E> de::MapAccess<'de> for MapDeserializer<'de, I, E>
+where
+ I: Iterator,
+ I::Item: private::Pair,
+ First<I::Item>: IntoDeserializer<'de, E>,
+ Second<I::Item>: IntoDeserializer<'de, E>,
+ 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.next_pair() {
+ Some((key, value)) => {
+ self.value = Some(value);
+ seed.deserialize(key.into_deserializer()).map(Some)
+ }
+ None => Ok(None),
+ }
+ }
+
+ fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ let value = self.value.take();
+ // Panic because this indicates a bug in the program rather than an
+ // expected failure.
+ let value = value.expect("MapAccess::next_value called before next_key");
+ seed.deserialize(value.into_deserializer())
+ }
+
+ fn next_entry_seed<TK, TV>(
+ &mut self,
+ kseed: TK,
+ vseed: TV,
+ ) -> Result<Option<(TK::Value, TV::Value)>, Self::Error>
+ where
+ TK: de::DeserializeSeed<'de>,
+ TV: de::DeserializeSeed<'de>,
+ {
+ match self.next_pair() {
+ Some((key, value)) => {
+ let key = try!(kseed.deserialize(key.into_deserializer()));
+ let value = try!(vseed.deserialize(value.into_deserializer()));
+ Ok(Some((key, value)))
+ }
+ None => Ok(None),
+ }
+ }
+
+ fn size_hint(&self) -> Option<usize> {
+ size_hint::from_bounds(&self.iter)
+ }
+}
+
+impl<'de, I, E> de::SeqAccess<'de> for MapDeserializer<'de, I, E>
+where
+ I: Iterator,
+ I::Item: private::Pair,
+ First<I::Item>: IntoDeserializer<'de, E>,
+ Second<I::Item>: IntoDeserializer<'de, E>,
+ 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.next_pair() {
+ Some((k, v)) => {
+ let de = PairDeserializer(k, v, PhantomData);
+ seed.deserialize(de).map(Some)
+ }
+ None => Ok(None),
+ }
+ }
+
+ fn size_hint(&self) -> Option<usize> {
+ size_hint::from_bounds(&self.iter)
+ }
+}
+
+// Cannot #[derive(Clone)] because of the bound `Second<I::Item>: Clone`.
+impl<'de, I, E> Clone for MapDeserializer<'de, I, E>
+where
+ I: Iterator + Clone,
+ I::Item: private::Pair,
+ Second<I::Item>: Clone,
+{
+ fn clone(&self) -> Self {
+ MapDeserializer {
+ iter: self.iter.clone(),
+ value: self.value.clone(),
+ count: self.count,
+ lifetime: self.lifetime,
+ error: self.error,
+ }
+ }
+}
+
+impl<'de, I, E> Debug for MapDeserializer<'de, I, E>
+where
+ I: Iterator + Debug,
+ I::Item: private::Pair,
+ Second<I::Item>: Debug,
+{
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_struct("MapDeserializer")
+ .field("iter", &self.iter)
+ .field("value", &self.value)
+ .field("count", &self.count)
+ .finish()
+ }
+}
+
+// Used in the `impl SeqAccess for MapDeserializer` to visit the map as a
+// sequence of pairs.
+struct PairDeserializer<A, B, E>(A, B, PhantomData<E>);
+
+impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer<A, B, E>
+where
+ A: IntoDeserializer<'de, E>,
+ B: IntoDeserializer<'de, E>,
+ E: de::Error,
+{
+ type Error = E;
+
+ 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 tuple_struct map
+ struct enum identifier ignored_any
+ }
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.deserialize_seq(visitor)
+ }
+
+ fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData);
+ let pair = try!(visitor.visit_seq(&mut pair_visitor));
+ if pair_visitor.1.is_none() {
+ Ok(pair)
+ } else {
+ let remaining = pair_visitor.size_hint().unwrap();
+ // First argument is the number of elements in the data, second
+ // argument is the number of elements expected by the Deserialize.
+ Err(de::Error::invalid_length(2, &ExpectedInSeq(2 - remaining)))
+ }
+ }
+
+ fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ if len == 2 {
+ self.deserialize_seq(visitor)
+ } else {
+ // First argument is the number of elements in the data, second
+ // argument is the number of elements expected by the Deserialize.
+ Err(de::Error::invalid_length(2, &ExpectedInSeq(len)))
+ }
+ }
+}
+
+struct PairVisitor<A, B, E>(Option<A>, Option<B>, PhantomData<E>);
+
+impl<'de, A, B, E> de::SeqAccess<'de> for PairVisitor<A, B, E>
+where
+ A: IntoDeserializer<'de, E>,
+ B: IntoDeserializer<'de, E>,
+ 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>,
+ {
+ if let Some(k) = self.0.take() {
+ seed.deserialize(k.into_deserializer()).map(Some)
+ } else if let Some(v) = self.1.take() {
+ seed.deserialize(v.into_deserializer()).map(Some)
+ } else {
+ Ok(None)
+ }
+ }
+
+ fn size_hint(&self) -> Option<usize> {
+ if self.0.is_some() {
+ Some(2)
+ } else if self.1.is_some() {
+ Some(1)
+ } else {
+ Some(0)
+ }
+ }
+}
+
+struct ExpectedInMap(usize);
+
+impl Expected for ExpectedInMap {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ if self.0 == 1 {
+ write!(formatter, "1 element in map")
+ } else {
+ write!(formatter, "{} elements in map", self.0)
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V>
+where
+ K: IntoDeserializer<'de, E> + Eq + Ord,
+ V: IntoDeserializer<'de, E>,
+ E: de::Error,
+{
+ type Deserializer = MapDeserializer<'de, <Self as IntoIterator>::IntoIter, E>;
+
+ fn into_deserializer(self) -> Self::Deserializer {
+ MapDeserializer::new(self.into_iter())
+ }
+}
+
+#[cfg(feature = "std")]
+impl<'de, K, V, S, E> IntoDeserializer<'de, E> for HashMap<K, V, S>
+where
+ K: IntoDeserializer<'de, E> + Eq + Hash,
+ V: IntoDeserializer<'de, E>,
+ S: BuildHasher,
+ E: de::Error,
+{
+ type Deserializer = MapDeserializer<'de, <Self as IntoIterator>::IntoIter, E>;
+
+ fn into_deserializer(self) -> Self::Deserializer {
+ MapDeserializer::new(self.into_iter())
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A deserializer holding a `MapAccess`.
+#[derive(Clone, Debug)]
+pub struct MapAccessDeserializer<A> {
+ map: A,
+}
+
+impl<A> MapAccessDeserializer<A> {
+ /// Construct a new `MapAccessDeserializer<A>`.
+ pub fn new(map: A) -> Self {
+ MapAccessDeserializer { map: map }
+ }
+}
+
+impl<'de, A> de::Deserializer<'de> for MapAccessDeserializer<A>
+where
+ A: de::MapAccess<'de>,
+{
+ type Error = A::Error;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_map(self.map)
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ _name: &str,
+ _variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_enum(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 identifier ignored_any
+ }
+}
+
+impl<'de, A> de::EnumAccess<'de> for MapAccessDeserializer<A>
+where
+ A: de::MapAccess<'de>,
+{
+ type Error = A::Error;
+ type Variant = private::MapAsEnum<A>;
+
+ fn variant_seed<T>(mut self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ match try!(self.map.next_key_seed(seed)) {
+ Some(key) => Ok((key, private::map_as_enum(self.map))),
+ None => Err(de::Error::invalid_type(de::Unexpected::Map, &"enum")),
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A deserializer holding an `EnumAccess`.
+#[derive(Clone, Debug)]
+pub struct EnumAccessDeserializer<A> {
+ access: A,
+}
+
+impl<A> EnumAccessDeserializer<A> {
+ /// Construct a new `EnumAccessDeserializer<A>`.
+ pub fn new(access: A) -> Self {
+ EnumAccessDeserializer { access: access }
+ }
+}
+
+impl<'de, A> de::Deserializer<'de> for EnumAccessDeserializer<A>
+where
+ A: de::EnumAccess<'de>,
+{
+ type Error = A::Error;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_enum(self.access)
+ }
+
+ 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
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+mod private {
+ use lib::*;
+
+ use de::{self, DeserializeSeed, Deserializer, MapAccess, Unexpected, VariantAccess, Visitor};
+
+ pub struct UnitOnly<E> {
+ marker: PhantomData<E>,
+ }
+
+ pub fn unit_only<T, E>(t: T) -> (T, UnitOnly<E>) {
+ (
+ t,
+ UnitOnly {
+ marker: PhantomData,
+ },
+ )
+ }
+
+ impl<'de, E> de::VariantAccess<'de> for UnitOnly<E>
+ where
+ E: de::Error,
+ {
+ type Error = E;
+
+ fn unit_variant(self) -> Result<(), Self::Error> {
+ Ok(())
+ }
+
+ fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ Err(de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"newtype variant",
+ ))
+ }
+
+ fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ Err(de::Error::invalid_type(
+ 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>,
+ {
+ Err(de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"struct variant",
+ ))
+ }
+ }
+
+ pub struct MapAsEnum<A> {
+ map: A,
+ }
+
+ pub fn map_as_enum<A>(map: A) -> MapAsEnum<A> {
+ MapAsEnum { map: map }
+ }
+
+ impl<'de, A> VariantAccess<'de> for MapAsEnum<A>
+ where
+ A: MapAccess<'de>,
+ {
+ type Error = A::Error;
+
+ fn unit_variant(mut self) -> Result<(), Self::Error> {
+ self.map.next_value()
+ }
+
+ fn newtype_variant_seed<T>(mut self, seed: T) -> Result<T::Value, Self::Error>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ self.map.next_value_seed(seed)
+ }
+
+ fn tuple_variant<V>(mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.map.next_value_seed(SeedTupleVariant {
+ len: len,
+ visitor: visitor,
+ })
+ }
+
+ fn struct_variant<V>(
+ mut self,
+ _fields: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.map
+ .next_value_seed(SeedStructVariant { visitor: visitor })
+ }
+ }
+
+ struct SeedTupleVariant<V> {
+ len: usize,
+ visitor: V,
+ }
+
+ impl<'de, V> DeserializeSeed<'de> for SeedTupleVariant<V>
+ where
+ V: Visitor<'de>,
+ {
+ type Value = V::Value;
+
+ fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_tuple(self.len, self.visitor)
+ }
+ }
+
+ struct SeedStructVariant<V> {
+ visitor: V,
+ }
+
+ impl<'de, V> DeserializeSeed<'de> for SeedStructVariant<V>
+ where
+ V: Visitor<'de>,
+ {
+ type Value = V::Value;
+
+ fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_map(self.visitor)
+ }
+ }
+
+ /// Avoid having to restate the generic types on `MapDeserializer`. The
+ /// `Iterator::Item` contains enough information to figure out K and V.
+ pub trait Pair {
+ type First;
+ type Second;
+ fn split(self) -> (Self::First, Self::Second);
+ }
+
+ impl<A, B> Pair for (A, B) {
+ type First = A;
+ type Second = B;
+ fn split(self) -> (A, B) {
+ self
+ }
+ }
+
+ pub type First<T> = <T as Pair>::First;
+ pub type Second<T> = <T as Pair>::Second;
+}
diff --git a/third_party/rust/serde/src/integer128.rs b/third_party/rust/serde/src/integer128.rs
new file mode 100644
index 0000000000..904c2a233f
--- /dev/null
+++ b/third_party/rust/serde/src/integer128.rs
@@ -0,0 +1,82 @@
+/// Conditional compilation depending on whether Serde is built with support for
+/// 128-bit integers.
+///
+/// Data formats that wish to support Rust compiler versions older than 1.26
+/// (or targets that lack 128-bit integers) may place the i128 / u128 methods
+/// of their Serializer and Deserializer behind this macro.
+///
+/// Data formats that require a minimum Rust compiler version of at least 1.26,
+/// or do not target platforms that lack 128-bit integers, do not need to
+/// bother with this macro and may assume support for 128-bit integers.
+///
+/// ```edition2018
+/// # use serde::__private::doc::Error;
+/// #
+/// # struct MySerializer;
+/// #
+/// use serde::{serde_if_integer128, Serializer};
+///
+/// impl Serializer for MySerializer {
+/// type Ok = ();
+/// type Error = Error;
+///
+/// fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
+/// /* ... */
+/// # unimplemented!()
+/// }
+///
+/// /* ... */
+///
+/// serde_if_integer128! {
+/// fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
+/// /* ... */
+/// # unimplemented!()
+/// }
+///
+/// fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
+/// /* ... */
+/// # unimplemented!()
+/// }
+/// }
+/// #
+/// # serde::__serialize_unimplemented! {
+/// # bool i8 i16 i32 u8 u16 u32 u64 f32 f64 char str bytes none some
+/// # unit unit_struct unit_variant newtype_struct newtype_variant seq
+/// # tuple tuple_struct tuple_variant map struct struct_variant
+/// # }
+/// }
+/// ```
+///
+/// When Serde is built with support for 128-bit integers, this macro expands
+/// transparently into just the input tokens.
+///
+/// ```edition2018
+/// macro_rules! serde_if_integer128 {
+/// ($($tt:tt)*) => {
+/// $($tt)*
+/// };
+/// }
+/// ```
+///
+/// When built without support for 128-bit integers, this macro expands to
+/// nothing.
+///
+/// ```edition2018
+/// macro_rules! serde_if_integer128 {
+/// ($($tt:tt)*) => {};
+/// }
+/// ```
+#[cfg(not(no_integer128))]
+#[macro_export]
+macro_rules! serde_if_integer128 {
+ ($($tt:tt)*) => {
+ $($tt)*
+ };
+}
+
+#[cfg(no_integer128)]
+#[macro_export]
+#[doc(hidden)]
+macro_rules! serde_if_integer128 {
+ ($($tt:tt)*) => {};
+}
diff --git a/third_party/rust/serde/src/lib.rs b/third_party/rust/serde/src/lib.rs
new file mode 100644
index 0000000000..e4bc7c8f19
--- /dev/null
+++ b/third_party/rust/serde/src/lib.rs
@@ -0,0 +1,334 @@
+//! # Serde
+//!
+//! Serde is a framework for ***ser***ializing and ***de***serializing Rust data
+//! structures efficiently and generically.
+//!
+//! The Serde ecosystem consists of data structures that know how to serialize
+//! and deserialize themselves along with data formats that know how to
+//! serialize and deserialize other things. Serde provides the layer by which
+//! these two groups interact with each other, allowing any supported data
+//! structure to be serialized and deserialized using any supported data format.
+//!
+//! See the Serde website <https://serde.rs/> for additional documentation and
+//! usage examples.
+//!
+//! ## Design
+//!
+//! Where many other languages rely on runtime reflection for serializing data,
+//! Serde is instead built on Rust's powerful trait system. A data structure
+//! that knows how to serialize and deserialize itself is one that implements
+//! Serde's `Serialize` and `Deserialize` traits (or uses Serde's derive
+//! attribute to automatically generate implementations at compile time). This
+//! avoids any overhead of reflection or runtime type information. In fact in
+//! many situations the interaction between data structure and data format can
+//! be completely optimized away by the Rust compiler, leaving Serde
+//! serialization to perform the same speed as a handwritten serializer for the
+//! specific selection of data structure and data format.
+//!
+//! ## Data formats
+//!
+//! The following is a partial list of data formats that have been implemented
+//! for Serde by the community.
+//!
+//! - [JSON], the ubiquitous JavaScript Object Notation used by many HTTP APIs.
+//! - [Postcard], a no\_std and embedded-systems friendly compact binary format.
+//! - [CBOR], a Concise Binary Object Representation designed for small message
+//! size without the need for version negotiation.
+//! - [YAML], a self-proclaimed human-friendly configuration language that ain't
+//! markup language.
+//! - [MessagePack], an efficient binary format that resembles a compact JSON.
+//! - [TOML], a minimal configuration format used by [Cargo].
+//! - [Pickle], a format common in the Python world.
+//! - [RON], a Rusty Object Notation.
+//! - [BSON], the data storage and network transfer format used by MongoDB.
+//! - [Avro], a binary format used within Apache Hadoop, with support for schema
+//! definition.
+//! - [JSON5], a superset of JSON including some productions from ES5.
+//! - [URL] query strings, in the x-www-form-urlencoded format.
+//! - [Envy], a way to deserialize environment variables into Rust structs.
+//! *(deserialization only)*
+//! - [Envy Store], a way to deserialize [AWS Parameter Store] parameters into
+//! Rust structs. *(deserialization only)*
+//! - [S-expressions], the textual representation of code and data used by the
+//! Lisp language family.
+//! - [D-Bus]'s binary wire format.
+//! - [FlexBuffers], the schemaless cousin of Google's FlatBuffers zero-copy
+//! serialization format.
+//! - [Bencode], a simple binary format used in the BitTorrent protocol.
+//! - [Token streams], for processing Rust procedural macro input.
+//! *(deserialization only)*
+//! - [DynamoDB Items], the format used by [rusoto_dynamodb] to transfer data to
+//! and from DynamoDB.
+//! - [Hjson], a syntax extension to JSON designed around human reading and
+//! editing. *(deserialization only)*
+//!
+//! [JSON]: https://github.com/serde-rs/json
+//! [Postcard]: https://github.com/jamesmunns/postcard
+//! [CBOR]: https://github.com/enarx/ciborium
+//! [YAML]: https://github.com/dtolnay/serde-yaml
+//! [MessagePack]: https://github.com/3Hren/msgpack-rust
+//! [TOML]: https://docs.rs/toml
+//! [Pickle]: https://github.com/birkenfeld/serde-pickle
+//! [RON]: https://github.com/ron-rs/ron
+//! [BSON]: https://github.com/mongodb/bson-rust
+//! [Avro]: https://docs.rs/apache-avro
+//! [JSON5]: https://github.com/callum-oakley/json5-rs
+//! [URL]: https://docs.rs/serde_qs
+//! [Envy]: https://github.com/softprops/envy
+//! [Envy Store]: https://github.com/softprops/envy-store
+//! [Cargo]: https://doc.rust-lang.org/cargo/reference/manifest.html
+//! [AWS Parameter Store]: https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html
+//! [S-expressions]: https://github.com/rotty/lexpr-rs
+//! [D-Bus]: https://docs.rs/zvariant
+//! [FlexBuffers]: https://github.com/google/flatbuffers/tree/master/rust/flexbuffers
+//! [Bencode]: https://github.com/P3KI/bendy
+//! [Token streams]: https://github.com/oxidecomputer/serde_tokenstream
+//! [DynamoDB Items]: https://docs.rs/serde_dynamo
+//! [rusoto_dynamodb]: https://docs.rs/rusoto_dynamodb
+//! [Hjson]: https://github.com/Canop/deser-hjson
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Serde types in rustdoc of other crates get linked to here.
+#![doc(html_root_url = "https://docs.rs/serde/1.0.152")]
+// Support using Serde without the standard library!
+#![cfg_attr(not(feature = "std"), no_std)]
+// Unstable functionality only if the user asks for it. For tracking and
+// discussion of these features please refer to this issue:
+//
+// https://github.com/serde-rs/serde/issues/812
+#![cfg_attr(feature = "unstable", feature(error_in_core, never_type))]
+#![allow(unknown_lints, bare_trait_objects, deprecated)]
+#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
+// Ignored clippy and clippy_pedantic lints
+#![cfg_attr(
+ feature = "cargo-clippy",
+ allow(
+ // clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
+ unnested_or_patterns,
+ // clippy bug: https://github.com/rust-lang/rust-clippy/issues/7768
+ semicolon_if_nothing_returned,
+ // not available in our oldest supported compiler
+ empty_enum,
+ type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/8772
+ // integer and float ser/de requires these sorts of casts
+ cast_possible_truncation,
+ cast_possible_wrap,
+ cast_sign_loss,
+ // things are often more readable this way
+ cast_lossless,
+ module_name_repetitions,
+ option_if_let_else,
+ single_match_else,
+ type_complexity,
+ use_self,
+ zero_prefixed_literal,
+ // correctly used
+ derive_partial_eq_without_eq,
+ enum_glob_use,
+ explicit_auto_deref,
+ map_err_ignore,
+ new_without_default,
+ result_unit_err,
+ wildcard_imports,
+ // not practical
+ needless_pass_by_value,
+ similar_names,
+ too_many_lines,
+ // preference
+ doc_markdown,
+ unseparated_literal_suffix,
+ // false positive
+ needless_doctest_main,
+ // noisy
+ missing_errors_doc,
+ must_use_candidate,
+ )
+)]
+// Rustc lints.
+#![deny(missing_docs, unused_imports)]
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "alloc")]
+extern crate alloc;
+
+/// A facade around all the types we need from the `std`, `core`, and `alloc`
+/// crates. This avoids elaborate import wrangling having to happen in every
+/// module.
+mod lib {
+ mod core {
+ #[cfg(not(feature = "std"))]
+ pub use core::*;
+ #[cfg(feature = "std")]
+ pub use std::*;
+ }
+
+ pub use self::core::{cmp, iter, mem, num, ptr, slice, str};
+ pub use self::core::{f32, f64};
+ pub use self::core::{i16, i32, i64, i8, isize};
+ pub use self::core::{u16, u32, u64, u8, usize};
+
+ pub use self::core::cell::{Cell, RefCell};
+ pub use self::core::clone::{self, Clone};
+ pub use self::core::convert::{self, From, Into};
+ pub use self::core::default::{self, Default};
+ pub use self::core::fmt::{self, Debug, Display};
+ pub use self::core::marker::{self, PhantomData};
+ pub use self::core::num::Wrapping;
+ pub use self::core::ops::Range;
+ pub use self::core::option::{self, Option};
+ pub use self::core::result::{self, Result};
+
+ #[cfg(all(feature = "alloc", not(feature = "std")))]
+ pub use alloc::borrow::{Cow, ToOwned};
+ #[cfg(feature = "std")]
+ pub use std::borrow::{Cow, ToOwned};
+
+ #[cfg(all(feature = "alloc", not(feature = "std")))]
+ pub use alloc::string::{String, ToString};
+ #[cfg(feature = "std")]
+ pub use std::string::{String, ToString};
+
+ #[cfg(all(feature = "alloc", not(feature = "std")))]
+ pub use alloc::vec::Vec;
+ #[cfg(feature = "std")]
+ pub use std::vec::Vec;
+
+ #[cfg(all(feature = "alloc", not(feature = "std")))]
+ pub use alloc::boxed::Box;
+ #[cfg(feature = "std")]
+ pub use std::boxed::Box;
+
+ #[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
+ pub use alloc::rc::{Rc, Weak as RcWeak};
+ #[cfg(all(feature = "rc", feature = "std"))]
+ pub use std::rc::{Rc, Weak as RcWeak};
+
+ #[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
+ pub use alloc::sync::{Arc, Weak as ArcWeak};
+ #[cfg(all(feature = "rc", feature = "std"))]
+ pub use std::sync::{Arc, Weak as ArcWeak};
+
+ #[cfg(all(feature = "alloc", not(feature = "std")))]
+ pub use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
+ #[cfg(feature = "std")]
+ pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
+
+ #[cfg(feature = "std")]
+ pub use std::{error, net};
+
+ #[cfg(feature = "std")]
+ pub use std::collections::{HashMap, HashSet};
+ #[cfg(feature = "std")]
+ pub use std::ffi::{CStr, CString, OsStr, OsString};
+ #[cfg(feature = "std")]
+ pub use std::hash::{BuildHasher, Hash};
+ #[cfg(feature = "std")]
+ pub use std::io::Write;
+ #[cfg(feature = "std")]
+ pub use std::path::{Path, PathBuf};
+ #[cfg(feature = "std")]
+ pub use std::sync::{Mutex, RwLock};
+ #[cfg(feature = "std")]
+ pub use std::time::{SystemTime, UNIX_EPOCH};
+
+ #[cfg(all(feature = "std", not(no_collections_bound), no_ops_bound))]
+ pub use std::collections::Bound;
+
+ #[cfg(not(no_core_reverse))]
+ pub use self::core::cmp::Reverse;
+
+ #[cfg(not(no_ops_bound))]
+ pub use self::core::ops::Bound;
+
+ #[cfg(not(no_range_inclusive))]
+ pub use self::core::ops::RangeInclusive;
+
+ #[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
+ pub use std::sync::atomic::{
+ AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
+ AtomicUsize, Ordering,
+ };
+ #[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))]
+ pub use std::sync::atomic::{AtomicI64, AtomicU64};
+
+ #[cfg(all(feature = "std", not(no_target_has_atomic)))]
+ pub use std::sync::atomic::Ordering;
+ #[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "8"))]
+ pub use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
+ #[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "16"))]
+ pub use std::sync::atomic::{AtomicI16, AtomicU16};
+ #[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "32"))]
+ pub use std::sync::atomic::{AtomicI32, AtomicU32};
+ #[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "64"))]
+ pub use std::sync::atomic::{AtomicI64, AtomicU64};
+ #[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "ptr"))]
+ pub use std::sync::atomic::{AtomicIsize, AtomicUsize};
+
+ #[cfg(any(feature = "std", not(no_core_duration)))]
+ pub use self::core::time::Duration;
+}
+
+// None of this crate's error handling needs the `From::from` error conversion
+// performed implicitly by the `?` operator or the standard library's `try!`
+// macro. This simplified macro gives a 5.5% improvement in compile time
+// compared to standard `try!`, and 9% improvement compared to `?`.
+macro_rules! try {
+ ($expr:expr) => {
+ match $expr {
+ Ok(val) => val,
+ Err(err) => return Err(err),
+ }
+ };
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[macro_use]
+mod macros;
+
+#[macro_use]
+mod integer128;
+
+pub mod de;
+pub mod ser;
+
+#[doc(inline)]
+pub use de::{Deserialize, Deserializer};
+#[doc(inline)]
+pub use ser::{Serialize, Serializer};
+
+// Used by generated code and doc tests. Not public API.
+#[doc(hidden)]
+#[path = "private/mod.rs"]
+pub mod __private;
+
+#[allow(unused_imports)]
+use self::__private as export;
+#[allow(unused_imports)]
+use self::__private as private;
+
+#[path = "de/seed.rs"]
+mod seed;
+
+#[cfg(not(any(feature = "std", feature = "unstable")))]
+mod std_error;
+
+// Re-export #[derive(Serialize, Deserialize)].
+//
+// The reason re-exporting is not enabled by default is that disabling it would
+// be annoying for crates that provide handwritten impls or data formats. They
+// would need to disable default features and then explicitly re-enable std.
+#[cfg(feature = "serde_derive")]
+#[allow(unused_imports)]
+#[macro_use]
+extern crate serde_derive;
+#[cfg(feature = "serde_derive")]
+#[doc(hidden)]
+pub use serde_derive::*;
+
+#[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))]
+mod actually_private {
+ pub struct T;
+}
diff --git a/third_party/rust/serde/src/macros.rs b/third_party/rust/serde/src/macros.rs
new file mode 100644
index 0000000000..6502a23a7a
--- /dev/null
+++ b/third_party/rust/serde/src/macros.rs
@@ -0,0 +1,236 @@
+// Super explicit first paragraph because this shows up at the top level and
+// trips up people who are just looking for basic Serialize / Deserialize
+// documentation.
+//
+/// Helper macro when implementing the `Deserializer` part of a new data format
+/// for Serde.
+///
+/// Some [`Deserializer`] implementations for self-describing formats do not
+/// care what hint the [`Visitor`] gives them, they just want to blindly call
+/// the [`Visitor`] method corresponding to the data they can tell is in the
+/// input. This requires repetitive implementations of all the [`Deserializer`]
+/// trait methods.
+///
+/// ```edition2018
+/// # use serde::forward_to_deserialize_any;
+/// # use serde::de::{value, Deserializer, Visitor};
+/// #
+/// # struct MyDeserializer;
+/// #
+/// # impl<'de> Deserializer<'de> for MyDeserializer {
+/// # type Error = value::Error;
+/// #
+/// # fn deserialize_any<V>(self, _: V) -> Result<V::Value, Self::Error>
+/// # where
+/// # V: Visitor<'de>,
+/// # {
+/// # unimplemented!()
+/// # }
+/// #
+/// #[inline]
+/// fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+/// where
+/// V: Visitor<'de>,
+/// {
+/// self.deserialize_any(visitor)
+/// }
+/// #
+/// # forward_to_deserialize_any! {
+/// # 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
+/// # }
+/// # }
+/// ```
+///
+/// The `forward_to_deserialize_any!` macro implements these simple forwarding
+/// methods so that they forward directly to [`Deserializer::deserialize_any`].
+/// You can choose which methods to forward.
+///
+/// ```edition2018
+/// # use serde::forward_to_deserialize_any;
+/// # use serde::de::{value, Deserializer, Visitor};
+/// #
+/// # struct MyDeserializer;
+/// #
+/// impl<'de> Deserializer<'de> for MyDeserializer {
+/// # type Error = value::Error;
+/// #
+/// fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+/// where
+/// V: Visitor<'de>,
+/// {
+/// /* ... */
+/// # let _ = visitor;
+/// # unimplemented!()
+/// }
+///
+/// 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
+/// }
+/// }
+/// ```
+///
+/// The macro assumes the convention that your `Deserializer` lifetime parameter
+/// is called `'de` and that the `Visitor` type parameters on each method are
+/// called `V`. A different type parameter and a different lifetime can be
+/// specified explicitly if necessary.
+///
+/// ```edition2018
+/// # use std::marker::PhantomData;
+/// #
+/// # use serde::forward_to_deserialize_any;
+/// # use serde::de::{value, Deserializer, Visitor};
+/// #
+/// # struct MyDeserializer<V>(PhantomData<V>);
+/// #
+/// # impl<'q, V> Deserializer<'q> for MyDeserializer<V> {
+/// # type Error = value::Error;
+/// #
+/// # fn deserialize_any<W>(self, visitor: W) -> Result<W::Value, Self::Error>
+/// # where
+/// # W: Visitor<'q>,
+/// # {
+/// # unimplemented!()
+/// # }
+/// #
+/// forward_to_deserialize_any! {
+/// <W: Visitor<'q>>
+/// 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
+/// }
+/// # }
+/// ```
+///
+/// [`Deserializer`]: trait.Deserializer.html
+/// [`Visitor`]: de/trait.Visitor.html
+/// [`Deserializer::deserialize_any`]: trait.Deserializer.html#tymethod.deserialize_any
+#[macro_export(local_inner_macros)]
+macro_rules! forward_to_deserialize_any {
+ (<$visitor:ident: Visitor<$lifetime:tt>> $($func:ident)*) => {
+ $(forward_to_deserialize_any_helper!{$func<$lifetime, $visitor>})*
+ };
+ // This case must be after the previous one.
+ ($($func:ident)*) => {
+ $(forward_to_deserialize_any_helper!{$func<'de, V>})*
+ };
+}
+
+#[doc(hidden)]
+#[macro_export]
+macro_rules! forward_to_deserialize_any_method {
+ ($func:ident<$l:tt, $v:ident>($($arg:ident : $ty:ty),*)) => {
+ #[inline]
+ fn $func<$v>(self, $($arg: $ty,)* visitor: $v) -> $crate::__private::Result<$v::Value, Self::Error>
+ where
+ $v: $crate::de::Visitor<$l>,
+ {
+ $(
+ let _ = $arg;
+ )*
+ self.deserialize_any(visitor)
+ }
+ };
+}
+
+#[doc(hidden)]
+#[macro_export(local_inner_macros)]
+macro_rules! forward_to_deserialize_any_helper {
+ (bool<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_bool<$l, $v>()}
+ };
+ (i8<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_i8<$l, $v>()}
+ };
+ (i16<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_i16<$l, $v>()}
+ };
+ (i32<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_i32<$l, $v>()}
+ };
+ (i64<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_i64<$l, $v>()}
+ };
+ (i128<$l:tt, $v:ident>) => {
+ serde_if_integer128! {
+ forward_to_deserialize_any_method!{deserialize_i128<$l, $v>()}
+ }
+ };
+ (u8<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_u8<$l, $v>()}
+ };
+ (u16<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_u16<$l, $v>()}
+ };
+ (u32<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_u32<$l, $v>()}
+ };
+ (u64<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_u64<$l, $v>()}
+ };
+ (u128<$l:tt, $v:ident>) => {
+ serde_if_integer128! {
+ forward_to_deserialize_any_method!{deserialize_u128<$l, $v>()}
+ }
+ };
+ (f32<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_f32<$l, $v>()}
+ };
+ (f64<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_f64<$l, $v>()}
+ };
+ (char<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_char<$l, $v>()}
+ };
+ (str<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_str<$l, $v>()}
+ };
+ (string<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_string<$l, $v>()}
+ };
+ (bytes<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_bytes<$l, $v>()}
+ };
+ (byte_buf<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_byte_buf<$l, $v>()}
+ };
+ (option<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_option<$l, $v>()}
+ };
+ (unit<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_unit<$l, $v>()}
+ };
+ (unit_struct<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_unit_struct<$l, $v>(name: &'static str)}
+ };
+ (newtype_struct<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_newtype_struct<$l, $v>(name: &'static str)}
+ };
+ (seq<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_seq<$l, $v>()}
+ };
+ (tuple<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_tuple<$l, $v>(len: usize)}
+ };
+ (tuple_struct<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_tuple_struct<$l, $v>(name: &'static str, len: usize)}
+ };
+ (map<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_map<$l, $v>()}
+ };
+ (struct<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_struct<$l, $v>(name: &'static str, fields: &'static [&'static str])}
+ };
+ (enum<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_enum<$l, $v>(name: &'static str, variants: &'static [&'static str])}
+ };
+ (identifier<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_identifier<$l, $v>()}
+ };
+ (ignored_any<$l:tt, $v:ident>) => {
+ forward_to_deserialize_any_method!{deserialize_ignored_any<$l, $v>()}
+ };
+}
diff --git a/third_party/rust/serde/src/private/de.rs b/third_party/rust/serde/src/private/de.rs
new file mode 100644
index 0000000000..e9c693d4dd
--- /dev/null
+++ b/third_party/rust/serde/src/private/de.rs
@@ -0,0 +1,2995 @@
+use lib::*;
+
+use de::value::{BorrowedBytesDeserializer, BytesDeserializer};
+use de::{Deserialize, Deserializer, Error, IntoDeserializer, Visitor};
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+use de::{DeserializeSeed, MapAccess, Unexpected};
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub use self::content::{
+ Content, ContentDeserializer, ContentRefDeserializer, EnumDeserializer,
+ InternallyTaggedUnitVisitor, TagContentOtherField, TagContentOtherFieldVisitor,
+ TagOrContentField, TagOrContentFieldVisitor, TaggedContentVisitor, UntaggedUnitVisitor,
+};
+
+pub use seed::InPlaceSeed;
+
+/// 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)
+}
+
+#[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 __private::size_hint;
+ use actually_private;
+ 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, Clone)]
+ 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_content(actually_private::T, 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,
+ expecting: &'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, expecting: &'static str) -> Self {
+ TaggedContentVisitor {
+ tag_name: name,
+ expecting: expecting,
+ 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(self.expecting)
+ }
+
+ 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 deserialize_float<V>(self, visitor: V) -> Result<V::Value, E>
+ where
+ V: Visitor<'de>,
+ {
+ match self.content {
+ Content::F32(v) => visitor.visit_f32(v),
+ Content::F64(v) => visitor.visit_f64(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),
+ _ => 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>,
+ {
+ self.deserialize_float(visitor)
+ }
+
+ fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_float(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(),
+
+ // Allow deserializing newtype variant containing unit.
+ //
+ // #[derive(Deserialize)]
+ // #[serde(tag = "result")]
+ // enum Response<T> {
+ // Success(T),
+ // }
+ //
+ // We want {"result":"Success"} to deserialize into Response<()>.
+ Content::Map(ref v) if v.is_empty() => 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/seq.
+ Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
+ Content::Seq(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()
+ }
+
+ fn __deserialize_content<V>(
+ self,
+ _: actually_private::T,
+ visitor: V,
+ ) -> Result<Content<'de>, Self::Error>
+ where
+ V: Visitor<'de, Value = Content<'de>>,
+ {
+ let _ = visitor;
+ Ok(self.content)
+ }
+ }
+
+ 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 deserialize_float<V>(self, visitor: V) -> Result<V::Value, E>
+ where
+ V: Visitor<'de>,
+ {
+ match *self.content {
+ Content::F32(v) => visitor.visit_f32(v),
+ Content::F64(v) => visitor.visit_f64(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),
+ _ => 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(|(k, 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>,
+ {
+ self.deserialize_float(visitor)
+ }
+
+ fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_float(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 (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))
+ }
+ 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()
+ }
+
+ fn __deserialize_content<V>(
+ self,
+ _: actually_private::T,
+ visitor: V,
+ ) -> Result<Content<'de>, Self::Error>
+ where
+ V: Visitor<'de, Value = Content<'de>>,
+ {
+ let _ = visitor;
+ Ok(self.content.clone())
+ }
+ }
+
+ 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(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(v)) => {
+ de::Deserializer::deserialize_any(MapRefDeserializer::new(v), visitor)
+ }
+ Some(Content::Seq(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((key, 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;
+}
+
+pub struct Borrowed<'de, T: 'de + ?Sized>(pub &'de T);
+
+impl<'de, E> IdentifierDeserializer<'de, E> for u64
+where
+ E: Error,
+{
+ type Deserializer = <u64 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<'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 BorrowedStrDeserializer<'de, E> {
+ value: &'de str,
+ marker: PhantomData<E>,
+}
+
+impl<'de, E> Deserializer<'de> for BorrowedStrDeserializer<'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_borrowed_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
+ }
+}
+
+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, E> IdentifierDeserializer<'de, E> for Borrowed<'de, str>
+where
+ E: Error,
+{
+ type Deserializer = BorrowedStrDeserializer<'de, E>;
+
+ fn from(self) -> Self::Deserializer {
+ BorrowedStrDeserializer {
+ value: self.0,
+ marker: PhantomData,
+ }
+ }
+}
+
+impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8]
+where
+ E: Error,
+{
+ type Deserializer = BytesDeserializer<'a, E>;
+
+ fn from(self) -> Self::Deserializer {
+ BytesDeserializer::new(self)
+ }
+}
+
+impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, [u8]>
+where
+ E: Error,
+{
+ type Deserializer = BorrowedBytesDeserializer<'de, E>;
+
+ fn from(self) -> Self::Deserializer {
+ BorrowedBytesDeserializer::new(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>,
+ {
+ for item in &mut self.iter {
+ // 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>,
+ {
+ for item in &mut self.iter {
+ 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/third_party/rust/serde/src/private/doc.rs b/third_party/rust/serde/src/private/doc.rs
new file mode 100644
index 0000000000..f597af8445
--- /dev/null
+++ b/third_party/rust/serde/src/private/doc.rs
@@ -0,0 +1,159 @@
+// Used only by Serde doc tests. Not public API.
+
+use lib::*;
+
+use ser;
+
+#[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!()
+ }
+}
+
+#[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(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::__private::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/third_party/rust/serde/src/private/mod.rs b/third_party/rust/serde/src/private/mod.rs
new file mode 100644
index 0000000000..e896902360
--- /dev/null
+++ b/third_party/rust/serde/src/private/mod.rs
@@ -0,0 +1,50 @@
+#[cfg(not(no_serde_derive))]
+pub mod de;
+#[cfg(not(no_serde_derive))]
+pub mod ser;
+
+pub mod size_hint;
+
+// FIXME: #[cfg(doctest)] once https://github.com/rust-lang/rust/issues/67295 is fixed.
+pub mod doc;
+
+pub use lib::clone::Clone;
+pub use lib::convert::{From, Into};
+pub use lib::default::Default;
+pub use lib::fmt::{self, Formatter};
+pub use lib::marker::PhantomData;
+pub use lib::option::Option::{self, None, Some};
+pub use lib::ptr;
+pub use lib::result::Result::{self, Err, Ok};
+
+pub use self::string::from_utf8_lossy;
+
+#[cfg(any(feature = "alloc", feature = "std"))]
+pub use lib::{ToString, Vec};
+
+#[cfg(not(no_core_try_from))]
+pub use lib::convert::TryFrom;
+
+mod string {
+ use lib::*;
+
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<str> {
+ String::from_utf8_lossy(bytes)
+ }
+
+ // The generated code calls this like:
+ //
+ // let value = &_serde::__private::from_utf8_lossy(bytes);
+ // Err(_serde::de::Error::unknown_variant(value, VARIANTS))
+ //
+ // so it is okay for the return type to be different from the std case as long
+ // as the above works.
+ #[cfg(not(any(feature = "std", feature = "alloc")))]
+ pub fn from_utf8_lossy(bytes: &[u8]) -> &str {
+ // Three unicode replacement characters if it fails. They look like a
+ // white-on-black question mark. The user will recognize it as invalid
+ // UTF-8.
+ str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}")
+ }
+}
diff --git a/third_party/rust/serde/src/private/ser.rs b/third_party/rust/serde/src/private/ser.rs
new file mode 100644
index 0000000000..528e8c125a
--- /dev/null
+++ b/third_party/rust/serde/src/private/ser.rs
@@ -0,0 +1,1310 @@
+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,
+ #[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"),
+ #[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> {
+ let mut map = try!(self.delegate.serialize_map(Some(1)));
+ try!(map.serialize_entry(self.tag, self.variant_name));
+ map.end()
+ }
+
+ 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))
+ }
+}
+
+#[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()
+ }
+ }
+
+ 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 (k, 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(())
+ }
+}
diff --git a/third_party/rust/serde/src/private/size_hint.rs b/third_party/rust/serde/src/private/size_hint.rs
new file mode 100644
index 0000000000..ca71e616bb
--- /dev/null
+++ b/third_party/rust/serde/src/private/size_hint.rs
@@ -0,0 +1,21 @@
+use lib::*;
+
+pub fn from_bounds<I>(iter: &I) -> Option<usize>
+where
+ I: Iterator,
+{
+ helper(iter.size_hint())
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+#[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,
+ }
+}
diff --git a/third_party/rust/serde/src/ser/fmt.rs b/third_party/rust/serde/src/ser/fmt.rs
new file mode 100644
index 0000000000..e7e09a1bfe
--- /dev/null
+++ b/third_party/rust/serde/src/ser/fmt.rs
@@ -0,0 +1,174 @@
+use lib::*;
+use ser::{Error, Impossible, Serialize, Serializer};
+
+impl Error for fmt::Error {
+ fn custom<T: Display>(_msg: T) -> Self {
+ fmt::Error
+ }
+}
+
+macro_rules! fmt_primitives {
+ ($($f:ident: $t:ty,)*) => {
+ $(
+ fn $f(self, v: $t) -> fmt::Result {
+ Display::fmt(&v, self)
+ }
+ )*
+ };
+}
+
+/// ```edition2018
+/// use serde::Serialize;
+/// use std::fmt::{self, Display};
+///
+/// #[derive(Serialize)]
+/// #[serde(rename_all = "kebab-case")]
+/// pub enum MessageType {
+/// StartRequest,
+/// EndRequest,
+/// }
+///
+/// impl Display for MessageType {
+/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+/// self.serialize(f)
+/// }
+/// }
+/// ```
+impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
+ type Ok = ();
+ type Error = fmt::Error;
+ type SerializeSeq = Impossible<(), fmt::Error>;
+ type SerializeTuple = Impossible<(), fmt::Error>;
+ type SerializeTupleStruct = Impossible<(), fmt::Error>;
+ type SerializeTupleVariant = Impossible<(), fmt::Error>;
+ type SerializeMap = Impossible<(), fmt::Error>;
+ type SerializeStruct = Impossible<(), fmt::Error>;
+ type SerializeStructVariant = Impossible<(), fmt::Error>;
+
+ fmt_primitives! {
+ serialize_bool: bool,
+ serialize_i8: i8,
+ serialize_i16: i16,
+ serialize_i32: i32,
+ serialize_i64: i64,
+ serialize_u8: u8,
+ serialize_u16: u16,
+ serialize_u32: u32,
+ serialize_u64: u64,
+ serialize_f32: f32,
+ serialize_f64: f64,
+ serialize_char: char,
+ serialize_str: &str,
+ serialize_unit_struct: &'static str,
+ }
+
+ serde_if_integer128! {
+ fmt_primitives! {
+ serialize_i128: i128,
+ serialize_u128: u128,
+ }
+ }
+
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ ) -> fmt::Result {
+ Display::fmt(variant, self)
+ }
+
+ fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T) -> fmt::Result
+ where
+ T: Serialize,
+ {
+ Serialize::serialize(value, self)
+ }
+
+ fn serialize_bytes(self, _v: &[u8]) -> fmt::Result {
+ Err(fmt::Error)
+ }
+
+ fn serialize_none(self) -> fmt::Result {
+ Err(fmt::Error)
+ }
+
+ fn serialize_some<T: ?Sized>(self, _value: &T) -> fmt::Result
+ where
+ T: Serialize,
+ {
+ Err(fmt::Error)
+ }
+
+ fn serialize_unit(self) -> fmt::Result {
+ Err(fmt::Error)
+ }
+
+ fn serialize_newtype_variant<T: ?Sized>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _value: &T,
+ ) -> fmt::Result
+ where
+ T: Serialize,
+ {
+ Err(fmt::Error)
+ }
+
+ fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, fmt::Error> {
+ Err(fmt::Error)
+ }
+
+ fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, fmt::Error> {
+ Err(fmt::Error)
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleStruct, fmt::Error> {
+ Err(fmt::Error)
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleVariant, fmt::Error> {
+ Err(fmt::Error)
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, fmt::Error> {
+ Err(fmt::Error)
+ }
+
+ fn serialize_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeStruct, fmt::Error> {
+ Err(fmt::Error)
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeStructVariant, fmt::Error> {
+ Err(fmt::Error)
+ }
+
+ fn collect_str<T: ?Sized>(self, value: &T) -> fmt::Result
+ where
+ T: Display,
+ {
+ Display::fmt(value, self)
+ }
+}
diff --git a/third_party/rust/serde/src/ser/impls.rs b/third_party/rust/serde/src/ser/impls.rs
new file mode 100644
index 0000000000..da26772611
--- /dev/null
+++ b/third_party/rust/serde/src/ser/impls.rs
@@ -0,0 +1,983 @@
+use lib::*;
+
+use ser::{Error, Serialize, SerializeTuple, Serializer};
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! primitive_impl {
+ ($ty:ident, $method:ident $($cast:tt)*) => {
+ impl Serialize for $ty {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.$method(*self $($cast)*)
+ }
+ }
+ }
+}
+
+primitive_impl!(bool, serialize_bool);
+primitive_impl!(isize, serialize_i64 as i64);
+primitive_impl!(i8, serialize_i8);
+primitive_impl!(i16, serialize_i16);
+primitive_impl!(i32, serialize_i32);
+primitive_impl!(i64, serialize_i64);
+primitive_impl!(usize, serialize_u64 as u64);
+primitive_impl!(u8, serialize_u8);
+primitive_impl!(u16, serialize_u16);
+primitive_impl!(u32, serialize_u32);
+primitive_impl!(u64, serialize_u64);
+primitive_impl!(f32, serialize_f32);
+primitive_impl!(f64, serialize_f64);
+primitive_impl!(char, serialize_char);
+
+serde_if_integer128! {
+ primitive_impl!(i128, serialize_i128);
+ primitive_impl!(u128, serialize_u128);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl Serialize for str {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_str(self)
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl Serialize for String {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_str(self)
+ }
+}
+
+impl<'a> Serialize for fmt::Arguments<'a> {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.collect_str(self)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "std")]
+impl Serialize for CStr {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_bytes(self.to_bytes())
+ }
+}
+
+#[cfg(feature = "std")]
+impl Serialize for CString {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_bytes(self.to_bytes())
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<T> Serialize for Option<T>
+where
+ T: Serialize,
+{
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match *self {
+ Some(ref value) => serializer.serialize_some(value),
+ None => serializer.serialize_none(),
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<T: ?Sized> Serialize for PhantomData<T> {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_unit_struct("PhantomData")
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Does not require T: Serialize.
+impl<T> Serialize for [T; 0] {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ try!(serializer.serialize_tuple(0)).end()
+ }
+}
+
+macro_rules! array_impls {
+ ($($len:tt)+) => {
+ $(
+ impl<T> Serialize for [T; $len]
+ where
+ T: Serialize,
+ {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ let mut seq = try!(serializer.serialize_tuple($len));
+ for e in self {
+ try!(seq.serialize_element(e));
+ }
+ seq.end()
+ }
+ }
+ )+
+ }
+}
+
+array_impls! {
+ 01 02 03 04 05 06 07 08 09 10
+ 11 12 13 14 15 16 17 18 19 20
+ 21 22 23 24 25 26 27 28 29 30
+ 31 32
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<T> Serialize for [T]
+where
+ T: Serialize,
+{
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.collect_seq(self)
+ }
+}
+
+#[cfg(all(any(feature = "std", feature = "alloc"), not(no_relaxed_trait_bounds)))]
+macro_rules! seq_impl {
+ ($ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>) => {
+ impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
+ where
+ T: Serialize,
+ {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.collect_seq(self)
+ }
+ }
+ }
+}
+
+#[cfg(all(any(feature = "std", feature = "alloc"), no_relaxed_trait_bounds))]
+macro_rules! seq_impl {
+ ($ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>) => {
+ impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
+ where
+ T: Serialize $(+ $tbound1 $(+ $tbound2)*)*,
+ $($typaram: $bound,)*
+ {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.collect_seq(self)
+ }
+ }
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+seq_impl!(BinaryHeap<T: Ord>);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+seq_impl!(BTreeSet<T: Ord>);
+
+#[cfg(feature = "std")]
+seq_impl!(HashSet<T: Eq + Hash, H: BuildHasher>);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+seq_impl!(LinkedList<T>);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+seq_impl!(Vec<T>);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+seq_impl!(VecDeque<T>);
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<Idx> Serialize for Range<Idx>
+where
+ Idx: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ use super::SerializeStruct;
+ let mut state = try!(serializer.serialize_struct("Range", 2));
+ try!(state.serialize_field("start", &self.start));
+ try!(state.serialize_field("end", &self.end));
+ state.end()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(not(no_range_inclusive))]
+impl<Idx> Serialize for RangeInclusive<Idx>
+where
+ Idx: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ use super::SerializeStruct;
+ let mut state = try!(serializer.serialize_struct("RangeInclusive", 2));
+ try!(state.serialize_field("start", &self.start()));
+ try!(state.serialize_field("end", &self.end()));
+ state.end()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(not(no_ops_bound), all(feature = "std", not(no_collections_bound))))]
+impl<T> Serialize for Bound<T>
+where
+ T: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match *self {
+ Bound::Unbounded => serializer.serialize_unit_variant("Bound", 0, "Unbounded"),
+ Bound::Included(ref value) => {
+ serializer.serialize_newtype_variant("Bound", 1, "Included", value)
+ }
+ Bound::Excluded(ref value) => {
+ serializer.serialize_newtype_variant("Bound", 2, "Excluded", value)
+ }
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl Serialize for () {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_unit()
+ }
+}
+
+#[cfg(feature = "unstable")]
+impl Serialize for ! {
+ fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ *self
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! tuple_impls {
+ ($($len:expr => ($($n:tt $name:ident)+))+) => {
+ $(
+ impl<$($name),+> Serialize for ($($name,)+)
+ where
+ $($name: Serialize,)+
+ {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ let mut tuple = try!(serializer.serialize_tuple($len));
+ $(
+ try!(tuple.serialize_element(&self.$n));
+ )+
+ tuple.end()
+ }
+ }
+ )+
+ }
+}
+
+tuple_impls! {
+ 1 => (0 T0)
+ 2 => (0 T0 1 T1)
+ 3 => (0 T0 1 T1 2 T2)
+ 4 => (0 T0 1 T1 2 T2 3 T3)
+ 5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
+ 6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
+ 7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
+ 8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
+ 9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
+ 10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
+ 11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
+ 12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
+ 13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
+ 14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
+ 15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
+ 16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(all(any(feature = "std", feature = "alloc"), not(no_relaxed_trait_bounds)))]
+macro_rules! map_impl {
+ ($ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>) => {
+ impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
+ where
+ K: Serialize,
+ V: Serialize,
+ {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.collect_map(self)
+ }
+ }
+ }
+}
+
+#[cfg(all(any(feature = "std", feature = "alloc"), no_relaxed_trait_bounds))]
+macro_rules! map_impl {
+ ($ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>) => {
+ impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
+ where
+ K: Serialize $(+ $kbound1 $(+ $kbound2)*)*,
+ V: Serialize,
+ $($typaram: $bound,)*
+ {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.collect_map(self)
+ }
+ }
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+map_impl!(BTreeMap<K: Ord, V>);
+
+#[cfg(feature = "std")]
+map_impl!(HashMap<K: Eq + Hash, V, H: BuildHasher>);
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! deref_impl {
+ (
+ $(#[doc = $doc:tt])*
+ <$($desc:tt)+
+ ) => {
+ $(#[doc = $doc])*
+ impl <$($desc)+ {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ (**self).serialize(serializer)
+ }
+ }
+ };
+}
+
+deref_impl!(<'a, T: ?Sized> Serialize for &'a T where T: Serialize);
+deref_impl!(<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize);
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+deref_impl!(<T: ?Sized> Serialize for Box<T> where T: Serialize);
+
+#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
+deref_impl! {
+ /// This impl requires the [`"rc"`] Cargo feature of Serde.
+ ///
+ /// Serializing a data structure containing `Rc` will serialize a copy of
+ /// the contents of the `Rc` each time the `Rc` is referenced within the
+ /// data structure. Serialization will not attempt to deduplicate these
+ /// repeated data.
+ ///
+ /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
+ <T: ?Sized> Serialize for Rc<T> where T: Serialize
+}
+
+#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
+deref_impl! {
+ /// This impl requires the [`"rc"`] Cargo feature of Serde.
+ ///
+ /// Serializing a data structure containing `Arc` will serialize a copy of
+ /// the contents of the `Arc` each time the `Arc` is referenced within the
+ /// data structure. Serialization will not attempt to deduplicate these
+ /// repeated data.
+ ///
+ /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
+ <T: ?Sized> Serialize for Arc<T> where T: Serialize
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+deref_impl!(<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned);
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// This impl requires the [`"rc"`] Cargo feature of Serde.
+///
+/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
+#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
+impl<T: ?Sized> Serialize for RcWeak<T>
+where
+ T: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.upgrade().serialize(serializer)
+ }
+}
+
+/// This impl requires the [`"rc"`] Cargo feature of Serde.
+///
+/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
+#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
+impl<T: ?Sized> Serialize for ArcWeak<T>
+where
+ T: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.upgrade().serialize(serializer)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! nonzero_integers {
+ ($($T:ident,)+) => {
+ $(
+ #[cfg(not(no_num_nonzero))]
+ impl Serialize for num::$T {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.get().serialize(serializer)
+ }
+ }
+ )+
+ }
+}
+
+nonzero_integers! {
+ NonZeroU8,
+ NonZeroU16,
+ NonZeroU32,
+ NonZeroU64,
+ NonZeroUsize,
+}
+
+#[cfg(not(no_num_nonzero_signed))]
+nonzero_integers! {
+ NonZeroI8,
+ NonZeroI16,
+ NonZeroI32,
+ NonZeroI64,
+ NonZeroIsize,
+}
+
+// Currently 128-bit integers do not work on Emscripten targets so we need an
+// additional `#[cfg]`
+serde_if_integer128! {
+ nonzero_integers! {
+ NonZeroU128,
+ }
+
+ #[cfg(not(no_num_nonzero_signed))]
+ nonzero_integers! {
+ NonZeroI128,
+ }
+}
+
+impl<T> Serialize for Cell<T>
+where
+ T: Serialize + Copy,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.get().serialize(serializer)
+ }
+}
+
+impl<T: ?Sized> Serialize for RefCell<T>
+where
+ T: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match self.try_borrow() {
+ Ok(value) => value.serialize(serializer),
+ Err(_) => Err(S::Error::custom("already mutably borrowed")),
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl<T: ?Sized> Serialize for Mutex<T>
+where
+ T: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match self.lock() {
+ Ok(locked) => locked.serialize(serializer),
+ Err(_) => Err(S::Error::custom("lock poison error while serializing")),
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl<T: ?Sized> Serialize for RwLock<T>
+where
+ T: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match self.read() {
+ Ok(locked) => locked.serialize(serializer),
+ Err(_) => Err(S::Error::custom("lock poison error while serializing")),
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<T, E> Serialize for Result<T, E>
+where
+ T: Serialize,
+ E: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match *self {
+ Result::Ok(ref value) => serializer.serialize_newtype_variant("Result", 0, "Ok", value),
+ Result::Err(ref value) => {
+ serializer.serialize_newtype_variant("Result", 1, "Err", value)
+ }
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(feature = "std", not(no_core_duration)))]
+impl Serialize for Duration {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ use super::SerializeStruct;
+ let mut state = try!(serializer.serialize_struct("Duration", 2));
+ try!(state.serialize_field("secs", &self.as_secs()));
+ try!(state.serialize_field("nanos", &self.subsec_nanos()));
+ state.end()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "std")]
+impl Serialize for SystemTime {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ use super::SerializeStruct;
+ let duration_since_epoch = match self.duration_since(UNIX_EPOCH) {
+ Ok(duration_since_epoch) => duration_since_epoch,
+ Err(_) => return Err(S::Error::custom("SystemTime must be later than UNIX_EPOCH")),
+ };
+ let mut state = try!(serializer.serialize_struct("SystemTime", 2));
+ try!(state.serialize_field("secs_since_epoch", &duration_since_epoch.as_secs()));
+ try!(state.serialize_field("nanos_since_epoch", &duration_since_epoch.subsec_nanos()));
+ state.end()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// Serialize a value that implements `Display` as a string, when that string is
+/// statically known to never have more than a constant `MAX_LEN` bytes.
+///
+/// Panics if the `Display` impl tries to write more than `MAX_LEN` bytes.
+#[cfg(feature = "std")]
+macro_rules! serialize_display_bounded_length {
+ ($value:expr, $max:expr, $serializer:expr) => {{
+ let mut buffer = [0u8; $max];
+ let remaining_len = {
+ let mut remaining = &mut buffer[..];
+ write!(remaining, "{}", $value).unwrap();
+ remaining.len()
+ };
+ let written_len = buffer.len() - remaining_len;
+ let written = &buffer[..written_len];
+
+ // write! only provides fmt::Formatter to Display implementations, which
+ // has methods write_str and write_char but no method to write arbitrary
+ // bytes. Therefore `written` must be valid UTF-8.
+ let written_str = str::from_utf8(written).expect("must be valid UTF-8");
+ $serializer.serialize_str(written_str)
+ }};
+}
+
+#[cfg(feature = "std")]
+impl Serialize for net::IpAddr {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ if serializer.is_human_readable() {
+ match *self {
+ net::IpAddr::V4(ref a) => a.serialize(serializer),
+ net::IpAddr::V6(ref a) => a.serialize(serializer),
+ }
+ } else {
+ match *self {
+ net::IpAddr::V4(ref a) => {
+ serializer.serialize_newtype_variant("IpAddr", 0, "V4", a)
+ }
+ net::IpAddr::V6(ref a) => {
+ serializer.serialize_newtype_variant("IpAddr", 1, "V6", a)
+ }
+ }
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+const DEC_DIGITS_LUT: &'static [u8] = b"\
+ 0001020304050607080910111213141516171819\
+ 2021222324252627282930313233343536373839\
+ 4041424344454647484950515253545556575859\
+ 6061626364656667686970717273747576777879\
+ 8081828384858687888990919293949596979899";
+
+#[cfg(feature = "std")]
+#[inline]
+fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
+ if n >= 100 {
+ let d1 = ((n % 100) << 1) as usize;
+ n /= 100;
+ out[0] = b'0' + n;
+ out[1] = DEC_DIGITS_LUT[d1];
+ out[2] = DEC_DIGITS_LUT[d1 + 1];
+ 3
+ } else if n >= 10 {
+ let d1 = (n << 1) as usize;
+ out[0] = DEC_DIGITS_LUT[d1];
+ out[1] = DEC_DIGITS_LUT[d1 + 1];
+ 2
+ } else {
+ out[0] = b'0' + n;
+ 1
+ }
+}
+
+#[cfg(feature = "std")]
+#[test]
+fn test_format_u8() {
+ let mut i = 0u8;
+
+ loop {
+ let mut buf = [0u8; 3];
+ let written = format_u8(i, &mut buf);
+ assert_eq!(i.to_string().as_bytes(), &buf[..written]);
+
+ match i.checked_add(1) {
+ Some(next) => i = next,
+ None => break,
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl Serialize for net::Ipv4Addr {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ if serializer.is_human_readable() {
+ const MAX_LEN: usize = 15;
+ debug_assert_eq!(MAX_LEN, "101.102.103.104".len());
+ let mut buf = [b'.'; MAX_LEN];
+ let mut written = format_u8(self.octets()[0], &mut buf);
+ for oct in &self.octets()[1..] {
+ // Skip over delimiters that we initialized buf with
+ written += format_u8(*oct, &mut buf[written + 1..]) + 1;
+ }
+ // Safety: We've only written ASCII bytes to the buffer, so it is valid UTF-8
+ let buf = unsafe { str::from_utf8_unchecked(&buf[..written]) };
+ serializer.serialize_str(buf)
+ } else {
+ self.octets().serialize(serializer)
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl Serialize for net::Ipv6Addr {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ if serializer.is_human_readable() {
+ const MAX_LEN: usize = 39;
+ debug_assert_eq!(MAX_LEN, "1001:1002:1003:1004:1005:1006:1007:1008".len());
+ serialize_display_bounded_length!(self, MAX_LEN, serializer)
+ } else {
+ self.octets().serialize(serializer)
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl Serialize for net::SocketAddr {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ if serializer.is_human_readable() {
+ match *self {
+ net::SocketAddr::V4(ref addr) => addr.serialize(serializer),
+ net::SocketAddr::V6(ref addr) => addr.serialize(serializer),
+ }
+ } else {
+ match *self {
+ net::SocketAddr::V4(ref addr) => {
+ serializer.serialize_newtype_variant("SocketAddr", 0, "V4", addr)
+ }
+ net::SocketAddr::V6(ref addr) => {
+ serializer.serialize_newtype_variant("SocketAddr", 1, "V6", addr)
+ }
+ }
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl Serialize for net::SocketAddrV4 {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ if serializer.is_human_readable() {
+ const MAX_LEN: usize = 21;
+ debug_assert_eq!(MAX_LEN, "101.102.103.104:65000".len());
+ serialize_display_bounded_length!(self, MAX_LEN, serializer)
+ } else {
+ (self.ip(), self.port()).serialize(serializer)
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl Serialize for net::SocketAddrV6 {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ if serializer.is_human_readable() {
+ const MAX_LEN: usize = 58;
+ debug_assert_eq!(
+ MAX_LEN,
+ "[1001:1002:1003:1004:1005:1006:1007:1008%4294967295]:65000".len()
+ );
+ serialize_display_bounded_length!(self, MAX_LEN, serializer)
+ } else {
+ (self.ip(), self.port()).serialize(serializer)
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "std")]
+impl Serialize for Path {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match self.to_str() {
+ Some(s) => s.serialize(serializer),
+ None => Err(Error::custom("path contains invalid UTF-8 characters")),
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl Serialize for PathBuf {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.as_path().serialize(serializer)
+ }
+}
+
+#[cfg(all(feature = "std", any(unix, windows)))]
+impl Serialize for OsStr {
+ #[cfg(unix)]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ use std::os::unix::ffi::OsStrExt;
+ serializer.serialize_newtype_variant("OsString", 0, "Unix", self.as_bytes())
+ }
+
+ #[cfg(windows)]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ use std::os::windows::ffi::OsStrExt;
+ let val = self.encode_wide().collect::<Vec<_>>();
+ serializer.serialize_newtype_variant("OsString", 1, "Windows", &val)
+ }
+}
+
+#[cfg(all(feature = "std", any(unix, windows)))]
+impl Serialize for OsString {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.as_os_str().serialize(serializer)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<T> Serialize for Wrapping<T>
+where
+ T: Serialize,
+{
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.0.serialize(serializer)
+ }
+}
+
+#[cfg(not(no_core_reverse))]
+impl<T> Serialize for Reverse<T>
+where
+ T: Serialize,
+{
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.0.serialize(serializer)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(all(feature = "std", not(no_std_atomic)))]
+macro_rules! atomic_impl {
+ ($($ty:ident $size:expr)*) => {
+ $(
+ #[cfg(any(no_target_has_atomic, target_has_atomic = $size))]
+ impl Serialize for $ty {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ // Matches the atomic ordering used in libcore for the Debug impl
+ self.load(Ordering::Relaxed).serialize(serializer)
+ }
+ }
+ )*
+ }
+}
+
+#[cfg(all(feature = "std", not(no_std_atomic)))]
+atomic_impl! {
+ AtomicBool "8"
+ AtomicI8 "8"
+ AtomicI16 "16"
+ AtomicI32 "32"
+ AtomicIsize "ptr"
+ AtomicU8 "8"
+ AtomicU16 "16"
+ AtomicU32 "32"
+ AtomicUsize "ptr"
+}
+
+#[cfg(all(feature = "std", not(no_std_atomic64)))]
+atomic_impl! {
+ AtomicI64 "64"
+ AtomicU64 "64"
+}
diff --git a/third_party/rust/serde/src/ser/impossible.rs b/third_party/rust/serde/src/ser/impossible.rs
new file mode 100644
index 0000000000..e8df9ca7aa
--- /dev/null
+++ b/third_party/rust/serde/src/ser/impossible.rs
@@ -0,0 +1,216 @@
+//! This module contains `Impossible` serializer and its implementations.
+
+use lib::*;
+
+use ser::{
+ self, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant,
+ SerializeTuple, SerializeTupleStruct, SerializeTupleVariant,
+};
+
+/// Helper type for implementing a `Serializer` that does not support
+/// serializing one of the compound types.
+///
+/// This type cannot be instantiated, but implements every one of the traits
+/// corresponding to the [`Serializer`] compound types: [`SerializeSeq`],
+/// [`SerializeTuple`], [`SerializeTupleStruct`], [`SerializeTupleVariant`],
+/// [`SerializeMap`], [`SerializeStruct`], and [`SerializeStructVariant`].
+///
+/// ```edition2018
+/// # use serde::ser::{Serializer, Impossible};
+/// # use serde::__private::doc::Error;
+/// #
+/// # struct MySerializer;
+/// #
+/// impl Serializer for MySerializer {
+/// type Ok = ();
+/// type Error = Error;
+///
+/// type SerializeSeq = Impossible<(), Error>;
+/// /* other associated types */
+///
+/// /// This data format does not support serializing sequences.
+/// fn serialize_seq(self,
+/// len: Option<usize>)
+/// -> Result<Self::SerializeSeq, Error> {
+/// // Given Impossible cannot be instantiated, the only
+/// // thing we can do here is to return an error.
+/// # stringify! {
+/// Err(...)
+/// # };
+/// # unimplemented!()
+/// }
+///
+/// /* other Serializer methods */
+/// # serde::__serialize_unimplemented! {
+/// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str bytes none some
+/// # unit unit_struct unit_variant newtype_struct newtype_variant
+/// # tuple tuple_struct tuple_variant map struct struct_variant
+/// # }
+/// }
+/// ```
+///
+/// [`Serializer`]: trait.Serializer.html
+/// [`SerializeSeq`]: trait.SerializeSeq.html
+/// [`SerializeTuple`]: trait.SerializeTuple.html
+/// [`SerializeTupleStruct`]: trait.SerializeTupleStruct.html
+/// [`SerializeTupleVariant`]: trait.SerializeTupleVariant.html
+/// [`SerializeMap`]: trait.SerializeMap.html
+/// [`SerializeStruct`]: trait.SerializeStruct.html
+/// [`SerializeStructVariant`]: trait.SerializeStructVariant.html
+pub struct Impossible<Ok, Error> {
+ void: Void,
+ ok: PhantomData<Ok>,
+ error: PhantomData<Error>,
+}
+
+enum Void {}
+
+impl<Ok, Error> SerializeSeq for Impossible<Ok, Error>
+where
+ Error: ser::Error,
+{
+ type Ok = Ok;
+ type Error = Error;
+
+ fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
+ where
+ T: Serialize,
+ {
+ let _ = value;
+ match self.void {}
+ }
+
+ fn end(self) -> Result<Ok, Error> {
+ match self.void {}
+ }
+}
+
+impl<Ok, Error> SerializeTuple for Impossible<Ok, Error>
+where
+ Error: ser::Error,
+{
+ type Ok = Ok;
+ type Error = Error;
+
+ fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
+ where
+ T: Serialize,
+ {
+ let _ = value;
+ match self.void {}
+ }
+
+ fn end(self) -> Result<Ok, Error> {
+ match self.void {}
+ }
+}
+
+impl<Ok, Error> SerializeTupleStruct for Impossible<Ok, Error>
+where
+ Error: ser::Error,
+{
+ type Ok = Ok;
+ type Error = Error;
+
+ fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
+ where
+ T: Serialize,
+ {
+ let _ = value;
+ match self.void {}
+ }
+
+ fn end(self) -> Result<Ok, Error> {
+ match self.void {}
+ }
+}
+
+impl<Ok, Error> SerializeTupleVariant for Impossible<Ok, Error>
+where
+ Error: ser::Error,
+{
+ type Ok = Ok;
+ type Error = Error;
+
+ fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
+ where
+ T: Serialize,
+ {
+ let _ = value;
+ match self.void {}
+ }
+
+ fn end(self) -> Result<Ok, Error> {
+ match self.void {}
+ }
+}
+
+impl<Ok, Error> SerializeMap for Impossible<Ok, Error>
+where
+ Error: ser::Error,
+{
+ type Ok = Ok;
+ type Error = Error;
+
+ fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Error>
+ where
+ T: Serialize,
+ {
+ let _ = key;
+ match self.void {}
+ }
+
+ fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
+ where
+ T: Serialize,
+ {
+ let _ = value;
+ match self.void {}
+ }
+
+ fn end(self) -> Result<Ok, Error> {
+ match self.void {}
+ }
+}
+
+impl<Ok, Error> SerializeStruct for Impossible<Ok, Error>
+where
+ Error: ser::Error,
+{
+ type Ok = Ok;
+ type Error = Error;
+
+ fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
+ where
+ T: Serialize,
+ {
+ let _ = key;
+ let _ = value;
+ match self.void {}
+ }
+
+ fn end(self) -> Result<Ok, Error> {
+ match self.void {}
+ }
+}
+
+impl<Ok, Error> SerializeStructVariant for Impossible<Ok, Error>
+where
+ Error: ser::Error,
+{
+ type Ok = Ok;
+ type Error = Error;
+
+ fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
+ where
+ T: Serialize,
+ {
+ let _ = key;
+ let _ = value;
+ match self.void {}
+ }
+
+ fn end(self) -> Result<Ok, Error> {
+ match self.void {}
+ }
+}
diff --git a/third_party/rust/serde/src/ser/mod.rs b/third_party/rust/serde/src/ser/mod.rs
new file mode 100644
index 0000000000..5c45426e80
--- /dev/null
+++ b/third_party/rust/serde/src/ser/mod.rs
@@ -0,0 +1,1988 @@
+//! Generic data structure serialization framework.
+//!
+//! The two most important traits in this module are [`Serialize`] and
+//! [`Serializer`].
+//!
+//! - **A type that implements `Serialize` is a data structure** that can be
+//! serialized to any data format supported by Serde, and conversely
+//! - **A type that implements `Serializer` is a data format** that can
+//! serialize any data structure supported by Serde.
+//!
+//! # The Serialize trait
+//!
+//! Serde provides [`Serialize`] implementations for many Rust primitive and
+//! standard library types. The complete list is below. All of these can be
+//! serialized using Serde out of the box.
+//!
+//! Additionally, Serde provides a procedural macro called [`serde_derive`] to
+//! automatically generate [`Serialize`] implementations for structs and enums
+//! in your program. See the [derive section of the manual] for how to use this.
+//!
+//! In rare cases it may be necessary to implement [`Serialize`] manually for
+//! some type in your program. See the [Implementing `Serialize`] section of the
+//! manual for more about this.
+//!
+//! Third-party crates may provide [`Serialize`] implementations for types that
+//! they expose. For example the [`linked-hash-map`] crate provides a
+//! [`LinkedHashMap<K, V>`] type that is serializable by Serde because the crate
+//! provides an implementation of [`Serialize`] for it.
+//!
+//! # The Serializer trait
+//!
+//! [`Serializer`] implementations are provided by third-party crates, for
+//! example [`serde_json`], [`serde_yaml`] and [`postcard`].
+//!
+//! A partial list of well-maintained formats is given on the [Serde
+//! website][data formats].
+//!
+//! # Implementations of Serialize provided by Serde
+//!
+//! - **Primitive types**:
+//! - bool
+//! - i8, i16, i32, i64, i128, isize
+//! - u8, u16, u32, u64, u128, usize
+//! - f32, f64
+//! - char
+//! - str
+//! - &T and &mut T
+//! - **Compound types**:
+//! - \[T\]
+//! - \[T; 0\] through \[T; 32\]
+//! - tuples up to size 16
+//! - **Common standard library types**:
+//! - String
+//! - Option\<T\>
+//! - Result\<T, E\>
+//! - PhantomData\<T\>
+//! - **Wrapper types**:
+//! - Box\<T\>
+//! - Cow\<'a, T\>
+//! - Cell\<T\>
+//! - RefCell\<T\>
+//! - Mutex\<T\>
+//! - RwLock\<T\>
+//! - Rc\<T\>&emsp;*(if* features = ["rc"] *is enabled)*
+//! - Arc\<T\>&emsp;*(if* features = ["rc"] *is enabled)*
+//! - **Collection types**:
+//! - BTreeMap\<K, V\>
+//! - BTreeSet\<T\>
+//! - BinaryHeap\<T\>
+//! - HashMap\<K, V, H\>
+//! - HashSet\<T, H\>
+//! - LinkedList\<T\>
+//! - VecDeque\<T\>
+//! - Vec\<T\>
+//! - **FFI types**:
+//! - CStr
+//! - CString
+//! - OsStr
+//! - OsString
+//! - **Miscellaneous standard library types**:
+//! - Duration
+//! - SystemTime
+//! - Path
+//! - PathBuf
+//! - Range\<T\>
+//! - RangeInclusive\<T\>
+//! - Bound\<T\>
+//! - num::NonZero*
+//! - `!` *(unstable)*
+//! - **Net types**:
+//! - IpAddr
+//! - Ipv4Addr
+//! - Ipv6Addr
+//! - SocketAddr
+//! - SocketAddrV4
+//! - SocketAddrV6
+//!
+//! [Implementing `Serialize`]: https://serde.rs/impl-serialize.html
+//! [`LinkedHashMap<K, V>`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html
+//! [`Serialize`]: ../trait.Serialize.html
+//! [`Serializer`]: ../trait.Serializer.html
+//! [`postcard`]: https://github.com/jamesmunns/postcard
+//! [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
+//! [`serde_derive`]: https://crates.io/crates/serde_derive
+//! [`serde_json`]: https://github.com/serde-rs/json
+//! [`serde_yaml`]: https://github.com/dtolnay/serde-yaml
+//! [derive section of the manual]: https://serde.rs/derive.html
+//! [data formats]: https://serde.rs/#data-formats
+
+use lib::*;
+
+mod fmt;
+mod impls;
+mod impossible;
+
+pub use self::impossible::Impossible;
+
+#[cfg(all(feature = "unstable", not(feature = "std")))]
+#[doc(inline)]
+pub use core::error::Error as StdError;
+#[cfg(feature = "std")]
+#[doc(no_inline)]
+pub use std::error::Error as StdError;
+#[cfg(not(any(feature = "std", feature = "unstable")))]
+#[doc(no_inline)]
+pub use std_error::Error as StdError;
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! declare_error_trait {
+ (Error: Sized $(+ $($supertrait:ident)::+)*) => {
+ /// Trait used by `Serialize` implementations to generically construct
+ /// errors belonging to the `Serializer` against which they are
+ /// currently running.
+ ///
+ /// # Example implementation
+ ///
+ /// The [example data format] presented on the website shows an error
+ /// type appropriate for a basic JSON data format.
+ ///
+ /// [example data format]: https://serde.rs/data-format.html
+ pub trait Error: Sized $(+ $($supertrait)::+)* {
+ /// Used when a [`Serialize`] implementation encounters any error
+ /// while serializing a type.
+ ///
+ /// The message should not be capitalized and should not end with a
+ /// period.
+ ///
+ /// For example, a filesystem [`Path`] may refuse to serialize
+ /// itself if it contains invalid UTF-8 data.
+ ///
+ /// ```edition2018
+ /// # struct Path;
+ /// #
+ /// # impl Path {
+ /// # fn to_str(&self) -> Option<&str> {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// use serde::ser::{self, Serialize, Serializer};
+ ///
+ /// impl Serialize for Path {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// match self.to_str() {
+ /// Some(s) => serializer.serialize_str(s),
+ /// None => Err(ser::Error::custom("path contains invalid UTF-8 characters")),
+ /// }
+ /// }
+ /// }
+ /// ```
+ ///
+ /// [`Path`]: https://doc.rust-lang.org/std/path/struct.Path.html
+ /// [`Serialize`]: ../trait.Serialize.html
+ fn custom<T>(msg: T) -> Self
+ where
+ T: Display;
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+declare_error_trait!(Error: Sized + StdError);
+
+#[cfg(not(feature = "std"))]
+declare_error_trait!(Error: Sized + Debug + Display);
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A **data structure** that can be serialized into any data format supported
+/// by Serde.
+///
+/// Serde provides `Serialize` implementations for many Rust primitive and
+/// standard library types. The complete list is [here][crate::ser]. All of
+/// these can be serialized using Serde out of the box.
+///
+/// Additionally, Serde provides a procedural macro called [`serde_derive`] to
+/// automatically generate `Serialize` implementations for structs and enums in
+/// your program. See the [derive section of the manual] for how to use this.
+///
+/// In rare cases it may be necessary to implement `Serialize` manually for some
+/// type in your program. See the [Implementing `Serialize`] section of the
+/// manual for more about this.
+///
+/// Third-party crates may provide `Serialize` implementations for types that
+/// they expose. For example the [`linked-hash-map`] crate provides a
+/// [`LinkedHashMap<K, V>`] type that is serializable by Serde because the crate
+/// provides an implementation of `Serialize` for it.
+///
+/// [Implementing `Serialize`]: https://serde.rs/impl-serialize.html
+/// [`LinkedHashMap<K, V>`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html
+/// [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
+/// [`serde_derive`]: https://crates.io/crates/serde_derive
+/// [derive section of the manual]: https://serde.rs/derive.html
+pub trait Serialize {
+ /// Serialize this value into the given Serde serializer.
+ ///
+ /// See the [Implementing `Serialize`] section of the manual for more
+ /// information about how to implement this method.
+ ///
+ /// ```edition2018
+ /// use serde::ser::{Serialize, SerializeStruct, Serializer};
+ ///
+ /// struct Person {
+ /// name: String,
+ /// age: u8,
+ /// phones: Vec<String>,
+ /// }
+ ///
+ /// // This is what #[derive(Serialize)] would generate.
+ /// impl Serialize for Person {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// let mut s = serializer.serialize_struct("Person", 3)?;
+ /// s.serialize_field("name", &self.name)?;
+ /// s.serialize_field("age", &self.age)?;
+ /// s.serialize_field("phones", &self.phones)?;
+ /// s.end()
+ /// }
+ /// }
+ /// ```
+ ///
+ /// [Implementing `Serialize`]: https://serde.rs/impl-serialize.html
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/// A **data format** that can serialize any data structure supported by Serde.
+///
+/// The role of this trait is to define the serialization half of the [Serde
+/// data model], which is a way to categorize every Rust data structure into one
+/// of 29 possible types. Each method of the `Serializer` trait corresponds to
+/// one of the types of the data model.
+///
+/// Implementations of `Serialize` map themselves into this data model by
+/// invoking exactly one of the `Serializer` methods.
+///
+/// The types that make up the Serde data model are:
+///
+/// - **14 primitive types**
+/// - bool
+/// - i8, i16, i32, i64, i128
+/// - u8, u16, u32, u64, u128
+/// - f32, f64
+/// - char
+/// - **string**
+/// - UTF-8 bytes with a length and no null terminator.
+/// - When serializing, all strings are handled equally. When deserializing,
+/// there are three flavors of strings: transient, owned, and borrowed.
+/// - **byte array** - \[u8\]
+/// - Similar to strings, during deserialization byte arrays can be
+/// transient, owned, or borrowed.
+/// - **option**
+/// - Either none or some value.
+/// - **unit**
+/// - The type of `()` in Rust. It represents an anonymous value containing
+/// no data.
+/// - **unit_struct**
+/// - For example `struct Unit` or `PhantomData<T>`. It represents a named
+/// value containing no data.
+/// - **unit_variant**
+/// - For example the `E::A` and `E::B` in `enum E { A, B }`.
+/// - **newtype_struct**
+/// - For example `struct Millimeters(u8)`.
+/// - **newtype_variant**
+/// - For example the `E::N` in `enum E { N(u8) }`.
+/// - **seq**
+/// - A variably sized heterogeneous sequence of values, for example
+/// `Vec<T>` or `HashSet<T>`. When serializing, the length may or may not
+/// be known before iterating through all the data. When deserializing,
+/// the length is determined by looking at the serialized data.
+/// - **tuple**
+/// - A statically sized heterogeneous sequence of values for which the
+/// length will be known at deserialization time without looking at the
+/// serialized data, for example `(u8,)` or `(String, u64, Vec<T>)` or
+/// `[u64; 10]`.
+/// - **tuple_struct**
+/// - A named tuple, for example `struct Rgb(u8, u8, u8)`.
+/// - **tuple_variant**
+/// - For example the `E::T` in `enum E { T(u8, u8) }`.
+/// - **map**
+/// - A heterogeneous key-value pairing, for example `BTreeMap<K, V>`.
+/// - **struct**
+/// - A heterogeneous key-value pairing in which the keys are strings and
+/// will be known at deserialization time without looking at the
+/// serialized data, for example `struct S { r: u8, g: u8, b: u8 }`.
+/// - **struct_variant**
+/// - For example the `E::S` in `enum E { S { r: u8, g: u8, b: u8 } }`.
+///
+/// Many Serde serializers produce text or binary data as output, for example
+/// JSON or Postcard. This is not a requirement of the `Serializer` trait, and
+/// there are serializers that do not produce text or binary output. One example
+/// is the `serde_json::value::Serializer` (distinct from the main `serde_json`
+/// serializer) that produces a `serde_json::Value` data structure in memory as
+/// output.
+///
+/// [Serde data model]: https://serde.rs/data-model.html
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website contains example code for
+/// a basic JSON `Serializer`.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait Serializer: Sized {
+ /// The output type produced by this `Serializer` during successful
+ /// serialization. Most serializers that produce text or binary output
+ /// should set `Ok = ()` and serialize into an [`io::Write`] or buffer
+ /// contained within the `Serializer` instance. Serializers that build
+ /// in-memory data structures may be simplified by using `Ok` to propagate
+ /// the data structure around.
+ ///
+ /// [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
+ type Ok;
+
+ /// The error type when some error occurs during serialization.
+ type Error: Error;
+
+ /// Type returned from [`serialize_seq`] for serializing the content of the
+ /// sequence.
+ ///
+ /// [`serialize_seq`]: #tymethod.serialize_seq
+ type SerializeSeq: SerializeSeq<Ok = Self::Ok, Error = Self::Error>;
+
+ /// Type returned from [`serialize_tuple`] for serializing the content of
+ /// the tuple.
+ ///
+ /// [`serialize_tuple`]: #tymethod.serialize_tuple
+ type SerializeTuple: SerializeTuple<Ok = Self::Ok, Error = Self::Error>;
+
+ /// Type returned from [`serialize_tuple_struct`] for serializing the
+ /// content of the tuple struct.
+ ///
+ /// [`serialize_tuple_struct`]: #tymethod.serialize_tuple_struct
+ type SerializeTupleStruct: SerializeTupleStruct<Ok = Self::Ok, Error = Self::Error>;
+
+ /// Type returned from [`serialize_tuple_variant`] for serializing the
+ /// content of the tuple variant.
+ ///
+ /// [`serialize_tuple_variant`]: #tymethod.serialize_tuple_variant
+ type SerializeTupleVariant: SerializeTupleVariant<Ok = Self::Ok, Error = Self::Error>;
+
+ /// Type returned from [`serialize_map`] for serializing the content of the
+ /// map.
+ ///
+ /// [`serialize_map`]: #tymethod.serialize_map
+ type SerializeMap: SerializeMap<Ok = Self::Ok, Error = Self::Error>;
+
+ /// Type returned from [`serialize_struct`] for serializing the content of
+ /// the struct.
+ ///
+ /// [`serialize_struct`]: #tymethod.serialize_struct
+ type SerializeStruct: SerializeStruct<Ok = Self::Ok, Error = Self::Error>;
+
+ /// Type returned from [`serialize_struct_variant`] for serializing the
+ /// content of the struct variant.
+ ///
+ /// [`serialize_struct_variant`]: #tymethod.serialize_struct_variant
+ type SerializeStructVariant: SerializeStructVariant<Ok = Self::Ok, Error = Self::Error>;
+
+ /// Serialize a `bool` value.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for bool {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_bool(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize an `i8` value.
+ ///
+ /// If the format does not differentiate between `i8` and `i64`, a
+ /// reasonable implementation would be to cast the value to `i64` and
+ /// forward to `serialize_i64`.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for i8 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_i8(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize an `i16` value.
+ ///
+ /// If the format does not differentiate between `i16` and `i64`, a
+ /// reasonable implementation would be to cast the value to `i64` and
+ /// forward to `serialize_i64`.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for i16 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_i16(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize an `i32` value.
+ ///
+ /// If the format does not differentiate between `i32` and `i64`, a
+ /// reasonable implementation would be to cast the value to `i64` and
+ /// forward to `serialize_i64`.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for i32 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_i32(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize an `i64` value.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for i64 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_i64(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error>;
+
+ serde_if_integer128! {
+ /// Serialize an `i128` value.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for i128 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_i128(*self)
+ /// }
+ /// }
+ /// ```
+ ///
+ /// This method is available only on Rust compiler versions >=1.26. The
+ /// default behavior unconditionally returns an error.
+ fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
+ let _ = v;
+ Err(Error::custom("i128 is not supported"))
+ }
+ }
+
+ /// Serialize a `u8` value.
+ ///
+ /// If the format does not differentiate between `u8` and `u64`, a
+ /// reasonable implementation would be to cast the value to `u64` and
+ /// forward to `serialize_u64`.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for u8 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_u8(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a `u16` value.
+ ///
+ /// If the format does not differentiate between `u16` and `u64`, a
+ /// reasonable implementation would be to cast the value to `u64` and
+ /// forward to `serialize_u64`.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for u16 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_u16(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a `u32` value.
+ ///
+ /// If the format does not differentiate between `u32` and `u64`, a
+ /// reasonable implementation would be to cast the value to `u64` and
+ /// forward to `serialize_u64`.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for u32 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_u32(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a `u64` value.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for u64 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_u64(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error>;
+
+ serde_if_integer128! {
+ /// Serialize a `u128` value.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for u128 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_u128(*self)
+ /// }
+ /// }
+ /// ```
+ ///
+ /// This method is available only on Rust compiler versions >=1.26. The
+ /// default behavior unconditionally returns an error.
+ fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
+ let _ = v;
+ Err(Error::custom("u128 is not supported"))
+ }
+ }
+
+ /// Serialize an `f32` value.
+ ///
+ /// If the format does not differentiate between `f32` and `f64`, a
+ /// reasonable implementation would be to cast the value to `f64` and
+ /// forward to `serialize_f64`.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for f32 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_f32(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize an `f64` value.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for f64 {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_f64(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a character.
+ ///
+ /// If the format does not support characters, it is reasonable to serialize
+ /// it as a single element `str` or a `u32`.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for char {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_char(*self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a `&str`.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for str {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_str(self)
+ /// }
+ /// }
+ /// ```
+ fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a chunk of raw byte data.
+ ///
+ /// Enables serializers to serialize byte slices more compactly or more
+ /// efficiently than other types of slices. If no efficient implementation
+ /// is available, a reasonable implementation would be to forward to
+ /// `serialize_seq`. If forwarded, the implementation looks usually just
+ /// like this:
+ ///
+ /// ```edition2018
+ /// # use serde::ser::{Serializer, SerializeSeq};
+ /// # use serde::__private::doc::Error;
+ /// #
+ /// # struct MySerializer;
+ /// #
+ /// # impl Serializer for MySerializer {
+ /// # type Ok = ();
+ /// # type Error = Error;
+ /// #
+ /// fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
+ /// let mut seq = self.serialize_seq(Some(v.len()))?;
+ /// for b in v {
+ /// seq.serialize_element(b)?;
+ /// }
+ /// seq.end()
+ /// }
+ /// #
+ /// # serde::__serialize_unimplemented! {
+ /// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str none some
+ /// # unit unit_struct unit_variant newtype_struct newtype_variant
+ /// # seq tuple tuple_struct tuple_variant map struct struct_variant
+ /// # }
+ /// # }
+ /// ```
+ fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a [`None`] value.
+ ///
+ /// ```edition2018
+ /// # use serde::{Serialize, Serializer};
+ /// #
+ /// # enum Option<T> {
+ /// # Some(T),
+ /// # None,
+ /// # }
+ /// #
+ /// # use self::Option::{Some, None};
+ /// #
+ /// impl<T> Serialize for Option<T>
+ /// where
+ /// T: Serialize,
+ /// {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// match *self {
+ /// Some(ref value) => serializer.serialize_some(value),
+ /// None => serializer.serialize_none(),
+ /// }
+ /// }
+ /// }
+ /// #
+ /// # fn main() {}
+ /// ```
+ ///
+ /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
+ fn serialize_none(self) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a [`Some(T)`] value.
+ ///
+ /// ```edition2018
+ /// # use serde::{Serialize, Serializer};
+ /// #
+ /// # enum Option<T> {
+ /// # Some(T),
+ /// # None,
+ /// # }
+ /// #
+ /// # use self::Option::{Some, None};
+ /// #
+ /// impl<T> Serialize for Option<T>
+ /// where
+ /// T: Serialize,
+ /// {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// match *self {
+ /// Some(ref value) => serializer.serialize_some(value),
+ /// None => serializer.serialize_none(),
+ /// }
+ /// }
+ /// }
+ /// #
+ /// # fn main() {}
+ /// ```
+ ///
+ /// [`Some(T)`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some
+ fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
+ where
+ T: Serialize;
+
+ /// Serialize a `()` value.
+ ///
+ /// ```edition2018
+ /// # use serde::Serializer;
+ /// #
+ /// # serde::__private_serialize!();
+ /// #
+ /// impl Serialize for () {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_unit()
+ /// }
+ /// }
+ /// ```
+ fn serialize_unit(self) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a unit struct like `struct Unit` or `PhantomData<T>`.
+ ///
+ /// A reasonable implementation would be to forward to `serialize_unit`.
+ ///
+ /// ```edition2018
+ /// use serde::{Serialize, Serializer};
+ ///
+ /// struct Nothing;
+ ///
+ /// impl Serialize for Nothing {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_unit_struct("Nothing")
+ /// }
+ /// }
+ /// ```
+ fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a unit variant like `E::A` in `enum E { A, B }`.
+ ///
+ /// The `name` is the name of the enum, the `variant_index` is the index of
+ /// this variant within the enum, and the `variant` is the name of the
+ /// variant.
+ ///
+ /// ```edition2018
+ /// use serde::{Serialize, Serializer};
+ ///
+ /// enum E {
+ /// A,
+ /// B,
+ /// }
+ ///
+ /// impl Serialize for E {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// match *self {
+ /// E::A => serializer.serialize_unit_variant("E", 0, "A"),
+ /// E::B => serializer.serialize_unit_variant("E", 1, "B"),
+ /// }
+ /// }
+ /// }
+ /// ```
+ fn serialize_unit_variant(
+ self,
+ name: &'static str,
+ variant_index: u32,
+ variant: &'static str,
+ ) -> Result<Self::Ok, Self::Error>;
+
+ /// Serialize a newtype struct like `struct Millimeters(u8)`.
+ ///
+ /// Serializers are encouraged to treat newtype structs as insignificant
+ /// wrappers around the data they contain. A reasonable implementation would
+ /// be to forward to `value.serialize(self)`.
+ ///
+ /// ```edition2018
+ /// use serde::{Serialize, Serializer};
+ ///
+ /// struct Millimeters(u8);
+ ///
+ /// impl Serialize for Millimeters {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.serialize_newtype_struct("Millimeters", &self.0)
+ /// }
+ /// }
+ /// ```
+ fn serialize_newtype_struct<T: ?Sized>(
+ self,
+ name: &'static str,
+ value: &T,
+ ) -> Result<Self::Ok, Self::Error>
+ where
+ T: Serialize;
+
+ /// Serialize a newtype variant like `E::N` in `enum E { N(u8) }`.
+ ///
+ /// The `name` is the name of the enum, the `variant_index` is the index of
+ /// this variant within the enum, and the `variant` is the name of the
+ /// variant. The `value` is the data contained within this newtype variant.
+ ///
+ /// ```edition2018
+ /// use serde::{Serialize, Serializer};
+ ///
+ /// enum E {
+ /// M(String),
+ /// N(u8),
+ /// }
+ ///
+ /// impl Serialize for E {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// match *self {
+ /// E::M(ref s) => serializer.serialize_newtype_variant("E", 0, "M", s),
+ /// E::N(n) => serializer.serialize_newtype_variant("E", 1, "N", &n),
+ /// }
+ /// }
+ /// }
+ /// ```
+ fn serialize_newtype_variant<T: ?Sized>(
+ self,
+ name: &'static str,
+ variant_index: u32,
+ variant: &'static str,
+ value: &T,
+ ) -> Result<Self::Ok, Self::Error>
+ where
+ T: Serialize;
+
+ /// Begin to serialize a variably sized sequence. This call must be
+ /// followed by zero or more calls to `serialize_element`, then a call to
+ /// `end`.
+ ///
+ /// The argument is the number of elements in the sequence, which may or may
+ /// not be computable before the sequence is iterated. Some serializers only
+ /// support sequences whose length is known up front.
+ ///
+ /// ```edition2018
+ /// # use std::marker::PhantomData;
+ /// #
+ /// # struct Vec<T>(PhantomData<T>);
+ /// #
+ /// # impl<T> Vec<T> {
+ /// # fn len(&self) -> usize {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// # impl<'a, T> IntoIterator for &'a Vec<T> {
+ /// # type Item = &'a T;
+ /// # type IntoIter = Box<Iterator<Item = &'a T>>;
+ /// #
+ /// # fn into_iter(self) -> Self::IntoIter {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// use serde::ser::{Serialize, Serializer, SerializeSeq};
+ ///
+ /// impl<T> Serialize for Vec<T>
+ /// where
+ /// T: Serialize,
+ /// {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// let mut seq = serializer.serialize_seq(Some(self.len()))?;
+ /// for element in self {
+ /// seq.serialize_element(element)?;
+ /// }
+ /// seq.end()
+ /// }
+ /// }
+ /// ```
+ fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error>;
+
+ /// Begin to serialize a statically sized sequence whose length will be
+ /// known at deserialization time without looking at the serialized data.
+ /// This call must be followed by zero or more calls to `serialize_element`,
+ /// then a call to `end`.
+ ///
+ /// ```edition2018
+ /// use serde::ser::{Serialize, Serializer, SerializeTuple};
+ ///
+ /// # mod fool {
+ /// # trait Serialize {}
+ /// impl<A, B, C> Serialize for (A, B, C)
+ /// # {}
+ /// # }
+ /// #
+ /// # struct Tuple3<A, B, C>(A, B, C);
+ /// #
+ /// # impl<A, B, C> Serialize for Tuple3<A, B, C>
+ /// where
+ /// A: Serialize,
+ /// B: Serialize,
+ /// C: Serialize,
+ /// {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// let mut tup = serializer.serialize_tuple(3)?;
+ /// tup.serialize_element(&self.0)?;
+ /// tup.serialize_element(&self.1)?;
+ /// tup.serialize_element(&self.2)?;
+ /// tup.end()
+ /// }
+ /// }
+ /// ```
+ ///
+ /// ```edition2018
+ /// use serde::ser::{Serialize, SerializeTuple, Serializer};
+ ///
+ /// const VRAM_SIZE: usize = 386;
+ /// struct Vram([u16; VRAM_SIZE]);
+ ///
+ /// impl Serialize for Vram {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// let mut seq = serializer.serialize_tuple(VRAM_SIZE)?;
+ /// for element in &self.0[..] {
+ /// seq.serialize_element(element)?;
+ /// }
+ /// seq.end()
+ /// }
+ /// }
+ /// ```
+ fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error>;
+
+ /// Begin to serialize a tuple struct like `struct Rgb(u8, u8, u8)`. This
+ /// call must be followed by zero or more calls to `serialize_field`, then a
+ /// call to `end`.
+ ///
+ /// The `name` is the name of the tuple struct and the `len` is the number
+ /// of data fields that will be serialized.
+ ///
+ /// ```edition2018
+ /// use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
+ ///
+ /// struct Rgb(u8, u8, u8);
+ ///
+ /// impl Serialize for Rgb {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// let mut ts = serializer.serialize_tuple_struct("Rgb", 3)?;
+ /// ts.serialize_field(&self.0)?;
+ /// ts.serialize_field(&self.1)?;
+ /// ts.serialize_field(&self.2)?;
+ /// ts.end()
+ /// }
+ /// }
+ /// ```
+ fn serialize_tuple_struct(
+ self,
+ name: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeTupleStruct, Self::Error>;
+
+ /// Begin to serialize a tuple variant like `E::T` in `enum E { T(u8, u8)
+ /// }`. This call must be followed by zero or more calls to
+ /// `serialize_field`, then a call to `end`.
+ ///
+ /// The `name` is the name of the enum, the `variant_index` is the index of
+ /// this variant within the enum, the `variant` is the name of the variant,
+ /// and the `len` is the number of data fields that will be serialized.
+ ///
+ /// ```edition2018
+ /// use serde::ser::{Serialize, SerializeTupleVariant, Serializer};
+ ///
+ /// enum E {
+ /// T(u8, u8),
+ /// U(String, u32, u32),
+ /// }
+ ///
+ /// impl Serialize for E {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// match *self {
+ /// E::T(ref a, ref b) => {
+ /// let mut tv = serializer.serialize_tuple_variant("E", 0, "T", 2)?;
+ /// tv.serialize_field(a)?;
+ /// tv.serialize_field(b)?;
+ /// tv.end()
+ /// }
+ /// E::U(ref a, ref b, ref c) => {
+ /// let mut tv = serializer.serialize_tuple_variant("E", 1, "U", 3)?;
+ /// tv.serialize_field(a)?;
+ /// tv.serialize_field(b)?;
+ /// tv.serialize_field(c)?;
+ /// tv.end()
+ /// }
+ /// }
+ /// }
+ /// }
+ /// ```
+ fn serialize_tuple_variant(
+ self,
+ name: &'static str,
+ variant_index: u32,
+ variant: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeTupleVariant, Self::Error>;
+
+ /// Begin to serialize a map. This call must be followed by zero or more
+ /// calls to `serialize_key` and `serialize_value`, then a call to `end`.
+ ///
+ /// The argument is the number of elements in the map, which may or may not
+ /// be computable before the map is iterated. Some serializers only support
+ /// maps whose length is known up front.
+ ///
+ /// ```edition2018
+ /// # use std::marker::PhantomData;
+ /// #
+ /// # struct HashMap<K, V>(PhantomData<K>, PhantomData<V>);
+ /// #
+ /// # impl<K, V> HashMap<K, V> {
+ /// # fn len(&self) -> usize {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// # impl<'a, K, V> IntoIterator for &'a HashMap<K, V> {
+ /// # type Item = (&'a K, &'a V);
+ /// # type IntoIter = Box<Iterator<Item = (&'a K, &'a V)>>;
+ /// #
+ /// # fn into_iter(self) -> Self::IntoIter {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// use serde::ser::{Serialize, Serializer, SerializeMap};
+ ///
+ /// impl<K, V> Serialize for HashMap<K, V>
+ /// where
+ /// K: Serialize,
+ /// V: Serialize,
+ /// {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// let mut map = serializer.serialize_map(Some(self.len()))?;
+ /// for (k, v) in self {
+ /// map.serialize_entry(k, v)?;
+ /// }
+ /// map.end()
+ /// }
+ /// }
+ /// ```
+ fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error>;
+
+ /// Begin to serialize a struct like `struct Rgb { r: u8, g: u8, b: u8 }`.
+ /// This call must be followed by zero or more calls to `serialize_field`,
+ /// then a call to `end`.
+ ///
+ /// The `name` is the name of the struct and the `len` is the number of
+ /// data fields that will be serialized.
+ ///
+ /// ```edition2018
+ /// use serde::ser::{Serialize, SerializeStruct, Serializer};
+ ///
+ /// struct Rgb {
+ /// r: u8,
+ /// g: u8,
+ /// b: u8,
+ /// }
+ ///
+ /// impl Serialize for Rgb {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// let mut rgb = serializer.serialize_struct("Rgb", 3)?;
+ /// rgb.serialize_field("r", &self.r)?;
+ /// rgb.serialize_field("g", &self.g)?;
+ /// rgb.serialize_field("b", &self.b)?;
+ /// rgb.end()
+ /// }
+ /// }
+ /// ```
+ fn serialize_struct(
+ self,
+ name: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeStruct, Self::Error>;
+
+ /// Begin to serialize a struct variant like `E::S` in `enum E { S { r: u8,
+ /// g: u8, b: u8 } }`. This call must be followed by zero or more calls to
+ /// `serialize_field`, then a call to `end`.
+ ///
+ /// The `name` is the name of the enum, the `variant_index` is the index of
+ /// this variant within the enum, the `variant` is the name of the variant,
+ /// and the `len` is the number of data fields that will be serialized.
+ ///
+ /// ```edition2018
+ /// use serde::ser::{Serialize, SerializeStructVariant, Serializer};
+ ///
+ /// enum E {
+ /// S { r: u8, g: u8, b: u8 },
+ /// }
+ ///
+ /// impl Serialize for E {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// match *self {
+ /// E::S {
+ /// ref r,
+ /// ref g,
+ /// ref b,
+ /// } => {
+ /// let mut sv = serializer.serialize_struct_variant("E", 0, "S", 3)?;
+ /// sv.serialize_field("r", r)?;
+ /// sv.serialize_field("g", g)?;
+ /// sv.serialize_field("b", b)?;
+ /// sv.end()
+ /// }
+ /// }
+ /// }
+ /// }
+ /// ```
+ fn serialize_struct_variant(
+ self,
+ name: &'static str,
+ variant_index: u32,
+ variant: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeStructVariant, Self::Error>;
+
+ /// Collect an iterator as a sequence.
+ ///
+ /// The default implementation serializes each item yielded by the iterator
+ /// using [`serialize_seq`]. Implementors should not need to override this
+ /// method.
+ ///
+ /// ```edition2018
+ /// use serde::{Serialize, Serializer};
+ ///
+ /// struct SecretlyOneHigher {
+ /// data: Vec<i32>,
+ /// }
+ ///
+ /// impl Serialize for SecretlyOneHigher {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.collect_seq(self.data.iter().map(|x| x + 1))
+ /// }
+ /// }
+ /// ```
+ ///
+ /// [`serialize_seq`]: #tymethod.serialize_seq
+ fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>
+ where
+ I: IntoIterator,
+ <I as IntoIterator>::Item: Serialize,
+ {
+ let iter = iter.into_iter();
+ let mut serializer = try!(self.serialize_seq(iterator_len_hint(&iter)));
+
+ #[cfg(not(no_iterator_try_fold))]
+ {
+ let mut iter = iter;
+ try!(iter.try_for_each(|item| serializer.serialize_element(&item)));
+ }
+
+ #[cfg(no_iterator_try_fold)]
+ {
+ for item in iter {
+ try!(serializer.serialize_element(&item));
+ }
+ }
+
+ serializer.end()
+ }
+
+ /// Collect an iterator as a map.
+ ///
+ /// The default implementation serializes each pair yielded by the iterator
+ /// using [`serialize_map`]. Implementors should not need to override this
+ /// method.
+ ///
+ /// ```edition2018
+ /// use serde::{Serialize, Serializer};
+ /// use std::collections::BTreeSet;
+ ///
+ /// struct MapToUnit {
+ /// keys: BTreeSet<i32>,
+ /// }
+ ///
+ /// // Serializes as a map in which the values are all unit.
+ /// impl Serialize for MapToUnit {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.collect_map(self.keys.iter().map(|k| (k, ())))
+ /// }
+ /// }
+ /// ```
+ ///
+ /// [`serialize_map`]: #tymethod.serialize_map
+ fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
+ where
+ K: Serialize,
+ V: Serialize,
+ I: IntoIterator<Item = (K, V)>,
+ {
+ let iter = iter.into_iter();
+ let mut serializer = try!(self.serialize_map(iterator_len_hint(&iter)));
+
+ #[cfg(not(no_iterator_try_fold))]
+ {
+ let mut iter = iter;
+ try!(iter.try_for_each(|(key, value)| serializer.serialize_entry(&key, &value)));
+ }
+
+ #[cfg(no_iterator_try_fold)]
+ {
+ for (key, value) in iter {
+ try!(serializer.serialize_entry(&key, &value));
+ }
+ }
+
+ serializer.end()
+ }
+
+ /// Serialize a string produced by an implementation of `Display`.
+ ///
+ /// The default implementation builds a heap-allocated [`String`] and
+ /// delegates to [`serialize_str`]. Serializers are encouraged to provide a
+ /// more efficient implementation if possible.
+ ///
+ /// ```edition2018
+ /// # struct DateTime;
+ /// #
+ /// # impl DateTime {
+ /// # fn naive_local(&self) -> () { () }
+ /// # fn offset(&self) -> () { () }
+ /// # }
+ /// #
+ /// use serde::{Serialize, Serializer};
+ ///
+ /// impl Serialize for DateTime {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.collect_str(&format_args!("{:?}{:?}",
+ /// self.naive_local(),
+ /// self.offset()))
+ /// }
+ /// }
+ /// ```
+ ///
+ /// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
+ /// [`serialize_str`]: #tymethod.serialize_str
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
+ where
+ T: Display,
+ {
+ self.serialize_str(&value.to_string())
+ }
+
+ /// Serialize a string produced by an implementation of `Display`.
+ ///
+ /// Serializers that use `no_std` are required to provide an implementation
+ /// of this method. If no more sensible behavior is possible, the
+ /// implementation is expected to return an error.
+ ///
+ /// ```edition2018
+ /// # struct DateTime;
+ /// #
+ /// # impl DateTime {
+ /// # fn naive_local(&self) -> () { () }
+ /// # fn offset(&self) -> () { () }
+ /// # }
+ /// #
+ /// use serde::{Serialize, Serializer};
+ ///
+ /// impl Serialize for DateTime {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// serializer.collect_str(&format_args!("{:?}{:?}",
+ /// self.naive_local(),
+ /// self.offset()))
+ /// }
+ /// }
+ /// ```
+ #[cfg(not(any(feature = "std", feature = "alloc")))]
+ fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
+ where
+ T: Display;
+
+ /// Determine whether `Serialize` implementations should serialize in
+ /// human-readable form.
+ ///
+ /// Some types have a human-readable form that may be somewhat expensive to
+ /// construct, as well as a binary form that is compact and efficient.
+ /// Generally text-based formats like JSON and YAML will prefer to use the
+ /// human-readable one and binary formats like Postcard will prefer the
+ /// compact one.
+ ///
+ /// ```edition2018
+ /// # use std::fmt::{self, Display};
+ /// #
+ /// # struct Timestamp;
+ /// #
+ /// # impl Timestamp {
+ /// # fn seconds_since_epoch(&self) -> u64 { unimplemented!() }
+ /// # }
+ /// #
+ /// # impl Display for Timestamp {
+ /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// use serde::{Serialize, Serializer};
+ ///
+ /// impl Serialize for Timestamp {
+ /// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ /// where
+ /// S: Serializer,
+ /// {
+ /// if serializer.is_human_readable() {
+ /// // Serialize to a human-readable string "2015-05-15T17:01:00Z".
+ /// self.to_string().serialize(serializer)
+ /// } else {
+ /// // Serialize to a compact binary representation.
+ /// self.seconds_since_epoch().serialize(serializer)
+ /// }
+ /// }
+ /// }
+ /// ```
+ ///
+ /// The default implementation of this method returns `true`. Data formats
+ /// may override this to `false` to request a compact form for types that
+ /// support one. Note that modifying this method to change a format from
+ /// human-readable to compact or vice versa should be regarded as a breaking
+ /// change, as a value serialized in human-readable mode is not required to
+ /// deserialize from the same data in compact mode.
+ #[inline]
+ fn is_human_readable(&self) -> bool {
+ true
+ }
+}
+
+/// Returned from `Serializer::serialize_seq`.
+///
+/// # Example use
+///
+/// ```edition2018
+/// # use std::marker::PhantomData;
+/// #
+/// # struct Vec<T>(PhantomData<T>);
+/// #
+/// # impl<T> Vec<T> {
+/// # fn len(&self) -> usize {
+/// # unimplemented!()
+/// # }
+/// # }
+/// #
+/// # impl<'a, T> IntoIterator for &'a Vec<T> {
+/// # type Item = &'a T;
+/// # type IntoIter = Box<Iterator<Item = &'a T>>;
+/// # fn into_iter(self) -> Self::IntoIter {
+/// # unimplemented!()
+/// # }
+/// # }
+/// #
+/// use serde::ser::{Serialize, Serializer, SerializeSeq};
+///
+/// impl<T> Serialize for Vec<T>
+/// where
+/// T: Serialize,
+/// {
+/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+/// where
+/// S: Serializer,
+/// {
+/// let mut seq = serializer.serialize_seq(Some(self.len()))?;
+/// for element in self {
+/// seq.serialize_element(element)?;
+/// }
+/// seq.end()
+/// }
+/// }
+/// ```
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `SerializeSeq` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait SerializeSeq {
+ /// Must match the `Ok` type of our `Serializer`.
+ type Ok;
+
+ /// Must match the `Error` type of our `Serializer`.
+ type Error: Error;
+
+ /// Serialize a sequence element.
+ fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
+ where
+ T: Serialize;
+
+ /// Finish serializing a sequence.
+ fn end(self) -> Result<Self::Ok, Self::Error>;
+}
+
+/// Returned from `Serializer::serialize_tuple`.
+///
+/// # Example use
+///
+/// ```edition2018
+/// use serde::ser::{Serialize, Serializer, SerializeTuple};
+///
+/// # mod fool {
+/// # trait Serialize {}
+/// impl<A, B, C> Serialize for (A, B, C)
+/// # {}
+/// # }
+/// #
+/// # struct Tuple3<A, B, C>(A, B, C);
+/// #
+/// # impl<A, B, C> Serialize for Tuple3<A, B, C>
+/// where
+/// A: Serialize,
+/// B: Serialize,
+/// C: Serialize,
+/// {
+/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+/// where
+/// S: Serializer,
+/// {
+/// let mut tup = serializer.serialize_tuple(3)?;
+/// tup.serialize_element(&self.0)?;
+/// tup.serialize_element(&self.1)?;
+/// tup.serialize_element(&self.2)?;
+/// tup.end()
+/// }
+/// }
+/// ```
+///
+/// ```edition2018
+/// # use std::marker::PhantomData;
+/// #
+/// # struct Array<T>(PhantomData<T>);
+/// #
+/// # impl<T> Array<T> {
+/// # fn len(&self) -> usize {
+/// # unimplemented!()
+/// # }
+/// # }
+/// #
+/// # impl<'a, T> IntoIterator for &'a Array<T> {
+/// # type Item = &'a T;
+/// # type IntoIter = Box<Iterator<Item = &'a T>>;
+/// # fn into_iter(self) -> Self::IntoIter {
+/// # unimplemented!()
+/// # }
+/// # }
+/// #
+/// use serde::ser::{Serialize, Serializer, SerializeTuple};
+///
+/// # mod fool {
+/// # trait Serialize {}
+/// impl<T> Serialize for [T; 16]
+/// # {}
+/// # }
+/// #
+/// # impl<T> Serialize for Array<T>
+/// where
+/// T: Serialize,
+/// {
+/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+/// where
+/// S: Serializer,
+/// {
+/// let mut seq = serializer.serialize_tuple(16)?;
+/// for element in self {
+/// seq.serialize_element(element)?;
+/// }
+/// seq.end()
+/// }
+/// }
+/// ```
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `SerializeTuple` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait SerializeTuple {
+ /// Must match the `Ok` type of our `Serializer`.
+ type Ok;
+
+ /// Must match the `Error` type of our `Serializer`.
+ type Error: Error;
+
+ /// Serialize a tuple element.
+ fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
+ where
+ T: Serialize;
+
+ /// Finish serializing a tuple.
+ fn end(self) -> Result<Self::Ok, Self::Error>;
+}
+
+/// Returned from `Serializer::serialize_tuple_struct`.
+///
+/// # Example use
+///
+/// ```edition2018
+/// use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
+///
+/// struct Rgb(u8, u8, u8);
+///
+/// impl Serialize for Rgb {
+/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+/// where
+/// S: Serializer,
+/// {
+/// let mut ts = serializer.serialize_tuple_struct("Rgb", 3)?;
+/// ts.serialize_field(&self.0)?;
+/// ts.serialize_field(&self.1)?;
+/// ts.serialize_field(&self.2)?;
+/// ts.end()
+/// }
+/// }
+/// ```
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `SerializeTupleStruct` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait SerializeTupleStruct {
+ /// Must match the `Ok` type of our `Serializer`.
+ type Ok;
+
+ /// Must match the `Error` type of our `Serializer`.
+ type Error: Error;
+
+ /// Serialize a tuple struct field.
+ fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
+ where
+ T: Serialize;
+
+ /// Finish serializing a tuple struct.
+ fn end(self) -> Result<Self::Ok, Self::Error>;
+}
+
+/// Returned from `Serializer::serialize_tuple_variant`.
+///
+/// # Example use
+///
+/// ```edition2018
+/// use serde::ser::{Serialize, SerializeTupleVariant, Serializer};
+///
+/// enum E {
+/// T(u8, u8),
+/// U(String, u32, u32),
+/// }
+///
+/// impl Serialize for E {
+/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+/// where
+/// S: Serializer,
+/// {
+/// match *self {
+/// E::T(ref a, ref b) => {
+/// let mut tv = serializer.serialize_tuple_variant("E", 0, "T", 2)?;
+/// tv.serialize_field(a)?;
+/// tv.serialize_field(b)?;
+/// tv.end()
+/// }
+/// E::U(ref a, ref b, ref c) => {
+/// let mut tv = serializer.serialize_tuple_variant("E", 1, "U", 3)?;
+/// tv.serialize_field(a)?;
+/// tv.serialize_field(b)?;
+/// tv.serialize_field(c)?;
+/// tv.end()
+/// }
+/// }
+/// }
+/// }
+/// ```
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `SerializeTupleVariant` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait SerializeTupleVariant {
+ /// Must match the `Ok` type of our `Serializer`.
+ type Ok;
+
+ /// Must match the `Error` type of our `Serializer`.
+ type Error: Error;
+
+ /// Serialize a tuple variant field.
+ fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
+ where
+ T: Serialize;
+
+ /// Finish serializing a tuple variant.
+ fn end(self) -> Result<Self::Ok, Self::Error>;
+}
+
+/// Returned from `Serializer::serialize_map`.
+///
+/// # Example use
+///
+/// ```edition2018
+/// # use std::marker::PhantomData;
+/// #
+/// # struct HashMap<K, V>(PhantomData<K>, PhantomData<V>);
+/// #
+/// # impl<K, V> HashMap<K, V> {
+/// # fn len(&self) -> usize {
+/// # unimplemented!()
+/// # }
+/// # }
+/// #
+/// # impl<'a, K, V> IntoIterator for &'a HashMap<K, V> {
+/// # type Item = (&'a K, &'a V);
+/// # type IntoIter = Box<Iterator<Item = (&'a K, &'a V)>>;
+/// #
+/// # fn into_iter(self) -> Self::IntoIter {
+/// # unimplemented!()
+/// # }
+/// # }
+/// #
+/// use serde::ser::{Serialize, Serializer, SerializeMap};
+///
+/// impl<K, V> Serialize for HashMap<K, V>
+/// where
+/// K: Serialize,
+/// V: Serialize,
+/// {
+/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+/// where
+/// S: Serializer,
+/// {
+/// let mut map = serializer.serialize_map(Some(self.len()))?;
+/// for (k, v) in self {
+/// map.serialize_entry(k, v)?;
+/// }
+/// map.end()
+/// }
+/// }
+/// ```
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `SerializeMap` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait SerializeMap {
+ /// Must match the `Ok` type of our `Serializer`.
+ type Ok;
+
+ /// Must match the `Error` type of our `Serializer`.
+ type Error: Error;
+
+ /// Serialize a map key.
+ ///
+ /// If possible, `Serialize` implementations are encouraged to use
+ /// `serialize_entry` instead as it may be implemented more efficiently in
+ /// some formats compared to a pair of calls to `serialize_key` and
+ /// `serialize_value`.
+ fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
+ where
+ T: Serialize;
+
+ /// Serialize a map value.
+ ///
+ /// # Panics
+ ///
+ /// Calling `serialize_value` before `serialize_key` is incorrect and is
+ /// allowed to panic or produce bogus results.
+ fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
+ where
+ T: Serialize;
+
+ /// Serialize a map entry consisting of a key and a value.
+ ///
+ /// Some [`Serialize`] types are not able to hold a key and value in memory
+ /// at the same time so `SerializeMap` implementations are required to
+ /// support [`serialize_key`] and [`serialize_value`] individually. The
+ /// `serialize_entry` method allows serializers to optimize for the case
+ /// where key and value are both available. [`Serialize`] implementations
+ /// are encouraged to use `serialize_entry` if possible.
+ ///
+ /// The default implementation delegates to [`serialize_key`] and
+ /// [`serialize_value`]. This is appropriate for serializers that do not
+ /// care about performance or are not able to optimize `serialize_entry` any
+ /// better than this.
+ ///
+ /// [`Serialize`]: ../trait.Serialize.html
+ /// [`serialize_key`]: #tymethod.serialize_key
+ /// [`serialize_value`]: #tymethod.serialize_value
+ fn serialize_entry<K: ?Sized, V: ?Sized>(
+ &mut self,
+ key: &K,
+ value: &V,
+ ) -> Result<(), Self::Error>
+ where
+ K: Serialize,
+ V: Serialize,
+ {
+ try!(self.serialize_key(key));
+ self.serialize_value(value)
+ }
+
+ /// Finish serializing a map.
+ fn end(self) -> Result<Self::Ok, Self::Error>;
+}
+
+/// Returned from `Serializer::serialize_struct`.
+///
+/// # Example use
+///
+/// ```edition2018
+/// use serde::ser::{Serialize, SerializeStruct, Serializer};
+///
+/// struct Rgb {
+/// r: u8,
+/// g: u8,
+/// b: u8,
+/// }
+///
+/// impl Serialize for Rgb {
+/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+/// where
+/// S: Serializer,
+/// {
+/// let mut rgb = serializer.serialize_struct("Rgb", 3)?;
+/// rgb.serialize_field("r", &self.r)?;
+/// rgb.serialize_field("g", &self.g)?;
+/// rgb.serialize_field("b", &self.b)?;
+/// rgb.end()
+/// }
+/// }
+/// ```
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `SerializeStruct` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait SerializeStruct {
+ /// Must match the `Ok` type of our `Serializer`.
+ type Ok;
+
+ /// Must match the `Error` type of our `Serializer`.
+ type Error: Error;
+
+ /// Serialize a struct field.
+ fn serialize_field<T: ?Sized>(
+ &mut self,
+ key: &'static str,
+ value: &T,
+ ) -> Result<(), Self::Error>
+ where
+ T: Serialize;
+
+ /// Indicate that a struct field has been skipped.
+ #[inline]
+ fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
+ let _ = key;
+ Ok(())
+ }
+
+ /// Finish serializing a struct.
+ fn end(self) -> Result<Self::Ok, Self::Error>;
+}
+
+/// Returned from `Serializer::serialize_struct_variant`.
+///
+/// # Example use
+///
+/// ```edition2018
+/// use serde::ser::{Serialize, SerializeStructVariant, Serializer};
+///
+/// enum E {
+/// S { r: u8, g: u8, b: u8 },
+/// }
+///
+/// impl Serialize for E {
+/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+/// where
+/// S: Serializer,
+/// {
+/// match *self {
+/// E::S {
+/// ref r,
+/// ref g,
+/// ref b,
+/// } => {
+/// let mut sv = serializer.serialize_struct_variant("E", 0, "S", 3)?;
+/// sv.serialize_field("r", r)?;
+/// sv.serialize_field("g", g)?;
+/// sv.serialize_field("b", b)?;
+/// sv.end()
+/// }
+/// }
+/// }
+/// }
+/// ```
+///
+/// # Example implementation
+///
+/// The [example data format] presented on the website demonstrates an
+/// implementation of `SerializeStructVariant` for a basic JSON data format.
+///
+/// [example data format]: https://serde.rs/data-format.html
+pub trait SerializeStructVariant {
+ /// Must match the `Ok` type of our `Serializer`.
+ type Ok;
+
+ /// Must match the `Error` type of our `Serializer`.
+ type Error: Error;
+
+ /// Serialize a struct variant field.
+ fn serialize_field<T: ?Sized>(
+ &mut self,
+ key: &'static str,
+ value: &T,
+ ) -> Result<(), Self::Error>
+ where
+ T: Serialize;
+
+ /// Indicate that a struct variant field has been skipped.
+ #[inline]
+ fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
+ let _ = key;
+ Ok(())
+ }
+
+ /// Finish serializing a struct variant.
+ fn end(self) -> Result<Self::Ok, Self::Error>;
+}
+
+fn iterator_len_hint<I>(iter: &I) -> Option<usize>
+where
+ I: Iterator,
+{
+ match iter.size_hint() {
+ (lo, Some(hi)) if lo == hi => Some(lo),
+ _ => None,
+ }
+}
diff --git a/third_party/rust/serde/src/std_error.rs b/third_party/rust/serde/src/std_error.rs
new file mode 100644
index 0000000000..1055e0ffbc
--- /dev/null
+++ b/third_party/rust/serde/src/std_error.rs
@@ -0,0 +1,48 @@
+use lib::{Debug, Display};
+
+/// Either a re-export of std::error::Error or a new identical trait, depending
+/// on whether Serde's "std" feature is enabled.
+///
+/// Serde's error traits [`serde::ser::Error`] and [`serde::de::Error`] require
+/// [`std::error::Error`] as a supertrait, but only when Serde is built with
+/// "std" enabled. Data formats that don't care about no\_std support should
+/// generally provide their error types with a `std::error::Error` impl
+/// directly:
+///
+/// ```edition2018
+/// #[derive(Debug)]
+/// struct MySerError {...}
+///
+/// impl serde::ser::Error for MySerError {...}
+///
+/// impl std::fmt::Display for MySerError {...}
+///
+/// // We don't support no_std!
+/// impl std::error::Error for MySerError {}
+/// ```
+///
+/// Data formats that *do* support no\_std may either have a "std" feature of
+/// their own:
+///
+/// ```toml
+/// [features]
+/// std = ["serde/std"]
+/// ```
+///
+/// ```edition2018
+/// #[cfg(feature = "std")]
+/// impl std::error::Error for MySerError {}
+/// ```
+///
+/// ... or else provide the std Error impl unconditionally via Serde's
+/// re-export:
+///
+/// ```edition2018
+/// impl serde::ser::StdError for MySerError {}
+/// ```
+pub trait Error: Debug + Display {
+ /// The underlying cause of this error, if any.
+ fn source(&self) -> Option<&(Error + 'static)> {
+ None
+ }
+}