17e2e9c0cSopenharmony_ci//! Building blocks for deserializing basic values using the `IntoDeserializer` 27e2e9c0cSopenharmony_ci//! trait. 37e2e9c0cSopenharmony_ci//! 47e2e9c0cSopenharmony_ci//! ```edition2021 57e2e9c0cSopenharmony_ci//! use serde::de::{value, Deserialize, IntoDeserializer}; 67e2e9c0cSopenharmony_ci//! use serde_derive::Deserialize; 77e2e9c0cSopenharmony_ci//! use std::str::FromStr; 87e2e9c0cSopenharmony_ci//! 97e2e9c0cSopenharmony_ci//! #[derive(Deserialize)] 107e2e9c0cSopenharmony_ci//! enum Setting { 117e2e9c0cSopenharmony_ci//! On, 127e2e9c0cSopenharmony_ci//! Off, 137e2e9c0cSopenharmony_ci//! } 147e2e9c0cSopenharmony_ci//! 157e2e9c0cSopenharmony_ci//! impl FromStr for Setting { 167e2e9c0cSopenharmony_ci//! type Err = value::Error; 177e2e9c0cSopenharmony_ci//! 187e2e9c0cSopenharmony_ci//! fn from_str(s: &str) -> Result<Self, Self::Err> { 197e2e9c0cSopenharmony_ci//! Self::deserialize(s.into_deserializer()) 207e2e9c0cSopenharmony_ci//! } 217e2e9c0cSopenharmony_ci//! } 227e2e9c0cSopenharmony_ci//! ``` 237e2e9c0cSopenharmony_ci 247e2e9c0cSopenharmony_ciuse crate::lib::*; 257e2e9c0cSopenharmony_ci 267e2e9c0cSopenharmony_ciuse self::private::{First, Second}; 277e2e9c0cSopenharmony_ciuse crate::de::{self, size_hint, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor}; 287e2e9c0cSopenharmony_ciuse crate::ser; 297e2e9c0cSopenharmony_ci 307e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 317e2e9c0cSopenharmony_ci 327e2e9c0cSopenharmony_ci// For structs that contain a PhantomData. We do not want the trait 337e2e9c0cSopenharmony_ci// bound `E: Clone` inferred by derive(Clone). 347e2e9c0cSopenharmony_cimacro_rules! impl_copy_clone { 357e2e9c0cSopenharmony_ci ($ty:ident $(<$lifetime:tt>)*) => { 367e2e9c0cSopenharmony_ci impl<$($lifetime,)* E> Copy for $ty<$($lifetime,)* E> {} 377e2e9c0cSopenharmony_ci 387e2e9c0cSopenharmony_ci impl<$($lifetime,)* E> Clone for $ty<$($lifetime,)* E> { 397e2e9c0cSopenharmony_ci fn clone(&self) -> Self { 407e2e9c0cSopenharmony_ci *self 417e2e9c0cSopenharmony_ci } 427e2e9c0cSopenharmony_ci } 437e2e9c0cSopenharmony_ci }; 447e2e9c0cSopenharmony_ci} 457e2e9c0cSopenharmony_ci 467e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 477e2e9c0cSopenharmony_ci 487e2e9c0cSopenharmony_ci/// A minimal representation of all possible errors that can occur using the 497e2e9c0cSopenharmony_ci/// `IntoDeserializer` trait. 507e2e9c0cSopenharmony_ci#[derive(Clone, PartialEq)] 517e2e9c0cSopenharmony_cipub struct Error { 527e2e9c0cSopenharmony_ci err: ErrorImpl, 537e2e9c0cSopenharmony_ci} 547e2e9c0cSopenharmony_ci 557e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 567e2e9c0cSopenharmony_citype ErrorImpl = Box<str>; 577e2e9c0cSopenharmony_ci#[cfg(not(any(feature = "std", feature = "alloc")))] 587e2e9c0cSopenharmony_citype ErrorImpl = (); 597e2e9c0cSopenharmony_ci 607e2e9c0cSopenharmony_ciimpl de::Error for Error { 617e2e9c0cSopenharmony_ci #[cfg(any(feature = "std", feature = "alloc"))] 627e2e9c0cSopenharmony_ci #[cold] 637e2e9c0cSopenharmony_ci fn custom<T>(msg: T) -> Self 647e2e9c0cSopenharmony_ci where 657e2e9c0cSopenharmony_ci T: Display, 667e2e9c0cSopenharmony_ci { 677e2e9c0cSopenharmony_ci Error { 687e2e9c0cSopenharmony_ci err: msg.to_string().into_boxed_str(), 697e2e9c0cSopenharmony_ci } 707e2e9c0cSopenharmony_ci } 717e2e9c0cSopenharmony_ci 727e2e9c0cSopenharmony_ci #[cfg(not(any(feature = "std", feature = "alloc")))] 737e2e9c0cSopenharmony_ci #[cold] 747e2e9c0cSopenharmony_ci fn custom<T>(msg: T) -> Self 757e2e9c0cSopenharmony_ci where 767e2e9c0cSopenharmony_ci T: Display, 777e2e9c0cSopenharmony_ci { 787e2e9c0cSopenharmony_ci let _ = msg; 797e2e9c0cSopenharmony_ci Error { err: () } 807e2e9c0cSopenharmony_ci } 817e2e9c0cSopenharmony_ci} 827e2e9c0cSopenharmony_ci 837e2e9c0cSopenharmony_ciimpl ser::Error for Error { 847e2e9c0cSopenharmony_ci #[cold] 857e2e9c0cSopenharmony_ci fn custom<T>(msg: T) -> Self 867e2e9c0cSopenharmony_ci where 877e2e9c0cSopenharmony_ci T: Display, 887e2e9c0cSopenharmony_ci { 897e2e9c0cSopenharmony_ci de::Error::custom(msg) 907e2e9c0cSopenharmony_ci } 917e2e9c0cSopenharmony_ci} 927e2e9c0cSopenharmony_ci 937e2e9c0cSopenharmony_ciimpl Display for Error { 947e2e9c0cSopenharmony_ci #[cfg(any(feature = "std", feature = "alloc"))] 957e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 967e2e9c0cSopenharmony_ci formatter.write_str(&self.err) 977e2e9c0cSopenharmony_ci } 987e2e9c0cSopenharmony_ci 997e2e9c0cSopenharmony_ci #[cfg(not(any(feature = "std", feature = "alloc")))] 1007e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 1017e2e9c0cSopenharmony_ci formatter.write_str("Serde deserialization error") 1027e2e9c0cSopenharmony_ci } 1037e2e9c0cSopenharmony_ci} 1047e2e9c0cSopenharmony_ci 1057e2e9c0cSopenharmony_ciimpl Debug for Error { 1067e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 1077e2e9c0cSopenharmony_ci let mut debug = formatter.debug_tuple("Error"); 1087e2e9c0cSopenharmony_ci #[cfg(any(feature = "std", feature = "alloc"))] 1097e2e9c0cSopenharmony_ci debug.field(&self.err); 1107e2e9c0cSopenharmony_ci debug.finish() 1117e2e9c0cSopenharmony_ci } 1127e2e9c0cSopenharmony_ci} 1137e2e9c0cSopenharmony_ci 1147e2e9c0cSopenharmony_ci#[cfg(feature = "std")] 1157e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] 1167e2e9c0cSopenharmony_ciimpl error::Error for Error { 1177e2e9c0cSopenharmony_ci fn description(&self) -> &str { 1187e2e9c0cSopenharmony_ci &self.err 1197e2e9c0cSopenharmony_ci } 1207e2e9c0cSopenharmony_ci} 1217e2e9c0cSopenharmony_ci 1227e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 1237e2e9c0cSopenharmony_ci 1247e2e9c0cSopenharmony_ciimpl<'de, E> IntoDeserializer<'de, E> for () 1257e2e9c0cSopenharmony_ciwhere 1267e2e9c0cSopenharmony_ci E: de::Error, 1277e2e9c0cSopenharmony_ci{ 1287e2e9c0cSopenharmony_ci type Deserializer = UnitDeserializer<E>; 1297e2e9c0cSopenharmony_ci 1307e2e9c0cSopenharmony_ci fn into_deserializer(self) -> UnitDeserializer<E> { 1317e2e9c0cSopenharmony_ci UnitDeserializer::new() 1327e2e9c0cSopenharmony_ci } 1337e2e9c0cSopenharmony_ci} 1347e2e9c0cSopenharmony_ci 1357e2e9c0cSopenharmony_ci/// A deserializer holding a `()`. 1367e2e9c0cSopenharmony_cipub struct UnitDeserializer<E> { 1377e2e9c0cSopenharmony_ci marker: PhantomData<E>, 1387e2e9c0cSopenharmony_ci} 1397e2e9c0cSopenharmony_ci 1407e2e9c0cSopenharmony_ciimpl_copy_clone!(UnitDeserializer); 1417e2e9c0cSopenharmony_ci 1427e2e9c0cSopenharmony_ciimpl<E> UnitDeserializer<E> { 1437e2e9c0cSopenharmony_ci #[allow(missing_docs)] 1447e2e9c0cSopenharmony_ci pub fn new() -> Self { 1457e2e9c0cSopenharmony_ci UnitDeserializer { 1467e2e9c0cSopenharmony_ci marker: PhantomData, 1477e2e9c0cSopenharmony_ci } 1487e2e9c0cSopenharmony_ci } 1497e2e9c0cSopenharmony_ci} 1507e2e9c0cSopenharmony_ci 1517e2e9c0cSopenharmony_ciimpl<'de, E> de::Deserializer<'de> for UnitDeserializer<E> 1527e2e9c0cSopenharmony_ciwhere 1537e2e9c0cSopenharmony_ci E: de::Error, 1547e2e9c0cSopenharmony_ci{ 1557e2e9c0cSopenharmony_ci type Error = E; 1567e2e9c0cSopenharmony_ci 1577e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 1587e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 1597e2e9c0cSopenharmony_ci bytes byte_buf unit unit_struct newtype_struct seq tuple tuple_struct 1607e2e9c0cSopenharmony_ci map struct enum identifier ignored_any 1617e2e9c0cSopenharmony_ci } 1627e2e9c0cSopenharmony_ci 1637e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 1647e2e9c0cSopenharmony_ci where 1657e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 1667e2e9c0cSopenharmony_ci { 1677e2e9c0cSopenharmony_ci visitor.visit_unit() 1687e2e9c0cSopenharmony_ci } 1697e2e9c0cSopenharmony_ci 1707e2e9c0cSopenharmony_ci fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> 1717e2e9c0cSopenharmony_ci where 1727e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 1737e2e9c0cSopenharmony_ci { 1747e2e9c0cSopenharmony_ci visitor.visit_none() 1757e2e9c0cSopenharmony_ci } 1767e2e9c0cSopenharmony_ci} 1777e2e9c0cSopenharmony_ci 1787e2e9c0cSopenharmony_ciimpl<E> Debug for UnitDeserializer<E> { 1797e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 1807e2e9c0cSopenharmony_ci formatter.debug_struct("UnitDeserializer").finish() 1817e2e9c0cSopenharmony_ci } 1827e2e9c0cSopenharmony_ci} 1837e2e9c0cSopenharmony_ci 1847e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 1857e2e9c0cSopenharmony_ci 1867e2e9c0cSopenharmony_ci/// A deserializer that cannot be instantiated. 1877e2e9c0cSopenharmony_ci#[cfg(feature = "unstable")] 1887e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "unstable")))] 1897e2e9c0cSopenharmony_cipub struct NeverDeserializer<E> { 1907e2e9c0cSopenharmony_ci never: !, 1917e2e9c0cSopenharmony_ci marker: PhantomData<E>, 1927e2e9c0cSopenharmony_ci} 1937e2e9c0cSopenharmony_ci 1947e2e9c0cSopenharmony_ci#[cfg(feature = "unstable")] 1957e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "unstable")))] 1967e2e9c0cSopenharmony_ciimpl<'de, E> IntoDeserializer<'de, E> for ! 1977e2e9c0cSopenharmony_ciwhere 1987e2e9c0cSopenharmony_ci E: de::Error, 1997e2e9c0cSopenharmony_ci{ 2007e2e9c0cSopenharmony_ci type Deserializer = NeverDeserializer<E>; 2017e2e9c0cSopenharmony_ci 2027e2e9c0cSopenharmony_ci fn into_deserializer(self) -> Self::Deserializer { 2037e2e9c0cSopenharmony_ci self 2047e2e9c0cSopenharmony_ci } 2057e2e9c0cSopenharmony_ci} 2067e2e9c0cSopenharmony_ci 2077e2e9c0cSopenharmony_ci#[cfg(feature = "unstable")] 2087e2e9c0cSopenharmony_ciimpl<'de, E> de::Deserializer<'de> for NeverDeserializer<E> 2097e2e9c0cSopenharmony_ciwhere 2107e2e9c0cSopenharmony_ci E: de::Error, 2117e2e9c0cSopenharmony_ci{ 2127e2e9c0cSopenharmony_ci type Error = E; 2137e2e9c0cSopenharmony_ci 2147e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error> 2157e2e9c0cSopenharmony_ci where 2167e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 2177e2e9c0cSopenharmony_ci { 2187e2e9c0cSopenharmony_ci self.never 2197e2e9c0cSopenharmony_ci } 2207e2e9c0cSopenharmony_ci 2217e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 2227e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 2237e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 2247e2e9c0cSopenharmony_ci tuple_struct map struct enum identifier ignored_any 2257e2e9c0cSopenharmony_ci } 2267e2e9c0cSopenharmony_ci} 2277e2e9c0cSopenharmony_ci 2287e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 2297e2e9c0cSopenharmony_ci 2307e2e9c0cSopenharmony_cimacro_rules! primitive_deserializer { 2317e2e9c0cSopenharmony_ci ($ty:ty, $doc:tt, $name:ident, $method:ident $($cast:tt)*) => { 2327e2e9c0cSopenharmony_ci #[doc = "A deserializer holding"] 2337e2e9c0cSopenharmony_ci #[doc = $doc] 2347e2e9c0cSopenharmony_ci pub struct $name<E> { 2357e2e9c0cSopenharmony_ci value: $ty, 2367e2e9c0cSopenharmony_ci marker: PhantomData<E> 2377e2e9c0cSopenharmony_ci } 2387e2e9c0cSopenharmony_ci 2397e2e9c0cSopenharmony_ci impl_copy_clone!($name); 2407e2e9c0cSopenharmony_ci 2417e2e9c0cSopenharmony_ci impl<'de, E> IntoDeserializer<'de, E> for $ty 2427e2e9c0cSopenharmony_ci where 2437e2e9c0cSopenharmony_ci E: de::Error, 2447e2e9c0cSopenharmony_ci { 2457e2e9c0cSopenharmony_ci type Deserializer = $name<E>; 2467e2e9c0cSopenharmony_ci 2477e2e9c0cSopenharmony_ci fn into_deserializer(self) -> $name<E> { 2487e2e9c0cSopenharmony_ci $name::new(self) 2497e2e9c0cSopenharmony_ci } 2507e2e9c0cSopenharmony_ci } 2517e2e9c0cSopenharmony_ci 2527e2e9c0cSopenharmony_ci impl<E> $name<E> { 2537e2e9c0cSopenharmony_ci #[allow(missing_docs)] 2547e2e9c0cSopenharmony_ci pub fn new(value: $ty) -> Self { 2557e2e9c0cSopenharmony_ci $name { 2567e2e9c0cSopenharmony_ci value, 2577e2e9c0cSopenharmony_ci marker: PhantomData, 2587e2e9c0cSopenharmony_ci } 2597e2e9c0cSopenharmony_ci } 2607e2e9c0cSopenharmony_ci } 2617e2e9c0cSopenharmony_ci 2627e2e9c0cSopenharmony_ci impl<'de, E> de::Deserializer<'de> for $name<E> 2637e2e9c0cSopenharmony_ci where 2647e2e9c0cSopenharmony_ci E: de::Error, 2657e2e9c0cSopenharmony_ci { 2667e2e9c0cSopenharmony_ci type Error = E; 2677e2e9c0cSopenharmony_ci 2687e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 2697e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str 2707e2e9c0cSopenharmony_ci string bytes byte_buf option unit unit_struct newtype_struct seq 2717e2e9c0cSopenharmony_ci tuple tuple_struct map struct enum identifier ignored_any 2727e2e9c0cSopenharmony_ci } 2737e2e9c0cSopenharmony_ci 2747e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 2757e2e9c0cSopenharmony_ci where 2767e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 2777e2e9c0cSopenharmony_ci { 2787e2e9c0cSopenharmony_ci visitor.$method(self.value $($cast)*) 2797e2e9c0cSopenharmony_ci } 2807e2e9c0cSopenharmony_ci } 2817e2e9c0cSopenharmony_ci 2827e2e9c0cSopenharmony_ci impl<E> Debug for $name<E> { 2837e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 2847e2e9c0cSopenharmony_ci formatter 2857e2e9c0cSopenharmony_ci .debug_struct(stringify!($name)) 2867e2e9c0cSopenharmony_ci .field("value", &self.value) 2877e2e9c0cSopenharmony_ci .finish() 2887e2e9c0cSopenharmony_ci } 2897e2e9c0cSopenharmony_ci } 2907e2e9c0cSopenharmony_ci } 2917e2e9c0cSopenharmony_ci} 2927e2e9c0cSopenharmony_ci 2937e2e9c0cSopenharmony_ciprimitive_deserializer!(bool, "a `bool`.", BoolDeserializer, visit_bool); 2947e2e9c0cSopenharmony_ciprimitive_deserializer!(i8, "an `i8`.", I8Deserializer, visit_i8); 2957e2e9c0cSopenharmony_ciprimitive_deserializer!(i16, "an `i16`.", I16Deserializer, visit_i16); 2967e2e9c0cSopenharmony_ciprimitive_deserializer!(i32, "an `i32`.", I32Deserializer, visit_i32); 2977e2e9c0cSopenharmony_ciprimitive_deserializer!(i64, "an `i64`.", I64Deserializer, visit_i64); 2987e2e9c0cSopenharmony_ciprimitive_deserializer!(i128, "an `i128`.", I128Deserializer, visit_i128); 2997e2e9c0cSopenharmony_ciprimitive_deserializer!(isize, "an `isize`.", IsizeDeserializer, visit_i64 as i64); 3007e2e9c0cSopenharmony_ciprimitive_deserializer!(u8, "a `u8`.", U8Deserializer, visit_u8); 3017e2e9c0cSopenharmony_ciprimitive_deserializer!(u16, "a `u16`.", U16Deserializer, visit_u16); 3027e2e9c0cSopenharmony_ciprimitive_deserializer!(u64, "a `u64`.", U64Deserializer, visit_u64); 3037e2e9c0cSopenharmony_ciprimitive_deserializer!(u128, "a `u128`.", U128Deserializer, visit_u128); 3047e2e9c0cSopenharmony_ciprimitive_deserializer!(usize, "a `usize`.", UsizeDeserializer, visit_u64 as u64); 3057e2e9c0cSopenharmony_ciprimitive_deserializer!(f32, "an `f32`.", F32Deserializer, visit_f32); 3067e2e9c0cSopenharmony_ciprimitive_deserializer!(f64, "an `f64`.", F64Deserializer, visit_f64); 3077e2e9c0cSopenharmony_ciprimitive_deserializer!(char, "a `char`.", CharDeserializer, visit_char); 3087e2e9c0cSopenharmony_ci 3097e2e9c0cSopenharmony_ci/// A deserializer holding a `u32`. 3107e2e9c0cSopenharmony_cipub struct U32Deserializer<E> { 3117e2e9c0cSopenharmony_ci value: u32, 3127e2e9c0cSopenharmony_ci marker: PhantomData<E>, 3137e2e9c0cSopenharmony_ci} 3147e2e9c0cSopenharmony_ci 3157e2e9c0cSopenharmony_ciimpl_copy_clone!(U32Deserializer); 3167e2e9c0cSopenharmony_ci 3177e2e9c0cSopenharmony_ciimpl<'de, E> IntoDeserializer<'de, E> for u32 3187e2e9c0cSopenharmony_ciwhere 3197e2e9c0cSopenharmony_ci E: de::Error, 3207e2e9c0cSopenharmony_ci{ 3217e2e9c0cSopenharmony_ci type Deserializer = U32Deserializer<E>; 3227e2e9c0cSopenharmony_ci 3237e2e9c0cSopenharmony_ci fn into_deserializer(self) -> U32Deserializer<E> { 3247e2e9c0cSopenharmony_ci U32Deserializer::new(self) 3257e2e9c0cSopenharmony_ci } 3267e2e9c0cSopenharmony_ci} 3277e2e9c0cSopenharmony_ci 3287e2e9c0cSopenharmony_ciimpl<E> U32Deserializer<E> { 3297e2e9c0cSopenharmony_ci #[allow(missing_docs)] 3307e2e9c0cSopenharmony_ci pub fn new(value: u32) -> Self { 3317e2e9c0cSopenharmony_ci U32Deserializer { 3327e2e9c0cSopenharmony_ci value, 3337e2e9c0cSopenharmony_ci marker: PhantomData, 3347e2e9c0cSopenharmony_ci } 3357e2e9c0cSopenharmony_ci } 3367e2e9c0cSopenharmony_ci} 3377e2e9c0cSopenharmony_ci 3387e2e9c0cSopenharmony_ciimpl<'de, E> de::Deserializer<'de> for U32Deserializer<E> 3397e2e9c0cSopenharmony_ciwhere 3407e2e9c0cSopenharmony_ci E: de::Error, 3417e2e9c0cSopenharmony_ci{ 3427e2e9c0cSopenharmony_ci type Error = E; 3437e2e9c0cSopenharmony_ci 3447e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 3457e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 3467e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 3477e2e9c0cSopenharmony_ci tuple_struct map struct identifier ignored_any 3487e2e9c0cSopenharmony_ci } 3497e2e9c0cSopenharmony_ci 3507e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 3517e2e9c0cSopenharmony_ci where 3527e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 3537e2e9c0cSopenharmony_ci { 3547e2e9c0cSopenharmony_ci visitor.visit_u32(self.value) 3557e2e9c0cSopenharmony_ci } 3567e2e9c0cSopenharmony_ci 3577e2e9c0cSopenharmony_ci fn deserialize_enum<V>( 3587e2e9c0cSopenharmony_ci self, 3597e2e9c0cSopenharmony_ci name: &str, 3607e2e9c0cSopenharmony_ci variants: &'static [&'static str], 3617e2e9c0cSopenharmony_ci visitor: V, 3627e2e9c0cSopenharmony_ci ) -> Result<V::Value, Self::Error> 3637e2e9c0cSopenharmony_ci where 3647e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 3657e2e9c0cSopenharmony_ci { 3667e2e9c0cSopenharmony_ci let _ = name; 3677e2e9c0cSopenharmony_ci let _ = variants; 3687e2e9c0cSopenharmony_ci visitor.visit_enum(self) 3697e2e9c0cSopenharmony_ci } 3707e2e9c0cSopenharmony_ci} 3717e2e9c0cSopenharmony_ci 3727e2e9c0cSopenharmony_ciimpl<'de, E> de::EnumAccess<'de> for U32Deserializer<E> 3737e2e9c0cSopenharmony_ciwhere 3747e2e9c0cSopenharmony_ci E: de::Error, 3757e2e9c0cSopenharmony_ci{ 3767e2e9c0cSopenharmony_ci type Error = E; 3777e2e9c0cSopenharmony_ci type Variant = private::UnitOnly<E>; 3787e2e9c0cSopenharmony_ci 3797e2e9c0cSopenharmony_ci fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> 3807e2e9c0cSopenharmony_ci where 3817e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 3827e2e9c0cSopenharmony_ci { 3837e2e9c0cSopenharmony_ci seed.deserialize(self).map(private::unit_only) 3847e2e9c0cSopenharmony_ci } 3857e2e9c0cSopenharmony_ci} 3867e2e9c0cSopenharmony_ci 3877e2e9c0cSopenharmony_ciimpl<E> Debug for U32Deserializer<E> { 3887e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 3897e2e9c0cSopenharmony_ci formatter 3907e2e9c0cSopenharmony_ci .debug_struct("U32Deserializer") 3917e2e9c0cSopenharmony_ci .field("value", &self.value) 3927e2e9c0cSopenharmony_ci .finish() 3937e2e9c0cSopenharmony_ci } 3947e2e9c0cSopenharmony_ci} 3957e2e9c0cSopenharmony_ci 3967e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 3977e2e9c0cSopenharmony_ci 3987e2e9c0cSopenharmony_ci/// A deserializer holding a `&str`. 3997e2e9c0cSopenharmony_cipub struct StrDeserializer<'a, E> { 4007e2e9c0cSopenharmony_ci value: &'a str, 4017e2e9c0cSopenharmony_ci marker: PhantomData<E>, 4027e2e9c0cSopenharmony_ci} 4037e2e9c0cSopenharmony_ci 4047e2e9c0cSopenharmony_ciimpl_copy_clone!(StrDeserializer<'de>); 4057e2e9c0cSopenharmony_ci 4067e2e9c0cSopenharmony_ciimpl<'de, 'a, E> IntoDeserializer<'de, E> for &'a str 4077e2e9c0cSopenharmony_ciwhere 4087e2e9c0cSopenharmony_ci E: de::Error, 4097e2e9c0cSopenharmony_ci{ 4107e2e9c0cSopenharmony_ci type Deserializer = StrDeserializer<'a, E>; 4117e2e9c0cSopenharmony_ci 4127e2e9c0cSopenharmony_ci fn into_deserializer(self) -> StrDeserializer<'a, E> { 4137e2e9c0cSopenharmony_ci StrDeserializer::new(self) 4147e2e9c0cSopenharmony_ci } 4157e2e9c0cSopenharmony_ci} 4167e2e9c0cSopenharmony_ci 4177e2e9c0cSopenharmony_ciimpl<'a, E> StrDeserializer<'a, E> { 4187e2e9c0cSopenharmony_ci #[allow(missing_docs)] 4197e2e9c0cSopenharmony_ci pub fn new(value: &'a str) -> Self { 4207e2e9c0cSopenharmony_ci StrDeserializer { 4217e2e9c0cSopenharmony_ci value, 4227e2e9c0cSopenharmony_ci marker: PhantomData, 4237e2e9c0cSopenharmony_ci } 4247e2e9c0cSopenharmony_ci } 4257e2e9c0cSopenharmony_ci} 4267e2e9c0cSopenharmony_ci 4277e2e9c0cSopenharmony_ciimpl<'de, 'a, E> de::Deserializer<'de> for StrDeserializer<'a, E> 4287e2e9c0cSopenharmony_ciwhere 4297e2e9c0cSopenharmony_ci E: de::Error, 4307e2e9c0cSopenharmony_ci{ 4317e2e9c0cSopenharmony_ci type Error = E; 4327e2e9c0cSopenharmony_ci 4337e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 4347e2e9c0cSopenharmony_ci where 4357e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 4367e2e9c0cSopenharmony_ci { 4377e2e9c0cSopenharmony_ci visitor.visit_str(self.value) 4387e2e9c0cSopenharmony_ci } 4397e2e9c0cSopenharmony_ci 4407e2e9c0cSopenharmony_ci fn deserialize_enum<V>( 4417e2e9c0cSopenharmony_ci self, 4427e2e9c0cSopenharmony_ci name: &str, 4437e2e9c0cSopenharmony_ci variants: &'static [&'static str], 4447e2e9c0cSopenharmony_ci visitor: V, 4457e2e9c0cSopenharmony_ci ) -> Result<V::Value, Self::Error> 4467e2e9c0cSopenharmony_ci where 4477e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 4487e2e9c0cSopenharmony_ci { 4497e2e9c0cSopenharmony_ci let _ = name; 4507e2e9c0cSopenharmony_ci let _ = variants; 4517e2e9c0cSopenharmony_ci visitor.visit_enum(self) 4527e2e9c0cSopenharmony_ci } 4537e2e9c0cSopenharmony_ci 4547e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 4557e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 4567e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 4577e2e9c0cSopenharmony_ci tuple_struct map struct identifier ignored_any 4587e2e9c0cSopenharmony_ci } 4597e2e9c0cSopenharmony_ci} 4607e2e9c0cSopenharmony_ci 4617e2e9c0cSopenharmony_ciimpl<'de, 'a, E> de::EnumAccess<'de> for StrDeserializer<'a, E> 4627e2e9c0cSopenharmony_ciwhere 4637e2e9c0cSopenharmony_ci E: de::Error, 4647e2e9c0cSopenharmony_ci{ 4657e2e9c0cSopenharmony_ci type Error = E; 4667e2e9c0cSopenharmony_ci type Variant = private::UnitOnly<E>; 4677e2e9c0cSopenharmony_ci 4687e2e9c0cSopenharmony_ci fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> 4697e2e9c0cSopenharmony_ci where 4707e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 4717e2e9c0cSopenharmony_ci { 4727e2e9c0cSopenharmony_ci seed.deserialize(self).map(private::unit_only) 4737e2e9c0cSopenharmony_ci } 4747e2e9c0cSopenharmony_ci} 4757e2e9c0cSopenharmony_ci 4767e2e9c0cSopenharmony_ciimpl<'a, E> Debug for StrDeserializer<'a, E> { 4777e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 4787e2e9c0cSopenharmony_ci formatter 4797e2e9c0cSopenharmony_ci .debug_struct("StrDeserializer") 4807e2e9c0cSopenharmony_ci .field("value", &self.value) 4817e2e9c0cSopenharmony_ci .finish() 4827e2e9c0cSopenharmony_ci } 4837e2e9c0cSopenharmony_ci} 4847e2e9c0cSopenharmony_ci 4857e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 4867e2e9c0cSopenharmony_ci 4877e2e9c0cSopenharmony_ci/// A deserializer holding a `&str` with a lifetime tied to another 4887e2e9c0cSopenharmony_ci/// deserializer. 4897e2e9c0cSopenharmony_cipub struct BorrowedStrDeserializer<'de, E> { 4907e2e9c0cSopenharmony_ci value: &'de str, 4917e2e9c0cSopenharmony_ci marker: PhantomData<E>, 4927e2e9c0cSopenharmony_ci} 4937e2e9c0cSopenharmony_ci 4947e2e9c0cSopenharmony_ciimpl_copy_clone!(BorrowedStrDeserializer<'de>); 4957e2e9c0cSopenharmony_ci 4967e2e9c0cSopenharmony_ciimpl<'de, E> BorrowedStrDeserializer<'de, E> { 4977e2e9c0cSopenharmony_ci /// Create a new borrowed deserializer from the given string. 4987e2e9c0cSopenharmony_ci pub fn new(value: &'de str) -> BorrowedStrDeserializer<'de, E> { 4997e2e9c0cSopenharmony_ci BorrowedStrDeserializer { 5007e2e9c0cSopenharmony_ci value, 5017e2e9c0cSopenharmony_ci marker: PhantomData, 5027e2e9c0cSopenharmony_ci } 5037e2e9c0cSopenharmony_ci } 5047e2e9c0cSopenharmony_ci} 5057e2e9c0cSopenharmony_ci 5067e2e9c0cSopenharmony_ciimpl<'de, E> de::Deserializer<'de> for BorrowedStrDeserializer<'de, E> 5077e2e9c0cSopenharmony_ciwhere 5087e2e9c0cSopenharmony_ci E: de::Error, 5097e2e9c0cSopenharmony_ci{ 5107e2e9c0cSopenharmony_ci type Error = E; 5117e2e9c0cSopenharmony_ci 5127e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 5137e2e9c0cSopenharmony_ci where 5147e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 5157e2e9c0cSopenharmony_ci { 5167e2e9c0cSopenharmony_ci visitor.visit_borrowed_str(self.value) 5177e2e9c0cSopenharmony_ci } 5187e2e9c0cSopenharmony_ci 5197e2e9c0cSopenharmony_ci fn deserialize_enum<V>( 5207e2e9c0cSopenharmony_ci self, 5217e2e9c0cSopenharmony_ci name: &str, 5227e2e9c0cSopenharmony_ci variants: &'static [&'static str], 5237e2e9c0cSopenharmony_ci visitor: V, 5247e2e9c0cSopenharmony_ci ) -> Result<V::Value, Self::Error> 5257e2e9c0cSopenharmony_ci where 5267e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 5277e2e9c0cSopenharmony_ci { 5287e2e9c0cSopenharmony_ci let _ = name; 5297e2e9c0cSopenharmony_ci let _ = variants; 5307e2e9c0cSopenharmony_ci visitor.visit_enum(self) 5317e2e9c0cSopenharmony_ci } 5327e2e9c0cSopenharmony_ci 5337e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 5347e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 5357e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 5367e2e9c0cSopenharmony_ci tuple_struct map struct identifier ignored_any 5377e2e9c0cSopenharmony_ci } 5387e2e9c0cSopenharmony_ci} 5397e2e9c0cSopenharmony_ci 5407e2e9c0cSopenharmony_ciimpl<'de, E> de::EnumAccess<'de> for BorrowedStrDeserializer<'de, E> 5417e2e9c0cSopenharmony_ciwhere 5427e2e9c0cSopenharmony_ci E: de::Error, 5437e2e9c0cSopenharmony_ci{ 5447e2e9c0cSopenharmony_ci type Error = E; 5457e2e9c0cSopenharmony_ci type Variant = private::UnitOnly<E>; 5467e2e9c0cSopenharmony_ci 5477e2e9c0cSopenharmony_ci fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> 5487e2e9c0cSopenharmony_ci where 5497e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 5507e2e9c0cSopenharmony_ci { 5517e2e9c0cSopenharmony_ci seed.deserialize(self).map(private::unit_only) 5527e2e9c0cSopenharmony_ci } 5537e2e9c0cSopenharmony_ci} 5547e2e9c0cSopenharmony_ci 5557e2e9c0cSopenharmony_ciimpl<'de, E> Debug for BorrowedStrDeserializer<'de, E> { 5567e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 5577e2e9c0cSopenharmony_ci formatter 5587e2e9c0cSopenharmony_ci .debug_struct("BorrowedStrDeserializer") 5597e2e9c0cSopenharmony_ci .field("value", &self.value) 5607e2e9c0cSopenharmony_ci .finish() 5617e2e9c0cSopenharmony_ci } 5627e2e9c0cSopenharmony_ci} 5637e2e9c0cSopenharmony_ci 5647e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 5657e2e9c0cSopenharmony_ci 5667e2e9c0cSopenharmony_ci/// A deserializer holding a `String`. 5677e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 5687e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] 5697e2e9c0cSopenharmony_cipub struct StringDeserializer<E> { 5707e2e9c0cSopenharmony_ci value: String, 5717e2e9c0cSopenharmony_ci marker: PhantomData<E>, 5727e2e9c0cSopenharmony_ci} 5737e2e9c0cSopenharmony_ci 5747e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 5757e2e9c0cSopenharmony_ciimpl<E> Clone for StringDeserializer<E> { 5767e2e9c0cSopenharmony_ci fn clone(&self) -> Self { 5777e2e9c0cSopenharmony_ci StringDeserializer { 5787e2e9c0cSopenharmony_ci value: self.value.clone(), 5797e2e9c0cSopenharmony_ci marker: PhantomData, 5807e2e9c0cSopenharmony_ci } 5817e2e9c0cSopenharmony_ci } 5827e2e9c0cSopenharmony_ci} 5837e2e9c0cSopenharmony_ci 5847e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 5857e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] 5867e2e9c0cSopenharmony_ciimpl<'de, E> IntoDeserializer<'de, E> for String 5877e2e9c0cSopenharmony_ciwhere 5887e2e9c0cSopenharmony_ci E: de::Error, 5897e2e9c0cSopenharmony_ci{ 5907e2e9c0cSopenharmony_ci type Deserializer = StringDeserializer<E>; 5917e2e9c0cSopenharmony_ci 5927e2e9c0cSopenharmony_ci fn into_deserializer(self) -> StringDeserializer<E> { 5937e2e9c0cSopenharmony_ci StringDeserializer::new(self) 5947e2e9c0cSopenharmony_ci } 5957e2e9c0cSopenharmony_ci} 5967e2e9c0cSopenharmony_ci 5977e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 5987e2e9c0cSopenharmony_ciimpl<E> StringDeserializer<E> { 5997e2e9c0cSopenharmony_ci #[allow(missing_docs)] 6007e2e9c0cSopenharmony_ci pub fn new(value: String) -> Self { 6017e2e9c0cSopenharmony_ci StringDeserializer { 6027e2e9c0cSopenharmony_ci value, 6037e2e9c0cSopenharmony_ci marker: PhantomData, 6047e2e9c0cSopenharmony_ci } 6057e2e9c0cSopenharmony_ci } 6067e2e9c0cSopenharmony_ci} 6077e2e9c0cSopenharmony_ci 6087e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 6097e2e9c0cSopenharmony_ciimpl<'de, E> de::Deserializer<'de> for StringDeserializer<E> 6107e2e9c0cSopenharmony_ciwhere 6117e2e9c0cSopenharmony_ci E: de::Error, 6127e2e9c0cSopenharmony_ci{ 6137e2e9c0cSopenharmony_ci type Error = E; 6147e2e9c0cSopenharmony_ci 6157e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 6167e2e9c0cSopenharmony_ci where 6177e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 6187e2e9c0cSopenharmony_ci { 6197e2e9c0cSopenharmony_ci visitor.visit_string(self.value) 6207e2e9c0cSopenharmony_ci } 6217e2e9c0cSopenharmony_ci 6227e2e9c0cSopenharmony_ci fn deserialize_enum<V>( 6237e2e9c0cSopenharmony_ci self, 6247e2e9c0cSopenharmony_ci name: &str, 6257e2e9c0cSopenharmony_ci variants: &'static [&'static str], 6267e2e9c0cSopenharmony_ci visitor: V, 6277e2e9c0cSopenharmony_ci ) -> Result<V::Value, Self::Error> 6287e2e9c0cSopenharmony_ci where 6297e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 6307e2e9c0cSopenharmony_ci { 6317e2e9c0cSopenharmony_ci let _ = name; 6327e2e9c0cSopenharmony_ci let _ = variants; 6337e2e9c0cSopenharmony_ci visitor.visit_enum(self) 6347e2e9c0cSopenharmony_ci } 6357e2e9c0cSopenharmony_ci 6367e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 6377e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 6387e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 6397e2e9c0cSopenharmony_ci tuple_struct map struct identifier ignored_any 6407e2e9c0cSopenharmony_ci } 6417e2e9c0cSopenharmony_ci} 6427e2e9c0cSopenharmony_ci 6437e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 6447e2e9c0cSopenharmony_ciimpl<'de, E> de::EnumAccess<'de> for StringDeserializer<E> 6457e2e9c0cSopenharmony_ciwhere 6467e2e9c0cSopenharmony_ci E: de::Error, 6477e2e9c0cSopenharmony_ci{ 6487e2e9c0cSopenharmony_ci type Error = E; 6497e2e9c0cSopenharmony_ci type Variant = private::UnitOnly<E>; 6507e2e9c0cSopenharmony_ci 6517e2e9c0cSopenharmony_ci fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> 6527e2e9c0cSopenharmony_ci where 6537e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 6547e2e9c0cSopenharmony_ci { 6557e2e9c0cSopenharmony_ci seed.deserialize(self).map(private::unit_only) 6567e2e9c0cSopenharmony_ci } 6577e2e9c0cSopenharmony_ci} 6587e2e9c0cSopenharmony_ci 6597e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 6607e2e9c0cSopenharmony_ciimpl<E> Debug for StringDeserializer<E> { 6617e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 6627e2e9c0cSopenharmony_ci formatter 6637e2e9c0cSopenharmony_ci .debug_struct("StringDeserializer") 6647e2e9c0cSopenharmony_ci .field("value", &self.value) 6657e2e9c0cSopenharmony_ci .finish() 6667e2e9c0cSopenharmony_ci } 6677e2e9c0cSopenharmony_ci} 6687e2e9c0cSopenharmony_ci 6697e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 6707e2e9c0cSopenharmony_ci 6717e2e9c0cSopenharmony_ci/// A deserializer holding a `Cow<str>`. 6727e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 6737e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] 6747e2e9c0cSopenharmony_cipub struct CowStrDeserializer<'a, E> { 6757e2e9c0cSopenharmony_ci value: Cow<'a, str>, 6767e2e9c0cSopenharmony_ci marker: PhantomData<E>, 6777e2e9c0cSopenharmony_ci} 6787e2e9c0cSopenharmony_ci 6797e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 6807e2e9c0cSopenharmony_ciimpl<'a, E> Clone for CowStrDeserializer<'a, E> { 6817e2e9c0cSopenharmony_ci fn clone(&self) -> Self { 6827e2e9c0cSopenharmony_ci CowStrDeserializer { 6837e2e9c0cSopenharmony_ci value: self.value.clone(), 6847e2e9c0cSopenharmony_ci marker: PhantomData, 6857e2e9c0cSopenharmony_ci } 6867e2e9c0cSopenharmony_ci } 6877e2e9c0cSopenharmony_ci} 6887e2e9c0cSopenharmony_ci 6897e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 6907e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] 6917e2e9c0cSopenharmony_ciimpl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str> 6927e2e9c0cSopenharmony_ciwhere 6937e2e9c0cSopenharmony_ci E: de::Error, 6947e2e9c0cSopenharmony_ci{ 6957e2e9c0cSopenharmony_ci type Deserializer = CowStrDeserializer<'a, E>; 6967e2e9c0cSopenharmony_ci 6977e2e9c0cSopenharmony_ci fn into_deserializer(self) -> CowStrDeserializer<'a, E> { 6987e2e9c0cSopenharmony_ci CowStrDeserializer::new(self) 6997e2e9c0cSopenharmony_ci } 7007e2e9c0cSopenharmony_ci} 7017e2e9c0cSopenharmony_ci 7027e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 7037e2e9c0cSopenharmony_ciimpl<'a, E> CowStrDeserializer<'a, E> { 7047e2e9c0cSopenharmony_ci #[allow(missing_docs)] 7057e2e9c0cSopenharmony_ci pub fn new(value: Cow<'a, str>) -> Self { 7067e2e9c0cSopenharmony_ci CowStrDeserializer { 7077e2e9c0cSopenharmony_ci value, 7087e2e9c0cSopenharmony_ci marker: PhantomData, 7097e2e9c0cSopenharmony_ci } 7107e2e9c0cSopenharmony_ci } 7117e2e9c0cSopenharmony_ci} 7127e2e9c0cSopenharmony_ci 7137e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 7147e2e9c0cSopenharmony_ciimpl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E> 7157e2e9c0cSopenharmony_ciwhere 7167e2e9c0cSopenharmony_ci E: de::Error, 7177e2e9c0cSopenharmony_ci{ 7187e2e9c0cSopenharmony_ci type Error = E; 7197e2e9c0cSopenharmony_ci 7207e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 7217e2e9c0cSopenharmony_ci where 7227e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 7237e2e9c0cSopenharmony_ci { 7247e2e9c0cSopenharmony_ci match self.value { 7257e2e9c0cSopenharmony_ci Cow::Borrowed(string) => visitor.visit_str(string), 7267e2e9c0cSopenharmony_ci Cow::Owned(string) => visitor.visit_string(string), 7277e2e9c0cSopenharmony_ci } 7287e2e9c0cSopenharmony_ci } 7297e2e9c0cSopenharmony_ci 7307e2e9c0cSopenharmony_ci fn deserialize_enum<V>( 7317e2e9c0cSopenharmony_ci self, 7327e2e9c0cSopenharmony_ci name: &str, 7337e2e9c0cSopenharmony_ci variants: &'static [&'static str], 7347e2e9c0cSopenharmony_ci visitor: V, 7357e2e9c0cSopenharmony_ci ) -> Result<V::Value, Self::Error> 7367e2e9c0cSopenharmony_ci where 7377e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 7387e2e9c0cSopenharmony_ci { 7397e2e9c0cSopenharmony_ci let _ = name; 7407e2e9c0cSopenharmony_ci let _ = variants; 7417e2e9c0cSopenharmony_ci visitor.visit_enum(self) 7427e2e9c0cSopenharmony_ci } 7437e2e9c0cSopenharmony_ci 7447e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 7457e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 7467e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 7477e2e9c0cSopenharmony_ci tuple_struct map struct identifier ignored_any 7487e2e9c0cSopenharmony_ci } 7497e2e9c0cSopenharmony_ci} 7507e2e9c0cSopenharmony_ci 7517e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 7527e2e9c0cSopenharmony_ciimpl<'de, 'a, E> de::EnumAccess<'de> for CowStrDeserializer<'a, E> 7537e2e9c0cSopenharmony_ciwhere 7547e2e9c0cSopenharmony_ci E: de::Error, 7557e2e9c0cSopenharmony_ci{ 7567e2e9c0cSopenharmony_ci type Error = E; 7577e2e9c0cSopenharmony_ci type Variant = private::UnitOnly<E>; 7587e2e9c0cSopenharmony_ci 7597e2e9c0cSopenharmony_ci fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> 7607e2e9c0cSopenharmony_ci where 7617e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 7627e2e9c0cSopenharmony_ci { 7637e2e9c0cSopenharmony_ci seed.deserialize(self).map(private::unit_only) 7647e2e9c0cSopenharmony_ci } 7657e2e9c0cSopenharmony_ci} 7667e2e9c0cSopenharmony_ci 7677e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 7687e2e9c0cSopenharmony_ciimpl<'a, E> Debug for CowStrDeserializer<'a, E> { 7697e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 7707e2e9c0cSopenharmony_ci formatter 7717e2e9c0cSopenharmony_ci .debug_struct("CowStrDeserializer") 7727e2e9c0cSopenharmony_ci .field("value", &self.value) 7737e2e9c0cSopenharmony_ci .finish() 7747e2e9c0cSopenharmony_ci } 7757e2e9c0cSopenharmony_ci} 7767e2e9c0cSopenharmony_ci 7777e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 7787e2e9c0cSopenharmony_ci 7797e2e9c0cSopenharmony_ci/// A deserializer holding a `&[u8]`. Always calls [`Visitor::visit_bytes`]. 7807e2e9c0cSopenharmony_cipub struct BytesDeserializer<'a, E> { 7817e2e9c0cSopenharmony_ci value: &'a [u8], 7827e2e9c0cSopenharmony_ci marker: PhantomData<E>, 7837e2e9c0cSopenharmony_ci} 7847e2e9c0cSopenharmony_ci 7857e2e9c0cSopenharmony_ciimpl<'a, E> BytesDeserializer<'a, E> { 7867e2e9c0cSopenharmony_ci /// Create a new deserializer from the given bytes. 7877e2e9c0cSopenharmony_ci pub fn new(value: &'a [u8]) -> Self { 7887e2e9c0cSopenharmony_ci BytesDeserializer { 7897e2e9c0cSopenharmony_ci value, 7907e2e9c0cSopenharmony_ci marker: PhantomData, 7917e2e9c0cSopenharmony_ci } 7927e2e9c0cSopenharmony_ci } 7937e2e9c0cSopenharmony_ci} 7947e2e9c0cSopenharmony_ci 7957e2e9c0cSopenharmony_ciimpl_copy_clone!(BytesDeserializer<'a>); 7967e2e9c0cSopenharmony_ci 7977e2e9c0cSopenharmony_ciimpl<'de, 'a, E> IntoDeserializer<'de, E> for &'a [u8] 7987e2e9c0cSopenharmony_ciwhere 7997e2e9c0cSopenharmony_ci E: de::Error, 8007e2e9c0cSopenharmony_ci{ 8017e2e9c0cSopenharmony_ci type Deserializer = BytesDeserializer<'a, E>; 8027e2e9c0cSopenharmony_ci 8037e2e9c0cSopenharmony_ci fn into_deserializer(self) -> BytesDeserializer<'a, E> { 8047e2e9c0cSopenharmony_ci BytesDeserializer::new(self) 8057e2e9c0cSopenharmony_ci } 8067e2e9c0cSopenharmony_ci} 8077e2e9c0cSopenharmony_ci 8087e2e9c0cSopenharmony_ciimpl<'de, 'a, E> Deserializer<'de> for BytesDeserializer<'a, E> 8097e2e9c0cSopenharmony_ciwhere 8107e2e9c0cSopenharmony_ci E: de::Error, 8117e2e9c0cSopenharmony_ci{ 8127e2e9c0cSopenharmony_ci type Error = E; 8137e2e9c0cSopenharmony_ci 8147e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 8157e2e9c0cSopenharmony_ci where 8167e2e9c0cSopenharmony_ci V: Visitor<'de>, 8177e2e9c0cSopenharmony_ci { 8187e2e9c0cSopenharmony_ci visitor.visit_bytes(self.value) 8197e2e9c0cSopenharmony_ci } 8207e2e9c0cSopenharmony_ci 8217e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 8227e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 8237e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 8247e2e9c0cSopenharmony_ci tuple_struct map struct enum identifier ignored_any 8257e2e9c0cSopenharmony_ci } 8267e2e9c0cSopenharmony_ci} 8277e2e9c0cSopenharmony_ci 8287e2e9c0cSopenharmony_ciimpl<'a, E> Debug for BytesDeserializer<'a, E> { 8297e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 8307e2e9c0cSopenharmony_ci formatter 8317e2e9c0cSopenharmony_ci .debug_struct("BytesDeserializer") 8327e2e9c0cSopenharmony_ci .field("value", &self.value) 8337e2e9c0cSopenharmony_ci .finish() 8347e2e9c0cSopenharmony_ci } 8357e2e9c0cSopenharmony_ci} 8367e2e9c0cSopenharmony_ci 8377e2e9c0cSopenharmony_ci/// A deserializer holding a `&[u8]` with a lifetime tied to another 8387e2e9c0cSopenharmony_ci/// deserializer. Always calls [`Visitor::visit_borrowed_bytes`]. 8397e2e9c0cSopenharmony_cipub struct BorrowedBytesDeserializer<'de, E> { 8407e2e9c0cSopenharmony_ci value: &'de [u8], 8417e2e9c0cSopenharmony_ci marker: PhantomData<E>, 8427e2e9c0cSopenharmony_ci} 8437e2e9c0cSopenharmony_ci 8447e2e9c0cSopenharmony_ciimpl<'de, E> BorrowedBytesDeserializer<'de, E> { 8457e2e9c0cSopenharmony_ci /// Create a new borrowed deserializer from the given borrowed bytes. 8467e2e9c0cSopenharmony_ci pub fn new(value: &'de [u8]) -> Self { 8477e2e9c0cSopenharmony_ci BorrowedBytesDeserializer { 8487e2e9c0cSopenharmony_ci value, 8497e2e9c0cSopenharmony_ci marker: PhantomData, 8507e2e9c0cSopenharmony_ci } 8517e2e9c0cSopenharmony_ci } 8527e2e9c0cSopenharmony_ci} 8537e2e9c0cSopenharmony_ci 8547e2e9c0cSopenharmony_ciimpl_copy_clone!(BorrowedBytesDeserializer<'de>); 8557e2e9c0cSopenharmony_ci 8567e2e9c0cSopenharmony_ciimpl<'de, E> Deserializer<'de> for BorrowedBytesDeserializer<'de, E> 8577e2e9c0cSopenharmony_ciwhere 8587e2e9c0cSopenharmony_ci E: de::Error, 8597e2e9c0cSopenharmony_ci{ 8607e2e9c0cSopenharmony_ci type Error = E; 8617e2e9c0cSopenharmony_ci 8627e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 8637e2e9c0cSopenharmony_ci where 8647e2e9c0cSopenharmony_ci V: Visitor<'de>, 8657e2e9c0cSopenharmony_ci { 8667e2e9c0cSopenharmony_ci visitor.visit_borrowed_bytes(self.value) 8677e2e9c0cSopenharmony_ci } 8687e2e9c0cSopenharmony_ci 8697e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 8707e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 8717e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 8727e2e9c0cSopenharmony_ci tuple_struct map struct enum identifier ignored_any 8737e2e9c0cSopenharmony_ci } 8747e2e9c0cSopenharmony_ci} 8757e2e9c0cSopenharmony_ci 8767e2e9c0cSopenharmony_ciimpl<'de, E> Debug for BorrowedBytesDeserializer<'de, E> { 8777e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 8787e2e9c0cSopenharmony_ci formatter 8797e2e9c0cSopenharmony_ci .debug_struct("BorrowedBytesDeserializer") 8807e2e9c0cSopenharmony_ci .field("value", &self.value) 8817e2e9c0cSopenharmony_ci .finish() 8827e2e9c0cSopenharmony_ci } 8837e2e9c0cSopenharmony_ci} 8847e2e9c0cSopenharmony_ci 8857e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 8867e2e9c0cSopenharmony_ci 8877e2e9c0cSopenharmony_ci/// A deserializer that iterates over a sequence. 8887e2e9c0cSopenharmony_ci#[derive(Clone)] 8897e2e9c0cSopenharmony_cipub struct SeqDeserializer<I, E> { 8907e2e9c0cSopenharmony_ci iter: iter::Fuse<I>, 8917e2e9c0cSopenharmony_ci count: usize, 8927e2e9c0cSopenharmony_ci marker: PhantomData<E>, 8937e2e9c0cSopenharmony_ci} 8947e2e9c0cSopenharmony_ci 8957e2e9c0cSopenharmony_ciimpl<I, E> SeqDeserializer<I, E> 8967e2e9c0cSopenharmony_ciwhere 8977e2e9c0cSopenharmony_ci I: Iterator, 8987e2e9c0cSopenharmony_ci{ 8997e2e9c0cSopenharmony_ci /// Construct a new `SeqDeserializer<I, E>`. 9007e2e9c0cSopenharmony_ci pub fn new(iter: I) -> Self { 9017e2e9c0cSopenharmony_ci SeqDeserializer { 9027e2e9c0cSopenharmony_ci iter: iter.fuse(), 9037e2e9c0cSopenharmony_ci count: 0, 9047e2e9c0cSopenharmony_ci marker: PhantomData, 9057e2e9c0cSopenharmony_ci } 9067e2e9c0cSopenharmony_ci } 9077e2e9c0cSopenharmony_ci} 9087e2e9c0cSopenharmony_ci 9097e2e9c0cSopenharmony_ciimpl<I, E> SeqDeserializer<I, E> 9107e2e9c0cSopenharmony_ciwhere 9117e2e9c0cSopenharmony_ci I: Iterator, 9127e2e9c0cSopenharmony_ci E: de::Error, 9137e2e9c0cSopenharmony_ci{ 9147e2e9c0cSopenharmony_ci /// Check for remaining elements after passing a `SeqDeserializer` to 9157e2e9c0cSopenharmony_ci /// `Visitor::visit_seq`. 9167e2e9c0cSopenharmony_ci pub fn end(self) -> Result<(), E> { 9177e2e9c0cSopenharmony_ci let remaining = self.iter.count(); 9187e2e9c0cSopenharmony_ci if remaining == 0 { 9197e2e9c0cSopenharmony_ci Ok(()) 9207e2e9c0cSopenharmony_ci } else { 9217e2e9c0cSopenharmony_ci // First argument is the number of elements in the data, second 9227e2e9c0cSopenharmony_ci // argument is the number of elements expected by the Deserialize. 9237e2e9c0cSopenharmony_ci Err(de::Error::invalid_length( 9247e2e9c0cSopenharmony_ci self.count + remaining, 9257e2e9c0cSopenharmony_ci &ExpectedInSeq(self.count), 9267e2e9c0cSopenharmony_ci )) 9277e2e9c0cSopenharmony_ci } 9287e2e9c0cSopenharmony_ci } 9297e2e9c0cSopenharmony_ci} 9307e2e9c0cSopenharmony_ci 9317e2e9c0cSopenharmony_ciimpl<'de, I, T, E> de::Deserializer<'de> for SeqDeserializer<I, E> 9327e2e9c0cSopenharmony_ciwhere 9337e2e9c0cSopenharmony_ci I: Iterator<Item = T>, 9347e2e9c0cSopenharmony_ci T: IntoDeserializer<'de, E>, 9357e2e9c0cSopenharmony_ci E: de::Error, 9367e2e9c0cSopenharmony_ci{ 9377e2e9c0cSopenharmony_ci type Error = E; 9387e2e9c0cSopenharmony_ci 9397e2e9c0cSopenharmony_ci fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> 9407e2e9c0cSopenharmony_ci where 9417e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 9427e2e9c0cSopenharmony_ci { 9437e2e9c0cSopenharmony_ci let v = tri!(visitor.visit_seq(&mut self)); 9447e2e9c0cSopenharmony_ci tri!(self.end()); 9457e2e9c0cSopenharmony_ci Ok(v) 9467e2e9c0cSopenharmony_ci } 9477e2e9c0cSopenharmony_ci 9487e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 9497e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 9507e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 9517e2e9c0cSopenharmony_ci tuple_struct map struct enum identifier ignored_any 9527e2e9c0cSopenharmony_ci } 9537e2e9c0cSopenharmony_ci} 9547e2e9c0cSopenharmony_ci 9557e2e9c0cSopenharmony_ciimpl<'de, I, T, E> de::SeqAccess<'de> for SeqDeserializer<I, E> 9567e2e9c0cSopenharmony_ciwhere 9577e2e9c0cSopenharmony_ci I: Iterator<Item = T>, 9587e2e9c0cSopenharmony_ci T: IntoDeserializer<'de, E>, 9597e2e9c0cSopenharmony_ci E: de::Error, 9607e2e9c0cSopenharmony_ci{ 9617e2e9c0cSopenharmony_ci type Error = E; 9627e2e9c0cSopenharmony_ci 9637e2e9c0cSopenharmony_ci fn next_element_seed<V>(&mut self, seed: V) -> Result<Option<V::Value>, Self::Error> 9647e2e9c0cSopenharmony_ci where 9657e2e9c0cSopenharmony_ci V: de::DeserializeSeed<'de>, 9667e2e9c0cSopenharmony_ci { 9677e2e9c0cSopenharmony_ci match self.iter.next() { 9687e2e9c0cSopenharmony_ci Some(value) => { 9697e2e9c0cSopenharmony_ci self.count += 1; 9707e2e9c0cSopenharmony_ci seed.deserialize(value.into_deserializer()).map(Some) 9717e2e9c0cSopenharmony_ci } 9727e2e9c0cSopenharmony_ci None => Ok(None), 9737e2e9c0cSopenharmony_ci } 9747e2e9c0cSopenharmony_ci } 9757e2e9c0cSopenharmony_ci 9767e2e9c0cSopenharmony_ci fn size_hint(&self) -> Option<usize> { 9777e2e9c0cSopenharmony_ci size_hint::from_bounds(&self.iter) 9787e2e9c0cSopenharmony_ci } 9797e2e9c0cSopenharmony_ci} 9807e2e9c0cSopenharmony_ci 9817e2e9c0cSopenharmony_cistruct ExpectedInSeq(usize); 9827e2e9c0cSopenharmony_ci 9837e2e9c0cSopenharmony_ciimpl Expected for ExpectedInSeq { 9847e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 9857e2e9c0cSopenharmony_ci if self.0 == 1 { 9867e2e9c0cSopenharmony_ci formatter.write_str("1 element in sequence") 9877e2e9c0cSopenharmony_ci } else { 9887e2e9c0cSopenharmony_ci write!(formatter, "{} elements in sequence", self.0) 9897e2e9c0cSopenharmony_ci } 9907e2e9c0cSopenharmony_ci } 9917e2e9c0cSopenharmony_ci} 9927e2e9c0cSopenharmony_ci 9937e2e9c0cSopenharmony_ciimpl<I, E> Debug for SeqDeserializer<I, E> 9947e2e9c0cSopenharmony_ciwhere 9957e2e9c0cSopenharmony_ci I: Debug, 9967e2e9c0cSopenharmony_ci{ 9977e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 9987e2e9c0cSopenharmony_ci formatter 9997e2e9c0cSopenharmony_ci .debug_struct("SeqDeserializer") 10007e2e9c0cSopenharmony_ci .field("iter", &self.iter) 10017e2e9c0cSopenharmony_ci .field("count", &self.count) 10027e2e9c0cSopenharmony_ci .finish() 10037e2e9c0cSopenharmony_ci } 10047e2e9c0cSopenharmony_ci} 10057e2e9c0cSopenharmony_ci 10067e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 10077e2e9c0cSopenharmony_ci 10087e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 10097e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] 10107e2e9c0cSopenharmony_ciimpl<'de, T, E> IntoDeserializer<'de, E> for Vec<T> 10117e2e9c0cSopenharmony_ciwhere 10127e2e9c0cSopenharmony_ci T: IntoDeserializer<'de, E>, 10137e2e9c0cSopenharmony_ci E: de::Error, 10147e2e9c0cSopenharmony_ci{ 10157e2e9c0cSopenharmony_ci type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>; 10167e2e9c0cSopenharmony_ci 10177e2e9c0cSopenharmony_ci fn into_deserializer(self) -> Self::Deserializer { 10187e2e9c0cSopenharmony_ci SeqDeserializer::new(self.into_iter()) 10197e2e9c0cSopenharmony_ci } 10207e2e9c0cSopenharmony_ci} 10217e2e9c0cSopenharmony_ci 10227e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 10237e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] 10247e2e9c0cSopenharmony_ciimpl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet<T> 10257e2e9c0cSopenharmony_ciwhere 10267e2e9c0cSopenharmony_ci T: IntoDeserializer<'de, E> + Eq + Ord, 10277e2e9c0cSopenharmony_ci E: de::Error, 10287e2e9c0cSopenharmony_ci{ 10297e2e9c0cSopenharmony_ci type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>; 10307e2e9c0cSopenharmony_ci 10317e2e9c0cSopenharmony_ci fn into_deserializer(self) -> Self::Deserializer { 10327e2e9c0cSopenharmony_ci SeqDeserializer::new(self.into_iter()) 10337e2e9c0cSopenharmony_ci } 10347e2e9c0cSopenharmony_ci} 10357e2e9c0cSopenharmony_ci 10367e2e9c0cSopenharmony_ci#[cfg(feature = "std")] 10377e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] 10387e2e9c0cSopenharmony_ciimpl<'de, T, S, E> IntoDeserializer<'de, E> for HashSet<T, S> 10397e2e9c0cSopenharmony_ciwhere 10407e2e9c0cSopenharmony_ci T: IntoDeserializer<'de, E> + Eq + Hash, 10417e2e9c0cSopenharmony_ci S: BuildHasher, 10427e2e9c0cSopenharmony_ci E: de::Error, 10437e2e9c0cSopenharmony_ci{ 10447e2e9c0cSopenharmony_ci type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>; 10457e2e9c0cSopenharmony_ci 10467e2e9c0cSopenharmony_ci fn into_deserializer(self) -> Self::Deserializer { 10477e2e9c0cSopenharmony_ci SeqDeserializer::new(self.into_iter()) 10487e2e9c0cSopenharmony_ci } 10497e2e9c0cSopenharmony_ci} 10507e2e9c0cSopenharmony_ci 10517e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 10527e2e9c0cSopenharmony_ci 10537e2e9c0cSopenharmony_ci/// A deserializer holding a `SeqAccess`. 10547e2e9c0cSopenharmony_ci#[derive(Clone, Debug)] 10557e2e9c0cSopenharmony_cipub struct SeqAccessDeserializer<A> { 10567e2e9c0cSopenharmony_ci seq: A, 10577e2e9c0cSopenharmony_ci} 10587e2e9c0cSopenharmony_ci 10597e2e9c0cSopenharmony_ciimpl<A> SeqAccessDeserializer<A> { 10607e2e9c0cSopenharmony_ci /// Construct a new `SeqAccessDeserializer<A>`. 10617e2e9c0cSopenharmony_ci pub fn new(seq: A) -> Self { 10627e2e9c0cSopenharmony_ci SeqAccessDeserializer { seq } 10637e2e9c0cSopenharmony_ci } 10647e2e9c0cSopenharmony_ci} 10657e2e9c0cSopenharmony_ci 10667e2e9c0cSopenharmony_ciimpl<'de, A> de::Deserializer<'de> for SeqAccessDeserializer<A> 10677e2e9c0cSopenharmony_ciwhere 10687e2e9c0cSopenharmony_ci A: de::SeqAccess<'de>, 10697e2e9c0cSopenharmony_ci{ 10707e2e9c0cSopenharmony_ci type Error = A::Error; 10717e2e9c0cSopenharmony_ci 10727e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 10737e2e9c0cSopenharmony_ci where 10747e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 10757e2e9c0cSopenharmony_ci { 10767e2e9c0cSopenharmony_ci visitor.visit_seq(self.seq) 10777e2e9c0cSopenharmony_ci } 10787e2e9c0cSopenharmony_ci 10797e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 10807e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 10817e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 10827e2e9c0cSopenharmony_ci tuple_struct map struct enum identifier ignored_any 10837e2e9c0cSopenharmony_ci } 10847e2e9c0cSopenharmony_ci} 10857e2e9c0cSopenharmony_ci 10867e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 10877e2e9c0cSopenharmony_ci 10887e2e9c0cSopenharmony_ci/// A deserializer that iterates over a map. 10897e2e9c0cSopenharmony_cipub struct MapDeserializer<'de, I, E> 10907e2e9c0cSopenharmony_ciwhere 10917e2e9c0cSopenharmony_ci I: Iterator, 10927e2e9c0cSopenharmony_ci I::Item: private::Pair, 10937e2e9c0cSopenharmony_ci{ 10947e2e9c0cSopenharmony_ci iter: iter::Fuse<I>, 10957e2e9c0cSopenharmony_ci value: Option<Second<I::Item>>, 10967e2e9c0cSopenharmony_ci count: usize, 10977e2e9c0cSopenharmony_ci lifetime: PhantomData<&'de ()>, 10987e2e9c0cSopenharmony_ci error: PhantomData<E>, 10997e2e9c0cSopenharmony_ci} 11007e2e9c0cSopenharmony_ci 11017e2e9c0cSopenharmony_ciimpl<'de, I, E> MapDeserializer<'de, I, E> 11027e2e9c0cSopenharmony_ciwhere 11037e2e9c0cSopenharmony_ci I: Iterator, 11047e2e9c0cSopenharmony_ci I::Item: private::Pair, 11057e2e9c0cSopenharmony_ci{ 11067e2e9c0cSopenharmony_ci /// Construct a new `MapDeserializer<I, E>`. 11077e2e9c0cSopenharmony_ci pub fn new(iter: I) -> Self { 11087e2e9c0cSopenharmony_ci MapDeserializer { 11097e2e9c0cSopenharmony_ci iter: iter.fuse(), 11107e2e9c0cSopenharmony_ci value: None, 11117e2e9c0cSopenharmony_ci count: 0, 11127e2e9c0cSopenharmony_ci lifetime: PhantomData, 11137e2e9c0cSopenharmony_ci error: PhantomData, 11147e2e9c0cSopenharmony_ci } 11157e2e9c0cSopenharmony_ci } 11167e2e9c0cSopenharmony_ci} 11177e2e9c0cSopenharmony_ci 11187e2e9c0cSopenharmony_ciimpl<'de, I, E> MapDeserializer<'de, I, E> 11197e2e9c0cSopenharmony_ciwhere 11207e2e9c0cSopenharmony_ci I: Iterator, 11217e2e9c0cSopenharmony_ci I::Item: private::Pair, 11227e2e9c0cSopenharmony_ci E: de::Error, 11237e2e9c0cSopenharmony_ci{ 11247e2e9c0cSopenharmony_ci /// Check for remaining elements after passing a `MapDeserializer` to 11257e2e9c0cSopenharmony_ci /// `Visitor::visit_map`. 11267e2e9c0cSopenharmony_ci pub fn end(self) -> Result<(), E> { 11277e2e9c0cSopenharmony_ci let remaining = self.iter.count(); 11287e2e9c0cSopenharmony_ci if remaining == 0 { 11297e2e9c0cSopenharmony_ci Ok(()) 11307e2e9c0cSopenharmony_ci } else { 11317e2e9c0cSopenharmony_ci // First argument is the number of elements in the data, second 11327e2e9c0cSopenharmony_ci // argument is the number of elements expected by the Deserialize. 11337e2e9c0cSopenharmony_ci Err(de::Error::invalid_length( 11347e2e9c0cSopenharmony_ci self.count + remaining, 11357e2e9c0cSopenharmony_ci &ExpectedInMap(self.count), 11367e2e9c0cSopenharmony_ci )) 11377e2e9c0cSopenharmony_ci } 11387e2e9c0cSopenharmony_ci } 11397e2e9c0cSopenharmony_ci} 11407e2e9c0cSopenharmony_ci 11417e2e9c0cSopenharmony_ciimpl<'de, I, E> MapDeserializer<'de, I, E> 11427e2e9c0cSopenharmony_ciwhere 11437e2e9c0cSopenharmony_ci I: Iterator, 11447e2e9c0cSopenharmony_ci I::Item: private::Pair, 11457e2e9c0cSopenharmony_ci{ 11467e2e9c0cSopenharmony_ci fn next_pair(&mut self) -> Option<(First<I::Item>, Second<I::Item>)> { 11477e2e9c0cSopenharmony_ci match self.iter.next() { 11487e2e9c0cSopenharmony_ci Some(kv) => { 11497e2e9c0cSopenharmony_ci self.count += 1; 11507e2e9c0cSopenharmony_ci Some(private::Pair::split(kv)) 11517e2e9c0cSopenharmony_ci } 11527e2e9c0cSopenharmony_ci None => None, 11537e2e9c0cSopenharmony_ci } 11547e2e9c0cSopenharmony_ci } 11557e2e9c0cSopenharmony_ci} 11567e2e9c0cSopenharmony_ci 11577e2e9c0cSopenharmony_ciimpl<'de, I, E> de::Deserializer<'de> for MapDeserializer<'de, I, E> 11587e2e9c0cSopenharmony_ciwhere 11597e2e9c0cSopenharmony_ci I: Iterator, 11607e2e9c0cSopenharmony_ci I::Item: private::Pair, 11617e2e9c0cSopenharmony_ci First<I::Item>: IntoDeserializer<'de, E>, 11627e2e9c0cSopenharmony_ci Second<I::Item>: IntoDeserializer<'de, E>, 11637e2e9c0cSopenharmony_ci E: de::Error, 11647e2e9c0cSopenharmony_ci{ 11657e2e9c0cSopenharmony_ci type Error = E; 11667e2e9c0cSopenharmony_ci 11677e2e9c0cSopenharmony_ci fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> 11687e2e9c0cSopenharmony_ci where 11697e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 11707e2e9c0cSopenharmony_ci { 11717e2e9c0cSopenharmony_ci let value = tri!(visitor.visit_map(&mut self)); 11727e2e9c0cSopenharmony_ci tri!(self.end()); 11737e2e9c0cSopenharmony_ci Ok(value) 11747e2e9c0cSopenharmony_ci } 11757e2e9c0cSopenharmony_ci 11767e2e9c0cSopenharmony_ci fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> 11777e2e9c0cSopenharmony_ci where 11787e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 11797e2e9c0cSopenharmony_ci { 11807e2e9c0cSopenharmony_ci let value = tri!(visitor.visit_seq(&mut self)); 11817e2e9c0cSopenharmony_ci tri!(self.end()); 11827e2e9c0cSopenharmony_ci Ok(value) 11837e2e9c0cSopenharmony_ci } 11847e2e9c0cSopenharmony_ci 11857e2e9c0cSopenharmony_ci fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> 11867e2e9c0cSopenharmony_ci where 11877e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 11887e2e9c0cSopenharmony_ci { 11897e2e9c0cSopenharmony_ci let _ = len; 11907e2e9c0cSopenharmony_ci self.deserialize_seq(visitor) 11917e2e9c0cSopenharmony_ci } 11927e2e9c0cSopenharmony_ci 11937e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 11947e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 11957e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct tuple_struct map 11967e2e9c0cSopenharmony_ci struct enum identifier ignored_any 11977e2e9c0cSopenharmony_ci } 11987e2e9c0cSopenharmony_ci} 11997e2e9c0cSopenharmony_ci 12007e2e9c0cSopenharmony_ciimpl<'de, I, E> de::MapAccess<'de> for MapDeserializer<'de, I, E> 12017e2e9c0cSopenharmony_ciwhere 12027e2e9c0cSopenharmony_ci I: Iterator, 12037e2e9c0cSopenharmony_ci I::Item: private::Pair, 12047e2e9c0cSopenharmony_ci First<I::Item>: IntoDeserializer<'de, E>, 12057e2e9c0cSopenharmony_ci Second<I::Item>: IntoDeserializer<'de, E>, 12067e2e9c0cSopenharmony_ci E: de::Error, 12077e2e9c0cSopenharmony_ci{ 12087e2e9c0cSopenharmony_ci type Error = E; 12097e2e9c0cSopenharmony_ci 12107e2e9c0cSopenharmony_ci fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> 12117e2e9c0cSopenharmony_ci where 12127e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 12137e2e9c0cSopenharmony_ci { 12147e2e9c0cSopenharmony_ci match self.next_pair() { 12157e2e9c0cSopenharmony_ci Some((key, value)) => { 12167e2e9c0cSopenharmony_ci self.value = Some(value); 12177e2e9c0cSopenharmony_ci seed.deserialize(key.into_deserializer()).map(Some) 12187e2e9c0cSopenharmony_ci } 12197e2e9c0cSopenharmony_ci None => Ok(None), 12207e2e9c0cSopenharmony_ci } 12217e2e9c0cSopenharmony_ci } 12227e2e9c0cSopenharmony_ci 12237e2e9c0cSopenharmony_ci fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> 12247e2e9c0cSopenharmony_ci where 12257e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 12267e2e9c0cSopenharmony_ci { 12277e2e9c0cSopenharmony_ci let value = self.value.take(); 12287e2e9c0cSopenharmony_ci // Panic because this indicates a bug in the program rather than an 12297e2e9c0cSopenharmony_ci // expected failure. 12307e2e9c0cSopenharmony_ci let value = value.expect("MapAccess::next_value called before next_key"); 12317e2e9c0cSopenharmony_ci seed.deserialize(value.into_deserializer()) 12327e2e9c0cSopenharmony_ci } 12337e2e9c0cSopenharmony_ci 12347e2e9c0cSopenharmony_ci fn next_entry_seed<TK, TV>( 12357e2e9c0cSopenharmony_ci &mut self, 12367e2e9c0cSopenharmony_ci kseed: TK, 12377e2e9c0cSopenharmony_ci vseed: TV, 12387e2e9c0cSopenharmony_ci ) -> Result<Option<(TK::Value, TV::Value)>, Self::Error> 12397e2e9c0cSopenharmony_ci where 12407e2e9c0cSopenharmony_ci TK: de::DeserializeSeed<'de>, 12417e2e9c0cSopenharmony_ci TV: de::DeserializeSeed<'de>, 12427e2e9c0cSopenharmony_ci { 12437e2e9c0cSopenharmony_ci match self.next_pair() { 12447e2e9c0cSopenharmony_ci Some((key, value)) => { 12457e2e9c0cSopenharmony_ci let key = tri!(kseed.deserialize(key.into_deserializer())); 12467e2e9c0cSopenharmony_ci let value = tri!(vseed.deserialize(value.into_deserializer())); 12477e2e9c0cSopenharmony_ci Ok(Some((key, value))) 12487e2e9c0cSopenharmony_ci } 12497e2e9c0cSopenharmony_ci None => Ok(None), 12507e2e9c0cSopenharmony_ci } 12517e2e9c0cSopenharmony_ci } 12527e2e9c0cSopenharmony_ci 12537e2e9c0cSopenharmony_ci fn size_hint(&self) -> Option<usize> { 12547e2e9c0cSopenharmony_ci size_hint::from_bounds(&self.iter) 12557e2e9c0cSopenharmony_ci } 12567e2e9c0cSopenharmony_ci} 12577e2e9c0cSopenharmony_ci 12587e2e9c0cSopenharmony_ciimpl<'de, I, E> de::SeqAccess<'de> for MapDeserializer<'de, I, E> 12597e2e9c0cSopenharmony_ciwhere 12607e2e9c0cSopenharmony_ci I: Iterator, 12617e2e9c0cSopenharmony_ci I::Item: private::Pair, 12627e2e9c0cSopenharmony_ci First<I::Item>: IntoDeserializer<'de, E>, 12637e2e9c0cSopenharmony_ci Second<I::Item>: IntoDeserializer<'de, E>, 12647e2e9c0cSopenharmony_ci E: de::Error, 12657e2e9c0cSopenharmony_ci{ 12667e2e9c0cSopenharmony_ci type Error = E; 12677e2e9c0cSopenharmony_ci 12687e2e9c0cSopenharmony_ci fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> 12697e2e9c0cSopenharmony_ci where 12707e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 12717e2e9c0cSopenharmony_ci { 12727e2e9c0cSopenharmony_ci match self.next_pair() { 12737e2e9c0cSopenharmony_ci Some((k, v)) => { 12747e2e9c0cSopenharmony_ci let de = PairDeserializer(k, v, PhantomData); 12757e2e9c0cSopenharmony_ci seed.deserialize(de).map(Some) 12767e2e9c0cSopenharmony_ci } 12777e2e9c0cSopenharmony_ci None => Ok(None), 12787e2e9c0cSopenharmony_ci } 12797e2e9c0cSopenharmony_ci } 12807e2e9c0cSopenharmony_ci 12817e2e9c0cSopenharmony_ci fn size_hint(&self) -> Option<usize> { 12827e2e9c0cSopenharmony_ci size_hint::from_bounds(&self.iter) 12837e2e9c0cSopenharmony_ci } 12847e2e9c0cSopenharmony_ci} 12857e2e9c0cSopenharmony_ci 12867e2e9c0cSopenharmony_ci// Cannot #[derive(Clone)] because of the bound `Second<I::Item>: Clone`. 12877e2e9c0cSopenharmony_ciimpl<'de, I, E> Clone for MapDeserializer<'de, I, E> 12887e2e9c0cSopenharmony_ciwhere 12897e2e9c0cSopenharmony_ci I: Iterator + Clone, 12907e2e9c0cSopenharmony_ci I::Item: private::Pair, 12917e2e9c0cSopenharmony_ci Second<I::Item>: Clone, 12927e2e9c0cSopenharmony_ci{ 12937e2e9c0cSopenharmony_ci fn clone(&self) -> Self { 12947e2e9c0cSopenharmony_ci MapDeserializer { 12957e2e9c0cSopenharmony_ci iter: self.iter.clone(), 12967e2e9c0cSopenharmony_ci value: self.value.clone(), 12977e2e9c0cSopenharmony_ci count: self.count, 12987e2e9c0cSopenharmony_ci lifetime: self.lifetime, 12997e2e9c0cSopenharmony_ci error: self.error, 13007e2e9c0cSopenharmony_ci } 13017e2e9c0cSopenharmony_ci } 13027e2e9c0cSopenharmony_ci} 13037e2e9c0cSopenharmony_ci 13047e2e9c0cSopenharmony_ciimpl<'de, I, E> Debug for MapDeserializer<'de, I, E> 13057e2e9c0cSopenharmony_ciwhere 13067e2e9c0cSopenharmony_ci I: Iterator + Debug, 13077e2e9c0cSopenharmony_ci I::Item: private::Pair, 13087e2e9c0cSopenharmony_ci Second<I::Item>: Debug, 13097e2e9c0cSopenharmony_ci{ 13107e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 13117e2e9c0cSopenharmony_ci formatter 13127e2e9c0cSopenharmony_ci .debug_struct("MapDeserializer") 13137e2e9c0cSopenharmony_ci .field("iter", &self.iter) 13147e2e9c0cSopenharmony_ci .field("value", &self.value) 13157e2e9c0cSopenharmony_ci .field("count", &self.count) 13167e2e9c0cSopenharmony_ci .finish() 13177e2e9c0cSopenharmony_ci } 13187e2e9c0cSopenharmony_ci} 13197e2e9c0cSopenharmony_ci 13207e2e9c0cSopenharmony_ci// Used in the `impl SeqAccess for MapDeserializer` to visit the map as a 13217e2e9c0cSopenharmony_ci// sequence of pairs. 13227e2e9c0cSopenharmony_cistruct PairDeserializer<A, B, E>(A, B, PhantomData<E>); 13237e2e9c0cSopenharmony_ci 13247e2e9c0cSopenharmony_ciimpl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer<A, B, E> 13257e2e9c0cSopenharmony_ciwhere 13267e2e9c0cSopenharmony_ci A: IntoDeserializer<'de, E>, 13277e2e9c0cSopenharmony_ci B: IntoDeserializer<'de, E>, 13287e2e9c0cSopenharmony_ci E: de::Error, 13297e2e9c0cSopenharmony_ci{ 13307e2e9c0cSopenharmony_ci type Error = E; 13317e2e9c0cSopenharmony_ci 13327e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 13337e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 13347e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct tuple_struct map 13357e2e9c0cSopenharmony_ci struct enum identifier ignored_any 13367e2e9c0cSopenharmony_ci } 13377e2e9c0cSopenharmony_ci 13387e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 13397e2e9c0cSopenharmony_ci where 13407e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 13417e2e9c0cSopenharmony_ci { 13427e2e9c0cSopenharmony_ci self.deserialize_seq(visitor) 13437e2e9c0cSopenharmony_ci } 13447e2e9c0cSopenharmony_ci 13457e2e9c0cSopenharmony_ci fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> 13467e2e9c0cSopenharmony_ci where 13477e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 13487e2e9c0cSopenharmony_ci { 13497e2e9c0cSopenharmony_ci let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData); 13507e2e9c0cSopenharmony_ci let pair = tri!(visitor.visit_seq(&mut pair_visitor)); 13517e2e9c0cSopenharmony_ci if pair_visitor.1.is_none() { 13527e2e9c0cSopenharmony_ci Ok(pair) 13537e2e9c0cSopenharmony_ci } else { 13547e2e9c0cSopenharmony_ci let remaining = pair_visitor.size_hint().unwrap(); 13557e2e9c0cSopenharmony_ci // First argument is the number of elements in the data, second 13567e2e9c0cSopenharmony_ci // argument is the number of elements expected by the Deserialize. 13577e2e9c0cSopenharmony_ci Err(de::Error::invalid_length(2, &ExpectedInSeq(2 - remaining))) 13587e2e9c0cSopenharmony_ci } 13597e2e9c0cSopenharmony_ci } 13607e2e9c0cSopenharmony_ci 13617e2e9c0cSopenharmony_ci fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> 13627e2e9c0cSopenharmony_ci where 13637e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 13647e2e9c0cSopenharmony_ci { 13657e2e9c0cSopenharmony_ci if len == 2 { 13667e2e9c0cSopenharmony_ci self.deserialize_seq(visitor) 13677e2e9c0cSopenharmony_ci } else { 13687e2e9c0cSopenharmony_ci // First argument is the number of elements in the data, second 13697e2e9c0cSopenharmony_ci // argument is the number of elements expected by the Deserialize. 13707e2e9c0cSopenharmony_ci Err(de::Error::invalid_length(2, &ExpectedInSeq(len))) 13717e2e9c0cSopenharmony_ci } 13727e2e9c0cSopenharmony_ci } 13737e2e9c0cSopenharmony_ci} 13747e2e9c0cSopenharmony_ci 13757e2e9c0cSopenharmony_cistruct PairVisitor<A, B, E>(Option<A>, Option<B>, PhantomData<E>); 13767e2e9c0cSopenharmony_ci 13777e2e9c0cSopenharmony_ciimpl<'de, A, B, E> de::SeqAccess<'de> for PairVisitor<A, B, E> 13787e2e9c0cSopenharmony_ciwhere 13797e2e9c0cSopenharmony_ci A: IntoDeserializer<'de, E>, 13807e2e9c0cSopenharmony_ci B: IntoDeserializer<'de, E>, 13817e2e9c0cSopenharmony_ci E: de::Error, 13827e2e9c0cSopenharmony_ci{ 13837e2e9c0cSopenharmony_ci type Error = E; 13847e2e9c0cSopenharmony_ci 13857e2e9c0cSopenharmony_ci fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> 13867e2e9c0cSopenharmony_ci where 13877e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 13887e2e9c0cSopenharmony_ci { 13897e2e9c0cSopenharmony_ci if let Some(k) = self.0.take() { 13907e2e9c0cSopenharmony_ci seed.deserialize(k.into_deserializer()).map(Some) 13917e2e9c0cSopenharmony_ci } else if let Some(v) = self.1.take() { 13927e2e9c0cSopenharmony_ci seed.deserialize(v.into_deserializer()).map(Some) 13937e2e9c0cSopenharmony_ci } else { 13947e2e9c0cSopenharmony_ci Ok(None) 13957e2e9c0cSopenharmony_ci } 13967e2e9c0cSopenharmony_ci } 13977e2e9c0cSopenharmony_ci 13987e2e9c0cSopenharmony_ci fn size_hint(&self) -> Option<usize> { 13997e2e9c0cSopenharmony_ci if self.0.is_some() { 14007e2e9c0cSopenharmony_ci Some(2) 14017e2e9c0cSopenharmony_ci } else if self.1.is_some() { 14027e2e9c0cSopenharmony_ci Some(1) 14037e2e9c0cSopenharmony_ci } else { 14047e2e9c0cSopenharmony_ci Some(0) 14057e2e9c0cSopenharmony_ci } 14067e2e9c0cSopenharmony_ci } 14077e2e9c0cSopenharmony_ci} 14087e2e9c0cSopenharmony_ci 14097e2e9c0cSopenharmony_cistruct ExpectedInMap(usize); 14107e2e9c0cSopenharmony_ci 14117e2e9c0cSopenharmony_ciimpl Expected for ExpectedInMap { 14127e2e9c0cSopenharmony_ci fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 14137e2e9c0cSopenharmony_ci if self.0 == 1 { 14147e2e9c0cSopenharmony_ci formatter.write_str("1 element in map") 14157e2e9c0cSopenharmony_ci } else { 14167e2e9c0cSopenharmony_ci write!(formatter, "{} elements in map", self.0) 14177e2e9c0cSopenharmony_ci } 14187e2e9c0cSopenharmony_ci } 14197e2e9c0cSopenharmony_ci} 14207e2e9c0cSopenharmony_ci 14217e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 14227e2e9c0cSopenharmony_ci 14237e2e9c0cSopenharmony_ci#[cfg(any(feature = "std", feature = "alloc"))] 14247e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] 14257e2e9c0cSopenharmony_ciimpl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V> 14267e2e9c0cSopenharmony_ciwhere 14277e2e9c0cSopenharmony_ci K: IntoDeserializer<'de, E> + Eq + Ord, 14287e2e9c0cSopenharmony_ci V: IntoDeserializer<'de, E>, 14297e2e9c0cSopenharmony_ci E: de::Error, 14307e2e9c0cSopenharmony_ci{ 14317e2e9c0cSopenharmony_ci type Deserializer = MapDeserializer<'de, <Self as IntoIterator>::IntoIter, E>; 14327e2e9c0cSopenharmony_ci 14337e2e9c0cSopenharmony_ci fn into_deserializer(self) -> Self::Deserializer { 14347e2e9c0cSopenharmony_ci MapDeserializer::new(self.into_iter()) 14357e2e9c0cSopenharmony_ci } 14367e2e9c0cSopenharmony_ci} 14377e2e9c0cSopenharmony_ci 14387e2e9c0cSopenharmony_ci#[cfg(feature = "std")] 14397e2e9c0cSopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] 14407e2e9c0cSopenharmony_ciimpl<'de, K, V, S, E> IntoDeserializer<'de, E> for HashMap<K, V, S> 14417e2e9c0cSopenharmony_ciwhere 14427e2e9c0cSopenharmony_ci K: IntoDeserializer<'de, E> + Eq + Hash, 14437e2e9c0cSopenharmony_ci V: IntoDeserializer<'de, E>, 14447e2e9c0cSopenharmony_ci S: BuildHasher, 14457e2e9c0cSopenharmony_ci E: de::Error, 14467e2e9c0cSopenharmony_ci{ 14477e2e9c0cSopenharmony_ci type Deserializer = MapDeserializer<'de, <Self as IntoIterator>::IntoIter, E>; 14487e2e9c0cSopenharmony_ci 14497e2e9c0cSopenharmony_ci fn into_deserializer(self) -> Self::Deserializer { 14507e2e9c0cSopenharmony_ci MapDeserializer::new(self.into_iter()) 14517e2e9c0cSopenharmony_ci } 14527e2e9c0cSopenharmony_ci} 14537e2e9c0cSopenharmony_ci 14547e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 14557e2e9c0cSopenharmony_ci 14567e2e9c0cSopenharmony_ci/// A deserializer holding a `MapAccess`. 14577e2e9c0cSopenharmony_ci#[derive(Clone, Debug)] 14587e2e9c0cSopenharmony_cipub struct MapAccessDeserializer<A> { 14597e2e9c0cSopenharmony_ci map: A, 14607e2e9c0cSopenharmony_ci} 14617e2e9c0cSopenharmony_ci 14627e2e9c0cSopenharmony_ciimpl<A> MapAccessDeserializer<A> { 14637e2e9c0cSopenharmony_ci /// Construct a new `MapAccessDeserializer<A>`. 14647e2e9c0cSopenharmony_ci pub fn new(map: A) -> Self { 14657e2e9c0cSopenharmony_ci MapAccessDeserializer { map } 14667e2e9c0cSopenharmony_ci } 14677e2e9c0cSopenharmony_ci} 14687e2e9c0cSopenharmony_ci 14697e2e9c0cSopenharmony_ciimpl<'de, A> de::Deserializer<'de> for MapAccessDeserializer<A> 14707e2e9c0cSopenharmony_ciwhere 14717e2e9c0cSopenharmony_ci A: de::MapAccess<'de>, 14727e2e9c0cSopenharmony_ci{ 14737e2e9c0cSopenharmony_ci type Error = A::Error; 14747e2e9c0cSopenharmony_ci 14757e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 14767e2e9c0cSopenharmony_ci where 14777e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 14787e2e9c0cSopenharmony_ci { 14797e2e9c0cSopenharmony_ci visitor.visit_map(self.map) 14807e2e9c0cSopenharmony_ci } 14817e2e9c0cSopenharmony_ci 14827e2e9c0cSopenharmony_ci fn deserialize_enum<V>( 14837e2e9c0cSopenharmony_ci self, 14847e2e9c0cSopenharmony_ci _name: &str, 14857e2e9c0cSopenharmony_ci _variants: &'static [&'static str], 14867e2e9c0cSopenharmony_ci visitor: V, 14877e2e9c0cSopenharmony_ci ) -> Result<V::Value, Self::Error> 14887e2e9c0cSopenharmony_ci where 14897e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 14907e2e9c0cSopenharmony_ci { 14917e2e9c0cSopenharmony_ci visitor.visit_enum(self) 14927e2e9c0cSopenharmony_ci } 14937e2e9c0cSopenharmony_ci 14947e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 14957e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 14967e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 14977e2e9c0cSopenharmony_ci tuple_struct map struct identifier ignored_any 14987e2e9c0cSopenharmony_ci } 14997e2e9c0cSopenharmony_ci} 15007e2e9c0cSopenharmony_ci 15017e2e9c0cSopenharmony_ciimpl<'de, A> de::EnumAccess<'de> for MapAccessDeserializer<A> 15027e2e9c0cSopenharmony_ciwhere 15037e2e9c0cSopenharmony_ci A: de::MapAccess<'de>, 15047e2e9c0cSopenharmony_ci{ 15057e2e9c0cSopenharmony_ci type Error = A::Error; 15067e2e9c0cSopenharmony_ci type Variant = private::MapAsEnum<A>; 15077e2e9c0cSopenharmony_ci 15087e2e9c0cSopenharmony_ci fn variant_seed<T>(mut self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> 15097e2e9c0cSopenharmony_ci where 15107e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 15117e2e9c0cSopenharmony_ci { 15127e2e9c0cSopenharmony_ci match tri!(self.map.next_key_seed(seed)) { 15137e2e9c0cSopenharmony_ci Some(key) => Ok((key, private::map_as_enum(self.map))), 15147e2e9c0cSopenharmony_ci None => Err(de::Error::invalid_type(de::Unexpected::Map, &"enum")), 15157e2e9c0cSopenharmony_ci } 15167e2e9c0cSopenharmony_ci } 15177e2e9c0cSopenharmony_ci} 15187e2e9c0cSopenharmony_ci 15197e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 15207e2e9c0cSopenharmony_ci 15217e2e9c0cSopenharmony_ci/// A deserializer holding an `EnumAccess`. 15227e2e9c0cSopenharmony_ci#[derive(Clone, Debug)] 15237e2e9c0cSopenharmony_cipub struct EnumAccessDeserializer<A> { 15247e2e9c0cSopenharmony_ci access: A, 15257e2e9c0cSopenharmony_ci} 15267e2e9c0cSopenharmony_ci 15277e2e9c0cSopenharmony_ciimpl<A> EnumAccessDeserializer<A> { 15287e2e9c0cSopenharmony_ci /// Construct a new `EnumAccessDeserializer<A>`. 15297e2e9c0cSopenharmony_ci pub fn new(access: A) -> Self { 15307e2e9c0cSopenharmony_ci EnumAccessDeserializer { access } 15317e2e9c0cSopenharmony_ci } 15327e2e9c0cSopenharmony_ci} 15337e2e9c0cSopenharmony_ci 15347e2e9c0cSopenharmony_ciimpl<'de, A> de::Deserializer<'de> for EnumAccessDeserializer<A> 15357e2e9c0cSopenharmony_ciwhere 15367e2e9c0cSopenharmony_ci A: de::EnumAccess<'de>, 15377e2e9c0cSopenharmony_ci{ 15387e2e9c0cSopenharmony_ci type Error = A::Error; 15397e2e9c0cSopenharmony_ci 15407e2e9c0cSopenharmony_ci fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> 15417e2e9c0cSopenharmony_ci where 15427e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 15437e2e9c0cSopenharmony_ci { 15447e2e9c0cSopenharmony_ci visitor.visit_enum(self.access) 15457e2e9c0cSopenharmony_ci } 15467e2e9c0cSopenharmony_ci 15477e2e9c0cSopenharmony_ci forward_to_deserialize_any! { 15487e2e9c0cSopenharmony_ci bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string 15497e2e9c0cSopenharmony_ci bytes byte_buf option unit unit_struct newtype_struct seq tuple 15507e2e9c0cSopenharmony_ci tuple_struct map struct enum identifier ignored_any 15517e2e9c0cSopenharmony_ci } 15527e2e9c0cSopenharmony_ci} 15537e2e9c0cSopenharmony_ci 15547e2e9c0cSopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 15557e2e9c0cSopenharmony_ci 15567e2e9c0cSopenharmony_cimod private { 15577e2e9c0cSopenharmony_ci use crate::lib::*; 15587e2e9c0cSopenharmony_ci 15597e2e9c0cSopenharmony_ci use crate::de::{ 15607e2e9c0cSopenharmony_ci self, DeserializeSeed, Deserializer, MapAccess, Unexpected, VariantAccess, Visitor, 15617e2e9c0cSopenharmony_ci }; 15627e2e9c0cSopenharmony_ci 15637e2e9c0cSopenharmony_ci pub struct UnitOnly<E> { 15647e2e9c0cSopenharmony_ci marker: PhantomData<E>, 15657e2e9c0cSopenharmony_ci } 15667e2e9c0cSopenharmony_ci 15677e2e9c0cSopenharmony_ci pub fn unit_only<T, E>(t: T) -> (T, UnitOnly<E>) { 15687e2e9c0cSopenharmony_ci ( 15697e2e9c0cSopenharmony_ci t, 15707e2e9c0cSopenharmony_ci UnitOnly { 15717e2e9c0cSopenharmony_ci marker: PhantomData, 15727e2e9c0cSopenharmony_ci }, 15737e2e9c0cSopenharmony_ci ) 15747e2e9c0cSopenharmony_ci } 15757e2e9c0cSopenharmony_ci 15767e2e9c0cSopenharmony_ci impl<'de, E> de::VariantAccess<'de> for UnitOnly<E> 15777e2e9c0cSopenharmony_ci where 15787e2e9c0cSopenharmony_ci E: de::Error, 15797e2e9c0cSopenharmony_ci { 15807e2e9c0cSopenharmony_ci type Error = E; 15817e2e9c0cSopenharmony_ci 15827e2e9c0cSopenharmony_ci fn unit_variant(self) -> Result<(), Self::Error> { 15837e2e9c0cSopenharmony_ci Ok(()) 15847e2e9c0cSopenharmony_ci } 15857e2e9c0cSopenharmony_ci 15867e2e9c0cSopenharmony_ci fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error> 15877e2e9c0cSopenharmony_ci where 15887e2e9c0cSopenharmony_ci T: de::DeserializeSeed<'de>, 15897e2e9c0cSopenharmony_ci { 15907e2e9c0cSopenharmony_ci Err(de::Error::invalid_type( 15917e2e9c0cSopenharmony_ci Unexpected::UnitVariant, 15927e2e9c0cSopenharmony_ci &"newtype variant", 15937e2e9c0cSopenharmony_ci )) 15947e2e9c0cSopenharmony_ci } 15957e2e9c0cSopenharmony_ci 15967e2e9c0cSopenharmony_ci fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error> 15977e2e9c0cSopenharmony_ci where 15987e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 15997e2e9c0cSopenharmony_ci { 16007e2e9c0cSopenharmony_ci Err(de::Error::invalid_type( 16017e2e9c0cSopenharmony_ci Unexpected::UnitVariant, 16027e2e9c0cSopenharmony_ci &"tuple variant", 16037e2e9c0cSopenharmony_ci )) 16047e2e9c0cSopenharmony_ci } 16057e2e9c0cSopenharmony_ci 16067e2e9c0cSopenharmony_ci fn struct_variant<V>( 16077e2e9c0cSopenharmony_ci self, 16087e2e9c0cSopenharmony_ci _fields: &'static [&'static str], 16097e2e9c0cSopenharmony_ci _visitor: V, 16107e2e9c0cSopenharmony_ci ) -> Result<V::Value, Self::Error> 16117e2e9c0cSopenharmony_ci where 16127e2e9c0cSopenharmony_ci V: de::Visitor<'de>, 16137e2e9c0cSopenharmony_ci { 16147e2e9c0cSopenharmony_ci Err(de::Error::invalid_type( 16157e2e9c0cSopenharmony_ci Unexpected::UnitVariant, 16167e2e9c0cSopenharmony_ci &"struct variant", 16177e2e9c0cSopenharmony_ci )) 16187e2e9c0cSopenharmony_ci } 16197e2e9c0cSopenharmony_ci } 16207e2e9c0cSopenharmony_ci 16217e2e9c0cSopenharmony_ci pub struct MapAsEnum<A> { 16227e2e9c0cSopenharmony_ci map: A, 16237e2e9c0cSopenharmony_ci } 16247e2e9c0cSopenharmony_ci 16257e2e9c0cSopenharmony_ci pub fn map_as_enum<A>(map: A) -> MapAsEnum<A> { 16267e2e9c0cSopenharmony_ci MapAsEnum { map } 16277e2e9c0cSopenharmony_ci } 16287e2e9c0cSopenharmony_ci 16297e2e9c0cSopenharmony_ci impl<'de, A> VariantAccess<'de> for MapAsEnum<A> 16307e2e9c0cSopenharmony_ci where 16317e2e9c0cSopenharmony_ci A: MapAccess<'de>, 16327e2e9c0cSopenharmony_ci { 16337e2e9c0cSopenharmony_ci type Error = A::Error; 16347e2e9c0cSopenharmony_ci 16357e2e9c0cSopenharmony_ci fn unit_variant(mut self) -> Result<(), Self::Error> { 16367e2e9c0cSopenharmony_ci self.map.next_value() 16377e2e9c0cSopenharmony_ci } 16387e2e9c0cSopenharmony_ci 16397e2e9c0cSopenharmony_ci fn newtype_variant_seed<T>(mut self, seed: T) -> Result<T::Value, Self::Error> 16407e2e9c0cSopenharmony_ci where 16417e2e9c0cSopenharmony_ci T: DeserializeSeed<'de>, 16427e2e9c0cSopenharmony_ci { 16437e2e9c0cSopenharmony_ci self.map.next_value_seed(seed) 16447e2e9c0cSopenharmony_ci } 16457e2e9c0cSopenharmony_ci 16467e2e9c0cSopenharmony_ci fn tuple_variant<V>(mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error> 16477e2e9c0cSopenharmony_ci where 16487e2e9c0cSopenharmony_ci V: Visitor<'de>, 16497e2e9c0cSopenharmony_ci { 16507e2e9c0cSopenharmony_ci self.map.next_value_seed(SeedTupleVariant { len, visitor }) 16517e2e9c0cSopenharmony_ci } 16527e2e9c0cSopenharmony_ci 16537e2e9c0cSopenharmony_ci fn struct_variant<V>( 16547e2e9c0cSopenharmony_ci mut self, 16557e2e9c0cSopenharmony_ci _fields: &'static [&'static str], 16567e2e9c0cSopenharmony_ci visitor: V, 16577e2e9c0cSopenharmony_ci ) -> Result<V::Value, Self::Error> 16587e2e9c0cSopenharmony_ci where 16597e2e9c0cSopenharmony_ci V: Visitor<'de>, 16607e2e9c0cSopenharmony_ci { 16617e2e9c0cSopenharmony_ci self.map.next_value_seed(SeedStructVariant { visitor }) 16627e2e9c0cSopenharmony_ci } 16637e2e9c0cSopenharmony_ci } 16647e2e9c0cSopenharmony_ci 16657e2e9c0cSopenharmony_ci struct SeedTupleVariant<V> { 16667e2e9c0cSopenharmony_ci len: usize, 16677e2e9c0cSopenharmony_ci visitor: V, 16687e2e9c0cSopenharmony_ci } 16697e2e9c0cSopenharmony_ci 16707e2e9c0cSopenharmony_ci impl<'de, V> DeserializeSeed<'de> for SeedTupleVariant<V> 16717e2e9c0cSopenharmony_ci where 16727e2e9c0cSopenharmony_ci V: Visitor<'de>, 16737e2e9c0cSopenharmony_ci { 16747e2e9c0cSopenharmony_ci type Value = V::Value; 16757e2e9c0cSopenharmony_ci 16767e2e9c0cSopenharmony_ci fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> 16777e2e9c0cSopenharmony_ci where 16787e2e9c0cSopenharmony_ci D: Deserializer<'de>, 16797e2e9c0cSopenharmony_ci { 16807e2e9c0cSopenharmony_ci deserializer.deserialize_tuple(self.len, self.visitor) 16817e2e9c0cSopenharmony_ci } 16827e2e9c0cSopenharmony_ci } 16837e2e9c0cSopenharmony_ci 16847e2e9c0cSopenharmony_ci struct SeedStructVariant<V> { 16857e2e9c0cSopenharmony_ci visitor: V, 16867e2e9c0cSopenharmony_ci } 16877e2e9c0cSopenharmony_ci 16887e2e9c0cSopenharmony_ci impl<'de, V> DeserializeSeed<'de> for SeedStructVariant<V> 16897e2e9c0cSopenharmony_ci where 16907e2e9c0cSopenharmony_ci V: Visitor<'de>, 16917e2e9c0cSopenharmony_ci { 16927e2e9c0cSopenharmony_ci type Value = V::Value; 16937e2e9c0cSopenharmony_ci 16947e2e9c0cSopenharmony_ci fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> 16957e2e9c0cSopenharmony_ci where 16967e2e9c0cSopenharmony_ci D: Deserializer<'de>, 16977e2e9c0cSopenharmony_ci { 16987e2e9c0cSopenharmony_ci deserializer.deserialize_map(self.visitor) 16997e2e9c0cSopenharmony_ci } 17007e2e9c0cSopenharmony_ci } 17017e2e9c0cSopenharmony_ci 17027e2e9c0cSopenharmony_ci /// Avoid having to restate the generic types on `MapDeserializer`. The 17037e2e9c0cSopenharmony_ci /// `Iterator::Item` contains enough information to figure out K and V. 17047e2e9c0cSopenharmony_ci pub trait Pair { 17057e2e9c0cSopenharmony_ci type First; 17067e2e9c0cSopenharmony_ci type Second; 17077e2e9c0cSopenharmony_ci fn split(self) -> (Self::First, Self::Second); 17087e2e9c0cSopenharmony_ci } 17097e2e9c0cSopenharmony_ci 17107e2e9c0cSopenharmony_ci impl<A, B> Pair for (A, B) { 17117e2e9c0cSopenharmony_ci type First = A; 17127e2e9c0cSopenharmony_ci type Second = B; 17137e2e9c0cSopenharmony_ci fn split(self) -> (A, B) { 17147e2e9c0cSopenharmony_ci self 17157e2e9c0cSopenharmony_ci } 17167e2e9c0cSopenharmony_ci } 17177e2e9c0cSopenharmony_ci 17187e2e9c0cSopenharmony_ci pub type First<T> = <T as Pair>::First; 17197e2e9c0cSopenharmony_ci pub type Second<T> = <T as Pair>::Second; 17207e2e9c0cSopenharmony_ci} 1721