summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/serde/src
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/serde/src')
-rw-r--r--third_party/rust/serde/src/de/format.rs30
-rw-r--r--third_party/rust/serde/src/de/ignored_any.rs238
-rw-r--r--third_party/rust/serde/src/de/impls.rs3071
-rw-r--r--third_party/rust/serde/src/de/mod.rs2292
-rw-r--r--third_party/rust/serde/src/de/seed.rs19
-rw-r--r--third_party/rust/serde/src/de/size_hint.rs29
-rw-r--r--third_party/rust/serde/src/de/value.rs1720
-rw-r--r--third_party/rust/serde/src/integer128.rs9
-rw-r--r--third_party/rust/serde/src/lib.rs334
-rw-r--r--third_party/rust/serde/src/macros.rs231
-rw-r--r--third_party/rust/serde/src/private/de.rs2892
-rw-r--r--third_party/rust/serde/src/private/doc.rs159
-rw-r--r--third_party/rust/serde/src/private/mod.rs48
-rw-r--r--third_party/rust/serde/src/private/ser.rs1385
-rw-r--r--third_party/rust/serde/src/ser/fmt.rs170
-rw-r--r--third_party/rust/serde/src/ser/impls.rs1077
-rw-r--r--third_party/rust/serde/src/ser/impossible.rs216
-rw-r--r--third_party/rust/serde/src/ser/mod.rs1952
-rw-r--r--third_party/rust/serde/src/std_error.rs48
19 files changed, 15920 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..9053cc0704
--- /dev/null
+++ b/third_party/rust/serde/src/de/format.rs
@@ -0,0 +1,30 @@
+use crate::lib::fmt::{self, Write};
+use crate::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..2360a17423
--- /dev/null
+++ b/third_party/rust/serde/src/de/ignored_any.rs
@@ -0,0 +1,238 @@
+use crate::lib::*;
+
+use crate::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.
+///
+/// ```edition2021
+/// use serde::de::{
+/// self, Deserialize, DeserializeSeed, Deserializer, IgnoredAny, SeqAccess, Visitor,
+/// };
+/// use std::fmt;
+/// use std::marker::PhantomData;
+///
+/// /// 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, PartialEq)]
+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)
+ }
+
+ #[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)
+ }
+
+ #[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) = tri!(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)) = tri!(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>,
+ {
+ tri!(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..413c997afd
--- /dev/null
+++ b/third_party/rust/serde/src/de/impls.rs
@@ -0,0 +1,3071 @@
+use crate::lib::*;
+
+use crate::de::{
+ Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, Unexpected, VariantAccess,
+ Visitor,
+};
+
+use crate::seed::InPlaceSeed;
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+use crate::de::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")]
+#[cfg_attr(doc_cfg, doc(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($($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! num_as_copysign_self {
+ ($ty:ident : $visit:ident) => {
+ #[inline]
+ fn $visit<E>(self, v: $ty) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ #[cfg(any(no_float_copysign, not(feature = "std")))]
+ {
+ Ok(v as Self::Value)
+ }
+
+ #[cfg(all(not(no_float_copysign), feature = "std"))]
+ {
+ // Preserve sign of NaN. The `as` produces a nondeterministic sign.
+ let sign = if v.is_sign_positive() { 1.0 } else { -1.0 };
+ Ok((v as Self::Value).copysign(sign))
+ }
+ }
+ };
+}
+
+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_copysign_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_copysign_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);
+}
+
+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"))]
+#[cfg_attr(doc_cfg, doc(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(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
+struct CStringVisitor;
+
+#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
+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 capacity = size_hint::cautious::<u8>(seq.size_hint());
+ let mut values = Vec::<u8>::with_capacity(capacity);
+
+ while let Some(value) = tri!(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(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
+#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+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 {
+ (
+ $(#[$attr:meta])*
+ ($($id:ident),*), $ty:ty, $func:expr
+ ) => {
+ $(#[$attr])*
+ 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)
+ }
+ }
+ }
+}
+
+forwarded_impl! {
+ #[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ (), Box<CStr>, CString::into_boxed_c_str
+}
+
+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)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! seq_impl {
+ (
+ $(#[$attr:meta])*
+ $ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
+ $access:ident,
+ $clear:expr,
+ $with_capacity:expr,
+ $reserve:expr,
+ $insert:expr
+ ) => {
+ $(#[$attr])*
+ 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) = tri!($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::<T>($access.size_hint()));
+
+ // FIXME: try to overwrite old values here? (Vec, VecDeque, LinkedList)
+ while let Some(value) = tri!($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) {}
+
+seq_impl!(
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ BinaryHeap<T: Ord>,
+ seq,
+ BinaryHeap::clear,
+ BinaryHeap::with_capacity(size_hint::cautious::<T>(seq.size_hint())),
+ BinaryHeap::reserve,
+ BinaryHeap::push
+);
+
+seq_impl!(
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ BTreeSet<T: Eq + Ord>,
+ seq,
+ BTreeSet::clear,
+ BTreeSet::new(),
+ nop_reserve,
+ BTreeSet::insert
+);
+
+seq_impl!(
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ LinkedList<T>,
+ seq,
+ LinkedList::clear,
+ LinkedList::new(),
+ nop_reserve,
+ LinkedList::push_back
+);
+
+seq_impl!(
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ HashSet<T: Eq + Hash, S: BuildHasher + Default>,
+ seq,
+ HashSet::clear,
+ HashSet::with_capacity_and_hasher(size_hint::cautious::<T>(seq.size_hint()), S::default()),
+ HashSet::reserve,
+ HashSet::insert
+);
+
+seq_impl!(
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ VecDeque<T>,
+ seq,
+ VecDeque::clear,
+ VecDeque::with_capacity(size_hint::cautious::<T>(seq.size_hint())),
+ VecDeque::reserve,
+ VecDeque::push_back
+);
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+#[cfg_attr(doc_cfg, doc(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 capacity = size_hint::cautious::<T>(seq.size_hint());
+ let mut values = Vec::<T>::with_capacity(capacity);
+
+ while let Some(value) = tri!(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::<T>(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]);
+ tri!(seq.next_element_seed(next_place))
+ };
+ if next.is_none() {
+ self.0.truncate(i);
+ return Ok(());
+ }
+ }
+
+ while let Some(value) = tri!(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 tri!(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 tri!(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 tri!(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 tri!(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)
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! map_impl {
+ (
+ $(#[$attr:meta])*
+ $ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
+ $access:ident,
+ $with_capacity:expr,
+ ) => {
+ $(#[$attr])*
+ 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)) = tri!($access.next_entry()) {
+ values.insert(key, value);
+ }
+
+ Ok(values)
+ }
+ }
+
+ let visitor = MapVisitor { marker: PhantomData };
+ deserializer.deserialize_map(visitor)
+ }
+ }
+ }
+}
+
+map_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ BTreeMap<K: Ord, V>,
+ map,
+ BTreeMap::new(),
+}
+
+map_impl! {
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
+ map,
+ HashMap::with_capacity_and_hasher(size_hint::cautious::<(K, V)>(map.size_hint()), S::default()),
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! parse_ip_impl {
+ (
+ $(#[$attr:meta])*
+ $ty:ty, $expecting:expr, $size:tt
+ ) => {
+ $(#[$attr])*
+ 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: &[&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 tri!(data.variant()) {
+ $(
+ ($name_kind :: $variant, v) => v.newtype_variant().map($name :: $variant),
+ )*
+ }
+ }
+ }
+ $deserializer.deserialize_enum(stringify!($name), VARIANTS, EnumVisitor)
+ }
+}
+
+#[cfg(feature = "std")]
+#[cfg_attr(doc_cfg, doc(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 crate::lib::net::IpAddr;
+ deserialize_enum! {
+ IpAddr IpAddrKind (V4; b"V4"; 0, V6; b"V6"; 1)
+ "`V4` or `V6`",
+ deserializer
+ }
+ }
+ }
+}
+
+parse_ip_impl! {
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ net::Ipv4Addr, "IPv4 address", 4
+}
+
+parse_ip_impl! {
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ net::Ipv6Addr, "IPv6 address", 16
+}
+
+macro_rules! parse_socket_impl {
+ (
+ $(#[$attr:meta])*
+ $ty:ty, $expecting:tt,
+ $new:expr,
+ ) => {
+ $(#[$attr])*
+ 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($new)
+ }
+ }
+ }
+ };
+}
+
+#[cfg(feature = "std")]
+#[cfg_attr(doc_cfg, doc(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 crate::lib::net::SocketAddr;
+ deserialize_enum! {
+ SocketAddr SocketAddrKind (V4; b"V4"; 0, V6; b"V6"; 1)
+ "`V4` or `V6`",
+ deserializer
+ }
+ }
+ }
+}
+
+parse_socket_impl! {
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ net::SocketAddrV4, "IPv4 socket address",
+ |(ip, port)| net::SocketAddrV4::new(ip, port),
+}
+
+parse_socket_impl! {
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ net::SocketAddrV6, "IPv6 socket address",
+ |(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")]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(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)
+ }
+}
+
+forwarded_impl! {
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ (), 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 tri!(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 tri!(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)))]
+#[cfg_attr(doc_cfg, doc(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)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+forwarded_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ (T), Box<T>, Box::new
+}
+
+forwarded_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ (T), Box<[T]>, Vec::into_boxed_slice
+}
+
+forwarded_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ (), Box<str>, String::into_boxed_str
+}
+
+forwarded_impl! {
+ #[cfg(all(feature = "std", any(unix, windows)))]
+ #[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", any(unix, windows)))))]
+ (), Box<OsStr>, OsString::into_boxed_os_str
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+#[cfg_attr(doc_cfg, doc(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")))]
+#[cfg_attr(
+ doc_cfg,
+ doc(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>,
+ {
+ tri!(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")))]
+#[cfg_attr(
+ doc_cfg,
+ doc(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>,
+ {
+ tri!(Option::<T>::deserialize(deserializer));
+ Ok(ArcWeak::new())
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! box_forwarded_impl {
+ (
+ $(#[$attr:meta])*
+ $t:ident
+ ) => {
+ $(#[$attr])*
+ 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)
+ }
+ }
+ };
+}
+
+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
+ #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
+ #[cfg_attr(doc_cfg, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))]
+ Rc
+}
+
+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
+ #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
+ #[cfg_attr(doc_cfg, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))]
+ 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
+}
+
+forwarded_impl! {
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ (T), Mutex<T>, Mutex::new
+}
+
+forwarded_impl! {
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ (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,
+// }
+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 = crate::__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 tri!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(0, &self));
+ }
+ };
+ let nanos: u32 = match tri!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(1, &self));
+ }
+ };
+ tri!(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) = tri!(map.next_key()) {
+ match key {
+ Field::Secs => {
+ if secs.is_some() {
+ return Err(<A::Error as Error>::duplicate_field("secs"));
+ }
+ secs = Some(tri!(map.next_value()));
+ }
+ Field::Nanos => {
+ if nanos.is_some() {
+ return Err(<A::Error as Error>::duplicate_field("nanos"));
+ }
+ nanos = Some(tri!(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")),
+ };
+ tri!(check_overflow(secs, nanos));
+ Ok(Duration::new(secs, nanos))
+ }
+ }
+
+ const FIELDS: &[&str] = &["secs", "nanos"];
+ deserializer.deserialize_struct("Duration", FIELDS, DurationVisitor)
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "std")]
+#[cfg_attr(doc_cfg, doc(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 tri!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(0, &self));
+ }
+ };
+ let nanos: u32 = match tri!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(1, &self));
+ }
+ };
+ tri!(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) = tri!(map.next_key()) {
+ match key {
+ Field::Secs => {
+ if secs.is_some() {
+ return Err(<A::Error as Error>::duplicate_field(
+ "secs_since_epoch",
+ ));
+ }
+ secs = Some(tri!(map.next_value()));
+ }
+ Field::Nanos => {
+ if nanos.is_some() {
+ return Err(<A::Error as Error>::duplicate_field(
+ "nanos_since_epoch",
+ ));
+ }
+ nanos = Some(tri!(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")),
+ };
+ tri!(check_overflow(secs, nanos));
+ Ok(Duration::new(secs, nanos))
+ }
+ }
+
+ const FIELDS: &[&str] = &["secs_since_epoch", "nanos_since_epoch"];
+ let duration = tri!(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<Idx> {
+// start: Idx,
+// end: Idx,
+// }
+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) = tri!(deserializer.deserialize_struct(
+ "Range",
+ range::FIELDS,
+ range::RangeVisitor {
+ expecting: "struct Range",
+ phantom: PhantomData,
+ },
+ ));
+ Ok(start..end)
+ }
+}
+
+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) = tri!(deserializer.deserialize_struct(
+ "RangeInclusive",
+ range::FIELDS,
+ range::RangeVisitor {
+ expecting: "struct RangeInclusive",
+ phantom: PhantomData,
+ },
+ ));
+ Ok(RangeInclusive::new(start, end))
+ }
+}
+
+mod range {
+ use crate::lib::*;
+
+ use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
+
+ pub const FIELDS: &[&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 = crate::__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 tri!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(0, &self));
+ }
+ };
+ let end: Idx = match tri!(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) = tri!(map.next_key()) {
+ match key {
+ Field::Start => {
+ if start.is_some() {
+ return Err(<A::Error as Error>::duplicate_field("start"));
+ }
+ start = Some(tri!(map.next_value()));
+ }
+ Field::End => {
+ if end.is_some() {
+ return Err(<A::Error as Error>::duplicate_field("end"));
+ }
+ end = Some(tri!(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))
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Similar to:
+//
+// #[derive(Deserialize)]
+// #[serde(deny_unknown_fields)]
+// struct RangeFrom<Idx> {
+// start: Idx,
+// }
+impl<'de, Idx> Deserialize<'de> for RangeFrom<Idx>
+where
+ Idx: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let start = tri!(deserializer.deserialize_struct(
+ "RangeFrom",
+ range_from::FIELDS,
+ range_from::RangeFromVisitor {
+ expecting: "struct RangeFrom",
+ phantom: PhantomData,
+ },
+ ));
+ Ok(start..)
+ }
+}
+
+mod range_from {
+ use crate::lib::*;
+
+ use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
+
+ pub const FIELDS: &[&str] = &["start"];
+
+ // If this were outside of the serde crate, it would just use:
+ //
+ // #[derive(Deserialize)]
+ // #[serde(field_identifier, rename_all = "lowercase")]
+ enum Field {
+ Start,
+ }
+
+ 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`")
+ }
+
+ fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ "start" => Ok(Field::Start),
+ _ => 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),
+ _ => {
+ let value = crate::__private::from_utf8_lossy(value);
+ Err(Error::unknown_field(&*value, FIELDS))
+ }
+ }
+ }
+ }
+
+ deserializer.deserialize_identifier(FieldVisitor)
+ }
+ }
+
+ pub struct RangeFromVisitor<Idx> {
+ pub expecting: &'static str,
+ pub phantom: PhantomData<Idx>,
+ }
+
+ impl<'de, Idx> Visitor<'de> for RangeFromVisitor<Idx>
+ where
+ Idx: Deserialize<'de>,
+ {
+ type Value = 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 tri!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(0, &self));
+ }
+ };
+ Ok(start)
+ }
+
+ fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
+ where
+ A: MapAccess<'de>,
+ {
+ let mut start: Option<Idx> = None;
+ while let Some(key) = tri!(map.next_key()) {
+ match key {
+ Field::Start => {
+ if start.is_some() {
+ return Err(<A::Error as Error>::duplicate_field("start"));
+ }
+ start = Some(tri!(map.next_value()));
+ }
+ }
+ }
+ let start = match start {
+ Some(start) => start,
+ None => return Err(<A::Error as Error>::missing_field("start")),
+ };
+ Ok(start)
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Similar to:
+//
+// #[derive(Deserialize)]
+// #[serde(deny_unknown_fields)]
+// struct RangeTo<Idx> {
+// end: Idx,
+// }
+impl<'de, Idx> Deserialize<'de> for RangeTo<Idx>
+where
+ Idx: Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let end = tri!(deserializer.deserialize_struct(
+ "RangeTo",
+ range_to::FIELDS,
+ range_to::RangeToVisitor {
+ expecting: "struct RangeTo",
+ phantom: PhantomData,
+ },
+ ));
+ Ok(..end)
+ }
+}
+
+mod range_to {
+ use crate::lib::*;
+
+ use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
+
+ pub const FIELDS: &[&str] = &["end"];
+
+ // If this were outside of the serde crate, it would just use:
+ //
+ // #[derive(Deserialize)]
+ // #[serde(field_identifier, rename_all = "lowercase")]
+ enum Field {
+ 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("`end`")
+ }
+
+ fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ match value {
+ "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"end" => Ok(Field::End),
+ _ => {
+ let value = crate::__private::from_utf8_lossy(value);
+ Err(Error::unknown_field(&*value, FIELDS))
+ }
+ }
+ }
+ }
+
+ deserializer.deserialize_identifier(FieldVisitor)
+ }
+ }
+
+ pub struct RangeToVisitor<Idx> {
+ pub expecting: &'static str,
+ pub phantom: PhantomData<Idx>,
+ }
+
+ impl<'de, Idx> Visitor<'de> for RangeToVisitor<Idx>
+ where
+ Idx: Deserialize<'de>,
+ {
+ type Value = 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 end: Idx = match tri!(seq.next_element()) {
+ Some(value) => value,
+ None => {
+ return Err(Error::invalid_length(0, &self));
+ }
+ };
+ Ok(end)
+ }
+
+ fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
+ where
+ A: MapAccess<'de>,
+ {
+ let mut end: Option<Idx> = None;
+ while let Some(key) = tri!(map.next_key()) {
+ match key {
+ Field::End => {
+ if end.is_some() {
+ return Err(<A::Error as Error>::duplicate_field("end"));
+ }
+ end = Some(tri!(map.next_value()));
+ }
+ }
+ }
+ let end = match end {
+ Some(end) => end,
+ None => return Err(<A::Error as Error>::missing_field("end")),
+ };
+ Ok(end)
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+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 tri!(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: &[&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 tri!(data.variant()) {
+ (Field::Ok, v) => v.newtype_variant().map(Ok),
+ (Field::Err, v) => v.newtype_variant().map(Err),
+ }
+ }
+ }
+
+ const VARIANTS: &[&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))]
+ #[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", 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,
+ 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..c9919d92b8
--- /dev/null
+++ b/third_party/rust/serde/src/de/mod.rs
@@ -0,0 +1,2292 @@
+//! 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 crate::lib::*;
+
+////////////////////////////////////////////////////////////////////////////////
+
+pub mod value;
+
+mod format;
+mod ignored_any;
+mod impls;
+pub(crate) mod size_hint;
+
+pub use self::ignored_any::IgnoredAny;
+
+#[cfg(not(any(feature = "std", feature = "unstable")))]
+#[doc(no_inline)]
+pub use crate::std_error::Error as StdError;
+#[cfg(all(feature = "unstable", not(feature = "std")))]
+#[doc(no_inline)]
+pub use core::error::Error as StdError;
+#[cfg(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.
+ ///
+ /// ```edition2021
+ /// # 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.
+///
+/// ```edition2021
+/// # 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.
+///
+/// ```edition2021
+/// # use serde::de::{self, Unexpected, Visitor};
+/// # use std::fmt;
+/// #
+/// # 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.
+///
+/// ```edition2021
+/// # 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 = tri!(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.
+///
+/// ```edition2021
+/// # 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:
+///
+/// ```edition2021
+/// # 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:
+///
+/// ```edition2021
+/// # 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.
+///
+/// ```edition2021
+/// use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
+/// use std::fmt;
+/// use std::marker::PhantomData;
+///
+/// // 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>;
+
+ /// Hint that the `Deserialize` type is expecting an `i128` value.
+ ///
+ /// 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>;
+
+ /// Hint that the `Deserialize` type is expecting an `u128` value.
+ ///
+ /// 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.
+ ///
+ /// ```edition2021
+ /// # 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,
+ _: crate::actually_private::T,
+ visitor: V,
+ ) -> Result<crate::__private::de::Content<'de>, Self::Error>
+ where
+ V: Visitor<'de, Value = crate::__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
+///
+/// ```edition2021
+/// # use serde::de::{self, Unexpected, Visitor};
+/// # use std::fmt;
+/// #
+/// /// 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.
+ ///
+ /// ```edition2021
+ /// # 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))
+ }
+
+ /// The input contains a `i128`.
+ ///
+ /// 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))
+ }
+
+ /// The input contains a `u128`.
+ ///
+ /// 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(v.encode_utf8(&mut [0u8; 4]))
+ }
+
+ /// 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"))]
+ #[cfg_attr(doc_cfg, doc(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,
+ {
+ 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"))]
+ #[cfg_attr(doc_cfg, doc(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 tri!(self.next_key_seed(kseed)) {
+ Some(key) => {
+ let value = tri!(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:
+ ///
+ /// ```edition2021
+ /// # 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:
+ ///
+ /// ```edition2021
+ /// # 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:
+ ///
+ /// ```edition2021
+ /// # 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:
+ ///
+ /// ```edition2021
+ /// # 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
+///
+/// ```edition2021
+/// use serde::de::{value, Deserialize, IntoDeserializer};
+/// use serde_derive::Deserialize;
+/// use std::str::FromStr;
+///
+/// #[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]),
+ _ => {
+ tri!(write!(formatter, "one of "));
+ for (i, alt) in self.names.iter().enumerate() {
+ if i > 0 {
+ tri!(write!(formatter, ", "));
+ }
+ tri!(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..52fb89d845
--- /dev/null
+++ b/third_party/rust/serde/src/de/seed.rs
@@ -0,0 +1,19 @@
+use crate::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/size_hint.rs b/third_party/rust/serde/src/de/size_hint.rs
new file mode 100644
index 0000000000..4a4fe25dc5
--- /dev/null
+++ b/third_party/rust/serde/src/de/size_hint.rs
@@ -0,0 +1,29 @@
+use crate::lib::*;
+
+pub fn from_bounds<I>(iter: &I) -> Option<usize>
+where
+ I: Iterator,
+{
+ helper(iter.size_hint())
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+pub fn cautious<Element>(hint: Option<usize>) -> usize {
+ const MAX_PREALLOC_BYTES: usize = 1024 * 1024;
+
+ if mem::size_of::<Element>() == 0 {
+ 0
+ } else {
+ cmp::min(
+ hint.unwrap_or(0),
+ MAX_PREALLOC_BYTES / mem::size_of::<Element>(),
+ )
+ }
+}
+
+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/de/value.rs b/third_party/rust/serde/src/de/value.rs
new file mode 100644
index 0000000000..b229ebab77
--- /dev/null
+++ b/third_party/rust/serde/src/de/value.rs
@@ -0,0 +1,1720 @@
+//! Building blocks for deserializing basic values using the `IntoDeserializer`
+//! trait.
+//!
+//! ```edition2021
+//! use serde::de::{value, Deserialize, IntoDeserializer};
+//! use serde_derive::Deserialize;
+//! use std::str::FromStr;
+//!
+//! #[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 crate::lib::*;
+
+use self::private::{First, Second};
+use crate::de::{self, size_hint, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor};
+use crate::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")]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(cfg(feature = "unstable")))]
+pub struct NeverDeserializer<E> {
+ never: !,
+ marker: PhantomData<E>,
+}
+
+#[cfg(feature = "unstable")]
+#[cfg_attr(doc_cfg, doc(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,
+ 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!(i128, "an `i128`.", I128Deserializer, visit_i128);
+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!(u128, "a `u128`.", U128Deserializer, visit_u128);
+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);
+
+/// 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,
+ 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,
+ 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,
+ 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"))]
+#[cfg_attr(doc_cfg, doc(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"))]
+#[cfg_attr(doc_cfg, doc(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,
+ 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"))]
+#[cfg_attr(doc_cfg, doc(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"))]
+#[cfg_attr(doc_cfg, doc(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,
+ 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,
+ 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,
+ 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 = tri!(visitor.visit_seq(&mut self));
+ tri!(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"))]
+#[cfg_attr(doc_cfg, doc(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"))]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(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 }
+ }
+}
+
+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 = tri!(visitor.visit_map(&mut self));
+ tri!(self.end());
+ Ok(value)
+ }
+
+ fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ let value = tri!(visitor.visit_seq(&mut self));
+ tri!(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 = tri!(kseed.deserialize(key.into_deserializer()));
+ let value = tri!(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 = tri!(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"))]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(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 }
+ }
+}
+
+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 tri!(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 }
+ }
+}
+
+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 crate::lib::*;
+
+ use crate::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 }
+ }
+
+ 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, 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 })
+ }
+ }
+
+ 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..2f94a644be
--- /dev/null
+++ b/third_party/rust/serde/src/integer128.rs
@@ -0,0 +1,9 @@
+// No longer used. Old versions of serde used this macro for supporting targets
+// that did not yet have 128-bit integer support.
+#[macro_export]
+#[doc(hidden)]
+macro_rules! serde_if_integer128 {
+ ($($tt: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..b0756af6dc
--- /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.
+//! - [Starlark], the format used for describing build targets by the Bazel and
+//! Buck build systems. *(serialization only)*
+//! - [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)*
+//! - [CSV], Comma-separated values is a tabular text file format.
+//!
+//! [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
+//! [Starlark]: https://github.com/dtolnay/serde-starlark
+//! [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
+//! [CSV]: https://docs.rs/csv
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Serde types in rustdoc of other crates get linked to here.
+#![doc(html_root_url = "https://docs.rs/serde/1.0.195")]
+// Support using Serde without the standard library!
+#![cfg_attr(not(feature = "std"), no_std)]
+// Show which crate feature enables conditionally compiled APIs in documentation.
+#![cfg_attr(doc_cfg, feature(doc_cfg))]
+// 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)]
+// Ignored clippy and clippy_pedantic lints
+#![allow(
+ // clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
+ clippy::unnested_or_patterns,
+ // clippy bug: https://github.com/rust-lang/rust-clippy/issues/7768
+ clippy::semicolon_if_nothing_returned,
+ // not available in our oldest supported compiler
+ clippy::empty_enum,
+ clippy::type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/8772
+ // integer and float ser/de requires these sorts of casts
+ clippy::cast_possible_truncation,
+ clippy::cast_possible_wrap,
+ clippy::cast_sign_loss,
+ // things are often more readable this way
+ clippy::cast_lossless,
+ clippy::module_name_repetitions,
+ clippy::single_match_else,
+ clippy::type_complexity,
+ clippy::use_self,
+ clippy::zero_prefixed_literal,
+ // correctly used
+ clippy::derive_partial_eq_without_eq,
+ clippy::enum_glob_use,
+ clippy::explicit_auto_deref,
+ clippy::let_underscore_untyped,
+ clippy::map_err_ignore,
+ clippy::new_without_default,
+ clippy::result_unit_err,
+ clippy::wildcard_imports,
+ // not practical
+ clippy::needless_pass_by_value,
+ clippy::similar_names,
+ clippy::too_many_lines,
+ // preference
+ clippy::doc_markdown,
+ clippy::unseparated_literal_suffix,
+ // false positive
+ clippy::needless_doctest_main,
+ // noisy
+ clippy::missing_errors_doc,
+ clippy::must_use_candidate,
+)]
+// Restrictions
+#![deny(clippy::question_mark_used)]
+// 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::{f32, f64};
+ pub use self::core::{i16, i32, i64, i8, isize};
+ pub use self::core::{iter, num, ptr, str};
+ pub use self::core::{u16, u32, u64, u8, usize};
+
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ pub use self::core::{cmp, mem, slice};
+
+ pub use self::core::cell::{Cell, RefCell};
+ pub use self::core::clone::{self, Clone};
+ pub use self::core::cmp::Reverse;
+ 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::{Bound, Range, RangeFrom, RangeInclusive, RangeTo};
+ pub use self::core::option::{self, Option};
+ pub use self::core::result::{self, Result};
+ pub use self::core::time::Duration;
+
+ #[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(all(not(no_core_cstr), not(feature = "std")))]
+ pub use self::core::ffi::CStr;
+ #[cfg(feature = "std")]
+ pub use std::ffi::CStr;
+
+ #[cfg(all(not(no_core_cstr), feature = "alloc", not(feature = "std")))]
+ pub use alloc::ffi::CString;
+ #[cfg(feature = "std")]
+ pub use std::ffi::CString;
+
+ #[cfg(feature = "std")]
+ pub use std::{error, net};
+
+ #[cfg(feature = "std")]
+ pub use std::collections::{HashMap, HashSet};
+ #[cfg(feature = "std")]
+ pub use std::ffi::{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", 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};
+}
+
+// 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! tri {
+ ($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 crate::de::{Deserialize, Deserializer};
+#[doc(inline)]
+pub use crate::ser::{Serialize, Serializer};
+
+// Used by generated code and doc tests. Not public API.
+#[doc(hidden)]
+#[path = "private/mod.rs"]
+pub mod __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")]
+extern crate serde_derive;
+
+/// Derive macro available if serde is built with `features = ["derive"]`.
+#[cfg(feature = "serde_derive")]
+#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]
+pub use serde_derive::{Deserialize, Serialize};
+
+#[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..a8fd85a379
--- /dev/null
+++ b/third_party/rust/serde/src/macros.rs
@@ -0,0 +1,231 @@
+// 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.
+///
+/// ```edition2021
+/// # 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.
+///
+/// ```edition2021
+/// # 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.
+///
+/// ```edition2021
+/// # use serde::forward_to_deserialize_any;
+/// # use serde::de::{value, Deserializer, Visitor};
+/// # use std::marker::PhantomData;
+/// #
+/// # 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>) => {
+ 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>) => {
+ 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..883e6909c1
--- /dev/null
+++ b/third_party/rust/serde/src/private/de.rs
@@ -0,0 +1,2892 @@
+use crate::lib::*;
+
+use crate::de::value::{BorrowedBytesDeserializer, BytesDeserializer};
+use crate::de::{
+ Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, IntoDeserializer, VariantAccess,
+ Visitor,
+};
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+use crate::de::{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 crate::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 crate::lib::*;
+
+ use crate::actually_private;
+ use crate::de::value::{MapDeserializer, SeqDeserializer};
+ use crate::de::{
+ self, size_hint, 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)
+ }
+ }
+
+ impl<'de, E> de::IntoDeserializer<'de, E> for Content<'de>
+ where
+ E: de::Error,
+ {
+ type Deserializer = ContentDeserializer<'de, E>;
+
+ fn into_deserializer(self) -> Self::Deserializer {
+ ContentDeserializer::new(self)
+ }
+ }
+
+ 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::<Content>::with_capacity(size_hint::cautious::<Content>(visitor.size_hint()));
+ while let Some(e) = tri!(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::<(Content, Content)>::with_capacity(
+ size_hint::cautious::<(Content, Content)>(visitor.size_hint()),
+ );
+ while let Some(kv) = tri!(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,
+ 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 TaggedContentVisitor<T> {
+ tag_name: &'static str,
+ expecting: &'static str,
+ value: PhantomData<T>,
+ }
+
+ impl<T> TaggedContentVisitor<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,
+ value: PhantomData,
+ }
+ }
+ }
+
+ impl<'de, T> Visitor<'de> for TaggedContentVisitor<T>
+ where
+ T: Deserialize<'de>,
+ {
+ type Value = (T, Content<'de>);
+
+ 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 tri!(seq.next_element()) {
+ Some(tag) => tag,
+ None => {
+ return Err(de::Error::missing_field(self.tag_name));
+ }
+ };
+ let rest = de::value::SeqAccessDeserializer::new(seq);
+ Ok((tag, tri!(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::<(Content, Content)>::with_capacity(size_hint::cautious::<(
+ Content,
+ Content,
+ )>(map.size_hint()));
+ while let Some(k) = tri!(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(tri!(map.next_value()));
+ }
+ TagOrContent::Content(k) => {
+ let v = tri!(map.next_value());
+ vec.push((k, v));
+ }
+ }
+ }
+ match tag {
+ None => Err(de::Error::missing_field(self.tag_name)),
+ Some(tag) => Ok((tag, 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_identifier(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_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ match field_index {
+ 0 => Ok(TagOrContentField::Tag),
+ 1 => Ok(TagOrContentField::Content),
+ _ => Err(de::Error::invalid_value(
+ Unexpected::Unsigned(field_index),
+ &self,
+ )),
+ }
+ }
+
+ 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))
+ }
+ }
+
+ fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ if field == self.tag.as_bytes() {
+ Ok(TagOrContentField::Tag)
+ } else if field == self.content.as_bytes() {
+ Ok(TagOrContentField::Content)
+ } else {
+ Err(de::Error::invalid_value(Unexpected::Bytes(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_identifier(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_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ match field_index {
+ 0 => Ok(TagContentOtherField::Tag),
+ 1 => Ok(TagContentOtherField::Content),
+ _ => Ok(TagContentOtherField::Other),
+ }
+ }
+
+ fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ self.visit_bytes(field.as_bytes())
+ }
+
+ fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ if field == self.tag.as_bytes() {
+ Ok(TagContentOtherField::Tag)
+ } else if field == self.content.as_bytes() {
+ 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 = SeqDeserializer::new(seq);
+ let value = tri!(visitor.visit_seq(&mut seq_visitor));
+ tri!(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 = MapDeserializer::new(map);
+ let value = tri!(visitor.visit_map(&mut map_visitor));
+ tri!(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,
+ 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,
+ 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.into_iter()), 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.into_iter()), visitor)
+ }
+ Some(Content::Seq(v)) => {
+ de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor)
+ }
+ Some(other) => Err(de::Error::invalid_type(
+ other.unexpected(),
+ &"struct variant",
+ )),
+ None => Err(de::Error::invalid_type(
+ de::Unexpected::UnitVariant,
+ &"struct variant",
+ )),
+ }
+ }
+ }
+
+ /// 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 = SeqDeserializer::new(seq);
+ let value = tri!(visitor.visit_seq(&mut seq_visitor));
+ tri!(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 = MapDeserializer::new(map);
+ let value = tri!(visitor.visit_map(&mut map_visitor));
+ tri!(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,
+ 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,
+ err: PhantomData,
+ }
+ }
+ }
+
+ impl<'a, 'de: 'a, E> Copy for ContentRefDeserializer<'a, 'de, E> {}
+
+ impl<'a, 'de: 'a, E> Clone for ContentRefDeserializer<'a, 'de, E> {
+ fn clone(&self) -> Self {
+ *self
+ }
+ }
+
+ 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 = tri!(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,
+ 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 tri!(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,
+ 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>,
+ {
+ self.deserialize_map(visitor)
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ name: &'static str,
+ variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>,
+ {
+ for entry in self.0 {
+ if let Some((key, value)) = flat_map_take_entry(entry, variants) {
+ 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 {
+ iter: self.0.iter(),
+ pending_content: None,
+ _marker: PhantomData,
+ })
+ }
+
+ 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 {
+ iter: self.0.iter_mut(),
+ pending_content: None,
+ fields,
+ _marker: PhantomData,
+ })
+ }
+
+ 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()
+ }
+
+ fn deserialize_ignored_any<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()
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+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> 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 {
+ // 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_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"))]
+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> 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>,
+ {
+ for entry in self.iter.by_ref() {
+ if let Some((key, content)) = flat_map_take_entry(entry, self.fields) {
+ 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")),
+ }
+ }
+}
+
+/// Claims one key-value pair from a FlatMapDeserializer's field buffer if the
+/// field name matches any of the recognized ones.
+#[cfg(any(feature = "std", feature = "alloc"))]
+fn flat_map_take_entry<'de>(
+ entry: &mut Option<(Content<'de>, Content<'de>)>,
+ recognized: &[&str],
+) -> Option<(Content<'de>, Content<'de>)> {
+ // Entries in the FlatMapDeserializer buffer are nulled out as they get
+ // claimed for deserialization. We only use an entry if it is still present
+ // and if the field is one recognized by the current data structure.
+ let is_recognized = match entry {
+ None => false,
+ Some((k, _v)) => k.as_str().map_or(false, |name| recognized.contains(&name)),
+ };
+
+ if is_recognized {
+ entry.take()
+ } else {
+ None
+ }
+}
+
+pub struct AdjacentlyTaggedEnumVariantSeed<F> {
+ pub enum_name: &'static str,
+ pub variants: &'static [&'static str],
+ pub fields_enum: PhantomData<F>,
+}
+
+pub struct AdjacentlyTaggedEnumVariantVisitor<F> {
+ enum_name: &'static str,
+ fields_enum: PhantomData<F>,
+}
+
+impl<'de, F> Visitor<'de> for AdjacentlyTaggedEnumVariantVisitor<F>
+where
+ F: Deserialize<'de>,
+{
+ type Value = F;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ write!(formatter, "variant of enum {}", self.enum_name)
+ }
+
+ fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ let (variant, variant_access) = tri!(data.variant());
+ tri!(variant_access.unit_variant());
+ Ok(variant)
+ }
+}
+
+impl<'de, F> DeserializeSeed<'de> for AdjacentlyTaggedEnumVariantSeed<F>
+where
+ F: Deserialize<'de>,
+{
+ type Value = F;
+
+ fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_enum(
+ self.enum_name,
+ self.variants,
+ AdjacentlyTaggedEnumVariantVisitor {
+ enum_name: self.enum_name,
+ fields_enum: PhantomData,
+ },
+ )
+ }
+}
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..1b18fe6b41
--- /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 crate::lib::*;
+
+use crate::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..177f8501b5
--- /dev/null
+++ b/third_party/rust/serde/src/private/mod.rs
@@ -0,0 +1,48 @@
+#[cfg(not(no_serde_derive))]
+pub mod de;
+#[cfg(not(no_serde_derive))]
+pub mod ser;
+
+// FIXME: #[cfg(doctest)] once https://github.com/rust-lang/rust/issues/67295 is fixed.
+pub mod doc;
+
+pub use crate::lib::clone::Clone;
+pub use crate::lib::convert::{From, Into};
+pub use crate::lib::default::Default;
+pub use crate::lib::fmt::{self, Formatter};
+pub use crate::lib::marker::PhantomData;
+pub use crate::lib::option::Option::{self, None, Some};
+pub use crate::lib::ptr;
+pub use crate::lib::result::Result::{self, Err, Ok};
+
+pub use self::string::from_utf8_lossy;
+
+#[cfg(any(feature = "alloc", feature = "std"))]
+pub use crate::lib::{ToString, Vec};
+
+#[cfg(not(no_core_try_from))]
+pub use crate::lib::convert::TryFrom;
+
+mod string {
+ use crate::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..50bcb251e3
--- /dev/null
+++ b/third_party/rust/serde/src/private/ser.rs
@@ -0,0 +1,1385 @@
+use crate::lib::*;
+
+use crate::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,
+ variant_ident,
+ tag,
+ 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 = tri!(self.delegate.serialize_map(Some(1)));
+ tri!(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 = tri!(self.delegate.serialize_map(Some(1)));
+ tri!(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 = tri!(self.delegate.serialize_map(Some(2)));
+ tri!(map.serialize_entry(self.tag, self.variant_name));
+ tri!(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 = tri!(self.delegate.serialize_map(Some(2)));
+ tri!(map.serialize_entry(self.tag, self.variant_name));
+ tri!(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 = tri!(self.delegate.serialize_map(Some(2)));
+ tri!(map.serialize_entry(self.tag, self.variant_name));
+ tri!(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 = tri!(self.delegate.serialize_map(len.map(|len| len + 1)));
+ tri!(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 = tri!(self.delegate.serialize_struct(name, len + 1));
+ tri!(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 = tri!(self.delegate.serialize_map(Some(2)));
+ tri!(map.serialize_entry(self.tag, self.variant_name));
+ tri!(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 crate::lib::*;
+
+ use crate::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,
+ 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 = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
+ self.fields.push(value);
+ Ok(())
+ }
+
+ fn end(mut self) -> Result<M::Ok, M::Error> {
+ tri!(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,
+ 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 = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
+ self.fields.push((key, value));
+ Ok(())
+ }
+
+ fn end(mut self) -> Result<M::Ok, M::Error> {
+ tri!(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 crate::ser::SerializeTuple;
+ let mut tuple = tri!(serializer.serialize_tuple(elements.len()));
+ for e in elements {
+ tri!(tuple.serialize_element(e));
+ }
+ tuple.end()
+ }
+ Content::TupleStruct(n, ref fields) => {
+ use crate::ser::SerializeTupleStruct;
+ let mut ts = tri!(serializer.serialize_tuple_struct(n, fields.len()));
+ for f in fields {
+ tri!(ts.serialize_field(f));
+ }
+ ts.end()
+ }
+ Content::TupleVariant(n, i, v, ref fields) => {
+ use crate::ser::SerializeTupleVariant;
+ let mut tv = tri!(serializer.serialize_tuple_variant(n, i, v, fields.len()));
+ for f in fields {
+ tri!(tv.serialize_field(f));
+ }
+ tv.end()
+ }
+ Content::Map(ref entries) => {
+ use crate::ser::SerializeMap;
+ let mut map = tri!(serializer.serialize_map(Some(entries.len())));
+ for (k, v) in entries {
+ tri!(map.serialize_entry(k, v));
+ }
+ map.end()
+ }
+ Content::Struct(n, ref fields) => {
+ use crate::ser::SerializeStruct;
+ let mut s = tri!(serializer.serialize_struct(n, fields.len()));
+ for &(k, ref v) in fields {
+ tri!(s.serialize_field(k, v));
+ }
+ s.end()
+ }
+ Content::StructVariant(n, i, v, ref fields) => {
+ use crate::ser::SerializeStructVariant;
+ let mut sv = tri!(serializer.serialize_struct_variant(n, i, v, fields.len()));
+ for &(k, ref v) in fields {
+ tri!(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(tri!(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(tri!(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(tri!(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,
+ 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,
+ variant_index,
+ 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,
+ 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,
+ variant_index,
+ 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 = tri!(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 = tri!(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 = tri!(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 = tri!(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 = tri!(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 = tri!(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 = tri!(key.serialize(ContentSerializer::<E>::new()));
+ let value = tri!(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 = tri!(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 = tri!(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 = FlatMapSerializeTupleVariantAsMapValue<'a, M>;
+ 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,
+ {
+ tri!(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,
+ variant: &'static str,
+ _: usize,
+ ) -> Result<Self::SerializeTupleVariant, Self::Error> {
+ tri!(self.0.serialize_key(variant));
+ Ok(FlatMapSerializeTupleVariantAsMapValue::new(self.0))
+ }
+
+ 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> {
+ tri!(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 FlatMapSerializeTupleVariantAsMapValue<'a, M: 'a> {
+ map: &'a mut M,
+ fields: Vec<Content>,
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, M> FlatMapSerializeTupleVariantAsMapValue<'a, M>
+where
+ M: SerializeMap + 'a,
+{
+ fn new(map: &'a mut M) -> Self {
+ FlatMapSerializeTupleVariantAsMapValue {
+ map,
+ fields: Vec::new(),
+ }
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl<'a, M> ser::SerializeTupleVariant for FlatMapSerializeTupleVariantAsMapValue<'a, M>
+where
+ M: SerializeMap + 'a,
+{
+ type Ok = ();
+ type Error = M::Error;
+
+ fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
+ where
+ T: Serialize,
+ {
+ let value = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
+ self.fields.push(value);
+ Ok(())
+ }
+
+ fn end(self) -> Result<(), Self::Error> {
+ tri!(self.map.serialize_value(&Content::Seq(self.fields)));
+ 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,
+ 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 = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
+ self.fields.push((key, value));
+ Ok(())
+ }
+
+ fn end(self) -> Result<(), Self::Error> {
+ tri!(self
+ .map
+ .serialize_value(&Content::Struct(self.name, self.fields)));
+ Ok(())
+ }
+}
+
+pub struct AdjacentlyTaggedEnumVariant {
+ pub enum_name: &'static str,
+ pub variant_index: u32,
+ pub variant_name: &'static str,
+}
+
+impl Serialize for AdjacentlyTaggedEnumVariant {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_unit_variant(self.enum_name, self.variant_index, self.variant_name)
+ }
+}
+
+// Error when Serialize for a non_exhaustive remote enum encounters a variant
+// that is not recognized.
+pub struct CannotSerializeVariant<T>(pub T);
+
+impl<T> Display for CannotSerializeVariant<T>
+where
+ T: Debug,
+{
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ write!(formatter, "enum variant cannot be serialized: {:?}", self.0)
+ }
+}
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..0650ab6f12
--- /dev/null
+++ b/third_party/rust/serde/src/ser/fmt.rs
@@ -0,0 +1,170 @@
+use crate::lib::*;
+use crate::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)
+ }
+ )*
+ };
+}
+
+/// ```edition2021
+/// use serde::ser::Serialize;
+/// use serde_derive::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_i128: i128,
+ serialize_u8: u8,
+ serialize_u16: u16,
+ serialize_u32: u32,
+ serialize_u64: u64,
+ serialize_u128: u128,
+ serialize_f32: f32,
+ serialize_f64: f64,
+ serialize_char: char,
+ serialize_str: &str,
+ serialize_unit_struct: &'static str,
+ }
+
+ 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..8c70634af0
--- /dev/null
+++ b/third_party/rust/serde/src/ser/impls.rs
@@ -0,0 +1,1077 @@
+use crate::lib::*;
+
+use crate::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!(i128, serialize_i128);
+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!(u128, serialize_u128);
+primitive_impl!(f32, serialize_f32);
+primitive_impl!(f64, serialize_f64);
+primitive_impl!(char, serialize_char);
+
+////////////////////////////////////////////////////////////////////////////////
+
+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"))]
+#[cfg_attr(doc_cfg, doc(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(any(feature = "std", not(no_core_cstr)))]
+#[cfg_attr(doc_cfg, doc(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(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
+#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+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,
+ {
+ tri!(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 = tri!(serializer.serialize_tuple($len));
+ for e in self {
+ tri!(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(not(no_relaxed_trait_bounds))]
+macro_rules! seq_impl {
+ (
+ $(#[$attr:meta])*
+ $ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>
+ ) => {
+ $(#[$attr])*
+ 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(no_relaxed_trait_bounds)]
+macro_rules! seq_impl {
+ (
+ $(#[$attr:meta])*
+ $ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>
+ ) => {
+ $(#[$attr])*
+ 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)
+ }
+ }
+ }
+}
+
+seq_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ BinaryHeap<T: Ord>
+}
+
+seq_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ BTreeSet<T: Ord>
+}
+
+seq_impl! {
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ HashSet<T: Eq + Hash, H: BuildHasher>
+}
+
+seq_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ LinkedList<T>
+}
+
+seq_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ Vec<T>
+}
+
+seq_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ 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 = tri!(serializer.serialize_struct("Range", 2));
+ tri!(state.serialize_field("start", &self.start));
+ tri!(state.serialize_field("end", &self.end));
+ state.end()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<Idx> Serialize for RangeFrom<Idx>
+where
+ Idx: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ use super::SerializeStruct;
+ let mut state = tri!(serializer.serialize_struct("RangeFrom", 1));
+ tri!(state.serialize_field("start", &self.start));
+ state.end()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+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 = tri!(serializer.serialize_struct("RangeInclusive", 2));
+ tri!(state.serialize_field("start", &self.start()));
+ tri!(state.serialize_field("end", &self.end()));
+ state.end()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl<Idx> Serialize for RangeTo<Idx>
+where
+ Idx: Serialize,
+{
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ use super::SerializeStruct;
+ let mut state = tri!(serializer.serialize_struct("RangeTo", 1));
+ tri!(state.serialize_field("end", &self.end));
+ state.end()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+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")]
+#[cfg_attr(doc_cfg, doc(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 = tri!(serializer.serialize_tuple($len));
+ $(
+ tri!(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(not(no_relaxed_trait_bounds))]
+macro_rules! map_impl {
+ (
+ $(#[$attr:meta])*
+ $ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>
+ ) => {
+ $(#[$attr])*
+ 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(no_relaxed_trait_bounds)]
+macro_rules! map_impl {
+ (
+ $(#[$attr:meta])*
+ $ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>
+ ) => {
+ $(#[$attr])*
+ 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)
+ }
+ }
+ }
+}
+
+map_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ BTreeMap<K: Ord, V>
+}
+
+map_impl! {
+ #[cfg(feature = "std")]
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
+ HashMap<K: Eq + Hash, V, H: BuildHasher>
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+macro_rules! deref_impl {
+ (
+ $(#[$attr:meta])*
+ <$($desc:tt)+
+ ) => {
+ $(#[$attr])*
+ 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
+}
+
+deref_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ <T: ?Sized> Serialize for Box<T> where T: Serialize
+}
+
+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
+ #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
+ #[cfg_attr(doc_cfg, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))]
+ <T: ?Sized> Serialize for Rc<T> where T: Serialize
+}
+
+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
+ #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
+ #[cfg_attr(doc_cfg, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))]
+ <T: ?Sized> Serialize for Arc<T> where T: Serialize
+}
+
+deref_impl! {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))]
+ <'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")))]
+#[cfg_attr(
+ doc_cfg,
+ doc(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")))]
+#[cfg_attr(
+ doc_cfg,
+ doc(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,)+) => {
+ $(
+ 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,
+ NonZeroU128,
+ NonZeroUsize,
+}
+
+#[cfg(not(no_num_nonzero_signed))]
+nonzero_integers! {
+ NonZeroI8,
+ NonZeroI16,
+ NonZeroI32,
+ NonZeroI64,
+ NonZeroI128,
+ NonZeroIsize,
+}
+
+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")]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(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)
+ }
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+impl Serialize for Duration {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ use super::SerializeStruct;
+ let mut state = tri!(serializer.serialize_struct("Duration", 2));
+ tri!(state.serialize_field("secs", &self.as_secs()));
+ tri!(state.serialize_field("nanos", &self.subsec_nanos()));
+ state.end()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "std")]
+#[cfg_attr(doc_cfg, doc(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 = tri!(serializer.serialize_struct("SystemTime", 2));
+ tri!(state.serialize_field("secs_since_epoch", &duration_since_epoch.as_secs()));
+ tri!(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")]
+#[cfg_attr(doc_cfg, doc(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: &[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")]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(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")]
+#[cfg_attr(doc_cfg, doc(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)))]
+#[cfg_attr(doc_cfg, doc(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)))]
+#[cfg_attr(doc_cfg, doc(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)
+ }
+}
+
+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))]
+ #[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", 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..479be94071
--- /dev/null
+++ b/third_party/rust/serde/src/ser/impossible.rs
@@ -0,0 +1,216 @@
+//! This module contains `Impossible` serializer and its implementations.
+
+use crate::lib::*;
+
+use crate::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`].
+///
+/// ```edition2021
+/// # 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..75c45140e2
--- /dev/null
+++ b/third_party/rust/serde/src/ser/mod.rs
@@ -0,0 +1,1952 @@
+//! 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 crate::lib::*;
+
+mod fmt;
+mod impls;
+mod impossible;
+
+pub use self::impossible::Impossible;
+
+#[cfg(not(any(feature = "std", feature = "unstable")))]
+#[doc(no_inline)]
+pub use crate::std_error::Error as StdError;
+#[cfg(all(feature = "unstable", not(feature = "std")))]
+#[doc(no_inline)]
+pub use core::error::Error as StdError;
+#[cfg(feature = "std")]
+#[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.
+ ///
+ /// ```edition2021
+ /// # 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.
+ ///
+ /// ```edition2021
+ /// 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.
+ ///
+ /// ```edition2021
+ /// # 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`.
+ ///
+ /// ```edition2021
+ /// # 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`.
+ ///
+ /// ```edition2021
+ /// # 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`.
+ ///
+ /// ```edition2021
+ /// # 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.
+ ///
+ /// ```edition2021
+ /// # 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>;
+
+ /// Serialize an `i128` value.
+ ///
+ /// ```edition2021
+ /// # 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)
+ /// }
+ /// }
+ /// ```
+ ///
+ /// 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`.
+ ///
+ /// ```edition2021
+ /// # 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`.
+ ///
+ /// ```edition2021
+ /// # 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`.
+ ///
+ /// ```edition2021
+ /// # 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.
+ ///
+ /// ```edition2021
+ /// # 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>;
+
+ /// Serialize a `u128` value.
+ ///
+ /// ```edition2021
+ /// # 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)
+ /// }
+ /// }
+ /// ```
+ ///
+ /// 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`.
+ ///
+ /// ```edition2021
+ /// # 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.
+ ///
+ /// ```edition2021
+ /// # 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`.
+ ///
+ /// ```edition2021
+ /// # 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`.
+ ///
+ /// ```edition2021
+ /// # 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:
+ ///
+ /// ```edition2021
+ /// # 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.
+ ///
+ /// ```edition2021
+ /// # 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.
+ ///
+ /// ```edition2021
+ /// # 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.
+ ///
+ /// ```edition2021
+ /// # 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`.
+ ///
+ /// ```edition2021
+ /// 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.
+ ///
+ /// ```edition2021
+ /// 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)`.
+ ///
+ /// ```edition2021
+ /// 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.
+ ///
+ /// ```edition2021
+ /// 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.
+ ///
+ /// ```edition2021
+ /// # 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<dyn Iterator<Item = &'a T>>;
+ /// #
+ /// # fn into_iter(self) -> Self::IntoIter {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// use serde::ser::{Serialize, SerializeSeq, Serializer};
+ ///
+ /// 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`.
+ ///
+ /// ```edition2021
+ /// use serde::ser::{Serialize, SerializeTuple, Serializer};
+ ///
+ /// # 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()
+ /// }
+ /// }
+ /// ```
+ ///
+ /// ```edition2021
+ /// 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.
+ ///
+ /// ```edition2021
+ /// 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.
+ ///
+ /// ```edition2021
+ /// 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.
+ ///
+ /// ```edition2021
+ /// # 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<dyn Iterator<Item = (&'a K, &'a V)>>;
+ /// #
+ /// # fn into_iter(self) -> Self::IntoIter {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// #
+ /// use serde::ser::{Serialize, SerializeMap, Serializer};
+ ///
+ /// 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.
+ ///
+ /// ```edition2021
+ /// 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.
+ ///
+ /// ```edition2021
+ /// 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.
+ ///
+ /// ```edition2021
+ /// 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 mut iter = iter.into_iter();
+ let mut serializer = tri!(self.serialize_seq(iterator_len_hint(&iter)));
+ tri!(iter.try_for_each(|item| 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.
+ ///
+ /// ```edition2021
+ /// 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 mut iter = iter.into_iter();
+ let mut serializer = tri!(self.serialize_map(iterator_len_hint(&iter)));
+ tri!(iter.try_for_each(|(key, value)| 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.
+ ///
+ /// ```edition2021
+ /// # 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.
+ ///
+ /// ```edition2021
+ /// # 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.
+ ///
+ /// ```edition2021
+ /// # 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
+///
+/// ```edition2021
+/// # 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<dyn Iterator<Item = &'a T>>;
+/// # fn into_iter(self) -> Self::IntoIter {
+/// # unimplemented!()
+/// # }
+/// # }
+/// #
+/// use serde::ser::{Serialize, SerializeSeq, Serializer};
+///
+/// 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
+///
+/// ```edition2021
+/// use serde::ser::{Serialize, SerializeTuple, Serializer};
+///
+/// # 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()
+/// }
+/// }
+/// ```
+///
+/// ```edition2021
+/// # 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<dyn Iterator<Item = &'a T>>;
+/// # fn into_iter(self) -> Self::IntoIter {
+/// # unimplemented!()
+/// # }
+/// # }
+/// #
+/// use serde::ser::{Serialize, SerializeTuple, Serializer};
+///
+/// # 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
+///
+/// ```edition2021
+/// 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
+///
+/// ```edition2021
+/// 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
+///
+/// ```edition2021
+/// # 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<dyn Iterator<Item = (&'a K, &'a V)>>;
+/// #
+/// # fn into_iter(self) -> Self::IntoIter {
+/// # unimplemented!()
+/// # }
+/// # }
+/// #
+/// use serde::ser::{Serialize, SerializeMap, Serializer};
+///
+/// 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,
+ {
+ tri!(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
+///
+/// ```edition2021
+/// 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
+///
+/// ```edition2021
+/// 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..f15a4d74ad
--- /dev/null
+++ b/third_party/rust/serde/src/std_error.rs
@@ -0,0 +1,48 @@
+use crate::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:
+///
+/// ```edition2021
+/// #[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"]
+/// ```
+///
+/// ```edition2021
+/// #[cfg(feature = "std")]
+/// impl std::error::Error for MySerError {}
+/// ```
+///
+/// ... or else provide the std Error impl unconditionally via Serde's
+/// re-export:
+///
+/// ```edition2021
+/// 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
+ }
+}