4 ms·
> Rust does need a better way to do backlinks. You can do it with Rc, RefCell, and Weak, but it involves run-time borrow checks that should never fail. Those sh
by pshc 2y ago
> Rust does need a better way to do backlinks. You can do it with Rc, RefCell, and Weak, but it involves run-time borrow checks that should never fail. Those should be checked at compile time.
It's not clear to me how rustc could detect a dangling backlink in a tree structure at compile time. Seems impossible short of adding proofs to the type system.
- o11c 2y agoIf the language actually supported a full set of memory policies it would be quite possible. Unfortunately it only thinks about are unique, an opinionated form of borrowed ownership, and a little bit about shared (weak I think is punted entirely to the library?), and not all other ownership policies can effectively be implemented on top of them. The usual thing approach would be: given two types, P and C (which may be the same type in case of homogeneous trees, but this), with at least the following fields: class P: child1: ChildPointer[C] class C: parent: ParentPointer[P] Then `p.child1 = c` will transparently be transformed into something like: p.child1?.parent = null c?.parent?.child1 = null # only needed if splice-to-steal is permitted; may need iteration if multiple children p.child1 = c p.child1?.parent = p Note that ChildPointer might be come in unique-like or shared-like implementations. ParentPointer[T] is basically Optional[UnopinionatedlyBorrowed[T]]. A have a list of several other ownership policies that people actually want: https://gist.github.com/o11c/dee52f11428b3d70914c4ed5652d43f7 https://gist.github.com/o11c/dee52f11428b3d70914c4ed5652d43f...
- SkiFire13 2y agoIs there some research that shows these are actually safe and can be reasonably checked by a compiler?