Expand description
Types that pin data to a location in memory.
It is sometimes useful to be able to rely upon a certain value not being able to move, in the sense that its address in memory cannot change. This is useful especially when there are one or more pointers pointing at that value. The ability to rely on this guarantee that the value a pointer is pointing at (its pointee) will
- Not be moved out of its memory location
- More generally, remain valid at that same memory location
is called “pinning.” We would say that a value which satisfies these guarantees has been
“pinned,” in that it has been permanently (until the end of its lifespan) attached to its
location in memory, as though pinned to a pinboard. Pinning a value is an incredibly useful
building block for unsafe
code to be able to reason about whether a raw pointer to the
pinned value is still valid. As we’ll see later, this is necessarily from the
time the value is first pinned until the end of its lifespan. This concept of “pinning” is
necessary to implement safe interfaces on top of things like self-referential types and
intrusive data structures which cannot currently be modeled in fully safe Rust using only
borrow-checked references.
“Pinning” allows us to put a value which exists at some location in memory into a state where
safe code cannot move that value to a different location in memory or otherwise invalidate it
at its current location (unless it implements Unpin
, which we will
talk about below). Anything that wants to interact with the pinned value in a way
that has the potential to violate these guar