[][src]Function erased_serde::serialize

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

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 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.

#[macro_use]
extern crate erased_serde;

serialize_trait_object!(Event);