this post was submitted on 15 Jun 2023
162 points (94.5% liked)
Programming
17495 readers
10 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities !webdev@programming.dev
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
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 beenPin
ned 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 toUnpin
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'vePin
ned something, it should stayPin
ned.