Pin<Box<T>> Auto-dereference pinned container

I don't think .as_mut() is your problem here, ultimately. At the end of the day, you can't access a Container simultaneously via a reference (like self) and via the parent pointer; there are multiple other discussions about this in this forum and on github. Whenever you access it via the reference, the compiler will disregard updates done via the pointer because of its uniqueness assumption. And once you develop your code further, this is almost impossible to avoid.

Putting the object in a Pin<Box<>> may mask that, but doesn't change that. In particular, the Pin part doesn't help at all (contrary to intuition), and the Box<> may currently mask it. Even if you get code that passes MIRI in its current version, once you turn track-raw-pointers on in Miri, it'll be flagged.

In practice, until Rust explicitly introduces a primitive that informs the compiler that all or part of a referenced object is also aliased via a raw pointer, I think this means that writing any code that uses both references and raw pointers is a fool's errand. (Example). This is a bit of a sad state of affairs for it means that code that necessitates raw pointers will ultimately be mostly if not exclusively C style.

2 Likes