This design is a thought experiment of mine, which has its origins in the ongoing discussion of the ECS (Entity Component System) design in Rust. The basic idea behind ECS is:
- Entities are owned at the top level, usually by a
Vec<T>
. - Indices into the
Vec
are used to reference a given entity within complex data structures.
This design works really well with Rust's borrow checker, because of the single ownership structure. Since indices alone don't allow read or write access to the data, Rust's borrow checker doesn't consider them aliases. It does have a couple drawbacks:
- If an entity is removed from the Vec without cleaning up all existing index references, then those indices are now stale, and may end up pointing at a different object, breaking application logic (but not causing any UB).
- It's difficult to assert that all index references to a given entity have been removed from nested data structures.
- Slots in the Vec now need to be managed to avoid fragmentation and unbounded growth.