Safe Intrusive Collections with Pinning

This is great! Props to @cramertj for having the presence of mind to think of this, and to @RalfJung for carrying through the investigation. :slight_smile:

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 to Rc and Weak 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 get Pins out of the RefCell but not &muts. (It seems we have wisely refrained from exposing a BorrowState enumerating all of the possible states a RefCell 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 the LinkedList API in std.

2 Likes