Suppose I have a Zoo
consisting of Animal
s, where Animal
s need to have a reference to their Species
. (Species
can't just be an owned value underneath Animal
, because it contains mutable data like population numbers, etc.) There are a few ways I can represent this. Off the top of my head:
Animal
can hold a reference to toSpecies
with a lifetime parameter onAnimal
Animal
can hold anArc<Species>
to avoid putting lifetime parameters everywhereAnimal
can hold aSpeciesID
(likely just wrapping ausize
), and just keep aMap<SpeciesID, Species>
elsewhere (basically implement my own reference to avoid the borrow checker altogether.)
I want to use serde to save/load the state of my Zoo
. AFAIK, this is impossible except by option #3 above, as from How do I use Serde to deserialize structs with references from a reader? it sounds like I can't serialize/de-serialize references at all.
This feels like I'm making a mistake, though: I'm basically re-inventing the wheel to avoid the borrow checker, by implementing my own clunky references through SpeciesID
. It also requires that the set of Species
be passed as a parameter to any operation on Animal
that has to access Species
, which wouldn't be the case for options 1 or 2.
As an added complication, I plan to have multi threading involved in this mess eventually, so Mutex
s may be a thing at some level.
I could also see having alternate forms for serialization vs. runtime, with some translation step to convert rust references to and from the SpeciesID
model around serialization. That sounds like a lot of error prone boiler plate, though.
Is there a better approach to this that I'm missing?