dtolnay / erased-serde

Type-erased Serialize, Serializer and Deserializer traits
Apache License 2.0
709 stars 36 forks source link

How to call a method which takes a serde::Serializer with an erased Serializer? #106

Open TheNeikos opened 3 months ago

TheNeikos commented 3 months ago

As the title says, I am trying to call a 'classic' serde serialize method:

pub fn serialize<__S>(
    __self: &Message,
    __serializer: __S
) -> Result<__S::Ok, __S::Error>
where
    __S: Serializer,

Where Message is a concrete type, that does not implement serde::Serialize (due to being a remote type).

In the context this is in, I have a &mut dyn Serializer and am expected to return a Result<(), erased::Error> so overall, I feel that this is possible.

Looking under the hood, this is also something that erased_serde does with the MakeSerializer struct. However it is not public.

Due to the license, I could copy that as a quick fix, but maybe there is a gotcha and/or an expected way this is solved?

Actually I can't copy it, because erased_serde::Serialize is sealed. :disappointed: