DEV Community

Discussion on: Rust looks awesome

Collapse
 
mmstick profile image
Michael Murphy

If you ever run into situation where you're needing to design a complex system where the borrow-checker and lifetimes get in the way, check out the slotmap and froggy crates. It's a common pain point for some that are trying to write self-referential and graph data structures.

Slotmap provides generational arenas returning entity keys to data in a slotmap, with secondary maps for storing additional optional components to an entity key. Essentially a simple entity-component system.

Froggy provides a component graph system, where components are stored in arenas, with runtime reference counting to free slots when a pointer ceases to exist.

Additionally, it's a good idea to look into taking those enums and combining them with channels for message-passing between threads. One of the neat things about the generational arena approach is that you can pass entity keys along with the enum to associate requests with an entity.

Collapse
 
louy2 profile image
Yufan Lou

Your link to froggy is still to slotmap.

Use this: crates.io/crates/froggy

Collapse
 
ivan profile image
Ivan Dlugos

Thanks for the info, I'll have a look. Indeed, graphs are one of the situations where borrowing wouldn't do so I'm curious how that can be solved.