rust-sen/sehn/src/se/impls.rs

144 lines
3.6 KiB
Rust

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<S: Serializer>(&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<S: Serializer>(&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<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
where
T: Serialize $(+ $tbound1 $(+ $tbound2)*)*,
$($typaram: $bound,)*
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<(), S::Error>
where
S: Serializer,
{
serializer.serialize_list(self)
}
}
}
}
//#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(Vec<T>);
// #[cfg(any(feature = "std", feature = "alloc"))]
// seq_impl!(BinaryHeap<T: Ord>);
// #[cfg(any(feature = "std", feature = "alloc"))]
// seq_impl!(BTreeSet<T: Ord>);
// #[cfg(feature = "std")]
// seq_impl!(HashSet<T: Eq + Hash, H: BuildHasher>);
// #[cfg(any(feature = "std", feature = "alloc"))]
// seq_impl!(LinkedList<T>);
// #[cfg(any(feature = "std", feature = "alloc"))]
// seq_impl!(VecDeque<T>);
////////////////////////////////////////////////////////////////////////////////
impl<T> Serialize for [T]
where
T: Serialize,
{
#[inline]
fn serialize<S>(&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<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
where
K: Serialize $(+ $kbound1 $(+ $kbound2)*)*,
V: Serialize,
$($typaram: $bound,)*
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<(), S::Error>
where
S: Serializer,
{
serializer.serialize_dict(self)
}
}
}
}
//#[cfg(any(feature = "std", feature = "alloc"))]
dict_impl!(BTreeMap<K: Ord, V>);
//#[cfg(feature = "std")]
dict_impl!(HashMap<K: Eq + Hash, V, H: BuildHasher>);
////////////////////////////////////////////////////////////////////////////////
macro_rules! deref_impl {
(
$(#[doc = $doc:tt])*
<$($desc:tt)+
) => {
$(#[doc = $doc])*
impl <$($desc)+ {
#[inline]
fn serialize<S>(&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!(<T: ?Sized> Serialize for Box<T> where T: Serialize);