diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:36 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:36 +0000 |
commit | e02c5b5930c2c9ba3e5423fe12e2ef0155017297 (patch) | |
tree | fd60ebbbb5299e16e5fca8c773ddb74f764760db /vendor/erased-serde/src | |
parent | Adding debian version 1.73.0+dfsg1-1. (diff) | |
download | rustc-e02c5b5930c2c9ba3e5423fe12e2ef0155017297.tar.xz rustc-e02c5b5930c2c9ba3e5423fe12e2ef0155017297.zip |
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/erased-serde/src')
-rw-r--r-- | vendor/erased-serde/src/any.rs | 179 | ||||
-rw-r--r-- | vendor/erased-serde/src/de.rs | 1628 | ||||
-rw-r--r-- | vendor/erased-serde/src/error.rs | 35 | ||||
-rw-r--r-- | vendor/erased-serde/src/features_check/error.rs | 1 | ||||
-rw-r--r-- | vendor/erased-serde/src/features_check/mod.rs | 13 | ||||
-rw-r--r-- | vendor/erased-serde/src/lib.rs | 144 | ||||
-rw-r--r-- | vendor/erased-serde/src/macros.rs | 165 | ||||
-rw-r--r-- | vendor/erased-serde/src/map.rs | 25 | ||||
-rw-r--r-- | vendor/erased-serde/src/private.rs | 16 | ||||
-rw-r--r-- | vendor/erased-serde/src/ser.rs | 1495 |
10 files changed, 3701 insertions, 0 deletions
diff --git a/vendor/erased-serde/src/any.rs b/vendor/erased-serde/src/any.rs new file mode 100644 index 000000000..c910fe909 --- /dev/null +++ b/vendor/erased-serde/src/any.rs @@ -0,0 +1,179 @@ +use crate::alloc::Box; +use core::any::TypeId; +use core::marker::PhantomData; +use core::mem::{self, MaybeUninit}; +use core::ptr; + +#[cfg(feature = "unstable-debug")] +use core::any; + +pub struct Any { + value: Value, + drop: unsafe fn(&mut Value), + type_id: TypeId, + + /// For panic messages only. Not used for comparison. + #[cfg(feature = "unstable-debug")] + type_name: &'static str, +} + +union Value { + ptr: *mut (), + inline: [MaybeUninit<usize>; 2], +} + +fn is_small<T>() -> bool { + mem::size_of::<T>() <= mem::size_of::<Value>() + && mem::align_of::<T>() <= mem::align_of::<Value>() +} + +impl Any { + // This is unsafe -- caller must not hold on to the Any beyond the lifetime + // of T. + // + // Example of bad code: + // + // let s = "bad".to_owned(); + // let a = Any::new(&s); + // drop(s); + // + // Now `a.view()` and `a.take()` return references to a dead String. + pub(crate) unsafe fn new<T>(t: T) -> Self { + let value: Value; + let drop: unsafe fn(&mut Value); + let type_id = non_static_type_id::<T>(); + + if is_small::<T>() { + let mut inline = [MaybeUninit::uninit(); 2]; + unsafe { ptr::write(inline.as_mut_ptr().cast::<T>(), t) }; + value = Value { inline }; + unsafe fn inline_drop<T>(value: &mut Value) { + unsafe { ptr::drop_in_place(value.inline.as_mut_ptr().cast::<T>()) } + } + drop = inline_drop::<T>; + } else { + let ptr = Box::into_raw(Box::new(t)).cast::<()>(); + value = Value { ptr }; + unsafe fn ptr_drop<T>(value: &mut Value) { + mem::drop(unsafe { Box::from_raw(value.ptr.cast::<T>()) }); + } + drop = ptr_drop::<T>; + }; + + Any { + value, + drop, + type_id, + #[cfg(feature = "unstable-debug")] + type_name: any::type_name::<T>(), + } + } + + // This is unsafe -- caller is responsible that T is the correct type. + pub(crate) unsafe fn view<T>(&mut self) -> &mut T { + if self.type_id != non_static_type_id::<T>() { + self.invalid_cast_to::<T>(); + } + + let ptr = if is_small::<T>() { + unsafe { self.value.inline.as_mut_ptr().cast::<T>() } + } else { + unsafe { self.value.ptr.cast::<T>() } + }; + + unsafe { &mut *ptr } + } + + // This is unsafe -- caller is responsible that T is the correct type. + pub(crate) unsafe fn take<T>(mut self) -> T { + if self.type_id != non_static_type_id::<T>() { + self.invalid_cast_to::<T>(); + } + + if is_small::<T>() { + let ptr = unsafe { self.value.inline.as_mut_ptr().cast::<T>() }; + let value = unsafe { ptr::read(ptr) }; + mem::forget(self); + value + } else { + let ptr = unsafe { self.value.ptr.cast::<T>() }; + let box_t = unsafe { Box::from_raw(ptr) }; + mem::forget(self); + *box_t + } + } + + #[cfg(not(feature = "unstable-debug"))] + fn invalid_cast_to<T>(&self) -> ! { + panic!("invalid cast; enable `unstable-debug` feature to debug"); + } + + #[cfg(feature = "unstable-debug")] + fn invalid_cast_to<T>(&self) -> ! { + let from = self.type_name; + let to = any::type_name::<T>(); + panic!("invalid cast: {} to {}", from, to); + } +} + +impl Drop for Any { + fn drop(&mut self) { + unsafe { (self.drop)(&mut self.value) } + } +} + +trait NonStaticAny { + fn get_type_id(&self) -> TypeId + where + Self: 'static; +} + +impl<T: ?Sized> NonStaticAny for PhantomData<T> { + fn get_type_id(&self) -> TypeId + where + Self: 'static, + { + TypeId::of::<T>() + } +} + +fn non_static_type_id<T>() -> TypeId { + let non_static_thing = &PhantomData::<T>; + let thing = unsafe { + mem::transmute::<&dyn NonStaticAny, &(dyn NonStaticAny + 'static)>(non_static_thing) + }; + NonStaticAny::get_type_id(thing) +} + +#[test] +fn test_non_static_type_id() { + assert_eq!(non_static_type_id::<usize>(), non_static_type_id::<usize>()); + assert_eq!( + non_static_type_id::<&str>(), + non_static_type_id::<&'static str>() + ); + + assert_ne!(non_static_type_id::<u32>(), non_static_type_id::<[u8; 4]>()); + assert_ne!( + non_static_type_id::<u32>(), + non_static_type_id::<[u32; 2]>() + ); + + assert_ne!(non_static_type_id::<usize>(), non_static_type_id::<isize>()); + assert_ne!( + non_static_type_id::<usize>(), + non_static_type_id::<&usize>() + ); + assert_ne!( + non_static_type_id::<&usize>(), + non_static_type_id::<&&usize>() + ); + assert_ne!( + non_static_type_id::<&usize>(), + non_static_type_id::<&mut usize>() + ); + + struct A; + struct B; + assert_ne!(non_static_type_id::<A>(), non_static_type_id::<B>()); +} diff --git a/vendor/erased-serde/src/de.rs b/vendor/erased-serde/src/de.rs new file mode 100644 index 000000000..e03666e3b --- /dev/null +++ b/vendor/erased-serde/src/de.rs @@ -0,0 +1,1628 @@ +use crate::alloc::*; +use crate::any::Any; +use crate::error::Error; +use crate::map::{OptionExt, ResultExt}; +use core::fmt::{self, Display}; + +/// Deserialize a value of type `T` from the given trait object. +/// +/// ```rust +/// use erased_serde::Deserializer; +/// use std::collections::BTreeMap as Map; +/// +/// fn main() { +/// static JSON: &'static [u8] = br#"{"A": 65, "B": 66}"#; +/// static CBOR: &'static [u8] = &[162, 97, 65, 24, 65, 97, 66, 24, 66]; +/// +/// // Construct some deserializers. +/// let json = &mut serde_json::Deserializer::from_slice(JSON); +/// let cbor = &mut serde_cbor::Deserializer::from_slice(CBOR); +/// +/// // The values in this map are boxed trait objects, which is not possible +/// // with the normal serde::Deserializer because of object safety. +/// let mut formats: Map<&str, Box<dyn Deserializer>> = Map::new(); +/// formats.insert("json", Box::new(<dyn Deserializer>::erase(json))); +/// formats.insert("cbor", Box::new(<dyn Deserializer>::erase(cbor))); +/// +/// // Pick a Deserializer out of the formats map. +/// let format = formats.get_mut("json").unwrap(); +/// +/// let data: Map<String, usize> = erased_serde::deserialize(format).unwrap(); +/// +/// println!("{}", data["A"] + data["B"]); +/// } +/// ``` +pub fn deserialize<'de, T>(deserializer: &mut dyn Deserializer<'de>) -> Result<T, Error> +where + T: serde::Deserialize<'de>, +{ + serde::Deserialize::deserialize(deserializer) +} + +// TRAITS ////////////////////////////////////////////////////////////////////// + +pub trait DeserializeSeed<'de> { + fn erased_deserialize_seed(&mut self, d: &mut dyn Deserializer<'de>) -> Result<Out, Error>; +} + +/// An object-safe equivalent of Serde's `Deserializer` trait. +/// +/// Any implementation of Serde's `Deserializer` can be converted to an +/// `&erased_serde::Deserializer` or `Box<erased_serde::Deserializer>` trait +/// object using `erased_serde::Deserializer::erase`. +/// +/// ```rust +/// use erased_serde::Deserializer; +/// use std::collections::BTreeMap as Map; +/// +/// fn main() { +/// static JSON: &'static [u8] = br#"{"A": 65, "B": 66}"#; +/// static CBOR: &'static [u8] = &[162, 97, 65, 24, 65, 97, 66, 24, 66]; +/// +/// // Construct some deserializers. +/// let json = &mut serde_json::Deserializer::from_slice(JSON); +/// let cbor = &mut serde_cbor::Deserializer::from_slice(CBOR); +/// +/// // The values in this map are boxed trait objects, which is not possible +/// // with the normal serde::Deserializer because of object safety. +/// let mut formats: Map<&str, Box<dyn Deserializer>> = Map::new(); +/// formats.insert("json", Box::new(<dyn Deserializer>::erase(json))); +/// formats.insert("cbor", Box::new(<dyn Deserializer>::erase(cbor))); +/// +/// // Pick a Deserializer out of the formats map. +/// let format = formats.get_mut("json").unwrap(); +/// +/// let data: Map<String, usize> = erased_serde::deserialize(format).unwrap(); +/// +/// println!("{}", data["A"] + data["B"]); +/// } +/// ``` +pub trait Deserializer<'de> { + fn erased_deserialize_any(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_bool(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_i8(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_i16(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_i32(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_i64(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_i128(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_u8(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_u16(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_u32(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_u64(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_u128(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_f32(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_f64(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_char(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_str(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_string(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_bytes(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_byte_buf(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_option(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_unit(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_unit_struct( + &mut self, + name: &'static str, + v: &mut dyn Visitor<'de>, + ) -> Result<Out, Error>; + fn erased_deserialize_newtype_struct( + &mut self, + name: &'static str, + v: &mut dyn Visitor<'de>, + ) -> Result<Out, Error>; + fn erased_deserialize_seq(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_tuple( + &mut self, + len: usize, + v: &mut dyn Visitor<'de>, + ) -> Result<Out, Error>; + fn erased_deserialize_tuple_struct( + &mut self, + name: &'static str, + len: usize, + v: &mut dyn Visitor<'de>, + ) -> Result<Out, Error>; + fn erased_deserialize_map(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_struct( + &mut self, + name: &'static str, + fields: &'static [&'static str], + v: &mut dyn Visitor<'de>, + ) -> Result<Out, Error>; + fn erased_deserialize_identifier(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_deserialize_enum( + &mut self, + name: &'static str, + variants: &'static [&'static str], + v: &mut dyn Visitor<'de>, + ) -> Result<Out, Error>; + fn erased_deserialize_ignored_any(&mut self, v: &mut dyn Visitor<'de>) -> Result<Out, Error>; + fn erased_is_human_readable(&self) -> bool; +} + +pub trait Visitor<'de> { + fn erased_expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result; + fn erased_visit_bool(&mut self, v: bool) -> Result<Out, Error>; + fn erased_visit_i8(&mut self, v: i8) -> Result<Out, Error>; + fn erased_visit_i16(&mut self, v: i16) -> Result<Out, Error>; + fn erased_visit_i32(&mut self, v: i32) -> Result<Out, Error>; + fn erased_visit_i64(&mut self, v: i64) -> Result<Out, Error>; + fn erased_visit_i128(&mut self, v: i128) -> Result<Out, Error>; + fn erased_visit_u8(&mut self, v: u8) -> Result<Out, Error>; + fn erased_visit_u16(&mut self, v: u16) -> Result<Out, Error>; + fn erased_visit_u32(&mut self, v: u32) -> Result<Out, Error>; + fn erased_visit_u64(&mut self, v: u64) -> Result<Out, Error>; + fn erased_visit_u128(&mut self, v: u128) -> Result<Out, Error>; + fn erased_visit_f32(&mut self, v: f32) -> Result<Out, Error>; + fn erased_visit_f64(&mut self, v: f64) -> Result<Out, Error>; + fn erased_visit_char(&mut self, v: char) -> Result<Out, Error>; + fn erased_visit_str(&mut self, v: &str) -> Result<Out, Error>; + fn erased_visit_borrowed_str(&mut self, v: &'de str) -> Result<Out, Error>; + #[cfg(any(feature = "std", feature = "alloc"))] + fn erased_visit_string(&mut self, v: String) -> Result<Out, Error>; + fn erased_visit_bytes(&mut self, v: &[u8]) -> Result<Out, Error>; + fn erased_visit_borrowed_bytes(&mut self, v: &'de [u8]) -> Result<Out, Error>; + #[cfg(any(feature = "std", feature = "alloc"))] + fn erased_visit_byte_buf(&mut self, v: Vec<u8>) -> Result<Out, Error>; + fn erased_visit_none(&mut self) -> Result<Out, Error>; + fn erased_visit_some(&mut self, d: &mut dyn Deserializer<'de>) -> Result<Out, Error>; + fn erased_visit_unit(&mut self) -> Result<Out, Error>; + fn erased_visit_newtype_struct(&mut self, d: &mut dyn Deserializer<'de>) -> Result<Out, Error>; + fn erased_visit_seq(&mut self, s: &mut dyn SeqAccess<'de>) -> Result<Out, Error>; + fn erased_visit_map(&mut self, m: &mut dyn MapAccess<'de>) -> Result<Out, Error>; + fn erased_visit_enum(&mut self, e: &mut dyn EnumAccess<'de>) -> Result<Out, Error>; +} + +pub trait SeqAccess<'de> { + fn erased_next_element( + &mut self, + d: &mut dyn DeserializeSeed<'de>, + ) -> Result<Option<Out>, Error>; + fn erased_size_hint(&self) -> Option<usize>; +} + +pub trait MapAccess<'de> { + fn erased_next_key(&mut self, d: &mut dyn DeserializeSeed<'de>) -> Result<Option<Out>, Error>; + fn erased_next_value(&mut self, d: &mut dyn DeserializeSeed<'de>) -> Result<Out, Error>; + fn erased_next_entry( + &mut self, + key: &mut dyn DeserializeSeed<'de>, + value: &mut dyn DeserializeSeed<'de>, + ) -> Result<Option<(Out, Out)>, Error>; + fn erased_size_hint(&self) -> Option<usize>; +} + +pub trait EnumAccess<'de> { + fn erased_variant_seed( + &mut self, + d: &mut dyn DeserializeSeed<'de>, + ) -> Result<(Out, Variant<'de>), Error>; +} + +impl<'de> dyn Deserializer<'de> { + /// Convert any Serde `Deserializer` to a trait object. + /// + /// ```rust + /// use erased_serde::Deserializer; + /// use std::collections::BTreeMap as Map; + /// + /// fn main() { + /// static JSON: &'static [u8] = br#"{"A": 65, "B": 66}"#; + /// static CBOR: &'static [u8] = &[162, 97, 65, 24, 65, 97, 66, 24, 66]; + /// + /// // Construct some deserializers. + /// let json = &mut serde_json::Deserializer::from_slice(JSON); + /// let cbor = &mut serde_cbor::Deserializer::from_slice(CBOR); + /// + /// // The values in this map are boxed trait objects, which is not possible + /// // with the normal serde::Deserializer because of object safety. + /// let mut formats: Map<&str, Box<dyn Deserializer>> = Map::new(); + /// formats.insert("json", Box::new(<dyn Deserializer>::erase(json))); + /// formats.insert("cbor", Box::new(<dyn Deserializer>::erase(cbor))); + /// + /// // Pick a Deserializer out of the formats map. + /// let format = formats.get_mut("json").unwrap(); + /// + /// let data: Map<String, usize> = erased_serde::deserialize(format).unwrap(); + /// + /// println!("{}", data["A"] + data["B"]); + /// } + /// ``` + pub fn erase<D>(deserializer: D) -> erase::Deserializer<D> + where + D: serde::Deserializer<'de>, + { + erase::Deserializer { + state: Some(deserializer), + } + } +} + +// OUT ///////////////////////////////////////////////////////////////////////// + +pub struct Out(Any); + +impl Out { + unsafe fn new<T>(t: T) -> Self { + Out(unsafe { Any::new(t) }) + } + + unsafe fn take<T>(self) -> T { + unsafe { self.0.take() } + } +} + +// IMPL ERASED SERDE FOR SERDE ///////////////////////////////////////////////// + +mod erase { + pub struct DeserializeSeed<D> { + pub(crate) state: Option<D>, + } + + impl<D> DeserializeSeed<D> { + pub(crate) fn take(&mut self) -> D { + self.state.take().unwrap() + } + } + + pub struct Deserializer<D> { + pub(crate) state: Option<D>, + } + + impl<D> Deserializer<D> { + pub(crate) fn take(&mut self) -> D { + self.state.take().unwrap() + } + pub(crate) fn as_ref(&self) -> &D { + self.state.as_ref().unwrap() + } + } + + pub struct Visitor<D> { + pub(crate) state: Option<D>, + } + + impl<D> Visitor<D> { + pub(crate) fn take(&mut self) -> D { + self.state.take().unwrap() + } + pub(crate) fn as_ref(&self) -> &D { + self.state.as_ref().unwrap() + } + } + + pub struct SeqAccess<D> { + pub(crate) state: D, + } + + impl<D> SeqAccess<D> { + pub(crate) fn as_ref(&self) -> &D { + &self.state + } + pub(crate) fn as_mut(&mut self) -> &mut D { + &mut self.state + } + } + + pub struct MapAccess<D> { + pub(crate) state: D, + } + + impl<D> MapAccess<D> { + pub(crate) fn as_ref(&self) -> &D { + &self.state + } + pub(crate) fn as_mut(&mut self) -> &mut D { + &mut self.state + } + } + + pub struct EnumAccess<D> { + pub(crate) state: Option<D>, + } + + impl<D> EnumAccess<D> { + pub(crate) fn take(&mut self) -> D { + self.state.take().unwrap() + } + } +} + +impl<'de, T> DeserializeSeed<'de> for erase::DeserializeSeed<T> +where + T: serde::de::DeserializeSeed<'de>, +{ + fn erased_deserialize_seed( + &mut self, + deserializer: &mut dyn Deserializer<'de>, + ) -> Result<Out, Error> { + unsafe { self.take().deserialize(deserializer).unsafe_map(Out::new) } + } +} + +impl<'de, T> Deserializer<'de> for erase::Deserializer<T> +where + T: serde::Deserializer<'de>, +{ + fn erased_deserialize_any(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_any(visitor).map_err(erase) + } + + fn erased_deserialize_bool(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_bool(visitor).map_err(erase) + } + + fn erased_deserialize_i8(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_i8(visitor).map_err(erase) + } + + fn erased_deserialize_i16(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_i16(visitor).map_err(erase) + } + + fn erased_deserialize_i32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_i32(visitor).map_err(erase) + } + + fn erased_deserialize_i64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_i64(visitor).map_err(erase) + } + + fn erased_deserialize_i128(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_i128(visitor).map_err(erase) + } + + fn erased_deserialize_u8(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_u8(visitor).map_err(erase) + } + + fn erased_deserialize_u16(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_u16(visitor).map_err(erase) + } + + fn erased_deserialize_u32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_u32(visitor).map_err(erase) + } + + fn erased_deserialize_u64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_u64(visitor).map_err(erase) + } + + fn erased_deserialize_u128(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_u128(visitor).map_err(erase) + } + + fn erased_deserialize_f32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_f32(visitor).map_err(erase) + } + + fn erased_deserialize_f64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_f64(visitor).map_err(erase) + } + + fn erased_deserialize_char(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_char(visitor).map_err(erase) + } + + fn erased_deserialize_str(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_str(visitor).map_err(erase) + } + + fn erased_deserialize_string(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_string(visitor).map_err(erase) + } + + fn erased_deserialize_bytes(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_bytes(visitor).map_err(erase) + } + + fn erased_deserialize_byte_buf( + &mut self, + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> { + self.take().deserialize_byte_buf(visitor).map_err(erase) + } + + fn erased_deserialize_option(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_option(visitor).map_err(erase) + } + + fn erased_deserialize_unit(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_unit(visitor).map_err(erase) + } + + fn erased_deserialize_unit_struct( + &mut self, + name: &'static str, + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> { + self.take() + .deserialize_unit_struct(name, visitor) + .map_err(erase) + } + + fn erased_deserialize_newtype_struct( + &mut self, + name: &'static str, + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> { + self.take() + .deserialize_newtype_struct(name, visitor) + .map_err(erase) + } + + fn erased_deserialize_seq(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_seq(visitor).map_err(erase) + } + + fn erased_deserialize_tuple( + &mut self, + len: usize, + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> { + self.take().deserialize_tuple(len, visitor).map_err(erase) + } + + fn erased_deserialize_tuple_struct( + &mut self, + name: &'static str, + len: usize, + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> { + self.take() + .deserialize_tuple_struct(name, len, visitor) + .map_err(erase) + } + + fn erased_deserialize_map(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + self.take().deserialize_map(visitor).map_err(erase) + } + + fn erased_deserialize_struct( + &mut self, + name: &'static str, + fields: &'static [&'static str], + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> { + self.take() + .deserialize_struct(name, fields, visitor) + .map_err(erase) + } + + fn erased_deserialize_identifier( + &mut self, + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> { + self.take().deserialize_identifier(visitor).map_err(erase) + } + + fn erased_deserialize_enum( + &mut self, + name: &'static str, + variants: &'static [&'static str], + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> { + self.take() + .deserialize_enum(name, variants, visitor) + .map_err(erase) + } + + fn erased_deserialize_ignored_any( + &mut self, + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> { + self.take().deserialize_ignored_any(visitor).map_err(erase) + } + + fn erased_is_human_readable(&self) -> bool { + self.as_ref().is_human_readable() + } +} + +impl<'de, T> Visitor<'de> for erase::Visitor<T> +where + T: serde::de::Visitor<'de>, +{ + fn erased_expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + self.as_ref().expecting(formatter) + } + + fn erased_visit_bool(&mut self, v: bool) -> Result<Out, Error> { + unsafe { self.take().visit_bool(v).unsafe_map(Out::new) } + } + + fn erased_visit_i8(&mut self, v: i8) -> Result<Out, Error> { + unsafe { self.take().visit_i8(v).unsafe_map(Out::new) } + } + + fn erased_visit_i16(&mut self, v: i16) -> Result<Out, Error> { + unsafe { self.take().visit_i16(v).unsafe_map(Out::new) } + } + + fn erased_visit_i32(&mut self, v: i32) -> Result<Out, Error> { + unsafe { self.take().visit_i32(v).unsafe_map(Out::new) } + } + + fn erased_visit_i64(&mut self, v: i64) -> Result<Out, Error> { + unsafe { self.take().visit_i64(v).unsafe_map(Out::new) } + } + + fn erased_visit_i128(&mut self, v: i128) -> Result<Out, Error> { + unsafe { self.take().visit_i128(v).unsafe_map(Out::new) } + } + + fn erased_visit_u8(&mut self, v: u8) -> Result<Out, Error> { + unsafe { self.take().visit_u8(v).unsafe_map(Out::new) } + } + + fn erased_visit_u16(&mut self, v: u16) -> Result<Out, Error> { + unsafe { self.take().visit_u16(v).unsafe_map(Out::new) } + } + + fn erased_visit_u32(&mut self, v: u32) -> Result<Out, Error> { + unsafe { self.take().visit_u32(v).unsafe_map(Out::new) } + } + + fn erased_visit_u64(&mut self, v: u64) -> Result<Out, Error> { + unsafe { self.take().visit_u64(v).unsafe_map(Out::new) } + } + + fn erased_visit_u128(&mut self, v: u128) -> Result<Out, Error> { + unsafe { self.take().visit_u128(v).unsafe_map(Out::new) } + } + + fn erased_visit_f32(&mut self, v: f32) -> Result<Out, Error> { + unsafe { self.take().visit_f32(v).unsafe_map(Out::new) } + } + + fn erased_visit_f64(&mut self, v: f64) -> Result<Out, Error> { + unsafe { self.take().visit_f64(v).unsafe_map(Out::new) } + } + + fn erased_visit_char(&mut self, v: char) -> Result<Out, Error> { + unsafe { self.take().visit_char(v).unsafe_map(Out::new) } + } + + fn erased_visit_str(&mut self, v: &str) -> Result<Out, Error> { + unsafe { self.take().visit_str(v).unsafe_map(Out::new) } + } + + fn erased_visit_borrowed_str(&mut self, v: &'de str) -> Result<Out, Error> { + unsafe { self.take().visit_borrowed_str(v).unsafe_map(Out::new) } + } + + #[cfg(any(feature = "std", feature = "alloc"))] + fn erased_visit_string(&mut self, v: String) -> Result<Out, Error> { + unsafe { self.take().visit_string(v).unsafe_map(Out::new) } + } + + fn erased_visit_bytes(&mut self, v: &[u8]) -> Result<Out, Error> { + unsafe { self.take().visit_bytes(v).unsafe_map(Out::new) } + } + + fn erased_visit_borrowed_bytes(&mut self, v: &'de [u8]) -> Result<Out, Error> { + unsafe { self.take().visit_borrowed_bytes(v).unsafe_map(Out::new) } + } + + #[cfg(any(feature = "std", feature = "alloc"))] + fn erased_visit_byte_buf(&mut self, v: Vec<u8>) -> Result<Out, Error> { + unsafe { self.take().visit_byte_buf(v).unsafe_map(Out::new) } + } + + fn erased_visit_none(&mut self) -> Result<Out, Error> { + unsafe { self.take().visit_none().unsafe_map(Out::new) } + } + + fn erased_visit_some( + &mut self, + deserializer: &mut dyn Deserializer<'de>, + ) -> Result<Out, Error> { + unsafe { self.take().visit_some(deserializer).unsafe_map(Out::new) } + } + + fn erased_visit_unit(&mut self) -> Result<Out, Error> { + unsafe { self.take().visit_unit().unsafe_map(Out::new) } + } + + fn erased_visit_newtype_struct( + &mut self, + deserializer: &mut dyn Deserializer<'de>, + ) -> Result<Out, Error> { + unsafe { + self.take() + .visit_newtype_struct(deserializer) + .unsafe_map(Out::new) + } + } + + fn erased_visit_seq(&mut self, seq: &mut dyn SeqAccess<'de>) -> Result<Out, Error> { + unsafe { self.take().visit_seq(seq).unsafe_map(Out::new) } + } + + fn erased_visit_map(&mut self, map: &mut dyn MapAccess<'de>) -> Result<Out, Error> { + unsafe { self.take().visit_map(map).unsafe_map(Out::new) } + } + + fn erased_visit_enum(&mut self, data: &mut dyn EnumAccess<'de>) -> Result<Out, Error> { + unsafe { self.take().visit_enum(data).unsafe_map(Out::new) } + } +} + +impl<'de, T> SeqAccess<'de> for erase::SeqAccess<T> +where + T: serde::de::SeqAccess<'de>, +{ + fn erased_next_element( + &mut self, + seed: &mut dyn DeserializeSeed<'de>, + ) -> Result<Option<Out>, Error> { + self.as_mut().next_element_seed(seed).map_err(erase) + } + + fn erased_size_hint(&self) -> Option<usize> { + self.as_ref().size_hint() + } +} + +impl<'de, T> MapAccess<'de> for erase::MapAccess<T> +where + T: serde::de::MapAccess<'de>, +{ + fn erased_next_key( + &mut self, + seed: &mut dyn DeserializeSeed<'de>, + ) -> Result<Option<Out>, Error> { + self.as_mut().next_key_seed(seed).map_err(erase) + } + + fn erased_next_value(&mut self, seed: &mut dyn DeserializeSeed<'de>) -> Result<Out, Error> { + self.as_mut().next_value_seed(seed).map_err(erase) + } + + fn erased_next_entry( + &mut self, + k: &mut dyn DeserializeSeed<'de>, + v: &mut dyn DeserializeSeed<'de>, + ) -> Result<Option<(Out, Out)>, Error> { + self.as_mut().next_entry_seed(k, v).map_err(erase) + } + + fn erased_size_hint(&self) -> Option<usize> { + self.as_ref().size_hint() + } +} + +impl<'de, T> EnumAccess<'de> for erase::EnumAccess<T> +where + T: serde::de::EnumAccess<'de>, +{ + fn erased_variant_seed( + &mut self, + seed: &mut dyn DeserializeSeed<'de>, + ) -> Result<(Out, Variant<'de>), Error> { + self.take() + .variant_seed(seed) + .map(|(out, variant)| { + use serde::de::VariantAccess; + let erased = Variant { + data: unsafe { Any::new(variant) }, + unit_variant: { + unsafe fn unit_variant<'de, T>(a: Any) -> Result<(), Error> + where + T: serde::de::EnumAccess<'de>, + { + unsafe { a.take::<T::Variant>().unit_variant().map_err(erase) } + } + unit_variant::<T> + }, + visit_newtype: { + unsafe fn visit_newtype<'de, T>( + a: Any, + seed: &mut dyn DeserializeSeed<'de>, + ) -> Result<Out, Error> + where + T: serde::de::EnumAccess<'de>, + { + unsafe { + a.take::<T::Variant>() + .newtype_variant_seed(seed) + .map_err(erase) + } + } + visit_newtype::<T> + }, + tuple_variant: { + unsafe fn tuple_variant<'de, T>( + a: Any, + len: usize, + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> + where + T: serde::de::EnumAccess<'de>, + { + unsafe { + a.take::<T::Variant>() + .tuple_variant(len, visitor) + .map_err(erase) + } + } + tuple_variant::<T> + }, + struct_variant: { + unsafe fn struct_variant<'de, T>( + a: Any, + fields: &'static [&'static str], + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error> + where + T: serde::de::EnumAccess<'de>, + { + unsafe { + a.take::<T::Variant>() + .struct_variant(fields, visitor) + .map_err(erase) + } + } + struct_variant::<T> + }, + }; + (out, erased) + }) + .map_err(erase) + } +} + +// IMPL SERDE FOR ERASED SERDE ///////////////////////////////////////////////// + +impl<'de> serde::de::DeserializeSeed<'de> for &mut (dyn DeserializeSeed<'de> + '_) { + type Value = Out; + fn deserialize<D>(self, deserializer: D) -> Result<Out, D::Error> + where + D: serde::Deserializer<'de>, + { + let mut erased = erase::Deserializer { + state: Some(deserializer), + }; + self.erased_deserialize_seed(&mut erased).map_err(unerase) + } +} + +macro_rules! impl_deserializer_for_trait_object { + ({$($mut:tt)*} $ty:ty) => { + impl<'de> serde::Deserializer<'de> for $ty { + type Error = Error; + + fn deserialize_any<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_any(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_bool<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_bool(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_i8<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_i8(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_i16<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_i16(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_i32<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_i32(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_i64<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_i64(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_i128<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_i128(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_u8<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_u8(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_u16<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_u16(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_u32<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_u32(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_u64<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_u64(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_u128<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_u128(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_f32<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_f32(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_f64<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_f64(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_char<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_char(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_str<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_str(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_string<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_string(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_bytes<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_bytes(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_byte_buf<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_byte_buf(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_option<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_option(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_unit<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_unit(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_unit_struct<V>($($mut)* self, name: &'static str, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_unit_struct(name, &mut erased).unsafe_map(Out::take) } + } + + fn deserialize_newtype_struct<V>($($mut)* self, name: &'static str, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_newtype_struct(name, &mut erased).unsafe_map(Out::take) } + } + + fn deserialize_seq<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_seq(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_tuple<V>($($mut)* self, len: usize, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_tuple(len, &mut erased).unsafe_map(Out::take) } + } + + fn deserialize_tuple_struct<V>($($mut)* self, name: &'static str, len: usize, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_tuple_struct(name, len, &mut erased).unsafe_map(Out::take) } + } + + fn deserialize_map<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_map(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_struct<V>($($mut)* self, name: &'static str, fields: &'static [&'static str], visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_struct(name, fields, &mut erased).unsafe_map(Out::take) } + } + + fn deserialize_identifier<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_identifier(&mut erased).unsafe_map(Out::take) } + } + + fn deserialize_enum<V>($($mut)* self, name: &'static str, variants: &'static [&'static str], visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_enum(name, variants, &mut erased).unsafe_map(Out::take) } + } + + fn deserialize_ignored_any<V>($($mut)* self, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { state: Some(visitor) }; + unsafe { self.erased_deserialize_ignored_any(&mut erased).unsafe_map(Out::take) } + } + + fn is_human_readable(&self) -> bool { + self.erased_is_human_readable() + } + } + }; +} + +impl_deserializer_for_trait_object!({} &mut (dyn Deserializer<'de> + '_)); +impl_deserializer_for_trait_object!({} &mut (dyn Deserializer<'de> + Send + '_)); +impl_deserializer_for_trait_object!({} &mut (dyn Deserializer<'de> + Sync + '_)); +impl_deserializer_for_trait_object!({} &mut (dyn Deserializer<'de> + Send + Sync + '_)); +impl_deserializer_for_trait_object!({mut} Box<dyn Deserializer<'de> + '_>); +impl_deserializer_for_trait_object!({mut} Box<dyn Deserializer<'de> + Send + '_>); +impl_deserializer_for_trait_object!({mut} Box<dyn Deserializer<'de> + Sync + '_>); +impl_deserializer_for_trait_object!({mut} Box<dyn Deserializer<'de> + Send + Sync + '_>); + +impl<'de> serde::de::Visitor<'de> for &mut (dyn Visitor<'de> + '_) { + type Value = Out; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + (**self).erased_expecting(formatter) + } + + fn visit_bool<E>(self, v: bool) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_bool(v).map_err(unerase) + } + + fn visit_i8<E>(self, v: i8) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_i8(v).map_err(unerase) + } + + fn visit_i16<E>(self, v: i16) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_i16(v).map_err(unerase) + } + + fn visit_i32<E>(self, v: i32) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_i32(v).map_err(unerase) + } + + fn visit_i64<E>(self, v: i64) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_i64(v).map_err(unerase) + } + + fn visit_i128<E>(self, v: i128) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_i128(v).map_err(unerase) + } + + fn visit_u8<E>(self, v: u8) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_u8(v).map_err(unerase) + } + + fn visit_u16<E>(self, v: u16) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_u16(v).map_err(unerase) + } + + fn visit_u32<E>(self, v: u32) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_u32(v).map_err(unerase) + } + + fn visit_u64<E>(self, v: u64) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_u64(v).map_err(unerase) + } + + fn visit_u128<E>(self, v: u128) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_u128(v).map_err(unerase) + } + + fn visit_f32<E>(self, v: f32) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_f32(v).map_err(unerase) + } + + fn visit_f64<E>(self, v: f64) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_f64(v).map_err(unerase) + } + + fn visit_char<E>(self, v: char) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_char(v).map_err(unerase) + } + + fn visit_str<E>(self, v: &str) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_str(v).map_err(unerase) + } + + fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_borrowed_str(v).map_err(unerase) + } + + #[cfg(any(feature = "std", feature = "alloc"))] + fn visit_string<E>(self, v: String) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_string(v).map_err(unerase) + } + + fn visit_bytes<E>(self, v: &[u8]) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_bytes(v).map_err(unerase) + } + + fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_borrowed_bytes(v).map_err(unerase) + } + + #[cfg(any(feature = "std", feature = "alloc"))] + fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_byte_buf(v).map_err(unerase) + } + + fn visit_none<E>(self) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_none().map_err(unerase) + } + + fn visit_some<D>(self, deserializer: D) -> Result<Out, D::Error> + where + D: serde::Deserializer<'de>, + { + let mut erased = erase::Deserializer { + state: Some(deserializer), + }; + self.erased_visit_some(&mut erased).map_err(unerase) + } + + fn visit_unit<E>(self) -> Result<Out, E> + where + E: serde::de::Error, + { + self.erased_visit_unit().map_err(unerase) + } + + fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Out, D::Error> + where + D: serde::Deserializer<'de>, + { + let mut erased = erase::Deserializer { + state: Some(deserializer), + }; + self.erased_visit_newtype_struct(&mut erased) + .map_err(unerase) + } + + fn visit_seq<V>(self, seq: V) -> Result<Out, V::Error> + where + V: serde::de::SeqAccess<'de>, + { + let mut erased = erase::SeqAccess { state: seq }; + self.erased_visit_seq(&mut erased).map_err(unerase) + } + + fn visit_map<V>(self, map: V) -> Result<Out, V::Error> + where + V: serde::de::MapAccess<'de>, + { + let mut erased = erase::MapAccess { state: map }; + self.erased_visit_map(&mut erased).map_err(unerase) + } + + fn visit_enum<V>(self, data: V) -> Result<Out, V::Error> + where + V: serde::de::EnumAccess<'de>, + { + let mut erased = erase::EnumAccess { state: Some(data) }; + self.erased_visit_enum(&mut erased).map_err(unerase) + } +} + +impl<'de> serde::de::SeqAccess<'de> for &mut (dyn SeqAccess<'de> + '_) { + type Error = Error; + + fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error> + where + T: serde::de::DeserializeSeed<'de>, + { + let mut seed = erase::DeserializeSeed { state: Some(seed) }; + unsafe { + (**self) + .erased_next_element(&mut seed) + .map(|opt| opt.unsafe_map(Out::take)) + } + } + + fn size_hint(&self) -> Option<usize> { + (**self).erased_size_hint() + } +} + +impl<'de> serde::de::MapAccess<'de> for &mut (dyn MapAccess<'de> + '_) { + type Error = Error; + + fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> + where + K: serde::de::DeserializeSeed<'de>, + { + let mut erased = erase::DeserializeSeed { state: Some(seed) }; + unsafe { + (**self) + .erased_next_key(&mut erased) + .map(|opt| opt.unsafe_map(Out::take)) + } + } + + fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> + where + V: serde::de::DeserializeSeed<'de>, + { + let mut erased = erase::DeserializeSeed { state: Some(seed) }; + unsafe { + (**self) + .erased_next_value(&mut erased) + .unsafe_map(Out::take) + } + } + + fn size_hint(&self) -> Option<usize> { + (**self).erased_size_hint() + } +} + +impl<'de> serde::de::EnumAccess<'de> for &mut (dyn EnumAccess<'de> + '_) { + type Error = Error; + type Variant = Variant<'de>; + + fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> + where + V: serde::de::DeserializeSeed<'de>, + { + let mut erased = erase::DeserializeSeed { state: Some(seed) }; + match self.erased_variant_seed(&mut erased) { + Ok((out, variant)) => Ok((unsafe { out.take() }, variant)), + Err(err) => Err(err), + } + } +} + +pub struct Variant<'de> { + data: Any, + unit_variant: unsafe fn(Any) -> Result<(), Error>, + visit_newtype: unsafe fn(Any, seed: &mut dyn DeserializeSeed<'de>) -> Result<Out, Error>, + tuple_variant: unsafe fn(Any, len: usize, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error>, + struct_variant: unsafe fn( + Any, + fields: &'static [&'static str], + visitor: &mut dyn Visitor<'de>, + ) -> Result<Out, Error>, +} + +impl<'de> serde::de::VariantAccess<'de> for Variant<'de> { + type Error = Error; + + fn unit_variant(self) -> Result<(), Error> { + unsafe { (self.unit_variant)(self.data) } + } + + fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Error> + where + T: serde::de::DeserializeSeed<'de>, + { + let mut erased = erase::DeserializeSeed { state: Some(seed) }; + unsafe { (self.visit_newtype)(self.data, &mut erased).unsafe_map(Out::take) } + } + + fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { + state: Some(visitor), + }; + unsafe { (self.tuple_variant)(self.data, len, &mut erased).unsafe_map(Out::take) } + } + + fn struct_variant<V>( + self, + fields: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, Error> + where + V: serde::de::Visitor<'de>, + { + let mut erased = erase::Visitor { + state: Some(visitor), + }; + unsafe { (self.struct_variant)(self.data, fields, &mut erased).unsafe_map(Out::take) } + } +} + +// IMPL ERASED SERDE FOR ERASED SERDE ////////////////////////////////////////// + +macro_rules! deref_erased_deserializer { + ($($imp:tt)+) => { + impl $($imp)+ { + fn erased_deserialize_any(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_any(visitor) + } + + fn erased_deserialize_bool(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_bool(visitor) + } + + fn erased_deserialize_i8(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_i8(visitor) + } + + fn erased_deserialize_i16(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_i16(visitor) + } + + fn erased_deserialize_i32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_i32(visitor) + } + + fn erased_deserialize_i64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_i64(visitor) + } + + fn erased_deserialize_i128(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_i128(visitor) + } + + fn erased_deserialize_u8(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_u8(visitor) + } + + fn erased_deserialize_u16(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_u16(visitor) + } + + fn erased_deserialize_u32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_u32(visitor) + } + + fn erased_deserialize_u64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_u64(visitor) + } + + fn erased_deserialize_u128(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_u128(visitor) + } + + fn erased_deserialize_f32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_f32(visitor) + } + + fn erased_deserialize_f64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_f64(visitor) + } + + fn erased_deserialize_char(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_char(visitor) + } + + fn erased_deserialize_str(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_str(visitor) + } + + fn erased_deserialize_string(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_string(visitor) + } + + fn erased_deserialize_bytes(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_bytes(visitor) + } + + fn erased_deserialize_byte_buf(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_byte_buf(visitor) + } + + fn erased_deserialize_option(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_option(visitor) + } + + fn erased_deserialize_unit(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_unit(visitor) + } + + fn erased_deserialize_unit_struct(&mut self, name: &'static str, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_unit_struct(name, visitor) + } + + fn erased_deserialize_newtype_struct(&mut self, name: &'static str, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_newtype_struct(name, visitor) + } + + fn erased_deserialize_seq(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_seq(visitor) + } + + fn erased_deserialize_tuple(&mut self, len: usize, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_tuple(len, visitor) + } + + fn erased_deserialize_tuple_struct(&mut self, name: &'static str, len: usize, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_tuple_struct(name, len, visitor) + } + + fn erased_deserialize_map(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_map(visitor) + } + + fn erased_deserialize_struct(&mut self, name: &'static str, fields: &'static [&'static str], visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_struct(name, fields, visitor) + } + + fn erased_deserialize_identifier(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_identifier(visitor) + } + + fn erased_deserialize_enum(&mut self, name: &'static str, variants: &'static [&'static str], visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_enum(name, variants, visitor) + } + + fn erased_deserialize_ignored_any(&mut self, visitor: &mut dyn Visitor<'de>) -> Result<Out, Error> { + (**self).erased_deserialize_ignored_any(visitor) + } + + fn erased_is_human_readable(&self) -> bool { + (**self).erased_is_human_readable() + } + } + }; +} + +deref_erased_deserializer!(<'de> Deserializer<'de> for Box<dyn Deserializer<'de> + '_>); +deref_erased_deserializer!(<'de> Deserializer<'de> for Box<dyn Deserializer<'de> + Send + '_>); +deref_erased_deserializer!(<'de> Deserializer<'de> for Box<dyn Deserializer<'de> + Sync + '_>); +deref_erased_deserializer!(<'de> Deserializer<'de> for Box<dyn Deserializer<'de> + Send + Sync + '_>); +deref_erased_deserializer!(<'de, T: ?Sized + Deserializer<'de>> Deserializer<'de> for &mut T); + +// ERROR /////////////////////////////////////////////////////////////////////// + +fn erase<E>(e: E) -> Error +where + E: Display, +{ + serde::de::Error::custom(e) +} + +fn unerase<E>(e: Error) -> E +where + E: serde::de::Error, +{ + E::custom(e) +} + +// TEST //////////////////////////////////////////////////////////////////////// + +#[cfg(test)] +mod tests { + use super::*; + use crate::alloc::ToOwned; + use core::fmt::Debug; + use serde_derive::Deserialize; + + fn test_json<'de, T>(json: &'de [u8]) + where + T: serde::Deserialize<'de> + PartialEq + Debug, + { + let expected: T = serde_json::from_slice(json).unwrap(); + + // test borrowed trait object + { + let mut de = serde_json::Deserializer::from_slice(json); + let de: &mut dyn Deserializer = &mut <dyn Deserializer>::erase(&mut de); + assert_eq!(expected, deserialize::<T>(de).unwrap()); + } + + // test boxed trait object + { + let mut de = serde_json::Deserializer::from_slice(json); + let mut de: Box<dyn Deserializer> = Box::new(<dyn Deserializer>::erase(&mut de)); + assert_eq!(expected, deserialize::<T>(&mut de).unwrap()); + } + } + + #[test] + fn test_value() { + test_json::<serde_json::Value>(br#"["a", 1, [true], {"a": 1}]"#); + } + + #[test] + fn test_struct() { + #[derive(Deserialize, PartialEq, Debug)] + struct S { + f: usize, + } + + test_json::<S>(br#"{"f":256}"#); + } + + #[test] + fn test_enum() { + #[derive(Deserialize, PartialEq, Debug)] + enum E { + Unit, + Newtype(bool), + Tuple(bool, bool), + Struct { t: bool, f: bool }, + } + + test_json::<E>(br#""Unit""#); + test_json::<E>(br#"{"Newtype":true}"#); + test_json::<E>(br#"{"Tuple":[true,false]}"#); + test_json::<E>(br#"{"Struct":{"t":true,"f":false}}"#); + } + + #[test] + fn test_borrowed() { + let bytes = br#""borrowed""#.to_owned(); + test_json::<&str>(&bytes); + } + + #[test] + fn assert_deserializer() { + fn assert<'de, T: serde::Deserializer<'de>>() {} + + assert::<&mut dyn Deserializer>(); + assert::<&mut (dyn Deserializer + Send)>(); + assert::<&mut (dyn Deserializer + Sync)>(); + assert::<&mut (dyn Deserializer + Send + Sync)>(); + assert::<&mut (dyn Deserializer + Sync + Send)>(); + + assert::<Box<dyn Deserializer>>(); + assert::<Box<dyn Deserializer + Send>>(); + assert::<Box<dyn Deserializer + Sync>>(); + assert::<Box<dyn Deserializer + Send + Sync>>(); + assert::<Box<dyn Deserializer + Sync + Send>>(); + } +} diff --git a/vendor/erased-serde/src/error.rs b/vendor/erased-serde/src/error.rs new file mode 100644 index 000000000..20952775b --- /dev/null +++ b/vendor/erased-serde/src/error.rs @@ -0,0 +1,35 @@ +use crate::alloc::{String, ToString}; +use core::fmt::{self, Display}; + +/// Error when a `Serializer` or `Deserializer` trait object fails. +#[derive(Debug)] +pub struct Error { + msg: String, +} + +/// Result type alias where the error is `erased_serde::Error`. +pub type Result<T> = core::result::Result<T, Error>; + +impl Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + self.msg.fmt(formatter) + } +} + +impl serde::ser::StdError for Error {} + +impl serde::ser::Error for Error { + fn custom<T: Display>(msg: T) -> Self { + Error { + msg: msg.to_string(), + } + } +} + +impl serde::de::Error for Error { + fn custom<T: Display>(msg: T) -> Self { + Error { + msg: msg.to_string(), + } + } +} diff --git a/vendor/erased-serde/src/features_check/error.rs b/vendor/erased-serde/src/features_check/error.rs new file mode 100644 index 000000000..b5e6060b5 --- /dev/null +++ b/vendor/erased-serde/src/features_check/error.rs @@ -0,0 +1 @@ +"erased-serde requires that either `std` (default) or `alloc` feature is enabled" diff --git a/vendor/erased-serde/src/features_check/mod.rs b/vendor/erased-serde/src/features_check/mod.rs new file mode 100644 index 000000000..d12032cef --- /dev/null +++ b/vendor/erased-serde/src/features_check/mod.rs @@ -0,0 +1,13 @@ +//! Shows a user-friendly compiler error on incompatible selected features. + +#[allow(unused_macros)] +macro_rules! hide_from_rustfmt { + ($mod:item) => { + $mod + }; +} + +#[cfg(not(any(feature = "std", feature = "alloc")))] +hide_from_rustfmt! { + mod error; +} diff --git a/vendor/erased-serde/src/lib.rs b/vendor/erased-serde/src/lib.rs new file mode 100644 index 000000000..6b52ccc1b --- /dev/null +++ b/vendor/erased-serde/src/lib.rs @@ -0,0 +1,144 @@ +//! [![github]](https://github.com/dtolnay/erased-serde) [![crates-io]](https://crates.io/crates/erased-serde) [![docs-rs]](https://docs.rs/erased-serde) +//! +//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github +//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust +//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs +//! +//! <br> +//! +//! This crate provides type-erased versions of Serde's `Serialize`, `Serializer` +//! and `Deserializer` traits that can be used as [trait objects]. +//! +//! [trait objects]: https://doc.rust-lang.org/book/trait-objects.html +//! +//! The usual Serde `Serialize`, `Serializer` and `Deserializer` traits cannot +//! be used as trait objects like `&dyn Serialize` or boxed trait objects like +//! `Box<dyn Serialize>` because of Rust's ["object safety" rules]. In +//! particular, all three traits contain generic methods which cannot be made +//! into a trait object. +//! +//! ["object safety" rules]: http://huonw.github.io/blog/2015/01/object-safety/ +//! +//! This library should be considered a low-level building block for interacting +//! with Serde APIs in an object-safe way. Most use cases will require higher +//! level functionality such as provided by [`typetag`] which uses this crate +//! internally. +//! +//! [`typetag`]: https://github.com/dtolnay/typetag +//! +//! **The traits in this crate work seamlessly with any existing Serde +//! `Serialize` and `Deserialize` type and any existing Serde `Serializer` and +//! `Deserializer` format.** +//! +//! ## Serialization +//! +//! ```rust +//! use erased_serde::{Serialize, Serializer}; +//! use std::collections::BTreeMap as Map; +//! use std::io; +//! +//! fn main() { +//! // Construct some serializers. +//! let json = &mut serde_json::Serializer::new(io::stdout()); +//! let cbor = &mut serde_cbor::Serializer::new(serde_cbor::ser::IoWrite::new(io::stdout())); +//! +//! // The values in this map are boxed trait objects. Ordinarily this would not +//! // be possible with serde::Serializer because of object safety, but type +//! // erasure makes it possible with erased_serde::Serializer. +//! let mut formats: Map<&str, Box<dyn Serializer>> = Map::new(); +//! formats.insert("json", Box::new(<dyn Serializer>::erase(json))); +//! formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor))); +//! +//! // These are boxed trait objects as well. Same thing here - type erasure +//! // makes this possible. +//! let mut values: Map<&str, Box<dyn Serialize>> = Map::new(); +//! values.insert("vec", Box::new(vec!["a", "b"])); +//! values.insert("int", Box::new(65536)); +//! +//! // Pick a Serializer out of the formats map. +//! let format = formats.get_mut("json").unwrap(); +//! +//! // Pick a Serialize out of the values map. +//! let value = values.get("vec").unwrap(); +//! +//! // This line prints `["a","b"]` to stdout. +//! value.erased_serialize(format).unwrap(); +//! } +//! ``` +//! +//! ## Deserialization +//! +//! ```rust +//! use erased_serde::Deserializer; +//! use std::collections::BTreeMap as Map; +//! +//! fn main() { +//! static JSON: &'static [u8] = br#"{"A": 65, "B": 66}"#; +//! static CBOR: &'static [u8] = &[162, 97, 65, 24, 65, 97, 66, 24, 66]; +//! +//! // Construct some deserializers. +//! let json = &mut serde_json::Deserializer::from_slice(JSON); +//! let cbor = &mut serde_cbor::Deserializer::from_slice(CBOR); +//! +//! // The values in this map are boxed trait objects, which is not possible +//! // with the normal serde::Deserializer because of object safety. +//! let mut formats: Map<&str, Box<dyn Deserializer>> = Map::new(); +//! formats.insert("json", Box::new(<dyn Deserializer>::erase(json))); +//! formats.insert("cbor", Box::new(<dyn Deserializer>::erase(cbor))); +//! +//! // Pick a Deserializer out of the formats map. +//! let format = formats.get_mut("json").unwrap(); +//! +//! let data: Map<String, usize> = erased_serde::deserialize(format).unwrap(); +//! +//! println!("{}", data["A"] + data["B"]); +//! } +//! ``` + +#![doc(html_root_url = "https://docs.rs/erased-serde/0.3.31")] +#![cfg_attr(not(feature = "std"), no_std)] +#![deny(unsafe_op_in_unsafe_fn)] +#![allow( + clippy::derive_partial_eq_without_eq, + clippy::extra_unused_type_parameters, + clippy::items_after_statements, + clippy::manual_map, // https://github.com/rust-lang/rust-clippy/issues/7820 + clippy::missing_errors_doc, + clippy::needless_doctest_main, + clippy::needless_pass_by_ref_mut, + clippy::semicolon_if_nothing_returned, // https://github.com/rust-lang/rust-clippy/issues/7324 + clippy::unused_self, + clippy::wildcard_imports +)] + +mod alloc { + #[cfg(not(feature = "std"))] + extern crate alloc; + + #[cfg(feature = "std")] + use std as alloc; + + pub use self::alloc::borrow::ToOwned; + pub use self::alloc::boxed::Box; + pub use self::alloc::string::{String, ToString}; + pub use self::alloc::{vec, vec::Vec}; +} + +#[macro_use] +mod macros; + +mod any; +mod de; +mod error; +mod features_check; +mod map; +mod ser; + +pub use crate::de::{deserialize, Deserializer}; +pub use crate::error::{Error, Result}; +pub use crate::ser::{serialize, Serialize, Serializer}; + +// Not public API. +#[doc(hidden)] +#[path = "private.rs"] +pub mod __private; diff --git a/vendor/erased-serde/src/macros.rs b/vendor/erased-serde/src/macros.rs new file mode 100644 index 000000000..393798c46 --- /dev/null +++ b/vendor/erased-serde/src/macros.rs @@ -0,0 +1,165 @@ +/// Implement `serde::Serialize` for a trait object that has +/// `erased_serde::Serialize` as a supertrait. +/// +/// ``` +/// use erased_serde::serialize_trait_object; +/// +/// trait Event: erased_serde::Serialize { +/// /* ... */ +/// } +/// +/// erased_serde::serialize_trait_object!(Event); +/// ``` +/// +/// The macro supports traits that have type parameters and/or `where` clauses. +/// +/// ``` +/// # use erased_serde::serialize_trait_object; +/// # +/// trait Difficult<T>: erased_serde::Serialize where T: Copy { +/// /* ... */ +/// } +/// +/// serialize_trait_object!(<T> Difficult<T> where T: Copy); +/// ``` +#[macro_export] +macro_rules! serialize_trait_object { + ($($path:tt)+) => { + $crate::__internal_serialize_trait_object!(begin $($path)+); + }; +} + +#[doc(hidden)] +#[macro_export] +macro_rules! __internal_serialize_trait_object { + // Invocation started with `<`, parse generics. + (begin < $($rest:tt)*) => { + $crate::__internal_serialize_trait_object!(generics () () $($rest)*); + }; + + // Invocation did not start with `<`. + (begin $first:tt $($rest:tt)*) => { + $crate::__internal_serialize_trait_object!(path () ($first) $($rest)*); + }; + + // End of generics with trailing comma. + (generics ($($generics:tt)*) () , > $($rest:tt)*) => { + $crate::__internal_serialize_trait_object!(path ($($generics)* ,) () $($rest)*); + }; + + // End of generics without trailing comma. + (generics ($($generics:tt)*) () > $($rest:tt)*) => { + $crate::__internal_serialize_trait_object!(path ($($generics)* ,) () $($rest)*); + }; + + // Generics open bracket. + (generics ($($generics:tt)*) ($($brackets:tt)*) < $($rest:tt)*) => { + $crate::__internal_serialize_trait_object!(generics ($($generics)* <) ($($brackets)* <) $($rest)*); + }; + + // Generics close bracket. + (generics ($($generics:tt)*) (< $($brackets:tt)*) > $($rest:tt)*) => { + $crate::__internal_serialize_trait_object!(generics ($($generics)* >) ($($brackets)*) $($rest)*); + }; + + // Token inside of generics. + (generics ($($generics:tt)*) ($($brackets:tt)*) $first:tt $($rest:tt)*) => { + $crate::__internal_serialize_trait_object!(generics ($($generics)* $first) ($($brackets)*) $($rest)*); + }; + + // End with `where` clause. + (path ($($generics:tt)*) ($($path:tt)*) where $($rest:tt)*) => { + $crate::__internal_serialize_trait_object!(sendsync ($($generics)*) ($($path)*) ($($rest)*)); + }; + + // End without `where` clause. + (path ($($generics:tt)*) ($($path:tt)*)) => { + $crate::__internal_serialize_trait_object!(sendsync ($($generics)*) ($($path)*) ()); + }; + + // Token inside of path. + (path ($($generics:tt)*) ($($path:tt)*) $first:tt $($rest:tt)*) => { + $crate::__internal_serialize_trait_object!(path ($($generics)*) ($($path)* $first) $($rest)*); + }; + + // Expand into four impls. + (sendsync ($($generics:tt)*) ($($path:tt)*) ($($bound:tt)*)) => { + $crate::__internal_serialize_trait_object!(impl ($($generics)*) ($($path)*) ($($bound)*) { + fn __check_erased_serialize_supertrait<$($generics)* __T>() + where + __T: ?$crate::__private::Sized + $($path)*, + $($bound)* + { + $crate::__private::require_erased_serialize_impl::<__T>(); + } + }); + $crate::__internal_serialize_trait_object!(impl ($($generics)*) ($($path)* + $crate::__private::Send) ($($bound)*)); + $crate::__internal_serialize_trait_object!(impl ($($generics)*) ($($path)* + $crate::__private::Sync) ($($bound)*)); + $crate::__internal_serialize_trait_object!(impl ($($generics)*) ($($path)* + $crate::__private::Send + $crate::__private::Sync) ($($bound)*)); + }; + + // The impl. + (impl ($($generics:tt)*) ($($path:tt)*) ($($bound:tt)*) $({$($body:tt)*})*) => { + impl<'erased, $($generics)*> $crate::__private::serde::Serialize for dyn $($path)* + 'erased + where + $($bound)* + { + fn serialize<S>(&self, serializer: S) -> $crate::__private::Result<S::Ok, S::Error> + where + S: $crate::__private::serde::Serializer, + { + $($($body)*)* + $crate::serialize(self, serializer) + } + } + }; +} + +// TEST //////////////////////////////////////////////////////////////////////// + +#[cfg(test)] +mod tests { + use crate::Serialize; + + fn assert_serialize<T: ?Sized + serde::Serialize>() {} + + #[test] + fn test_plain() { + trait Trait: Serialize {} + + serialize_trait_object!(Trait); + assert_serialize::<dyn Trait>(); + assert_serialize::<dyn Trait + Send>(); + } + + #[test] + fn test_type_parameter() { + trait Trait<T>: Serialize {} + + serialize_trait_object!(<T> Trait<T>); + assert_serialize::<dyn Trait<u32>>(); + assert_serialize::<dyn Trait<u32> + Send>(); + } + + #[test] + fn test_generic_bound() { + trait Trait<T: PartialEq<T>, U>: Serialize {} + + serialize_trait_object!(<T: PartialEq<T>, U> Trait<T, U>); + assert_serialize::<dyn Trait<u32, ()>>(); + assert_serialize::<dyn Trait<u32, ()> + Send>(); + } + + #[test] + fn test_where_clause() { + trait Trait<T>: Serialize + where + T: Clone, + { + } + + serialize_trait_object!(<T> Trait<T> where T: Clone); + assert_serialize::<dyn Trait<u32>>(); + assert_serialize::<dyn Trait<u32> + Send>(); + } +} diff --git a/vendor/erased-serde/src/map.rs b/vendor/erased-serde/src/map.rs new file mode 100644 index 000000000..c670805e3 --- /dev/null +++ b/vendor/erased-serde/src/map.rs @@ -0,0 +1,25 @@ +pub(crate) trait ResultExt<T, E> { + unsafe fn unsafe_map<U>(self, op: unsafe fn(T) -> U) -> Result<U, E>; +} + +impl<T, E> ResultExt<T, E> for Result<T, E> { + unsafe fn unsafe_map<U>(self, op: unsafe fn(T) -> U) -> Result<U, E> { + match self { + Ok(t) => Ok(unsafe { op(t) }), + Err(e) => Err(e), + } + } +} + +pub(crate) trait OptionExt<T> { + unsafe fn unsafe_map<U>(self, op: unsafe fn(T) -> U) -> Option<U>; +} + +impl<T> OptionExt<T> for Option<T> { + unsafe fn unsafe_map<U>(self, op: unsafe fn(T) -> U) -> Option<U> { + match self { + Some(t) => Some(unsafe { op(t) }), + None => None, + } + } +} diff --git a/vendor/erased-serde/src/private.rs b/vendor/erased-serde/src/private.rs new file mode 100644 index 000000000..c23ab3722 --- /dev/null +++ b/vendor/erased-serde/src/private.rs @@ -0,0 +1,16 @@ +//! Not public API. Used as `$crate::__private` by macros. + +#[doc(hidden)] +pub use core::marker::{Send, Sized, Sync}; +#[doc(hidden)] +pub use serde; + +#[doc(hidden)] +pub type Result<T, E> = core::result::Result<T, E>; + +#[doc(hidden)] +pub fn require_erased_serialize_impl<T>() +where + T: ?Sized + crate::Serialize, +{ +} diff --git a/vendor/erased-serde/src/ser.rs b/vendor/erased-serde/src/ser.rs new file mode 100644 index 000000000..10c486bcb --- /dev/null +++ b/vendor/erased-serde/src/ser.rs @@ -0,0 +1,1495 @@ +use crate::alloc::Box; +use crate::any::Any; +use crate::error::Error; +use crate::map::ResultExt; +use core::fmt::Display; +use core::marker::PhantomData; +use serde::ser::{ + SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple, + SerializeTupleStruct, SerializeTupleVariant, +}; + +// TRAITS ////////////////////////////////////////////////////////////////////// + +/// An object-safe equivalent of Serde's `Serialize` trait. +/// +/// Any implementation of Serde's `Serialize` converts seamlessly to an +/// `&erased_serde::Serialize` or `Box<erased_serde::Serialize>` trait object. +/// +/// ```rust +/// use erased_serde::{Serialize, Serializer}; +/// use std::collections::BTreeMap as Map; +/// use std::io; +/// +/// fn main() { +/// // Construct some serializers. +/// let json = &mut serde_json::Serializer::new(io::stdout()); +/// let cbor = &mut serde_cbor::Serializer::new(serde_cbor::ser::IoWrite::new(io::stdout())); +/// +/// // The values in this map are boxed trait objects. Ordinarily this would not +/// // be possible with serde::Serializer because of object safety, but type +/// // erasure makes it possible with erased_serde::Serializer. +/// let mut formats: Map<&str, Box<dyn Serializer>> = Map::new(); +/// formats.insert("json", Box::new(<dyn Serializer>::erase(json))); +/// formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor))); +/// +/// // These are boxed trait objects as well. Same thing here - type erasure +/// // makes this possible. +/// let mut values: Map<&str, Box<dyn Serialize>> = Map::new(); +/// values.insert("vec", Box::new(vec!["a", "b"])); +/// values.insert("int", Box::new(65536)); +/// +/// // Pick a Serializer out of the formats map. +/// let format = formats.get_mut("json").unwrap(); +/// +/// // Pick a Serialize out of the values map. +/// let value = values.get("vec").unwrap(); +/// +/// // This line prints `["a","b"]` to stdout. +/// value.erased_serialize(format).unwrap(); +/// } +/// ``` +pub trait Serialize { + fn erased_serialize(&self, v: &mut dyn Serializer) -> Result<Ok, Error>; +} + +/// An object-safe equivalent of Serde's `Serializer` trait. +/// +/// Any implementation of Serde's `Serializer` can be converted to an +/// `&erased_serde::Serializer` or `Box<erased_serde::Serializer>` trait object +/// using `erased_serde::Serializer::erase`. +/// +/// ```rust +/// use erased_serde::{Serialize, Serializer}; +/// use std::collections::BTreeMap as Map; +/// use std::io; +/// +/// fn main() { +/// // Construct some serializers. +/// let json = &mut serde_json::Serializer::new(io::stdout()); +/// let cbor = &mut serde_cbor::Serializer::new(serde_cbor::ser::IoWrite::new(io::stdout())); +/// +/// // The values in this map are boxed trait objects. Ordinarily this would not +/// // be possible with serde::Serializer because of object safety, but type +/// // erasure makes it possible with erased_serde::Serializer. +/// let mut formats: Map<&str, Box<dyn Serializer>> = Map::new(); +/// formats.insert("json", Box::new(<dyn Serializer>::erase(json))); +/// formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor))); +/// +/// // These are boxed trait objects as well. Same thing here - type erasure +/// // makes this possible. +/// let mut values: Map<&str, Box<dyn Serialize>> = Map::new(); +/// values.insert("vec", Box::new(vec!["a", "b"])); +/// values.insert("int", Box::new(65536)); +/// +/// // Pick a Serializer out of the formats map. +/// let format = formats.get_mut("json").unwrap(); +/// +/// // Pick a Serialize out of the values map. +/// let value = values.get("vec").unwrap(); +/// +/// // This line prints `["a","b"]` to stdout. +/// value.erased_serialize(format).unwrap(); +/// } +/// ``` +pub trait Serializer { + fn erased_serialize_bool(&mut self, v: bool) -> Result<Ok, Error>; + fn erased_serialize_i8(&mut self, v: i8) -> Result<Ok, Error>; + fn erased_serialize_i16(&mut self, v: i16) -> Result<Ok, Error>; + fn erased_serialize_i32(&mut self, v: i32) -> Result<Ok, Error>; + fn erased_serialize_i64(&mut self, v: i64) -> Result<Ok, Error>; + fn erased_serialize_i128(&mut self, v: i128) -> Result<Ok, Error>; + fn erased_serialize_u8(&mut self, v: u8) -> Result<Ok, Error>; + fn erased_serialize_u16(&mut self, v: u16) -> Result<Ok, Error>; + fn erased_serialize_u32(&mut self, v: u32) -> Result<Ok, Error>; + fn erased_serialize_u64(&mut self, v: u64) -> Result<Ok, Error>; + fn erased_serialize_u128(&mut self, v: u128) -> Result<Ok, Error>; + fn erased_serialize_f32(&mut self, v: f32) -> Result<Ok, Error>; + fn erased_serialize_f64(&mut self, v: f64) -> Result<Ok, Error>; + fn erased_serialize_char(&mut self, v: char) -> Result<Ok, Error>; + fn erased_serialize_str(&mut self, v: &str) -> Result<Ok, Error>; + fn erased_serialize_bytes(&mut self, v: &[u8]) -> Result<Ok, Error>; + fn erased_serialize_none(&mut self) -> Result<Ok, Error>; + fn erased_serialize_some(&mut self, v: &dyn Serialize) -> Result<Ok, Error>; + fn erased_serialize_unit(&mut self) -> Result<Ok, Error>; + fn erased_serialize_unit_struct(&mut self, name: &'static str) -> Result<Ok, Error>; + fn erased_serialize_unit_variant( + &mut self, + name: &'static str, + variant_index: u32, + variant: &'static str, + ) -> Result<Ok, Error>; + fn erased_serialize_newtype_struct( + &mut self, + name: &'static str, + v: &dyn Serialize, + ) -> Result<Ok, Error>; + fn erased_serialize_newtype_variant( + &mut self, + name: &'static str, + variant_index: u32, + variant: &'static str, + v: &dyn Serialize, + ) -> Result<Ok, Error>; + fn erased_serialize_seq(&mut self, len: Option<usize>) -> Result<Seq, Error>; + fn erased_serialize_tuple(&mut self, len: usize) -> Result<Tuple, Error>; + fn erased_serialize_tuple_struct( + &mut self, + name: &'static str, + len: usize, + ) -> Result<TupleStruct, Error>; + fn erased_serialize_tuple_variant( + &mut self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<TupleVariant, Error>; + fn erased_serialize_map(&mut self, len: Option<usize>) -> Result<Map, Error>; + fn erased_serialize_struct(&mut self, name: &'static str, len: usize) -> Result<Struct, Error>; + fn erased_serialize_struct_variant( + &mut self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<StructVariant, Error>; + fn erased_is_human_readable(&self) -> bool; +} + +impl dyn Serializer { + /// Convert any Serde `Serializer` to a trait object. + /// + /// ```rust + /// use erased_serde::{Serialize, Serializer}; + /// use std::collections::BTreeMap as Map; + /// use std::io; + /// + /// fn main() { + /// // Construct some serializers. + /// let json = &mut serde_json::Serializer::new(io::stdout()); + /// let cbor = &mut serde_cbor::Serializer::new(serde_cbor::ser::IoWrite::new(io::stdout())); + /// + /// // The values in this map are boxed trait objects. Ordinarily this would not + /// // be possible with serde::Serializer because of object safety, but type + /// // erasure makes it possible with erased_serde::Serializer. + /// let mut formats: Map<&str, Box<dyn Serializer>> = Map::new(); + /// formats.insert("json", Box::new(<dyn Serializer>::erase(json))); + /// formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor))); + /// + /// // These are boxed trait objects as well. Same thing here - type erasure + /// // makes this possible. + /// let mut values: Map<&str, Box<dyn Serialize>> = Map::new(); + /// values.insert("vec", Box::new(vec!["a", "b"])); + /// values.insert("int", Box::new(65536)); + /// + /// // Pick a Serializer out of the formats map. + /// let format = formats.get_mut("json").unwrap(); + /// + /// // Pick a Serialize out of the values map. + /// let value = values.get("vec").unwrap(); + /// + /// // This line prints `["a","b"]` to stdout. + /// value.erased_serialize(format).unwrap(); + /// } + /// ``` + pub fn erase<S>(serializer: S) -> erase::Serializer<S> + where + S: serde::Serializer, + S::Ok: 'static, + { + erase::Serializer { + state: Some(serializer), + } + } +} + +// OK ////////////////////////////////////////////////////////////////////////// + +// Corresponds to the Serializer::Ok associated type. +// +// This struct is exposed to users by invoking methods on the Serialize or +// Serializer trait objects, so we need to make sure they do not hold on to the +// Ok beyond the lifetime of the data in the Any. +// +// We do this by enforcing S::Ok is 'static for every Serializer trait object +// created by the user. +pub struct Ok { + data: Any, +} + +impl Ok { + unsafe fn new<T>(t: T) -> Self { + Ok { + data: unsafe { Any::new(t) }, + } + } + + unsafe fn take<T>(self) -> T { + unsafe { self.data.take() } + } +} + +// IMPL ERASED SERDE FOR SERDE ///////////////////////////////////////////////// + +impl<T> Serialize for T +where + T: ?Sized + serde::Serialize, +{ + fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error> { + self.serialize(serializer) + } +} + +mod erase { + pub struct Serializer<S> { + pub(crate) state: Option<S>, + } + + impl<S> Serializer<S> { + pub(crate) fn take(&mut self) -> S { + self.state.take().unwrap() + } + + pub(crate) fn as_ref(&self) -> &S { + self.state.as_ref().unwrap() + } + } +} + +impl<T> Serializer for erase::Serializer<T> +where + T: serde::Serializer, +{ + fn erased_serialize_bool(&mut self, v: bool) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_bool(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_i8(&mut self, v: i8) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_i8(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_i16(&mut self, v: i16) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_i16(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_i32(&mut self, v: i32) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_i32(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_i64(&mut self, v: i64) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_i64(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_i128(&mut self, v: i128) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_i128(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_u8(&mut self, v: u8) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_u8(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_u16(&mut self, v: u16) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_u16(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_u32(&mut self, v: u32) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_u32(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_u64(&mut self, v: u64) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_u64(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_u128(&mut self, v: u128) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_u128(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_f32(&mut self, v: f32) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_f32(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_f64(&mut self, v: f64) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_f64(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_char(&mut self, v: char) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_char(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_str(&mut self, v: &str) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_str(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_bytes(&mut self, v: &[u8]) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_bytes(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_none(&mut self) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_none() + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_some(&mut self, v: &dyn Serialize) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_some(v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_unit(&mut self) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_unit() + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_unit_struct(&mut self, name: &'static str) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_unit_struct(name) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_unit_variant( + &mut self, + name: &'static str, + variant_index: u32, + variant: &'static str, + ) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_unit_variant(name, variant_index, variant) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_newtype_struct( + &mut self, + name: &'static str, + v: &dyn Serialize, + ) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_newtype_struct(name, v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_newtype_variant( + &mut self, + name: &'static str, + variant_index: u32, + variant: &'static str, + v: &dyn Serialize, + ) -> Result<Ok, Error> { + unsafe { + self.take() + .serialize_newtype_variant(name, variant_index, variant, v) + .unsafe_map(Ok::new) + .map_err(erase) + } + } + + fn erased_serialize_seq(&mut self, len: Option<usize>) -> Result<Seq, Error> { + unsafe { + self.take() + .serialize_seq(len) + .unsafe_map(Seq::new) + .map_err(erase) + } + } + + fn erased_serialize_tuple(&mut self, len: usize) -> Result<Tuple, Error> { + unsafe { + self.take() + .serialize_tuple(len) + .unsafe_map(Tuple::new) + .map_err(erase) + } + } + + fn erased_serialize_tuple_struct( + &mut self, + name: &'static str, + len: usize, + ) -> Result<TupleStruct, Error> { + unsafe { + self.take() + .serialize_tuple_struct(name, len) + .unsafe_map(TupleStruct::new) + .map_err(erase) + } + } + + fn erased_serialize_tuple_variant( + &mut self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<TupleVariant, Error> { + unsafe { + self.take() + .serialize_tuple_variant(name, variant_index, variant, len) + .unsafe_map(TupleVariant::new) + .map_err(erase) + } + } + + fn erased_serialize_map(&mut self, len: Option<usize>) -> Result<Map, Error> { + unsafe { + self.take() + .serialize_map(len) + .unsafe_map(Map::new) + .map_err(erase) + } + } + + fn erased_serialize_struct(&mut self, name: &'static str, len: usize) -> Result<Struct, Error> { + unsafe { + self.take() + .serialize_struct(name, len) + .unsafe_map(Struct::new) + .map_err(erase) + } + } + + fn erased_serialize_struct_variant( + &mut self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<StructVariant, Error> { + unsafe { + self.take() + .serialize_struct_variant(name, variant_index, variant, len) + .unsafe_map(StructVariant::new) + .map_err(erase) + } + } + + fn erased_is_human_readable(&self) -> bool { + self.as_ref().is_human_readable() + } +} + +// IMPL SERDE FOR ERASED SERDE ///////////////////////////////////////////////// + +/// Serialize the given type-erased serializable value. +/// +/// This can be used to implement `serde::Serialize` for trait objects that have +/// `erased_serde::Serialize` as a supertrait. +/// +/// ``` +/// trait Event: erased_serde::Serialize { +/// /* ... */ +/// } +/// +/// impl<'a> serde::Serialize for dyn Event + 'a { +/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> +/// where S: serde::Serializer +/// { +/// erased_serde::serialize(self, serializer) +/// } +/// } +/// ``` +/// +/// Since this is reasonably common, the `serialize_trait_object!` macro +/// generates such a Serialize impl. +/// +/// ``` +/// use erased_serde::serialize_trait_object; +/// # +/// # trait Event: erased_serde::Serialize {} +/// +/// serialize_trait_object!(Event); +/// ``` +pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> +where + T: ?Sized + Serialize, + S: serde::Serializer, +{ + let mut erased = erase::Serializer { + state: Some(serializer), + }; + unsafe { + value + .erased_serialize(&mut erased) + .unsafe_map(Ok::take) + .map_err(unerase) + } +} + +serialize_trait_object!(Serialize); + +macro_rules! impl_serializer_for_trait_object { + ($ty:ty) => { + impl<'a> serde::Serializer for $ty { + type Ok = Ok; + type Error = Error; + type SerializeSeq = Seq<'a>; + type SerializeTuple = Tuple<'a>; + type SerializeTupleStruct = TupleStruct<'a>; + type SerializeTupleVariant = TupleVariant<'a>; + type SerializeMap = Map<'a>; + type SerializeStruct = Struct<'a>; + type SerializeStructVariant = StructVariant<'a>; + + fn serialize_bool(self, v: bool) -> Result<Ok, Error> { + self.erased_serialize_bool(v) + } + + fn serialize_i8(self, v: i8) -> Result<Ok, Error> { + self.erased_serialize_i8(v) + } + + fn serialize_i16(self, v: i16) -> Result<Ok, Error> { + self.erased_serialize_i16(v) + } + + fn serialize_i32(self, v: i32) -> Result<Ok, Error> { + self.erased_serialize_i32(v) + } + + fn serialize_i64(self, v: i64) -> Result<Ok, Error> { + self.erased_serialize_i64(v) + } + + fn serialize_i128(self, v: i128) -> Result<Ok, Error> { + self.erased_serialize_i128(v) + } + + fn serialize_u8(self, v: u8) -> Result<Ok, Error> { + self.erased_serialize_u8(v) + } + + fn serialize_u16(self, v: u16) -> Result<Ok, Error> { + self.erased_serialize_u16(v) + } + + fn serialize_u32(self, v: u32) -> Result<Ok, Error> { + self.erased_serialize_u32(v) + } + + fn serialize_u64(self, v: u64) -> Result<Ok, Error> { + self.erased_serialize_u64(v) + } + + fn serialize_u128(self, v: u128) -> Result<Ok, Error> { + self.erased_serialize_u128(v) + } + + fn serialize_f32(self, v: f32) -> Result<Ok, Error> { + self.erased_serialize_f32(v) + } + + fn serialize_f64(self, v: f64) -> Result<Ok, Error> { + self.erased_serialize_f64(v) + } + + fn serialize_char(self, v: char) -> Result<Ok, Error> { + self.erased_serialize_char(v) + } + + fn serialize_str(self, v: &str) -> Result<Ok, Error> { + self.erased_serialize_str(v) + } + + fn serialize_bytes(self, v: &[u8]) -> Result<Ok, Error> { + self.erased_serialize_bytes(v) + } + + fn serialize_none(self) -> Result<Ok, Error> { + self.erased_serialize_none() + } + + fn serialize_some<T>(self, v: &T) -> Result<Ok, Error> + where + T: ?Sized + serde::Serialize, + { + self.erased_serialize_some(&v) + } + + fn serialize_unit(self) -> Result<Ok, Error> { + self.erased_serialize_unit() + } + + fn serialize_unit_struct(self, name: &'static str) -> Result<Ok, Error> { + self.erased_serialize_unit_struct(name) + } + + fn serialize_unit_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + ) -> Result<Ok, Error> { + self.erased_serialize_unit_variant(name, variant_index, variant) + } + + fn serialize_newtype_struct<T>(self, name: &'static str, v: &T) -> Result<Ok, Error> + where + T: ?Sized + serde::Serialize, + { + self.erased_serialize_newtype_struct(name, &v) + } + + fn serialize_newtype_variant<T>( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + v: &T, + ) -> Result<Ok, Error> + where + T: ?Sized + serde::Serialize, + { + self.erased_serialize_newtype_variant(name, variant_index, variant, &v) + } + + fn serialize_seq(self, len: Option<usize>) -> Result<Seq<'a>, Error> { + self.erased_serialize_seq(len) + } + + fn serialize_tuple(self, len: usize) -> Result<Tuple<'a>, Error> { + self.erased_serialize_tuple(len) + } + + fn serialize_tuple_struct( + self, + name: &'static str, + len: usize, + ) -> Result<TupleStruct<'a>, Error> { + self.erased_serialize_tuple_struct(name, len) + } + + fn serialize_tuple_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<TupleVariant<'a>, Error> { + self.erased_serialize_tuple_variant(name, variant_index, variant, len) + } + + fn serialize_map(self, len: Option<usize>) -> Result<Map<'a>, Error> { + self.erased_serialize_map(len) + } + + fn serialize_struct(self, name: &'static str, len: usize) -> Result<Struct<'a>, Error> { + self.erased_serialize_struct(name, len) + } + + fn serialize_struct_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<StructVariant<'a>, Error> { + self.erased_serialize_struct_variant(name, variant_index, variant, len) + } + + #[cfg(not(any(feature = "std", feature = "alloc")))] + fn collect_str<T>(self, value: &T) -> Result<Self::Ok, Self::Error> + where + T: ?Sized + Display, + { + unreachable!() + } + + fn is_human_readable(&self) -> bool { + self.erased_is_human_readable() + } + } + }; +} + +impl_serializer_for_trait_object!(&'a mut (dyn Serializer + '_)); +impl_serializer_for_trait_object!(&'a mut (dyn Serializer + Send + '_)); +impl_serializer_for_trait_object!(&'a mut (dyn Serializer + Sync + '_)); +impl_serializer_for_trait_object!(&'a mut (dyn Serializer + Send + Sync + '_)); + +pub struct Seq<'a> { + data: Any, + serialize_element: unsafe fn(&mut Any, &dyn Serialize) -> Result<(), Error>, + end: unsafe fn(Any) -> Result<Ok, Error>, + lifetime: PhantomData<&'a dyn Serializer>, +} + +impl<'a> Seq<'a> { + unsafe fn new<T>(data: T) -> Self + where + T: serde::ser::SerializeSeq, + { + Seq { + data: unsafe { Any::new(data) }, + serialize_element: { + unsafe fn serialize_element<T>( + data: &mut Any, + v: &dyn Serialize, + ) -> Result<(), Error> + where + T: serde::ser::SerializeSeq, + { + unsafe { data.view::<T>().serialize_element(v).map_err(erase) } + } + serialize_element::<T> + }, + end: { + unsafe fn end<T>(data: Any) -> Result<Ok, Error> + where + T: serde::ser::SerializeSeq, + { + unsafe { data.take::<T>().end().unsafe_map(Ok::new).map_err(erase) } + } + end::<T> + }, + lifetime: PhantomData, + } + } +} + +impl<'a> SerializeSeq for Seq<'a> { + type Ok = Ok; + type Error = Error; + + fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error> + where + T: ?Sized + serde::Serialize, + { + unsafe { (self.serialize_element)(&mut self.data, &value) } + } + + fn end(self) -> Result<Ok, Error> { + unsafe { (self.end)(self.data) } + } +} + +pub struct Tuple<'a> { + data: Any, + serialize_element: unsafe fn(&mut Any, &dyn Serialize) -> Result<(), Error>, + end: unsafe fn(Any) -> Result<Ok, Error>, + lifetime: PhantomData<&'a dyn Serializer>, +} + +impl<'a> Tuple<'a> { + unsafe fn new<T>(data: T) -> Self + where + T: serde::ser::SerializeTuple, + { + Tuple { + data: unsafe { Any::new(data) }, + serialize_element: { + unsafe fn serialize_element<T>( + data: &mut Any, + v: &dyn Serialize, + ) -> Result<(), Error> + where + T: serde::ser::SerializeTuple, + { + unsafe { data.view::<T>().serialize_element(v).map_err(erase) } + } + serialize_element::<T> + }, + end: { + unsafe fn end<T>(data: Any) -> Result<Ok, Error> + where + T: serde::ser::SerializeTuple, + { + unsafe { data.take::<T>().end().unsafe_map(Ok::new).map_err(erase) } + } + end::<T> + }, + lifetime: PhantomData, + } + } +} + +impl<'a> SerializeTuple for Tuple<'a> { + type Ok = Ok; + type Error = Error; + + fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error> + where + T: ?Sized + serde::Serialize, + { + unsafe { (self.serialize_element)(&mut self.data, &value) } + } + + fn end(self) -> Result<Ok, Error> { + unsafe { (self.end)(self.data) } + } +} + +pub struct TupleStruct<'a> { + data: Any, + serialize_field: unsafe fn(&mut Any, &dyn Serialize) -> Result<(), Error>, + end: unsafe fn(Any) -> Result<Ok, Error>, + lifetime: PhantomData<&'a dyn Serializer>, +} + +impl<'a> TupleStruct<'a> { + unsafe fn new<T>(data: T) -> Self + where + T: serde::ser::SerializeTupleStruct, + { + TupleStruct { + data: unsafe { Any::new(data) }, + serialize_field: { + unsafe fn serialize_field<T>(data: &mut Any, v: &dyn Serialize) -> Result<(), Error> + where + T: serde::ser::SerializeTupleStruct, + { + unsafe { data.view::<T>().serialize_field(v).map_err(erase) } + } + serialize_field::<T> + }, + end: { + unsafe fn end<T>(data: Any) -> Result<Ok, Error> + where + T: serde::ser::SerializeTupleStruct, + { + unsafe { data.take::<T>().end().unsafe_map(Ok::new).map_err(erase) } + } + end::<T> + }, + lifetime: PhantomData, + } + } +} + +impl<'a> SerializeTupleStruct for TupleStruct<'a> { + type Ok = Ok; + type Error = Error; + + fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error> + where + T: ?Sized + serde::Serialize, + { + unsafe { (self.serialize_field)(&mut self.data, &value) } + } + + fn end(self) -> Result<Ok, Error> { + unsafe { (self.end)(self.data) } + } +} + +pub struct TupleVariant<'a> { + data: Any, + serialize_field: unsafe fn(&mut Any, &dyn Serialize) -> Result<(), Error>, + end: unsafe fn(Any) -> Result<Ok, Error>, + lifetime: PhantomData<&'a dyn Serializer>, +} + +impl<'a> TupleVariant<'a> { + unsafe fn new<T>(data: T) -> Self + where + T: serde::ser::SerializeTupleVariant, + { + TupleVariant { + data: unsafe { Any::new(data) }, + serialize_field: { + unsafe fn serialize_field<T>(data: &mut Any, v: &dyn Serialize) -> Result<(), Error> + where + T: serde::ser::SerializeTupleVariant, + { + unsafe { data.view::<T>().serialize_field(v).map_err(erase) } + } + serialize_field::<T> + }, + end: { + unsafe fn end<T>(data: Any) -> Result<Ok, Error> + where + T: serde::ser::SerializeTupleVariant, + { + unsafe { data.take::<T>().end().unsafe_map(Ok::new).map_err(erase) } + } + end::<T> + }, + lifetime: PhantomData, + } + } +} + +impl<'a> SerializeTupleVariant for TupleVariant<'a> { + type Ok = Ok; + type Error = Error; + + fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error> + where + T: ?Sized + serde::Serialize, + { + unsafe { (self.serialize_field)(&mut self.data, &value) } + } + + fn end(self) -> Result<Ok, Error> { + unsafe { (self.end)(self.data) } + } +} + +pub struct Map<'a> { + data: Any, + serialize_key: unsafe fn(&mut Any, &dyn Serialize) -> Result<(), Error>, + serialize_value: unsafe fn(&mut Any, &dyn Serialize) -> Result<(), Error>, + serialize_entry: unsafe fn(&mut Any, &dyn Serialize, &dyn Serialize) -> Result<(), Error>, + end: unsafe fn(Any) -> Result<Ok, Error>, + lifetime: PhantomData<&'a dyn Serializer>, +} + +impl<'a> Map<'a> { + unsafe fn new<T>(data: T) -> Self + where + T: serde::ser::SerializeMap, + { + Map { + data: unsafe { Any::new(data) }, + serialize_key: { + unsafe fn serialize_key<T>(data: &mut Any, v: &dyn Serialize) -> Result<(), Error> + where + T: serde::ser::SerializeMap, + { + unsafe { data.view::<T>().serialize_key(v).map_err(erase) } + } + serialize_key::<T> + }, + serialize_value: { + unsafe fn serialize_value<T>(data: &mut Any, v: &dyn Serialize) -> Result<(), Error> + where + T: serde::ser::SerializeMap, + { + unsafe { data.view::<T>().serialize_value(v).map_err(erase) } + } + serialize_value::<T> + }, + serialize_entry: { + unsafe fn serialize_entry<T>( + data: &mut Any, + k: &dyn Serialize, + v: &dyn Serialize, + ) -> Result<(), Error> + where + T: serde::ser::SerializeMap, + { + unsafe { data.view::<T>().serialize_entry(k, v).map_err(erase) } + } + serialize_entry::<T> + }, + end: { + unsafe fn end<T>(data: Any) -> Result<Ok, Error> + where + T: serde::ser::SerializeMap, + { + unsafe { data.take::<T>().end().unsafe_map(Ok::new).map_err(erase) } + } + end::<T> + }, + lifetime: PhantomData, + } + } +} + +impl<'a> SerializeMap for Map<'a> { + type Ok = Ok; + type Error = Error; + + fn serialize_key<T>(&mut self, key: &T) -> Result<(), Error> + where + T: ?Sized + serde::Serialize, + { + unsafe { (self.serialize_key)(&mut self.data, &key) } + } + + fn serialize_value<T>(&mut self, value: &T) -> Result<(), Error> + where + T: ?Sized + serde::Serialize, + { + unsafe { (self.serialize_value)(&mut self.data, &value) } + } + + fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), Error> + where + K: ?Sized + serde::Serialize, + V: ?Sized + serde::Serialize, + { + unsafe { (self.serialize_entry)(&mut self.data, &key, &value) } + } + + fn end(self) -> Result<Ok, Error> { + unsafe { (self.end)(self.data) } + } +} + +pub struct Struct<'a> { + data: Any, + serialize_field: unsafe fn(&mut Any, &'static str, &dyn Serialize) -> Result<(), Error>, + end: unsafe fn(Any) -> Result<Ok, Error>, + lifetime: PhantomData<&'a dyn Serializer>, +} + +impl<'a> Struct<'a> { + unsafe fn new<T>(data: T) -> Self + where + T: serde::ser::SerializeStruct, + { + Struct { + data: unsafe { Any::new(data) }, + serialize_field: { + unsafe fn serialize_field<T>( + data: &mut Any, + k: &'static str, + v: &dyn Serialize, + ) -> Result<(), Error> + where + T: serde::ser::SerializeStruct, + { + unsafe { data.view::<T>().serialize_field(k, v).map_err(erase) } + } + serialize_field::<T> + }, + end: { + unsafe fn end<T>(data: Any) -> Result<Ok, Error> + where + T: serde::ser::SerializeStruct, + { + unsafe { data.take::<T>().end().unsafe_map(Ok::new).map_err(erase) } + } + end::<T> + }, + lifetime: PhantomData, + } + } +} + +impl<'a> SerializeStruct for Struct<'a> { + type Ok = Ok; + type Error = Error; + + fn serialize_field<T>(&mut self, name: &'static str, field: &T) -> Result<(), Error> + where + T: ?Sized + serde::Serialize, + { + unsafe { (self.serialize_field)(&mut self.data, name, &field) } + } + + fn end(self) -> Result<Ok, Error> { + unsafe { (self.end)(self.data) } + } +} + +pub struct StructVariant<'a> { + data: Any, + serialize_field: unsafe fn(&mut Any, &'static str, &dyn Serialize) -> Result<(), Error>, + end: unsafe fn(Any) -> Result<Ok, Error>, + lifetime: PhantomData<&'a dyn Serializer>, +} + +impl<'a> StructVariant<'a> { + unsafe fn new<T>(data: T) -> Self + where + T: serde::ser::SerializeStructVariant, + { + StructVariant { + data: unsafe { Any::new(data) }, + serialize_field: { + unsafe fn serialize_field<T>( + data: &mut Any, + k: &'static str, + v: &dyn Serialize, + ) -> Result<(), Error> + where + T: serde::ser::SerializeStructVariant, + { + unsafe { data.view::<T>().serialize_field(k, v).map_err(erase) } + } + serialize_field::<T> + }, + end: { + unsafe fn end<T>(data: Any) -> Result<Ok, Error> + where + T: serde::ser::SerializeStructVariant, + { + unsafe { data.take::<T>().end().unsafe_map(Ok::new).map_err(erase) } + } + end::<T> + }, + lifetime: PhantomData, + } + } +} + +impl<'a> SerializeStructVariant for StructVariant<'a> { + type Ok = Ok; + type Error = Error; + + fn serialize_field<T>(&mut self, name: &'static str, field: &T) -> Result<(), Error> + where + T: ?Sized + serde::Serialize, + { + unsafe { (self.serialize_field)(&mut self.data, name, &field) } + } + + fn end(self) -> Result<Ok, Error> { + unsafe { (self.end)(self.data) } + } +} + +// IMPL ERASED SERDE FOR ERASED SERDE ////////////////////////////////////////// + +macro_rules! deref_erased_serializer { + ($($imp:tt)+) => { + impl $($imp)+ { + fn erased_serialize_bool(&mut self, v: bool) -> Result<Ok, Error> { + (**self).erased_serialize_bool(v) + } + + fn erased_serialize_i8(&mut self, v: i8) -> Result<Ok, Error> { + (**self).erased_serialize_i8(v) + } + + fn erased_serialize_i16(&mut self, v: i16) -> Result<Ok, Error> { + (**self).erased_serialize_i16(v) + } + + fn erased_serialize_i32(&mut self, v: i32) -> Result<Ok, Error> { + (**self).erased_serialize_i32(v) + } + + fn erased_serialize_i64(&mut self, v: i64) -> Result<Ok, Error> { + (**self).erased_serialize_i64(v) + } + + fn erased_serialize_i128(&mut self, v: i128) -> Result<Ok, Error> { + (**self).erased_serialize_i128(v) + } + + fn erased_serialize_u8(&mut self, v: u8) -> Result<Ok, Error> { + (**self).erased_serialize_u8(v) + } + + fn erased_serialize_u16(&mut self, v: u16) -> Result<Ok, Error> { + (**self).erased_serialize_u16(v) + } + + fn erased_serialize_u32(&mut self, v: u32) -> Result<Ok, Error> { + (**self).erased_serialize_u32(v) + } + + fn erased_serialize_u64(&mut self, v: u64) -> Result<Ok, Error> { + (**self).erased_serialize_u64(v) + } + + fn erased_serialize_u128(&mut self, v: u128) -> Result<Ok, Error> { + (**self).erased_serialize_u128(v) + } + + fn erased_serialize_f32(&mut self, v: f32) -> Result<Ok, Error> { + (**self).erased_serialize_f32(v) + } + + fn erased_serialize_f64(&mut self, v: f64) -> Result<Ok, Error> { + (**self).erased_serialize_f64(v) + } + + fn erased_serialize_char(&mut self, v: char) -> Result<Ok, Error> { + (**self).erased_serialize_char(v) + } + + fn erased_serialize_str(&mut self, v: &str) -> Result<Ok, Error> { + (**self).erased_serialize_str(v) + } + + fn erased_serialize_bytes(&mut self, v: &[u8]) -> Result<Ok, Error> { + (**self).erased_serialize_bytes(v) + } + + fn erased_serialize_none(&mut self) -> Result<Ok, Error> { + (**self).erased_serialize_none() + } + + fn erased_serialize_some(&mut self, v: &dyn Serialize) -> Result<Ok, Error> { + (**self).erased_serialize_some(v) + } + + fn erased_serialize_unit(&mut self) -> Result<Ok, Error> { + (**self).erased_serialize_unit() + } + + fn erased_serialize_unit_struct(&mut self, name: &'static str) -> Result<Ok, Error> { + (**self).erased_serialize_unit_struct(name) + } + + fn erased_serialize_unit_variant(&mut self, name: &'static str, variant_index: u32, variant: &'static str) -> Result<Ok, Error> { + (**self).erased_serialize_unit_variant(name, variant_index, variant) + } + + fn erased_serialize_newtype_struct(&mut self, name: &'static str, v: &dyn Serialize) -> Result<Ok, Error> { + (**self).erased_serialize_newtype_struct(name, v) + } + + fn erased_serialize_newtype_variant(&mut self, name: &'static str, variant_index: u32, variant: &'static str, v: &dyn Serialize) -> Result<Ok, Error> { + (**self).erased_serialize_newtype_variant(name, variant_index, variant, v) + } + + fn erased_serialize_seq(&mut self, len: Option<usize>) -> Result<Seq, Error> { + (**self).erased_serialize_seq(len) + } + + fn erased_serialize_tuple(&mut self, len: usize) -> Result<Tuple, Error> { + (**self).erased_serialize_tuple(len) + } + + fn erased_serialize_tuple_struct(&mut self, name: &'static str, len: usize) -> Result<TupleStruct, Error> { + (**self).erased_serialize_tuple_struct(name, len) + } + + fn erased_serialize_tuple_variant(&mut self, name: &'static str, variant_index: u32, variant: &'static str, len: usize) -> Result<TupleVariant, Error> { + (**self).erased_serialize_tuple_variant(name, variant_index, variant, len) + } + + fn erased_serialize_map(&mut self, len: Option<usize>) -> Result<Map, Error> { + (**self).erased_serialize_map(len) + } + + fn erased_serialize_struct(&mut self, name: &'static str, len: usize) -> Result<Struct, Error> { + (**self).erased_serialize_struct(name, len) + } + + fn erased_serialize_struct_variant(&mut self, name: &'static str, variant_index: u32, variant: &'static str, len: usize) -> Result<StructVariant, Error> { + (**self).erased_serialize_struct_variant(name, variant_index, variant, len) + } + + fn erased_is_human_readable(&self) -> bool { + (**self).erased_is_human_readable() + } + } + }; +} + +deref_erased_serializer!(<'a> Serializer for Box<dyn Serializer + 'a>); +deref_erased_serializer!(<'a> Serializer for Box<dyn Serializer + Send + 'a>); +deref_erased_serializer!(<'a> Serializer for Box<dyn Serializer + Sync + 'a>); +deref_erased_serializer!(<'a> Serializer for Box<dyn Serializer + Send + Sync + 'a>); +deref_erased_serializer!(<'a, T: ?Sized + Serializer> Serializer for &'a mut T); + +// ERROR /////////////////////////////////////////////////////////////////////// + +fn erase<E>(e: E) -> Error +where + E: Display, +{ + serde::ser::Error::custom(e) +} + +fn unerase<E>(e: Error) -> E +where + E: serde::ser::Error, +{ + E::custom(e) +} + +// TEST //////////////////////////////////////////////////////////////////////// + +#[cfg(test)] +mod tests { + use super::*; + use crate::alloc::{vec, Vec}; + use serde_derive::Serialize; + + fn test_json<T>(t: T) + where + T: serde::Serialize, + { + let expected = serde_json::to_vec(&t).unwrap(); + + // test borrowed trait object + { + let obj: &dyn Serialize = &t; + + let mut buf = Vec::new(); + + { + let mut ser = serde_json::Serializer::new(&mut buf); + let ser: &mut dyn Serializer = &mut <dyn Serializer>::erase(&mut ser); + + obj.erased_serialize(ser).unwrap(); + } + + assert_eq!(buf, expected); + } + + // test boxed trait object + { + let obj: Box<dyn Serialize> = Box::new(t); + + let mut buf = Vec::new(); + + { + let mut ser = serde_json::Serializer::new(&mut buf); + let mut ser: Box<dyn Serializer> = Box::new(<dyn Serializer>::erase(&mut ser)); + + obj.erased_serialize(&mut ser).unwrap(); + } + + assert_eq!(buf, expected); + } + } + + #[test] + fn test_vec() { + test_json(vec!["a", "b"]); + } + + #[test] + fn test_struct() { + #[derive(Serialize)] + struct S { + f: usize, + } + + test_json(S { f: 256 }); + } + + #[test] + fn test_enum() { + #[derive(Serialize)] + enum E { + Unit, + Newtype(bool), + Tuple(bool, bool), + Struct { t: bool, f: bool }, + } + + test_json(E::Unit); + test_json(E::Newtype(true)); + test_json(E::Tuple(true, false)); + test_json(E::Struct { t: true, f: false }); + } + + #[test] + fn assert_serialize() { + fn assert<T: serde::Serialize>() {} + + assert::<&dyn Serialize>(); + assert::<&(dyn Serialize + Send)>(); + assert::<&(dyn Serialize + Sync)>(); + assert::<&(dyn Serialize + Send + Sync)>(); + assert::<&(dyn Serialize + Sync + Send)>(); + assert::<Vec<&dyn Serialize>>(); + assert::<Vec<&(dyn Serialize + Send)>>(); + + assert::<Box<dyn Serialize>>(); + assert::<Box<dyn Serialize + Send>>(); + assert::<Box<dyn Serialize + Sync>>(); + assert::<Box<dyn Serialize + Send + Sync>>(); + assert::<Box<dyn Serialize + Sync + Send>>(); + assert::<Vec<Box<dyn Serialize>>>(); + assert::<Vec<Box<dyn Serialize + Send>>>(); + } + + #[test] + fn assert_serializer() { + fn assert<T: serde::Serializer>() {} + + assert::<&mut dyn Serializer>(); + assert::<&mut (dyn Serializer + Send)>(); + assert::<&mut (dyn Serializer + Sync)>(); + assert::<&mut (dyn Serializer + Send + Sync)>(); + assert::<&mut (dyn Serializer + Sync + Send)>(); + } +} |