liveBook · Manning

Pin

As explained in Rust Servers, Services, and Apps

Pin is a Rust standard-library pointer type that prevents a referenced value from being moved, preserving memory-location guarantees required by some asynchronous state machines.

Pin is a Rust standard-library pointer type used when futures must be polled repeatedly without moving the referenced value in memory. Treat it as an advanced technical requirement for implementing futures; the memory-safety rationale is not developed further here. section 10.4

Pinning a custom future

A custom future implements poll with the signature `fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>`. Tokio polls the future, which returns `Poll::Pending` while waiting or `Poll::Ready(value)` when complete; the example uses `ReadFileFuture` and a Tokio task. section 10.4

How Rust Servers, Services, and Apps covers it

How deep it goes
Defines the pinned poll interface and memory-location requirement, but defers the underlying rationale as an advanced concept.
Examples
Uses a ReadFileFuture implementation whose pinned poll method progresses from Poll::Pending to Poll::Ready.
What it assumes
Assumes familiarity with asynchronous Rust futures and treats Pin itself as an advanced requirement rather than teaching it from first principles.
What you can do after
Build it from scratch by implementing a custom Future with a Pin-based poll method under Tokio.
How it connects
Connects Pin with the Future trait, Poll, Context, Waker, and the Tokio executor.

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.