I also do not know how it's a full solution. Part of the problem with answering questions like this on a forum is that they are sometimes minimized too much (as we saw here) and then expanded with details that actually matter, but not enough to get the full picture of how the situation came about in the first place. Classic XY problem stuff.
The RefCell
makes the lifetime invariant [1] on the cyclic loans. And invariant shared references have some limitations (only shared access is available, no moving, no non-trivial destructors) but they are actually ergonomic to use, as long as you can avoid the potholes.
The error you showed is just that: taking a mutable reference when only shared references are allowed. At that point, the invariant shared lifetime has been released into the wild, and there is no putting it back in the bottle. It's "shared forever".
To mutate the map after this point of no return, it needs interior mutability. Replacing HashMap
with FrozenMap
is a potential solution, here. (The values need to be boxed, per the FrozenMap
constraints.)
If you would like to learn more about arenas, you might be interested in Arenas in Rust - In Pursuit of Laziness
I was reminded of this fact when corrected in Incremental borrow accumulation = anti-pattern? - #8 by programmerjake. ↩︎