Skip to content

Commit e0210e6

Browse files
fu5haManishearth
authored andcommitted
justify motivation of Unpin better
1 parent 469c78b commit e0210e6

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

library/core/src/marker.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,13 @@ marker_impls! {
910910
/// to the pointee value like it normally would, thus allowing the user to do anything that they
911911
/// normally could with a non-[`Pin`]-wrapped `Ptr` to that value.
912912
///
913+
/// The idea of this trait is to alleviate the reduced ergonomics of APIs that require the use
914+
/// of [`Pin`] for soundness for some types, but which also want to be used by other types that
915+
/// don't care about pinning. The prime example of such an API is [`Future::poll`]. There are many
916+
/// [`Future`] types that don't care about pinning. These futures can implement `Unpin` and
917+
/// therefore get around the pinning related restrictions in the API, while still allowing the
918+
/// subset of [`Future`]s which *do* require pinning to be implemented soundly.
919+
///
913920
/// For more discussion on the consequences of [`Unpin`] within the wider scope of the pinning
914921
/// system, see [the section about `Unpin`] in the [`pin` module].
915922
///
@@ -947,6 +954,8 @@ marker_impls! {
947954
/// by adding [`PhantomPinned`] field. For more details, see the [`pin` module] docs.
948955
///
949956
/// [`mem::replace`]: crate::mem::replace "mem replace"
957+
/// [`Future`]: crate::future::Future "Future"
958+
/// [`Future::poll`]: crate::future::Future::poll "Future poll"
950959
/// [`Pin`]: crate::pin::Pin "Pin"
951960
/// [`Pin<Ptr>`]: crate::pin::Pin "Pin"
952961
/// [`pin` module]: crate::pin "pin module"

library/core/src/pin.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,15 @@
349349
//! implement the [`Unpin`] auto-trait, which cancels the restrictive effects of
350350
//! [`Pin`] when the *pointee* type `T` is [`Unpin`]. When [`T: Unpin`][Unpin],
351351
//! <code>[Pin]<[Box]\<T>></code> functions identically to a non-pinning [`Box<T>`]; similarly,
352-
//! <code>[Pin]<[&mut] T></code> would impose no additional restrictions above a regular [`&mut T`].
352+
//! <code>[Pin]<[&mut] T></code> would impose no additional restrictions above a regular
353+
//! [`&mut T`].
354+
//!
355+
//! The idea of this trait is to alleviate the reduced ergonomics of APIs that require the use
356+
//! of [`Pin`] for soundness for some types, but which also want to be used by other types that
357+
//! don't care about pinning. The prime example of such an API is [`Future::poll`]. There are many
358+
//! [`Future`] types that don't care about pinning. These futures can implement [`Unpin`] and
359+
//! therefore get around the pinning related restrictions in the API, while still allowing the
360+
//! subset of [`Future`]s which *do* require pinning to be implemented soundly.
353361
//!
354362
//! Note that the interaction between a [`Pin<Ptr>`] and [`Unpin`] is through the type of the
355363
//! **pointee** value, [`<Ptr as Deref>::Target`][Target]. Whether the `Ptr` type itself
@@ -945,15 +953,14 @@ use crate::{
945953
///
946954
/// In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a
947955
/// [`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
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.
956+
/// will not be *moved* or [otherwise invalidated][subtle-details]. If the pointee value's type
957+
/// implements [`Unpin`], we are free to disregard these requirements entirely and can wrap any
958+
/// pointer to that value in [`Pin`] directly via [`Pin::new`]. If the pointee value's type does
959+
/// not implement [`Unpin`], then Rust will not let us use the [`Pin::new`] function directly and
960+
/// we'll need to construct a [`Pin`]-wrapped pointer in one of the more specialized manners
961+
/// discussed below.
955962
///
956-
/// We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning ref, or pinning
963+
/// We call such a [`Pin`]-wrapped pointer a **pinning pointer** (or pinning ref, or pinning
957964
/// [`Box`], etc.) because its existince is the thing that is pinning the underlying pointee in
958965
/// place: it is the metaphorical "pin" securing the data in place on the pinboard (in memory).
959966
///
@@ -962,18 +969,18 @@ use crate::{
962969
/// the pointer's ***pointee** value*.
963970
///
964971
/// 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
972+
/// compiler-generated state machines that implement [`Future`] for the return value of
973+
/// `async fn`s. These compiler-generated [`Future`]s may contain self-referrential pointers, one
974+
/// of the most common use cases for [`Pin`]. More details on this point are provided in the
968975
/// [`pin` module] docs, but suffice it to say they require the guarantees provided by pinning to
969976
/// be implemented soundly.
970977
///
971-
/// This requirement from the implementation of `async fn`s means that the [`Future`] trait
978+
/// This requirement for the implementation of `async fn`s means that the [`Future`] trait
972979
/// requires all calls to [`poll`] to use a <code>self: [Pin]\<&mut Self></code> parameter instead
973980
/// of the usual `&mut self`. Therefore, when manually polling a future, you will need to pin it
974981
/// first.
975982
///
976-
/// You may notice that `async fn`-generated [`Future`]s are only a small percentage of all
983+
/// You may notice that `async fn`-sourced [`Future`]s are only a small percentage of all
977984
/// [`Future`]s that exist, yet we had to modify the signature of [`poll`] for all [`Future`]s
978985
/// to accommodate them. This is unfortunate, but there is a way that the language attempts to
979986
/// alleviate the extra friction that this API choice incurs: the [`Unpin`] trait.

0 commit comments

Comments
 (0)