• 0 Posts
  • 12 Comments
Joined 2 years ago
cake
Cake day: June 15th, 2023

help-circle











  • Eh, if by smart pointer you mean Pin. It’s not really a smart pointer. It’s just a struct that holds onto a particular reference kind. What it holds onto can be a smart pointer, or a mutable reference. Either way, once done, the constraints of the language’s ownership and borrowing mean the item that has been Pinned can’t be moved.

    An item being unable to be moved is pretty important for self referential structures of course, since to self reference, you generally refer to something by some form of pointer inside yourself. If you are able to be moved, your own root address changes and thus the address of anything inside you would be different, which would invalidate your self references.

    Pin was quite a clever realization.

    However, unfortunately, not all considerations you need to be aware of when using Pin can be enforced by the type system, usually around when you need to Unpin something. And you get that wrong you might end up in a place that would cause Undefined Behavior. Which is why the general advice is, once you’ve Pinned something, it should stay Pinned.