dtolnay / erased-serde

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

Implement `serde::Deserialize` on `Box<dyn MyTrait>` #60

Closed tqwewe closed 1 year ago

tqwewe commented 2 years ago

I'm sorry if this may be a duplicate, but I've gone down a bit of a rabbit hole trying to get this to work.

I have a trait Subscriber which I need to use as a trait object (Box<dyn Subscriber>), but I also require that it implements Serialize + DeserializeOwned from serde.

pub trait Subscriber {
    fn enabled(&self) -> bool;
}

I've tried adding where Self: erased_serde::Serialize, though it seems like this crate doesn't provide a Deserialize trait? So I attempted to implement it manually:

impl<'de> Deserialize<'de> for Box<dyn Subscriber> {
    // ...
}

Though I'm not sure what I could put for the implementation.

After struggling, I came across the typetag crate, but sadly my target is wasm and I am having issues with ctor: https://github.com/mmastrac/rust-ctor/issues/14

StefanNienhuis commented 1 year ago

Has anyone found a solution for this that’s compatible with WASM?

dtolnay commented 1 year ago

Let's centralize discussion of typetag's wasm support in https://github.com/dtolnay/typetag/issues/54.