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> = Map::new(); /// formats.insert("json", Box::new(::erase(json))); /// formats.insert("cbor", Box::new(::erase(cbor))); /// /// // Pick a Deserializer out of the formats map. /// let format = formats.get_mut("json").unwrap(); /// /// let data: Map = erased_serde::deserialize(format).unwrap(); /// /// println!("{}", data["A"] + data["B"]); /// } /// ``` pub fn deserialize<'de, T>(deserializer: &mut dyn Deserializer<'de>) -> Result 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; } /// 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` 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> = Map::new(); /// formats.insert("json", Box::new(::erase(json))); /// formats.insert("cbor", Box::new(::erase(cbor))); /// /// // Pick a Deserializer out of the formats map. /// let format = formats.get_mut("json").unwrap(); /// /// let data: Map = 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; fn erased_deserialize_bool(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_i8(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_i16(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_i32(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_i64(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_i128(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_u8(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_u16(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_u32(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_u64(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_u128(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_f32(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_f64(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_char(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_str(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_string(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_bytes(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_byte_buf(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_option(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_unit(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_unit_struct( &mut self, name: &'static str, v: &mut dyn Visitor<'de>, ) -> Result; fn erased_deserialize_newtype_struct( &mut self, name: &'static str, v: &mut dyn Visitor<'de>, ) -> Result; fn erased_deserialize_seq(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_tuple( &mut self, len: usize, v: &mut dyn Visitor<'de>, ) -> Result; fn erased_deserialize_tuple_struct( &mut self, name: &'static str, len: usize, v: &mut dyn Visitor<'de>, ) -> Result; fn erased_deserialize_map(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_struct( &mut self, name: &'static str, fields: &'static [&'static str], v: &mut dyn Visitor<'de>, ) -> Result; fn erased_deserialize_identifier(&mut self, v: &mut dyn Visitor<'de>) -> Result; fn erased_deserialize_enum( &mut self, name: &'static str, variants: &'static [&'static str], v: &mut dyn Visitor<'de>, ) -> Result; fn erased_deserialize_ignored_any(&mut self, v: &mut dyn Visitor<'de>) -> Result; 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; fn erased_visit_i8(&mut self, v: i8) -> Result; fn erased_visit_i16(&mut self, v: i16) -> Result; fn erased_visit_i32(&mut self, v: i32) -> Result; fn erased_visit_i64(&mut self, v: i64) -> Result; fn erased_visit_i128(&mut self, v: i128) -> Result; fn erased_visit_u8(&mut self, v: u8) -> Result; fn erased_visit_u16(&mut self, v: u16) -> Result; fn erased_visit_u32(&mut self, v: u32) -> Result; fn erased_visit_u64(&mut self, v: u64) -> Result; fn erased_visit_u128(&mut self, v: u128) -> Result; fn erased_visit_f32(&mut self, v: f32) -> Result; fn erased_visit_f64(&mut self, v: f64) -> Result; fn erased_visit_char(&mut self, v: char) -> Result; fn erased_visit_str(&mut self, v: &str) -> Result; fn erased_visit_borrowed_str(&mut self, v: &'de str) -> Result; #[cfg(any(feature = "std", feature = "alloc"))] fn erased_visit_string(&mut self, v: String) -> Result; fn erased_visit_bytes(&mut self, v: &[u8]) -> Result; fn erased_visit_borrowed_bytes(&mut self, v: &'de [u8]) -> Result; #[cfg(any(feature = "std", feature = "alloc"))] fn erased_visit_byte_buf(&mut self, v: Vec) -> Result; fn erased_visit_none(&mut self) -> Result; fn erased_visit_some(&mut self, d: &mut dyn Deserializer<'de>) -> Result; fn erased_visit_unit(&mut self) -> Result; fn erased_visit_newtype_struct(&mut self, d: &mut dyn Deserializer<'de>) -> Result; fn erased_visit_seq(&mut self, s: &mut dyn SeqAccess<'de>) -> Result; fn erased_visit_map(&mut self, m: &mut dyn MapAccess<'de>) -> Result; fn erased_visit_enum(&mut self, e: &mut dyn EnumAccess<'de>) -> Result; } pub trait SeqAccess<'de> { fn erased_next_element( &mut self, d: &mut dyn DeserializeSeed<'de>, ) -> Result, Error>; fn erased_size_hint(&self) -> Option; } pub trait MapAccess<'de> { fn erased_next_key(&mut self, d: &mut dyn DeserializeSeed<'de>) -> Result, Error>; fn erased_next_value(&mut self, d: &mut dyn DeserializeSeed<'de>) -> Result; fn erased_next_entry( &mut self, key: &mut dyn DeserializeSeed<'de>, value: &mut dyn DeserializeSeed<'de>, ) -> Result, Error>; fn erased_size_hint(&self) -> Option; } 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> = Map::new(); /// formats.insert("json", Box::new(::erase(json))); /// formats.insert("cbor", Box::new(::erase(cbor))); /// /// // Pick a Deserializer out of the formats map. /// let format = formats.get_mut("json").unwrap(); /// /// let data: Map = erased_serde::deserialize(format).unwrap(); /// /// println!("{}", data["A"] + data["B"]); /// } /// ``` pub fn erase(deserializer: D) -> erase::Deserializer where D: serde::Deserializer<'de>, { erase::Deserializer { state: Some(deserializer), } } } // OUT ///////////////////////////////////////////////////////////////////////// pub struct Out(Any); impl Out { unsafe fn new(t: T) -> Self { Out(unsafe { Any::new(t) }) } unsafe fn take(self) -> T { unsafe { self.0.take() } } } // IMPL ERASED SERDE FOR SERDE ///////////////////////////////////////////////// mod erase { pub struct DeserializeSeed { pub(crate) state: Option, } impl DeserializeSeed { pub(crate) fn take(&mut self) -> D { self.state.take().unwrap() } } pub struct Deserializer { pub(crate) state: Option, } impl Deserializer { 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 { pub(crate) state: Option, } impl Visitor { 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 { pub(crate) state: D, } impl SeqAccess { pub(crate) fn as_ref(&self) -> &D { &self.state } pub(crate) fn as_mut(&mut self) -> &mut D { &mut self.state } } pub struct MapAccess { pub(crate) state: D, } impl MapAccess { pub(crate) fn as_ref(&self) -> &D { &self.state } pub(crate) fn as_mut(&mut self) -> &mut D { &mut self.state } } pub struct EnumAccess { pub(crate) state: Option, } impl EnumAccess { pub(crate) fn take(&mut self) -> D { self.state.take().unwrap() } } } impl<'de, T> DeserializeSeed<'de> for erase::DeserializeSeed where T: serde::de::DeserializeSeed<'de>, { fn erased_deserialize_seed( &mut self, deserializer: &mut dyn Deserializer<'de>, ) -> Result { unsafe { self.take().deserialize(deserializer).unsafe_map(Out::new) } } } impl<'de, T> Deserializer<'de> for erase::Deserializer where T: serde::Deserializer<'de>, { fn erased_deserialize_any(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_any(visitor).map_err(erase) } fn erased_deserialize_bool(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_bool(visitor).map_err(erase) } fn erased_deserialize_i8(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_i8(visitor).map_err(erase) } fn erased_deserialize_i16(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_i16(visitor).map_err(erase) } fn erased_deserialize_i32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_i32(visitor).map_err(erase) } fn erased_deserialize_i64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_i64(visitor).map_err(erase) } fn erased_deserialize_i128(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_i128(visitor).map_err(erase) } fn erased_deserialize_u8(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_u8(visitor).map_err(erase) } fn erased_deserialize_u16(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_u16(visitor).map_err(erase) } fn erased_deserialize_u32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_u32(visitor).map_err(erase) } fn erased_deserialize_u64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_u64(visitor).map_err(erase) } fn erased_deserialize_u128(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_u128(visitor).map_err(erase) } fn erased_deserialize_f32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_f32(visitor).map_err(erase) } fn erased_deserialize_f64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_f64(visitor).map_err(erase) } fn erased_deserialize_char(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_char(visitor).map_err(erase) } fn erased_deserialize_str(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_str(visitor).map_err(erase) } fn erased_deserialize_string(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_string(visitor).map_err(erase) } fn erased_deserialize_bytes(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_bytes(visitor).map_err(erase) } fn erased_deserialize_byte_buf( &mut self, visitor: &mut dyn Visitor<'de>, ) -> Result { self.take().deserialize_byte_buf(visitor).map_err(erase) } fn erased_deserialize_option(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_option(visitor).map_err(erase) } fn erased_deserialize_unit(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_unit(visitor).map_err(erase) } fn erased_deserialize_unit_struct( &mut self, name: &'static str, visitor: &mut dyn Visitor<'de>, ) -> Result { 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 { self.take() .deserialize_newtype_struct(name, visitor) .map_err(erase) } fn erased_deserialize_seq(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { self.take().deserialize_seq(visitor).map_err(erase) } fn erased_deserialize_tuple( &mut self, len: usize, visitor: &mut dyn Visitor<'de>, ) -> Result { 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 { self.take() .deserialize_tuple_struct(name, len, visitor) .map_err(erase) } fn erased_deserialize_map(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { 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 { self.take() .deserialize_struct(name, fields, visitor) .map_err(erase) } fn erased_deserialize_identifier( &mut self, visitor: &mut dyn Visitor<'de>, ) -> Result { 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 { self.take() .deserialize_enum(name, variants, visitor) .map_err(erase) } fn erased_deserialize_ignored_any( &mut self, visitor: &mut dyn Visitor<'de>, ) -> Result { 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 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 { unsafe { self.take().visit_bool(v).unsafe_map(Out::new) } } fn erased_visit_i8(&mut self, v: i8) -> Result { unsafe { self.take().visit_i8(v).unsafe_map(Out::new) } } fn erased_visit_i16(&mut self, v: i16) -> Result { unsafe { self.take().visit_i16(v).unsafe_map(Out::new) } } fn erased_visit_i32(&mut self, v: i32) -> Result { unsafe { self.take().visit_i32(v).unsafe_map(Out::new) } } fn erased_visit_i64(&mut self, v: i64) -> Result { unsafe { self.take().visit_i64(v).unsafe_map(Out::new) } } fn erased_visit_i128(&mut self, v: i128) -> Result { unsafe { self.take().visit_i128(v).unsafe_map(Out::new) } } fn erased_visit_u8(&mut self, v: u8) -> Result { unsafe { self.take().visit_u8(v).unsafe_map(Out::new) } } fn erased_visit_u16(&mut self, v: u16) -> Result { unsafe { self.take().visit_u16(v).unsafe_map(Out::new) } } fn erased_visit_u32(&mut self, v: u32) -> Result { unsafe { self.take().visit_u32(v).unsafe_map(Out::new) } } fn erased_visit_u64(&mut self, v: u64) -> Result { unsafe { self.take().visit_u64(v).unsafe_map(Out::new) } } fn erased_visit_u128(&mut self, v: u128) -> Result { unsafe { self.take().visit_u128(v).unsafe_map(Out::new) } } fn erased_visit_f32(&mut self, v: f32) -> Result { unsafe { self.take().visit_f32(v).unsafe_map(Out::new) } } fn erased_visit_f64(&mut self, v: f64) -> Result { unsafe { self.take().visit_f64(v).unsafe_map(Out::new) } } fn erased_visit_char(&mut self, v: char) -> Result { unsafe { self.take().visit_char(v).unsafe_map(Out::new) } } fn erased_visit_str(&mut self, v: &str) -> Result { unsafe { self.take().visit_str(v).unsafe_map(Out::new) } } fn erased_visit_borrowed_str(&mut self, v: &'de str) -> Result { 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 { unsafe { self.take().visit_string(v).unsafe_map(Out::new) } } fn erased_visit_bytes(&mut self, v: &[u8]) -> Result { unsafe { self.take().visit_bytes(v).unsafe_map(Out::new) } } fn erased_visit_borrowed_bytes(&mut self, v: &'de [u8]) -> Result { 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) -> Result { unsafe { self.take().visit_byte_buf(v).unsafe_map(Out::new) } } fn erased_visit_none(&mut self) -> Result { unsafe { self.take().visit_none().unsafe_map(Out::new) } } fn erased_visit_some( &mut self, deserializer: &mut dyn Deserializer<'de>, ) -> Result { unsafe { self.take().visit_some(deserializer).unsafe_map(Out::new) } } fn erased_visit_unit(&mut self) -> Result { unsafe { self.take().visit_unit().unsafe_map(Out::new) } } fn erased_visit_newtype_struct( &mut self, deserializer: &mut dyn Deserializer<'de>, ) -> Result { unsafe { self.take() .visit_newtype_struct(deserializer) .unsafe_map(Out::new) } } fn erased_visit_seq(&mut self, seq: &mut dyn SeqAccess<'de>) -> Result { unsafe { self.take().visit_seq(seq).unsafe_map(Out::new) } } fn erased_visit_map(&mut self, map: &mut dyn MapAccess<'de>) -> Result { unsafe { self.take().visit_map(map).unsafe_map(Out::new) } } fn erased_visit_enum(&mut self, data: &mut dyn EnumAccess<'de>) -> Result { unsafe { self.take().visit_enum(data).unsafe_map(Out::new) } } } impl<'de, T> SeqAccess<'de> for erase::SeqAccess where T: serde::de::SeqAccess<'de>, { fn erased_next_element( &mut self, seed: &mut dyn DeserializeSeed<'de>, ) -> Result, Error> { self.as_mut().next_element_seed(seed).map_err(erase) } fn erased_size_hint(&self) -> Option { self.as_ref().size_hint() } } impl<'de, T> MapAccess<'de> for erase::MapAccess where T: serde::de::MapAccess<'de>, { fn erased_next_key( &mut self, seed: &mut dyn DeserializeSeed<'de>, ) -> Result, Error> { self.as_mut().next_key_seed(seed).map_err(erase) } fn erased_next_value(&mut self, seed: &mut dyn DeserializeSeed<'de>) -> Result { 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, Error> { self.as_mut().next_entry_seed(k, v).map_err(erase) } fn erased_size_hint(&self) -> Option { self.as_ref().size_hint() } } impl<'de, T> EnumAccess<'de> for erase::EnumAccess 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::().unit_variant().map_err(erase) } } unit_variant:: }, visit_newtype: { unsafe fn visit_newtype<'de, T>( a: Any, seed: &mut dyn DeserializeSeed<'de>, ) -> Result where T: serde::de::EnumAccess<'de>, { unsafe { a.take::() .newtype_variant_seed(seed) .map_err(erase) } } visit_newtype:: }, tuple_variant: { unsafe fn tuple_variant<'de, T>( a: Any, len: usize, visitor: &mut dyn Visitor<'de>, ) -> Result where T: serde::de::EnumAccess<'de>, { unsafe { a.take::() .tuple_variant(len, visitor) .map_err(erase) } } tuple_variant:: }, struct_variant: { unsafe fn struct_variant<'de, T>( a: Any, fields: &'static [&'static str], visitor: &mut dyn Visitor<'de>, ) -> Result where T: serde::de::EnumAccess<'de>, { unsafe { a.take::() .struct_variant(fields, visitor) .map_err(erase) } } struct_variant:: }, }; (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(self, deserializer: D) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, name: &'static str, visitor: V) -> Result 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($($mut)* self, name: &'static str, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, len: usize, visitor: V) -> Result 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($($mut)* self, name: &'static str, len: usize, visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, name: &'static str, fields: &'static [&'static str], visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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($($mut)* self, name: &'static str, variants: &'static [&'static str], visitor: V) -> Result 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($($mut)* self, visitor: V) -> Result 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 + '_>); impl_deserializer_for_trait_object!({mut} Box + Send + '_>); impl_deserializer_for_trait_object!({mut} Box + Sync + '_>); impl_deserializer_for_trait_object!({mut} Box + 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(self, v: bool) -> Result where E: serde::de::Error, { self.erased_visit_bool(v).map_err(unerase) } fn visit_i8(self, v: i8) -> Result where E: serde::de::Error, { self.erased_visit_i8(v).map_err(unerase) } fn visit_i16(self, v: i16) -> Result where E: serde::de::Error, { self.erased_visit_i16(v).map_err(unerase) } fn visit_i32(self, v: i32) -> Result where E: serde::de::Error, { self.erased_visit_i32(v).map_err(unerase) } fn visit_i64(self, v: i64) -> Result where E: serde::de::Error, { self.erased_visit_i64(v).map_err(unerase) } fn visit_i128(self, v: i128) -> Result where E: serde::de::Error, { self.erased_visit_i128(v).map_err(unerase) } fn visit_u8(self, v: u8) -> Result where E: serde::de::Error, { self.erased_visit_u8(v).map_err(unerase) } fn visit_u16(self, v: u16) -> Result where E: serde::de::Error, { self.erased_visit_u16(v).map_err(unerase) } fn visit_u32(self, v: u32) -> Result where E: serde::de::Error, { self.erased_visit_u32(v).map_err(unerase) } fn visit_u64(self, v: u64) -> Result where E: serde::de::Error, { self.erased_visit_u64(v).map_err(unerase) } fn visit_u128(self, v: u128) -> Result where E: serde::de::Error, { self.erased_visit_u128(v).map_err(unerase) } fn visit_f32(self, v: f32) -> Result where E: serde::de::Error, { self.erased_visit_f32(v).map_err(unerase) } fn visit_f64(self, v: f64) -> Result where E: serde::de::Error, { self.erased_visit_f64(v).map_err(unerase) } fn visit_char(self, v: char) -> Result where E: serde::de::Error, { self.erased_visit_char(v).map_err(unerase) } fn visit_str(self, v: &str) -> Result where E: serde::de::Error, { self.erased_visit_str(v).map_err(unerase) } fn visit_borrowed_str(self, v: &'de str) -> Result where E: serde::de::Error, { self.erased_visit_borrowed_str(v).map_err(unerase) } #[cfg(any(feature = "std", feature = "alloc"))] fn visit_string(self, v: String) -> Result where E: serde::de::Error, { self.erased_visit_string(v).map_err(unerase) } fn visit_bytes(self, v: &[u8]) -> Result where E: serde::de::Error, { self.erased_visit_bytes(v).map_err(unerase) } fn visit_borrowed_bytes(self, v: &'de [u8]) -> Result where E: serde::de::Error, { self.erased_visit_borrowed_bytes(v).map_err(unerase) } #[cfg(any(feature = "std", feature = "alloc"))] fn visit_byte_buf(self, v: Vec) -> Result where E: serde::de::Error, { self.erased_visit_byte_buf(v).map_err(unerase) } fn visit_none(self) -> Result where E: serde::de::Error, { self.erased_visit_none().map_err(unerase) } fn visit_some(self, deserializer: D) -> Result 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(self) -> Result where E: serde::de::Error, { self.erased_visit_unit().map_err(unerase) } fn visit_newtype_struct(self, deserializer: D) -> Result 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(self, seq: V) -> Result 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(self, map: V) -> Result 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(self, data: V) -> Result 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(&mut self, seed: T) -> Result, 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 { (**self).erased_size_hint() } } impl<'de> serde::de::MapAccess<'de> for &mut (dyn MapAccess<'de> + '_) { type Error = Error; fn next_key_seed(&mut self, seed: K) -> Result, 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(&mut self, seed: V) -> Result 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 { (**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(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, tuple_variant: unsafe fn(Any, len: usize, visitor: &mut dyn Visitor<'de>) -> Result, struct_variant: unsafe fn( Any, fields: &'static [&'static str], visitor: &mut dyn Visitor<'de>, ) -> Result, } 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(self, seed: T) -> Result 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(self, len: usize, visitor: V) -> Result 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( self, fields: &'static [&'static str], visitor: V, ) -> Result 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 { (**self).erased_deserialize_any(visitor) } fn erased_deserialize_bool(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_bool(visitor) } fn erased_deserialize_i8(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_i8(visitor) } fn erased_deserialize_i16(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_i16(visitor) } fn erased_deserialize_i32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_i32(visitor) } fn erased_deserialize_i64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_i64(visitor) } fn erased_deserialize_i128(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_i128(visitor) } fn erased_deserialize_u8(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_u8(visitor) } fn erased_deserialize_u16(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_u16(visitor) } fn erased_deserialize_u32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_u32(visitor) } fn erased_deserialize_u64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_u64(visitor) } fn erased_deserialize_u128(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_u128(visitor) } fn erased_deserialize_f32(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_f32(visitor) } fn erased_deserialize_f64(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_f64(visitor) } fn erased_deserialize_char(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_char(visitor) } fn erased_deserialize_str(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_str(visitor) } fn erased_deserialize_string(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_string(visitor) } fn erased_deserialize_bytes(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_bytes(visitor) } fn erased_deserialize_byte_buf(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_byte_buf(visitor) } fn erased_deserialize_option(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_option(visitor) } fn erased_deserialize_unit(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_unit(visitor) } fn erased_deserialize_unit_struct(&mut self, name: &'static str, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_unit_struct(name, visitor) } fn erased_deserialize_newtype_struct(&mut self, name: &'static str, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_newtype_struct(name, visitor) } fn erased_deserialize_seq(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_seq(visitor) } fn erased_deserialize_tuple(&mut self, len: usize, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_tuple(len, visitor) } fn erased_deserialize_tuple_struct(&mut self, name: &'static str, len: usize, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_tuple_struct(name, len, visitor) } fn erased_deserialize_map(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_map(visitor) } fn erased_deserialize_struct(&mut self, name: &'static str, fields: &'static [&'static str], visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_struct(name, fields, visitor) } fn erased_deserialize_identifier(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_identifier(visitor) } fn erased_deserialize_enum(&mut self, name: &'static str, variants: &'static [&'static str], visitor: &mut dyn Visitor<'de>) -> Result { (**self).erased_deserialize_enum(name, variants, visitor) } fn erased_deserialize_ignored_any(&mut self, visitor: &mut dyn Visitor<'de>) -> Result { (**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 + '_>); deref_erased_deserializer!(<'de> Deserializer<'de> for Box + Send + '_>); deref_erased_deserializer!(<'de> Deserializer<'de> for Box + Sync + '_>); deref_erased_deserializer!(<'de> Deserializer<'de> for Box + Send + Sync + '_>); deref_erased_deserializer!(<'de, T: ?Sized + Deserializer<'de>> Deserializer<'de> for &mut T); // ERROR /////////////////////////////////////////////////////////////////////// fn erase(e: E) -> Error where E: Display, { serde::de::Error::custom(e) } fn unerase(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 ::erase(&mut de); assert_eq!(expected, deserialize::(de).unwrap()); } // test boxed trait object { let mut de = serde_json::Deserializer::from_slice(json); let mut de: Box = Box::new(::erase(&mut de)); assert_eq!(expected, deserialize::(&mut de).unwrap()); } } #[test] fn test_value() { test_json::(br#"["a", 1, [true], {"a": 1}]"#); } #[test] fn test_struct() { #[derive(Deserialize, PartialEq, Debug)] struct S { f: usize, } test_json::(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::(br#""Unit""#); test_json::(br#"{"Newtype":true}"#); test_json::(br#"{"Tuple":[true,false]}"#); test_json::(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::>(); assert::>(); assert::>(); assert::>(); assert::>(); } }