I have a struct with string fields. I'd like to control how the memory for the strings is allocated. In particular, I'd like to allocate them using something like copy_arena
.
Maybe I could make a custom ArenaString
type, but I don't see how to get a reference to the Arena
into the deserialization code, and assuming that's possible, then I'll have to deal with the arena lifetime, right?
Here is one possible implementation that uses
serde::de::DeserializeSeed
to expose the arena allocator to the deserialization code.In a more elaborate use case you may want to write a procedural macro to generate such impls.
If arena allocation is not a strict requirement and you just need to amortize the cost of string allocation across lots of deserialized objects,
Deserialize::deserialize_in_place
is a more concise alternative.