Rust Pinning

Written
  • Useful Reading
  • Pinning is useful in async because executors have references to Futures while polling. If the executor tries to use a future that was moved, then bad things happen. So the Pin keeps it in one place while the executor can poll it.
  • Pin<Box<T>> is a pattern that shows up a lot
    • This is similar to using an Arc<Mutex<T>> or similar for lifetimes. It's not the most efficient way to write things but can be a lot easier to put together.
    • It allows a type that is normally !Unpin to be used where an Unpin is needed, because the pinned type is then the Box and while the Box value can move, the value that it points to will always stay in the same location on the heap until it is deallocated, as required by the Pin semantics.

Thanks for reading! If you have any questions or comments, please send me a note on Twitter. And if you enjoyed this, I also have a newsletter where I write about tech thoughts, interesting things I've read, and project updates each Thursday.

You can check out a recent issue, or enter your email below to subscribe.