Skip to content

Commit 87ac118

Browse files
committed
Add more links to core::pin to improve visual consistency.
1 parent ceaeb24 commit 87ac118

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

library/core/src/pin.rs

+44-39
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
//! for more details.
1515
//!
1616
//! By default, all types in Rust are movable. Rust allows passing all types by-value,
17-
//! and common smart-pointer types such as <code>[Box]\<T></code> and `&mut T` allow replacing and
17+
//! and common smart-pointer types such as <code>[Box]\<T></code> and <code>[&mut] T</code> allow replacing and
1818
//! moving the values they contain: you can move out of a <code>[Box]\<T></code>, or you can use [`mem::swap`].
1919
//! <code>[Pin]\<P></code> wraps a pointer type `P`, so <code>[Pin]<[Box]\<T>></code> functions much like a regular
2020
//! <code>[Box]\<T></code>: when a <code>[Pin]<[Box]\<T>></code> gets dropped, so do its contents, and the memory gets
21-
//! deallocated. Similarly, <code>[Pin]<&mut T></code> is a lot like `&mut T`. However, <code>[Pin]\<P></code> does
22-
//! not let clients actually obtain a <code>[Box]\<T></code> or `&mut T` to pinned data, which implies that you
21+
//! deallocated. Similarly, <code>[Pin]<[&mut] T></code> is a lot like <code>[&mut] T</code>. However, <code>[Pin]\<P></code> does
22+
//! not let clients actually obtain a <code>[Box]\<T></code> or <code>[&mut] T</code> to pinned data, which implies that you
2323
//! cannot use operations such as [`mem::swap`]:
2424
//!
2525
//! ```
@@ -35,12 +35,12 @@
3535
//! It is worth reiterating that <code>[Pin]\<P></code> does *not* change the fact that a Rust compiler
3636
//! considers all types movable. [`mem::swap`] remains callable for any `T`. Instead, <code>[Pin]\<P></code>
3737
//! prevents certain *values* (pointed to by pointers wrapped in <code>[Pin]\<P></code>) from being
38-
//! moved by making it impossible to call methods that require `&mut T` on them
38+
//! moved by making it impossible to call methods that require <code>[&mut] T</code> on them
3939
//! (like [`mem::swap`]).
4040
//!
4141
//! <code>[Pin]\<P></code> can be used to wrap any pointer type `P`, and as such it interacts with
42-
//! [`Deref`] and [`DerefMut`]. A <code>[Pin]\<P></code> where `P: Deref` should be considered
43-
//! as a "`P`-style pointer" to a pinned `P::Target` -- so, a <code>[Pin]<[Box]\<T>></code> is
42+
//! [`Deref`] and [`DerefMut`]. A <code>[Pin]\<P></code> where <code>P: [Deref]</code> should be considered
43+
//! as a "`P`-style pointer" to a pinned <code>P::[Target]</code> – so, a <code>[Pin]<[Box]\<T>></code> is
4444
//! an owned pointer to a pinned `T`, and a <code>[Pin]<[Rc]\<T>></code> is a reference-counted
4545
//! pointer to a pinned `T`.
4646
//! For correctness, <code>[Pin]\<P></code> relies on the implementations of [`Deref`] and
@@ -53,19 +53,19 @@
5353
//! rely on having a stable address. This includes all the basic types (like
5454
//! [`bool`], [`i32`], and references) as well as types consisting solely of these
5555
//! types. Types that do not care about pinning implement the [`Unpin`]
56-
//! auto-trait, which cancels the effect of <code>[Pin]\<P></code>. For `T: Unpin`,
57-
//! <code>[Pin]<[Box]\<T>></code> and <code>[Box]\<T></code> function identically, as do <code>[Pin]<&mut T></code> and
58-
//! `&mut T`.
56+
//! auto-trait, which cancels the effect of <code>[Pin]\<P></code>. For <code>T: [Unpin]</code>,
57+
//! <code>[Pin]<[Box]\<T>></code> and <code>[Box]\<T></code> function identically, as do <code>[Pin]<[&mut] T></code> and
58+
//! <code>[&mut] T</code>.
5959
//!
60-
//! Note that pinning and [`Unpin`] only affect the pointed-to type `P::Target`, not the pointer
60+
//! Note that pinning and [`Unpin`] only affect the pointed-to type <code>P::[Target]</code>, not the pointer
6161
//! type `P` itself that got wrapped in <code>[Pin]\<P></code>. For example, whether or not <code>[Box]\<T></code> is
6262
//! [`Unpin`] has no effect on the behavior of <code>[Pin]<[Box]\<T>></code> (here, `T` is the
6363
//! pointed-to type).
6464
//!
6565
//! # Example: self-referential struct
6666
//!
6767
//! Before we go into more details to explain the guarantees and choices
68-
//! associated with `Pin<T>`, we discuss some examples for how it might be used.
68+
//! associated with <code>[Pin]\<P></code>, we discuss some examples for how it might be used.
6969
//! Feel free to [skip to where the theoretical discussion continues](#drop-guarantee).
7070
//!
7171
//! ```rust
@@ -165,18 +165,18 @@
165165
//! # `Drop` implementation
166166
//!
167167
//! If your type uses pinning (such as the two examples above), you have to be careful
168-
//! when implementing [`Drop`]. The [`drop`] function takes `&mut self`, but this
168+
//! when implementing [`Drop`]. The [`drop`] function takes <code>[&mut] self</code>, but this
169169
//! is called *even if your type was previously pinned*! It is as if the
170170
//! compiler automatically called [`Pin::get_unchecked_mut`].
171171
//!
172172
//! This can never cause a problem in safe code because implementing a type that
173173
//! relies on pinning requires unsafe code, but be aware that deciding to make
174174
//! use of pinning in your type (for example by implementing some operation on
175-
//! <code>[Pin]<&Self></code> or <code>[Pin]<&mut Self></code>) has consequences for your [`Drop`]
175+
//! <code>[Pin]<[&]Self></code> or <code>[Pin]<[&mut] Self></code>) has consequences for your [`Drop`]
176176
//! implementation as well: if an element of your type could have been pinned,
177-
//! you must treat [`Drop`] as implicitly taking <code>[Pin]<&mut Self></code>.
177+
//! you must treat [`Drop`] as implicitly taking <code>[Pin]<[&mut] Self></code>.
178178
//!
179-
//! For example, you could implement `Drop` as follows:
179+
//! For example, you could implement [`Drop`] as follows:
180180
//!
181181
//! ```rust,no_run
182182
//! # use std::pin::Pin;
@@ -204,18 +204,18 @@
204204
//! # Projections and Structural Pinning
205205
//!
206206
//! When working with pinned structs, the question arises how one can access the
207-
//! fields of that struct in a method that takes just <code>[Pin]<&mut Struct></code>.
207+
//! fields of that struct in a method that takes just <code>[Pin]<[&mut] Struct></code>.
208208
//! The usual approach is to write helper methods (so called *projections*)
209-
//! that turn <code>[Pin]<&mut Struct></code> into a reference to the field, but what
210-
//! type should that reference have? Is it <code>[Pin]<&mut Field></code> or `&mut Field`?
209+
//! that turn <code>[Pin]<[&mut] Struct></code> into a reference to the field, but what
210+
//! type should that reference have? Is it <code>[Pin]<[&mut] Field></code> or <code>[&mut] Field</code>?
211211
//! The same question arises with the fields of an `enum`, and also when considering
212212
//! container/wrapper types such as <code>[Vec]\<T></code>, <code>[Box]\<T></code>, or <code>[RefCell]\<T></code>.
213213
//! (This question applies to both mutable and shared references, we just
214214
//! use the more common case of mutable references here for illustration.)
215215
//!
216216
//! It turns out that it is actually up to the author of the data structure
217217
//! to decide whether the pinned projection for a particular field turns
218-
//! <code>[Pin]<&mut Struct></code> into <code>[Pin]<&mut Field></code> or `&mut Field`. There are some
218+
//! <code>[Pin]<[&mut] Struct></code> into <code>[Pin]<[&mut] Field></code> or <code>[&mut] Field</code>. There are some
219219
//! constraints though, and the most important constraint is *consistency*:
220220
//! every field can be *either* projected to a pinned reference, *or* have
221221
//! pinning removed as part of the projection. If both are done for the same field,
@@ -230,12 +230,12 @@
230230
//! ## Pinning *is not* structural for `field`
231231
//!
232232
//! It may seem counter-intuitive that the field of a pinned struct might not be pinned,
233-
//! but that is actually the easiest choice: if a <code>[Pin]<&mut Field></code> is never created,
233+
//! but that is actually the easiest choice: if a <code>[Pin]<[&mut] Field></code> is never created,
234234
//! nothing can go wrong! So, if you decide that some field does not have structural pinning,
235235
//! all you have to ensure is that you never create a pinned reference to that field.
236236
//!
237237
//! Fields without structural pinning may have a projection method that turns
238-
//! <code>[Pin]<&mut Struct></code> into `&mut Field`:
238+
//! <code>[Pin]<[&mut] Struct></code> into <code>[&mut] Field</code>:
239239
//!
240240
//! ```rust,no_run
241241
//! # use std::pin::Pin;
@@ -249,16 +249,16 @@
249249
//! }
250250
//! ```
251251
//!
252-
//! You may also `impl Unpin for Struct` *even if* the type of `field`
252+
//! You may also <code>impl [Unpin] for Struct</code> *even if* the type of `field`
253253
//! is not [`Unpin`]. What that type thinks about pinning is not relevant
254-
//! when no <code>[Pin]<&mut Field></code> is ever created.
254+
//! when no <code>[Pin]<[&mut] Field></code> is ever created.
255255
//!
256256
//! ## Pinning *is* structural for `field`
257257
//!
258258
//! The other option is to decide that pinning is "structural" for `field`,
259259
//! meaning that if the struct is pinned then so is the field.
260260
//!
261-
//! This allows writing a projection that creates a <code>[Pin]<&mut Field></code>, thus
261+
//! This allows writing a projection that creates a <code>[Pin]<[&mut] Field></code>, thus
262262
//! witnessing that the field is pinned:
263263
//!
264264
//! ```rust,no_run
@@ -278,12 +278,12 @@
278278
//! 1. The struct must only be [`Unpin`] if all the structural fields are
279279
//! [`Unpin`]. This is the default, but [`Unpin`] is a safe trait, so as the author of
280280
//! the struct it is your responsibility *not* to add something like
281-
//! `impl<T> Unpin for Struct<T>`. (Notice that adding a projection operation
281+
//! <code>impl\<T> [Unpin] for Struct\<T></code>. (Notice that adding a projection operation
282282
//! requires unsafe code, so the fact that [`Unpin`] is a safe trait does not break
283-
//! the principle that you only have to worry about any of this if you use `unsafe`.)
283+
//! the principle that you only have to worry about any of this if you use [`unsafe`].)
284284
//! 2. The destructor of the struct must not move structural fields out of its argument. This
285-
//! is the exact point that was raised in the [previous section][drop-impl]: `drop` takes
286-
//! `&mut self`, but the struct (and hence its fields) might have been pinned before.
285+
//! is the exact point that was raised in the [previous section][drop-impl]: [`drop`] takes
286+
//! <code>[&mut] self</code>, but the struct (and hence its fields) might have been pinned before.
287287
//! You have to guarantee that you do not move a field inside your [`Drop`] implementation.
288288
//! In particular, as explained previously, this means that your struct must *not*
289289
//! be `#[repr(packed)]`.
@@ -299,13 +299,13 @@
299299
//! does not cause unsoundness.)
300300
//! 4. You must not offer any other operations that could lead to data being moved out of
301301
//! the structural fields when your type is pinned. For example, if the struct contains an
302-
//! <code>[Option]\<T></code> and there is a `take`-like operation with type
303-
//! `fn(Pin<&mut Struct<T>>) -> Option<T>`,
304-
//! that operation can be used to move a `T` out of a pinned `Struct<T>` -- which means
302+
//! <code>[Option]\<T></code> and there is a [`take`][Option::take]-like operation with type
303+
//! <code>fn([Pin]<[&mut] Struct\<T>>) -> [Option]\<T></code>,
304+
//! that operation can be used to move a `T` out of a pinned `Struct<T>` which means
305305
//! pinning cannot be structural for the field holding this data.
306306
//!
307307
//! For a more complex example of moving data out of a pinned type, imagine if <code>[RefCell]\<T></code>
308-
//! had a method `fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T>`.
308+
//! had a method <code>fn get_pin_mut(self: [Pin]<[&mut] Self>) -> [Pin]<[&mut] T></code>.
309309
//! Then we could do the following:
310310
//! ```compile_fail
311311
//! fn exploit_ref_cell<T>(rc: Pin<&mut RefCell<T>>) {
@@ -316,7 +316,7 @@
316316
//! }
317317
//! ```
318318
//! This is catastrophic, it means we can first pin the content of the <code>[RefCell]\<T></code>
319-
//! (using `RefCell::get_pin_mut`) and then move that content using the mutable
319+
//! (using <code>[RefCell]::get_pin_mut</code>) and then move that content using the mutable
320320
//! reference we got later.
321321
//!
322322
//! ## Examples
@@ -328,15 +328,15 @@
328328
//! contents! Nor could it allow [`push`][Vec::push], which might reallocate and thus also move the
329329
//! contents.
330330
//!
331-
//! A <code>[Vec]\<T></code> without structural pinning could `impl<T> Unpin for Vec<T>`, because the contents
331+
//! A <code>[Vec]\<T></code> without structural pinning could <code>impl\<T> [Unpin] for [Vec]\<T></code>, because the contents
332332
//! are never pinned and the <code>[Vec]\<T></code> itself is fine with being moved as well.
333333
//! At that point pinning just has no effect on the vector at all.
334334
//!
335335
//! In the standard library, pointer types generally do not have structural pinning,
336-
//! and thus they do not offer pinning projections. This is why `Box<T>: Unpin` holds for all `T`.
337-
//! It makes sense to do this for pointer types, because moving the `Box<T>`
338-
//! does not actually move the `T`: the <code>[Box]\<T></code> can be freely movable (aka `Unpin`) even if
339-
//! the `T` is not. In fact, even <code>[Pin]<[Box]\<T>></code> and <code>[Pin]<&mut T></code> are always
336+
//! and thus they do not offer pinning projections. This is why <code>[Box]\<T>: [Unpin]</code> holds for all `T`.
337+
//! It makes sense to do this for pointer types, because moving the <code>[Box]\<T></code>
338+
//! does not actually move the `T`: the <code>[Box]\<T></code> can be freely movable (aka [`Unpin`]) even if
339+
//! the `T` is not. In fact, even <code>[Pin]<[Box]\<T>></code> and <code>[Pin]<[&mut] T></code> are always
340340
//! [`Unpin`] themselves, for the same reason: their contents (the `T`) are pinned, but the
341341
//! pointers themselves can be moved without moving the pinned data. For both <code>[Box]\<T></code> and
342342
//! <code>[Pin]<[Box]\<T>></code>, whether the content is pinned is entirely independent of whether the
@@ -346,10 +346,12 @@
346346
//! for the nested futures, as you need to get pinned references to them to call [`poll`].
347347
//! But if your combinator contains any other data that does not need to be pinned,
348348
//! you can make those fields not structural and hence freely access them with a
349-
//! mutable reference even when you just have <code>[Pin]<&mut Self></code> (such as in your own
349+
//! mutable reference even when you just have <code>[Pin]<[&mut] Self></code> (such as in your own
350350
//! [`poll`] implementation).
351351
//!
352+
//! [Deref]: crate::ops::Deref
352353
//! [`Deref`]: crate::ops::Deref
354+
//! [Target]: crate::ops::Deref::Target
353355
//! [`DerefMut`]: crate::ops::DerefMut
354356
//! [`mem::swap`]: crate::mem::swap
355357
//! [`mem::forget`]: crate::mem::forget
@@ -367,6 +369,9 @@
367369
//! [drop-impl]: #drop-implementation
368370
//! [drop-guarantee]: #drop-guarantee
369371
//! [`poll`]: crate::future::Future::poll
372+
//! [&]: ../../std/primitive.reference.html
373+
//! [&mut]: ../../std/primitive.reference.html
374+
//! [`unsafe`]: ../../std/keyword.unsafe.html
370375
371376
#![stable(feature = "pin", since = "1.33.0")]
372377

0 commit comments

Comments
 (0)