I would like to make the following struct serializable by constraining the various items in the vector to be serializable themselves.
#[derive(Serialize, Debug)]
struct Card {
span: u8,
title: String,
content: Vec<Box<dyn Serialize>>
}
The problem here is that it is not possible to turn the default Serialize trait from Serde into a trait object due to the serialize
method using generic type parameters. One (or perhaps the only) solution to this problem is to use the type erased versions of the traits from dtolay's erased_serde
crate.
In light of this, I have updated my dependencies as follows and bought erased_serde::Serialize
into scope.
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
serde-pickle = { version = "0.6" }
erased-serde = { version = "0.3" }
However, now I have the problem that erased_serde::Serialize
doesn't seem to work with #[derive(Serialize)]
for structs. Is it possible to use #[derive(Serialize)]
with the trait from erased_serde
?