Back references in Rust usually require using a reference-counted object in some form. It's hard for a single-owner struct to find its owner. This is a problem for doubly-linked lists, trees with backlinks, and anything where you just need access to the owning object. The lack of a way to do this safely under single ownership has been a long-standing problem in Rust.
Here's an approach that might work for fixing that: make it possible to derive a function that tells a struct who its owner is. A structure S which can be owned by a structure T could have a derived function. S cannot make T stay around, so this is a form of weak pointer.
self.owner()
with signature
fn owner(&self) -> Option<&T>
This is an Option because the owner might not be a struct of type T. The owner can be a local variable, for example, something that can't be referenced from elsewhere. So at times it must be None.
To generate such a function, Derive might be used:
#[derive (Owner(T))]
That would be convenient. How hard would it be to implement?
This creates a requirement that the owning struct T have some extra machinery to update the hidden backpointer in S whenever the relevant field is altered. So, in struct S, the owning pointer of T needs to be identified. Perhaps like this:
struct T {
link_to_t: OwnerOf<S>
}
How to implement this? Can this be done entirely with macros? I suspect not, but if anyone can see a way, that would be the easiest approach. Comments?