@@ -945,8 +945,13 @@ use crate::{
945945///
946946/// In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a
947947/// [`Pin<Ptr>`]. [`Pin<Ptr>`] can wrap any pointer type, forming a promise that the **pointee**
948- /// will not be *moved* or [otherwise invalidated][subtle-details]. Note that it is impossible
949- /// to create or misuse a [`Pin<Ptr>`] which can violate this promise without using [`unsafe`].
948+ /// will not be *moved* or [otherwise invalidated][subtle-details]. Note that it is
949+ /// impossible to create or misuse a [`Pin<Ptr>`] to violate this promise without using [`unsafe`].
950+ /// If the pointee value's type implements [`Unpin`], we are free to disregard these requirements
951+ /// entirely and can wrap any pointer to that value in [`Pin`] directly via [`Pin::new`].
952+ /// If the pointee value's type does not implement [`Unpin`], then Rust will not let us use the
953+ /// [`Pin::new`] function directly and we'll need to construct a [`Pin`]-wrapped pointer in one of
954+ /// the more specialized manners discussed below.
950955///
951956/// We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning ref, or pinning
952957/// [`Box`], etc.) because its existince is the thing that is pinning the underlying pointee in
@@ -956,25 +961,57 @@ use crate::{
956961/// itself, but rather a pointer to that value! A [`Pin<Ptr>`] does not pin the `Ptr` but rather
957962/// the pointer's ***pointee** value*.
958963///
959- /// For the vast majoriy of Rust types, pinning a value of that type will actually have no effect.
960- /// This is because the vast majority of types implement the [`Unpin`] trait, which entirely opts
961- /// all values of that type out of pinning-related guarantees. The most common exception
962- /// to this is the compiler-generated types that implement [`Future`] for the return value
963- /// of `async fn`s. These compiler-generated [`Future`]s do not implement [`Unpin`] for reasons
964- /// explained more in the [`pin` module] docs, but suffice it to say they require the guarantees
965- /// provided by pinning to be implemented soundly.
964+ /// The most common set of types which require pinning related guarantees for soundness are the
965+ /// state machines that implement [`Future`] for the return value of `async fn`s under the
966+ /// hood. These compiler-generated [`Future`]s may contain self-referrential pointers, one of the
967+ /// most common use cases for [`Pin`]. More details on this point are provided in the
968+ /// [`pin` module] docs, but suffice it to say they require the guarantees provided by pinning to
969+ /// be implemented soundly.
966970///
967- /// This requirement in the implementation of `async fn`s means that the [`Future`] trait requires
968- /// any [`Future`] to be pinned in order to call [`poll`] on it. Therefore, when manually polling
969- /// a future, you will need to pin it first.
971+ /// This requirement from the implementation of `async fn`s means that the [`Future`] trait
972+ /// requires all calls to [`poll`] to use a <code>self: [Pin]\<&mut Self></code> parameter instead
973+ /// of the usual `&mut self`. Therefore, when manually polling a future, you will need to pin it
974+ /// first.
975+ ///
976+ /// You may notice that `async fn`-generated [`Future`]s are only a small percentage of all
977+ /// [`Future`]s that exist, yet we had to modify the signature of [`poll`] for all [`Future`]s
978+ /// to accommodate them. This is unfortunate, but there is a way that the language attempts to
979+ /// alleviate the extra friction that this API choice incurs: the [`Unpin`] trait.
980+ ///
981+ /// The vast majority of Rust types have no reason to ever care about being pinned. These
982+ /// types implement the [`Unpin`] trait, which entirely opts all values of that type out of
983+ /// pinning-related guarantees. For values of these types, pinning a value by pointing to it with a
984+ /// [`Pin<Ptr>`] will have no actual effect.
985+ ///
986+ /// The reason this distinction exists is exactly to allow APIs like [`Future::poll`] to take a
987+ /// [`Pin<Ptr>`] as an argument for all types while only forcing [`Future`] types that actually
988+ /// care about pinning guarantees pay the ergonomics cost. For the majority of [`Future`] types
989+ /// that don't have a reason to care about being pinned and therefore implement [`Unpin`], the
990+ /// <code>[Pin]\<&mut Self></code> will act exactly like a regular `&mut Self`, allowing direct
991+ /// access to the underlying value. Only types that *don't* implement [`Unpin`] will be restricted.
992+ ///
993+ /// ### Pinning a value of a type that implements [`Unpin`]
994+ ///
995+ /// If the type of the value you need to "pin" implements [`Unpin`], you can trivially wrap any
996+ /// pointer to that value in a [`Pin`] by calling [`Pin::new`].
997+ ///
998+ /// ```
999+ /// use std::pin::Pin;
1000+ ///
1001+ /// // Create a value of a type that implements `Unpin`
1002+ /// let mut unpin_future = std::future::ready(5);
1003+ ///
1004+ /// // Pin it by creating a pinning mutable reference to it (ready to be `poll`ed!)
1005+ /// let my_pinned_unpin_future: Pin<&mut _> = Pin::new(&mut unpin_future);
1006+ /// ```
9701007///
9711008/// ### Pinning a value inside a [`Box`]
9721009///
973- /// The simplest and most flexible way to pin a value is to put that value inside a [`Box `] and
974- /// then turn that [`Box`] into a "pinning [`Box`]" by wrapping it in a [`Pin`].
975- /// You can do both of these in a single step using [`Box::pin`]. Let's see an example of using
976- /// this flow to pin a [`Future`] returned from calling an `async fn`, a common use case
977- /// as described above.
1010+ /// The simplest and most flexible way to pin a value that does not implement [`Unpin `] is to put
1011+ /// that value inside a [`Box`] and then turn that [`Box`] into a "pinning [`Box`]" by wrapping it
1012+ /// in a [`Pin`]. You can do both of these in a single step using [`Box::pin`]. Let's see an
1013+ /// example of using this flow to pin a [`Future`] returned from calling an `async fn`, a common
1014+ /// use case as described above.
9781015///
9791016/// ```
9801017/// use std::pin::Pin;
@@ -1018,8 +1055,8 @@ use crate::{
10181055///
10191056/// There are some situations where it is desirable or even required (for example, in a `#[no_std]`
10201057/// context where you don't have access to the standard library or allocation in general) to
1021- /// pin a value to its location on the stack. Doing so is possible using the [`pin!`] macro. See
1022- /// its documentation for more.
1058+ /// pin a value which does not implement [`Unpin`] to its location on the stack. Doing so is
1059+ /// possible using the [`pin!`] macro. See its documentation for more.
10231060///
10241061/// ## Layout and ABI
10251062///
@@ -1032,6 +1069,7 @@ use crate::{
10321069/// [`pin!`]: crate::pin::pin "pin!"
10331070/// [`Future`]: crate::future::Future "Future"
10341071/// [`poll`]: crate::future::Future::poll "Future::poll"
1072+ /// [`Future::poll`]: crate::future::Future::poll "Future::poll"
10351073/// [`pin` module]: self "pin module"
10361074/// [`Rc`]: ../../std/rc/struct.Rc.html "Rc"
10371075/// [`Arc`]: ../../std/sync/struct.Arc.html "Arc"
@@ -1137,7 +1175,10 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
11371175 /// use std::pin::Pin;
11381176 ///
11391177 /// let mut val: u8 = 5;
1140- /// // We can pin the value, since it doesn't care about being moved
1178+ ///
1179+ /// // Since `val` doesn't care about being moved, we can safely create a "facade" `Pin`
1180+ /// // which will allow `val` to participate in `Pin`-bound apis without checking that
1181+ /// // pinning guarantees are actually upheld.
11411182 /// let mut pinned: Pin<&mut u8> = Pin::new(&mut val);
11421183 /// ```
11431184 #[ inline( always) ]
@@ -1161,7 +1202,10 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
11611202 ///
11621203 /// let mut val: u8 = 5;
11631204 /// let pinned: Pin<&mut u8> = Pin::new(&mut val);
1164- /// // Unwrap the pin to get a reference to the value
1205+ ///
1206+ /// // Unwrap the pin to get the underlying mutable reference to the value. We can do
1207+ /// // this because `val` doesn't care about being moved, so the `Pin` was just
1208+ /// // a "facade" anyway.
11651209 /// let r = Pin::into_inner(pinned);
11661210 /// assert_eq!(*r, 5);
11671211 /// ```
@@ -1317,7 +1361,7 @@ impl<Ptr: Deref> Pin<Ptr> {
13171361 unsafe { Pin :: new_unchecked ( & * self . pointer ) }
13181362 }
13191363
1320- /// Unwraps this `Pin<Ptr>` returning the underlying pointer .
1364+ /// Unwraps this `Pin<Ptr>`, returning the underlying `Ptr` .
13211365 ///
13221366 /// # Safety
13231367 ///
@@ -1330,7 +1374,7 @@ impl<Ptr: Deref> Pin<Ptr> {
13301374 ///
13311375 /// Note that you must be able to guarantee that the data pointed to by `Ptr`
13321376 /// will be treated as pinned all the way until its `drop` handler is complete!
1333- ///
1377+ ///
13341378 /// *For more information, see the [`pin` module docs][self]*
13351379 ///
13361380 /// If the underlying data is [`Unpin`], [`Pin::into_inner`] should be used
0 commit comments