# Safe Intrusive Collections with Pinning

**URL:** <https://internals.rust-lang.org/t/safe-intrusive-collections-with-pinning/7281>\
**Category:** language design\
**Created:** [April 10, 2018, 12:00pm UTC](https://internals.rust-lang.org/t/safe-intrusive-collections-with-pinning/7281 "2018-04-10T12:00:12Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![glaebhoerl](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/glaebhoerl/32/1978_2.png) [@glaebhoerl](https://internals.rust-lang.org/u/glaebhoerl)\
**Post date:** [April 10, 2018, 10:45pm UTC](https://internals.rust-lang.org/t/safe-intrusive-collections-with-pinning/7281/11 "2018-04-10T22:45:17Z")

</div>

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 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 `Pin`s out of the `RefCell` but not `&mut`s. (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`.

---

_[View the full topic](https://internals.rust-lang.org/t/safe-intrusive-collections-with-pinning/7281)._
