3 ms·
On trees: Can you use regular references for the children, and weak references for the link back to the parent? That's a valid structure that tears down properl
by Animats 10y ago
On trees: Can you use regular references for the children, and weak references for the link back to the parent? That's a valid structure that tears down properly. With strong backpointers, when one of the objects is deleted, there's a moment when there's still a live reference to it. That's invalid under Rust's rules, so you can't do that.
There's also the option to own all tree objects with some collection for allocation purposes, and use weak references for all inter-object links.
As for traits, Rust does seem to have an overly novel and complex type system, and I'm not going to defend it.
- Gankro 10y agoThe only real difference between a weak and strong pointer is that weak pointers are less prone to leaking memory (in exchange for the possibility of "sorry I'm dead" being a response to accesses). As soon as you upgrade such a pointer you've created a cyclic graph. He only way for Rust's type system to understand these kinds of access patterns in a way that doesn't require the insertion of "shared mutability" types (e.g. Mutexes) involves mutating the tree during traversals. A simple version of such a pattern: a "doubly linked" list which is actually two singly linked lists. One "traverses" this list by popping nodes off one and pushing them onto the other. The user has easy access to the heads of both lists. You could probably do the same with trees but this is a pretty unpleasant pattern.
- lmm 10y agoTree zippers can be pretty nice to work with, no?
- pcwalton 10y agoYou don't need weak references. Just use strong ones.
- Animats 10y agoThen you have an ownership loop.
- pcwalton 10y agoI meant reference counted strong pointers. Non-reference counted weak pointers make no sense from a systems point of view: in order to be memory safe you have to track the number of outstanding pointers, which is effectively a reference count, so you might as well be honest and use a real reference count.