4 ms·
The problem arises by the fact that `Class` needs to refer to other classes: pub struct Class<'a> { pub superclass: Option<ClassRef<'a>>, }
by andreabergia 3y ago
The problem arises by the fact that `Class` needs to refer to other classes:
pub struct Class<'a> {
pub superclass: Option<ClassRef<'a>>,
}
pub type ClassRef<'a> = &'a Class<'a>;
Given that classes are managed by the arena, I know the reference will not be dangling thanks to the 'a lifetime.
Initially I had implemented this with raw pointers, without lifetimes, but then I switched to the reference because it felt more "idiomatic" and I had to put the lifetimes just about everywhere to make the compiler happy.
If there are better ways to do this, I would be really happy to learn, though!
- bialpio 3y agoYeah, I think I understand why you had to write it this way, but the fact that this lifetime needs to be bubbled up so far up is really non-intuitive to me. > If there are better ways to do this, I would be really happy to learn, though! Me, too. :) I find myself limited by my way of thinking here, coming from C++ ("I _know_ it lives long enough, why don't you let me express this?").
- andreabergia 3y agoAgreed, it _is_ annoying. It bubbles up everywhere and it feels like "something to silence the compiler" more than "something to express the safety of the code", as other people have pointed out.
- 0b01 3y agoSame way to write self referential structs - use index types into the whatever arena you are using. Indexes are usually 32 bit so they are a bit faster than pointers. If you are building one off trees such as for parsing and ast transforms, bumpalo is your friend. In your case, you can look into generational arenas and slabs which are useful for graphs.
- pie_flavor 3y agoThe better way to do this is to use an indexing arena such as `slotmap` or `generational-arena`, so `ClassRef` is not actually a memory pointer to anything but can still be looked up in the context of the class collection. Or, if you never need to remove any values, you can use Vec as your arena and usize as your key.
- fleventynine 3y agoIMHO, using an vector index as a pointer should be a last resort (as an alternative to unsafe). Although it's memory safe, you still have many of the traditional issues like stale pointers / use-after-free, plus you also have the cost of a bounds-check on every access and making sure you use your stored indexes with the correct container instance. If you can solve your problem by using safe code with lifetimes, that should be preferred.
- pie_flavor 3y agoWhat is presented here is a self-referential type, and Rust does not allow you to declare self-referential types, and an indexed arena is a solution to that. You are correct that you still have all the issues you described - that doesn't mean it should be a 'last resort' to stop using an incorrect system of lifetimes, because there is no 'safe code with lifetimes' that encodes a self-referential type. The type safety issue is solved by using slotmap or typed-generational-arena, which use unique key types.
- kevincox 3y agoFor things like classes that aren't going to be created and destroyed quickly it likely is best to just use a Rc. Then you know it will be available and don't need to bother with lifetimes. Of course there is a good chance that you will eventually want a VM-scoped lifetime for something else so maybe it is best to just start now.