Skip to content

Commit b3dcfc6

Browse files
committed
improve Pin struct docs and add examples
1 parent 331d545 commit b3dcfc6

File tree

1 file changed

+89
-8
lines changed

1 file changed

+89
-8
lines changed

library/core/src/pin.rs

+89-8
Original file line numberDiff line numberDiff line change
@@ -901,14 +901,17 @@ use crate::{
901901
/// A pointer which pins its pointee in place.
902902
///
903903
/// [`Pin`] is a wrapper around some kind of pointer `Ptr` which makes that pointer "pin" its
904-
/// pointee value in place, thus preventing the value referenced by that pointer from being moved or
905-
/// otherwise invalidated at that place in memory unless it implements [`Unpin`].
904+
/// pointee value in place, thus preventing the value referenced by that pointer from being moved
905+
/// or otherwise invalidated at that place in memory unless it implements [`Unpin`].
906+
///
907+
/// *See the [`pin` module] documentation for a more thorough exploration of pinning.*
906908
///
907909
/// ## Pinning values with [`Pin<Ptr>`]
908910
///
909911
/// In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a
910912
/// [`Pin<Ptr>`]. [`Pin<Ptr>`] can wrap any pointer type, forming a promise that the **pointee**
911-
/// will not be *moved* or [otherwise invalidated][subtle-details].
913+
/// will not be *moved* or [otherwise invalidated][subtle-details]. Note that it is impossible
914+
/// to create or misuse a [`Pin<Ptr>`] which can violate this promise without using [`unsafe`].
912915
///
913916
/// We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning ref, or pinning
914917
/// [`Box`], etc.) because its existince is the thing that is pinning the underlying pointee in
@@ -918,13 +921,91 @@ use crate::{
918921
/// itself, but rather a pointer to that value! A [`Pin<Ptr>`] does not pin the `Ptr` but rather
919922
/// the pointer's ***pointee** value*.
920923
///
921-
/// `Pin<P>` is guaranteed to have the same memory layout and ABI as `P`.
924+
/// For the vast majoriy of Rust types, pinning a value of that type will actually have no effect.
925+
/// This is because the vast majority of types implement the [`Unpin`] trait, which entirely opts
926+
/// all values of that type out of pinning-related guarantees. The most common exception
927+
/// to this is the compiler-generated types that implement [`Future`] for the return value
928+
/// of `async fn`s. These compiler-generated [`Future`]s do not implement [`Unpin`] for reasons
929+
/// explained more in the [`pin` module] docs, but suffice it to say they require the guarantees
930+
/// provided by pinning to be implemented soundly.
922931
///
923-
/// *See the [`pin` module] documentation for a more thorough exploration of pinning.*
932+
/// This requirement in the implementation of `async fn`s means that the [`Future`] trait requires
933+
/// any [`Future`] to be pinned in order to call [`poll`] on it. Therefore, when manually polling
934+
/// a future, you will need to pin it first.
935+
///
936+
/// ### Pinning a value inside a [`Box`]
937+
///
938+
/// The simplest and most flexible way to pin a value is to put that value inside a [`Box`] and
939+
/// then turn that [`Box`] into a "pinning [`Box`]" by wrapping it in a [`Pin`].
940+
/// You can do both of these in a single step using [`Box::pin`]. Let's see an example of using
941+
/// this flow to pin a [`Future`] returned from calling an `async fn`, a common use case
942+
/// as described above.
943+
///
944+
/// ```
945+
/// use std::pin::Pin;
946+
///
947+
/// async fn add_one(x: u32) -> u32 {
948+
/// x + 1
949+
/// }
950+
///
951+
/// // Call the async function to get a future back
952+
/// let fut = add_one(42);
953+
///
954+
/// // Pin the future inside a pinning box
955+
/// let pinned_fut: Pin<Box<_>> = Box::pin(fut);
956+
/// ```
957+
///
958+
/// If you have a value which is already boxed, for example a [`Box<dyn Future>`][Box], you can pin
959+
/// that value in-place at its current memory address using [`Box::into_pin`].
960+
///
961+
/// ```
962+
/// use std::pin::Pin;
963+
/// use std::future::Future;
964+
///
965+
/// async fn add_one(x: u32) -> u32 {
966+
/// x + 1
967+
/// }
968+
///
969+
/// fn boxed_add_one(x: u32) -> Box<dyn Future<Output = u32>> {
970+
/// Box::new(add_one(x))
971+
/// }
972+
///
973+
/// let boxed_fut = boxed_add_one(42);
974+
///
975+
/// // Pin the future inside the existing box
976+
/// let pinned_fut: Pin<Box<_>> = Box::into_pin(boxed_fut);
977+
/// ```
978+
///
979+
/// There are similar pinning methods offered on the other standard library smart pointer types
980+
/// as well, like [`Rc`] and [`Arc`].
981+
///
982+
/// ### Pinning a value on the stack using [`pin!`]
983+
///
984+
/// There are some situations where it is desirable or even required (for example, in a `#[no_std]`
985+
/// context where you don't have access to the standard library or allocation in general) to
986+
/// pin a value to its location on the stack. Doing so is possible using the [`pin!`] macro. See
987+
/// its documentation for more.
988+
///
989+
/// ## Layout and ABI
990+
///
991+
/// [`Pin<Ptr>`] is guaranteed to have the same memory layout and ABI[^noalias] as `Ptr`.
992+
///
993+
/// [^noalias]: There is a bit of nuance here that is still being decided about whether the
994+
/// aliasing semantics of `Pin<&mut T>` should be different than `&mut T`, but this is true as of
995+
/// today.
924996
///
925-
/// [`pin` module]: self
926-
/// [`Box`]: ../../std/boxed/struct.Box.html
927-
/// [subtle-details]: self#subtle-details-and-the-drop-guarantee
997+
/// [`pin!`]: crate::pin::pin "pin!"
998+
/// [`Future`]: crate::future::Future "Future"
999+
/// [`poll`]: crate::future::Future::poll "Future::poll"
1000+
/// [`pin` module]: self "pin module"
1001+
/// [`Rc`]: ../../std/rc/struct.Rc.html "Rc"
1002+
/// [`Arc`]: ../../std/sync/struct.Arc.html "Arc"
1003+
/// [Box]: ../../std/boxed/struct.Box.html "Box"
1004+
/// [`Box`]: ../../std/boxed/struct.Box.html "Box"
1005+
/// [`Box::pin`]: ../../std/boxed/struct.Box.html#method.pin "Box::pin"
1006+
/// [`Box::into_pin`]: ../../std/boxed/struct.Box.html#method.into_pin "Box::into_pin"
1007+
/// [subtle-details]: self#subtle-details-and-the-drop-guarantee "pin subtle details"
1008+
/// [`unsafe`]: ../../std/keyword.unsafe.html "keyword unsafe"
9281009
//
9291010
// Note: the `Clone` derive below causes unsoundness as it's possible to implement
9301011
// `Clone` for mutable references.

0 commit comments

Comments
 (0)