@@ -945,8 +945,13 @@ use crate::{
945
945
///
946
946
/// In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a
947
947
/// [`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.
950
955
///
951
956
/// We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning ref, or pinning
952
957
/// [`Box`], etc.) because its existince is the thing that is pinning the underlying pointee in
@@ -956,25 +961,57 @@ use crate::{
956
961
/// itself, but rather a pointer to that value! A [`Pin<Ptr>`] does not pin the `Ptr` but rather
957
962
/// the pointer's ***pointee** value*.
958
963
///
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.
966
970
///
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
+ /// ```
970
1007
///
971
1008
/// ### Pinning a value inside a [`Box`]
972
1009
///
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.
978
1015
///
979
1016
/// ```
980
1017
/// use std::pin::Pin;
@@ -1018,8 +1055,8 @@ use crate::{
1018
1055
///
1019
1056
/// There are some situations where it is desirable or even required (for example, in a `#[no_std]`
1020
1057
/// 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.
1023
1060
///
1024
1061
/// ## Layout and ABI
1025
1062
///
@@ -1032,6 +1069,7 @@ use crate::{
1032
1069
/// [`pin!`]: crate::pin::pin "pin!"
1033
1070
/// [`Future`]: crate::future::Future "Future"
1034
1071
/// [`poll`]: crate::future::Future::poll "Future::poll"
1072
+ /// [`Future::poll`]: crate::future::Future::poll "Future::poll"
1035
1073
/// [`pin` module]: self "pin module"
1036
1074
/// [`Rc`]: ../../std/rc/struct.Rc.html "Rc"
1037
1075
/// [`Arc`]: ../../std/sync/struct.Arc.html "Arc"
@@ -1137,7 +1175,10 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
1137
1175
/// use std::pin::Pin;
1138
1176
///
1139
1177
/// 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.
1141
1182
/// let mut pinned: Pin<&mut u8> = Pin::new(&mut val);
1142
1183
/// ```
1143
1184
#[ inline( always) ]
@@ -1161,7 +1202,10 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
1161
1202
///
1162
1203
/// let mut val: u8 = 5;
1163
1204
/// 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.
1165
1209
/// let r = Pin::into_inner(pinned);
1166
1210
/// assert_eq!(*r, 5);
1167
1211
/// ```
@@ -1317,7 +1361,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1317
1361
unsafe { Pin :: new_unchecked ( & * self . pointer ) }
1318
1362
}
1319
1363
1320
- /// Unwraps this `Pin<Ptr>` returning the underlying pointer .
1364
+ /// Unwraps this `Pin<Ptr>`, returning the underlying `Ptr` .
1321
1365
///
1322
1366
/// # Safety
1323
1367
///
@@ -1330,7 +1374,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1330
1374
///
1331
1375
/// Note that you must be able to guarantee that the data pointed to by `Ptr`
1332
1376
/// will be treated as pinned all the way until its `drop` handler is complete!
1333
- ///
1377
+ ///
1334
1378
/// *For more information, see the [`pin` module docs][self]*
1335
1379
///
1336
1380
/// If the underlying data is [`Unpin`], [`Pin::into_inner`] should be used
0 commit comments