This is great! Props to @cramertj for having the presence of mind to think of this, and to @RalfJung for carrying through the investigation.
I only have three random thoughts.
-
It’s vaguely bothered me for a while that Rust doesn’t have a clear answer for how to safely implement things like Qt’s
QObject
hierarchies, which have both parent-child and child-parent pointers, without resorting toRc
andWeak
pointers (which C++ doesn’t need to). This seems to be exactly what is needed. I don’t know if one would typically want to do such a thing, but it’s good to know how it’d now (hopefully, presumably) be possible! -
Could
RefCell
add a bit to its borrow flags to keep track of whether its contents are pinned, slash would doing this make sense? A bit that once set, stays set, and if it is set, you can getPin
s out of theRefCell
but not&mut
s. (It seems we have wisely refrained from exposing aBorrowState
enumerating all of the possible states aRefCell
can be in.) -
Could you use this to implement a doubly-linked list in safe code? Or at least, less-
unsafe
code? Not as a (publicly) intrusive collection, but something like theLinkedList
API instd
.