#[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 {
(begin < $($rest:tt)*) => {
$crate::__internal_serialize_trait_object!(generics () () $($rest)*);
};
(begin $first:tt $($rest:tt)*) => {
$crate::__internal_serialize_trait_object!(path () ($first) $($rest)*);
};
(generics ($($generics:tt)*) () , > $($rest:tt)*) => {
$crate::__internal_serialize_trait_object!(path ($($generics)* ,) () $($rest)*);
};
(generics ($($generics:tt)*) () > $($rest:tt)*) => {
$crate::__internal_serialize_trait_object!(path ($($generics)* ,) () $($rest)*);
};
(generics ($($generics:tt)*) ($($brackets:tt)*) < $($rest:tt)*) => {
$crate::__internal_serialize_trait_object!(generics ($($generics)* <) ($($brackets)* <) $($rest)*);
};
(generics ($($generics:tt)*) (< $($brackets:tt)*) > $($rest:tt)*) => {
$crate::__internal_serialize_trait_object!(generics ($($generics)* >) ($($brackets)*) $($rest)*);
};
(generics ($($generics:tt)*) ($($brackets:tt)*) $first:tt $($rest:tt)*) => {
$crate::__internal_serialize_trait_object!(generics ($($generics)* $first) ($($brackets)*) $($rest)*);
};
(path ($($generics:tt)*) ($($path:tt)*) where $($rest:tt)*) => {
$crate::__internal_serialize_trait_object!(sendsync ($($generics)*) ($($path)*) ($($rest)*));
};
(path ($($generics:tt)*) ($($path:tt)*)) => {
$crate::__internal_serialize_trait_object!(sendsync ($($generics)*) ($($path)*) ());
};
(path ($($generics:tt)*) ($($path:tt)*) $first:tt $($rest:tt)*) => {
$crate::__internal_serialize_trait_object!(path ($($generics)*) ($($path)* $first) $($rest)*);
};
(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)*));
};
(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)
}
}
};
}
#[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>();
}
}