use std::hash::{Hash, BuildHasher}; use std::collections::HashMap; use std::collections::BTreeMap; use super::{Serialize, Serializer}; //////////////////////////////////////////////////////////////////////////////// macro_rules! real_impl { ($prim:ty, $func:ident) => { impl Serialize for $prim { fn serialize(&self, s: S) -> Result<(), S::Error> { s.$func(*self) } } } } real_impl!(u8, serialize_real_u8); real_impl!(u16, serialize_real_u16); real_impl!(u32, serialize_real_u32); real_impl!(u64, serialize_real_u64); real_impl!(i8, serialize_real_i8); real_impl!(i16, serialize_real_i16); real_impl!(i32, serialize_real_i32); real_impl!(i64, serialize_real_i64); real_impl!(f32, serialize_real_f32); real_impl!(f64, serialize_real_f64); //////////////////////////////////////////////////////////////////////////////// impl Serialize for str { fn serialize(&self, s: S) -> Result<(), S::Error> { s.serialize_text(self) } } //////////////////////////////////////////////////////////////////////////////// //#[cfg(any(feature = "std", feature = "alloc"))] macro_rules! seq_impl { ($ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)* >) => { impl Serialize for $ty where T: Serialize $(+ $tbound1 $(+ $tbound2)*)*, $($typaram: $bound,)* { #[inline] fn serialize(&self, serializer: S) -> Result<(), S::Error> where S: Serializer, { serializer.serialize_list(self) } } } } //#[cfg(any(feature = "std", feature = "alloc"))] seq_impl!(Vec); // #[cfg(any(feature = "std", feature = "alloc"))] // seq_impl!(BinaryHeap); // #[cfg(any(feature = "std", feature = "alloc"))] // seq_impl!(BTreeSet); // #[cfg(feature = "std")] // seq_impl!(HashSet); // #[cfg(any(feature = "std", feature = "alloc"))] // seq_impl!(LinkedList); // #[cfg(any(feature = "std", feature = "alloc"))] // seq_impl!(VecDeque); //////////////////////////////////////////////////////////////////////////////// impl Serialize for [T] where T: Serialize, { #[inline] fn serialize(&self, serializer: S) -> Result<(), S::Error> where S: Serializer, { serializer.serialize_list(self) } } //////////////////////////////////////////////////////////////////////////////// //#[cfg(any(feature = "std", feature = "alloc"))] macro_rules! dict_impl { ($ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)* >) => { impl Serialize for $ty where K: Serialize $(+ $kbound1 $(+ $kbound2)*)*, V: Serialize, $($typaram: $bound,)* { #[inline] fn serialize(&self, serializer: S) -> Result<(), S::Error> where S: Serializer, { serializer.serialize_dict(self) } } } } //#[cfg(any(feature = "std", feature = "alloc"))] dict_impl!(BTreeMap); //#[cfg(feature = "std")] dict_impl!(HashMap); //////////////////////////////////////////////////////////////////////////////// macro_rules! deref_impl { ( $(#[doc = $doc:tt])* <$($desc:tt)+ ) => { $(#[doc = $doc])* impl <$($desc)+ { #[inline] fn serialize(&self, serializer: S) -> Result<(), S::Error> where S: Serializer, { (**self).serialize(serializer) } } }; } deref_impl!(<'a, T: ?Sized> Serialize for &'a T where T: Serialize); deref_impl!(<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize); //#[cfg(any(feature = "std", feature = "alloc"))] deref_impl!( Serialize for Box where T: Serialize);