erased_serde

Function serialize

Source
pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where T: ?Sized + Serialize, S: Serializer,
Expand description

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;

serialize_trait_object!(Event);