4 ms·
Arenas have more to do with the allocation pattern, they don't solve the cycle problem, right?
by jeffdavis 9y ago
Arenas have more to do with the allocation pattern, they don't solve the cycle problem, right?
- trishume 9y agoYou're correct, that crate doesn't solve the cycle problem. There is a different way of doing arenas in Rust that does though. What you do is you put all of your tree nodes in a big Vec<Node> and instead of referring to children and parents via pointers, you do so via indices. It's less convenient because you have to pass around a reference to your "arena" everywhere (the Vec or a slice of it), and it incurs bounds checks (pretty cheap though). But, it solves the problem in a way that is guaranteed safe.
- GolDDranks 9y agoIn a sense, they solve the cycle problem in Rust. The trick is that once something is allocated to an arena, it has the same lifetime as the whole arena, and that allows cyclical links because none of the nodes "outlive each other". However, you must have interior mutability (achieved using Cell types), because otherwise you can't mutate the parent nodes to add links to child nodes. Check the typed arena crate in crates.io and the Cell type in the standard library.