1
Fork 0

Beautify pin! docs

This makes pin docs a little bit less jargon-y and easier to read, by

* splitting up the sentences
* making them less interrupted by punctuation
* turning the footnotes into paragraphs, as they contain useful information
  that shouldn't be hidden in footnotes. Footnotes also interrupt the read flow.
* other improvements and simplifications
This commit is contained in:
est31 2023-03-10 10:05:10 +01:00
parent cd6c574af3
commit f663f09467

View file

@ -1003,22 +1003,25 @@ impl<P, U> CoerceUnsized<Pin<U>> for Pin<P> where P: CoerceUnsized<U> {}
#[stable(feature = "pin", since = "1.33.0")] #[stable(feature = "pin", since = "1.33.0")]
impl<P, U> DispatchFromDyn<Pin<U>> for Pin<P> where P: DispatchFromDyn<U> {} impl<P, U> DispatchFromDyn<Pin<U>> for Pin<P> where P: DispatchFromDyn<U> {}
/// Constructs a <code>[Pin]<[&mut] T></code>, by pinning[^1] a `value: T` _locally_[^2]. /// Constructs a <code>[Pin]<[&mut] T></code>, by pinning a `value: T` locally.
/// ///
/// Unlike [`Box::pin`], this does not involve a heap allocation. /// Unlike [`Box::pin`], this does not create a new heap allocation. As explained
/// below, the element might still end up on the heap however.
/// ///
/// [^1]: If the (type `T` of the) given value does not implement [`Unpin`], then this /// The local pinning performed by this macro is usually dubbed "stack"-pinning.
/// effectively pins the `value` in memory, where it will be unable to be moved. /// Outside of `async` contexts locals do indeed get stored on the stack. In
/// Otherwise, <code>[Pin]<[&mut] T></code> behaves like <code>[&mut] T</code>, and operations such /// `async` functions or blocks however, any locals crossing an `.await` point
/// as [`mem::replace()`][crate::mem::replace] will allow extracting that value, and therefore, /// are part of the state captured by the `Future`, and will use the storage of
/// moving it. /// those. That storage can either be on the heap or on the stack. Therefore,
/// See [the `Unpin` section of the `pin` module][self#unpin] for more info. /// local pinning is a more accurate term.
/// ///
/// [^2]: This is usually dubbed "stack"-pinning. And whilst local values are almost always located /// If the type of the given value does not implement [`Unpin`], then this macro
/// in the stack (_e.g._, when within the body of a non-`async` function), the truth is that inside /// pins the value in memory in a way that prevents moves. On the other hand,
/// the body of an `async fn` or block —more generally, the body of a generator— any locals crossing /// if the type does implement [`Unpin`], <code>[Pin]<[&mut] T></code> behaves
/// an `.await` point —a `yield` point— end up being part of the state captured by the `Future` —by /// like <code>[&mut] T</code>, and operations such as
/// the `Generator`—, and thus will be stored wherever that one is. /// [`mem::replace()`][crate::mem::replace] or [`mem::take()`](crate::mem::take)
/// will allow moves of the value.
/// See [the `Unpin` section of the `pin` module][self#unpin] for details.
/// ///
/// ## Examples /// ## Examples
/// ///
@ -1158,9 +1161,9 @@ impl<P, U> DispatchFromDyn<Pin<U>> for Pin<P> where P: DispatchFromDyn<U> {}
/// ///
/// If you really need to return a pinned value, consider using [`Box::pin`] instead. /// If you really need to return a pinned value, consider using [`Box::pin`] instead.
/// ///
/// On the other hand, pinning to the stack[<sup>2</sup>](#fn2) using [`pin!`] is likely to be /// On the other hand, local pinning using [`pin!`] is likely to be cheaper than
/// cheaper than pinning into a fresh heap allocation using [`Box::pin`]. Moreover, by virtue of not /// pinning into a fresh heap allocation using [`Box::pin`]. Moreover, by virtue of not
/// even needing an allocator, [`pin!`] is the main non-`unsafe` `#![no_std]`-compatible [`Pin`] /// requiring an allocator, [`pin!`] is the main non-`unsafe` `#![no_std]`-compatible [`Pin`]
/// constructor. /// constructor.
/// ///
/// [`Box::pin`]: ../../std/boxed/struct.Box.html#method.pin /// [`Box::pin`]: ../../std/boxed/struct.Box.html#method.pin