Closed AlexEne closed 6 years ago
My cargo.toml:
[dependencies]
sdl2 = "0.31.0"
imgui = "0.0.18"
gl = "0.6.0"
memoffset = "0.1"
png = "0.11.0"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
erased-serde = "0.3"
It is hard to tell without seeing more of your code but serialize_trait_object!
is typically how you want to set up serializable trait objects.
trait Task: erased_serde::Serialize {
/* ... */
}
serialize_trait_object!(Task);
Wow, thanks! How did I miss that? It solved the serialize error I was describing.
But now, I have another question for the same scenario.
How would deserialize work in that case? I see no macro for deserialize (or maybe I don't know where to look).
Do I need to just call erased_serde::deserialize() like so Vec<Box<Task>> tasks = erased_serde::deserialize()
for the holder of that array?
P.S After I solve deserialization I will submit a PR with this exact but simplified example because it may be useful for others
There is no such thing as Box<Deserialize>
. You may be interested in deserializing to serde_value::Value
instead when the concrete type is not known.
I suspected that's the solution. Thanks for the help.
Hello,
With the risk of being annoying since this was discussed before, I am currently trying to resolve this exact issue here https://github.com/dtolnay/erased-serde/issues/5, however I am stuck at the following step:
This comes from something similar to the example on the above-mentioned issue:
Following the suggestion in the error and requiring using the following:
where S: serde::Serializer + serde::Deserializer
doesn't work either. What I am basically trying to do is save a vector of BoxAll Tasks structs that implement the Task trait, also implement Serialize in a similar way to this:
I also tried
#derive[Serialize]
on the CraftingTask but it doesn't help either. There is also no clear guide on converting the error types to Serializer::Ok / Error. The map Example in the issue I linked above is not valid anymore (The return type is not Result<(), Error>, but Result<S::Ok, S::Error>If I manage to solve this, I am happy to contribute a guide in the readme for this use case, but I don't really get how I should fix it right now.
One approach would be just manually implementing Workbench serialize (for the holder of Vec, then manually calling some simpler task_serialize function there.
Something like this:
In that method I can implement whatever I need for the serialization routine possibly.