erased-serde icon indicating copy to clipboard operation
erased-serde copied to clipboard

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

Open tqwewe opened this issue 2 years ago • 0 comments

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

tqwewe avatar Aug 01 '22 09:08 tqwewe