Skip to content

Commit ceaeb24

Browse files
committed
Exclude single type parameters from links in core::pin for more visual consistency.
1 parent d3915c5 commit ceaeb24

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

library/core/src/pin.rs

+33-35
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
//! as moving an object with pointers to itself will invalidate them, which could cause undefined
77
//! behavior.
88
//!
9-
//! At a high level, a [`Pin<P>`] ensures that the pointee of any pointer type
9+
//! At a high level, a <code>[Pin]\<P></code> ensures that the pointee of any pointer type
1010
//! `P` has a stable location in memory, meaning it cannot be moved elsewhere
1111
//! and its memory cannot be deallocated until it gets dropped. We say that the
1212
//! pointee is "pinned". Things get more subtle when discussing types that
1313
//! combine pinned with non-pinned data; [see below](#projections-and-structural-pinning)
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 [`Box<T>`] and `&mut T` allow replacing and
18-
//! moving the values they contain: you can move out of a [`Box<T>`], or you can use [`mem::swap`].
19-
//! [`Pin<P>`] wraps a pointer type `P`, so <code>[Pin]<[Box]\<T>></code> functions much like a regular
20-
//! [`Box<T>`]: 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, [`Pin<P>`] does
22-
//! not let clients actually obtain a [`Box<T>`] or `&mut T` to pinned data, which implies that you
17+
//! and common smart-pointer types such as <code>[Box]\<T></code> and `&mut T` allow replacing and
18+
//! moving the values they contain: you can move out of a <code>[Box]\<T></code>, or you can use [`mem::swap`].
19+
//! <code>[Pin]\<P></code> wraps a pointer type `P`, so <code>[Pin]<[Box]\<T>></code> functions much like a regular
20+
//! <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
2323
//! cannot use operations such as [`mem::swap`]:
2424
//!
2525
//! ```
@@ -32,18 +32,18 @@
3232
//! }
3333
//! ```
3434
//!
35-
//! It is worth reiterating that [`Pin<P>`] does *not* change the fact that a Rust compiler
36-
//! considers all types movable. [`mem::swap`] remains callable for any `T`. Instead, [`Pin<P>`]
37-
//! prevents certain *values* (pointed to by pointers wrapped in [`Pin<P>`]) from being
35+
//! It is worth reiterating that <code>[Pin]\<P></code> does *not* change the fact that a Rust compiler
36+
//! considers all types movable. [`mem::swap`] remains callable for any `T`. Instead, <code>[Pin]\<P></code>
37+
//! prevents certain *values* (pointed to by pointers wrapped in <code>[Pin]\<P></code>) from being
3838
//! moved by making it impossible to call methods that require `&mut T` on them
3939
//! (like [`mem::swap`]).
4040
//!
41-
//! [`Pin<P>`] can be used to wrap any pointer type `P`, and as such it interacts with
42-
//! [`Deref`] and [`DerefMut`]. A [`Pin<P>`] where `P: Deref` should be considered
41+
//! <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
4343
//! as a "`P`-style pointer" to a pinned `P::Target` -- 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`.
46-
//! For correctness, [`Pin<P>`] relies on the implementations of [`Deref`] and
46+
//! For correctness, <code>[Pin]\<P></code> relies on the implementations of [`Deref`] and
4747
//! [`DerefMut`] not to move out of their `self` parameter, and only ever to
4848
//! return a pointer to pinned data when they are called on a pinned pointer.
4949
//!
@@ -53,12 +53,12 @@
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 [`Pin<P>`]. For `T: Unpin`,
57-
//! <code>[Pin]<[Box]\<T>></code> and [`Box<T>`] function identically, as do <code>[Pin]<&mut T></code> and
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
5858
//! `&mut T`.
5959
//!
6060
//! Note that pinning and [`Unpin`] only affect the pointed-to type `P::Target`, not the pointer
61-
//! type `P` itself that got wrapped in [`Pin<P>`]. For example, whether or not [`Box<T>`] is
61+
//! 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
//!
@@ -149,7 +149,7 @@
149149
//! when [`drop`] is called*. Only once [`drop`] returns or panics, the memory may be reused.
150150
//!
151151
//! Memory can be "invalidated" by deallocation, but also by
152-
//! replacing a [`Some(v)`] by [`None`], or calling [`Vec::set_len`] to "kill" some elements
152+
//! replacing a <code>[Some]\(v)</code> by [`None`], or calling [`Vec::set_len`] to "kill" some elements
153153
//! off of a vector. It can be repurposed by using [`ptr::write`] to overwrite it without
154154
//! calling the destructor first. None of this is allowed for pinned data without calling [`drop`].
155155
//!
@@ -209,7 +209,7 @@
209209
//! that turn <code>[Pin]<&mut Struct></code> into a reference to the field, but what
210210
//! type should that reference have? Is it <code>[Pin]<&mut Field></code> or `&mut Field`?
211211
//! The same question arises with the fields of an `enum`, and also when considering
212-
//! container/wrapper types such as [`Vec<T>`], [`Box<T>`], or [`RefCell<T>`].
212+
//! 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
//!
@@ -292,19 +292,19 @@
292292
//! 3. You must make sure that you uphold the [`Drop` guarantee][drop-guarantee]:
293293
//! once your struct is pinned, the memory that contains the
294294
//! content is not overwritten or deallocated without calling the content's destructors.
295-
//! This can be tricky, as witnessed by [`VecDeque<T>`]: the destructor of [`VecDeque<T>`]
295+
//! This can be tricky, as witnessed by <code>[VecDeque]\<T></code>: the destructor of <code>[VecDeque]\<T></code>
296296
//! can fail to call [`drop`] on all elements if one of the destructors panics. This violates
297297
//! the [`Drop`] guarantee, because it can lead to elements being deallocated without
298-
//! their destructor being called. ([`VecDeque<T>`] has no pinning projections, so this
298+
//! their destructor being called. (<code>[VecDeque]\<T></code> has no pinning projections, so this
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-
//! [`Option<T>`] and there is a `take`-like operation with type
302+
//! <code>[Option]\<T></code> and there is a `take`-like operation with type
303303
//! `fn(Pin<&mut Struct<T>>) -> Option<T>`,
304304
//! 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
//!
307-
//! For a more complex example of moving data out of a pinned type, imagine if [`RefCell<T>`]
307+
//! For a more complex example of moving data out of a pinned type, imagine if <code>[RefCell]\<T></code>
308308
//! had a method `fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T>`.
309309
//! Then we could do the following:
310310
//! ```compile_fail
@@ -315,30 +315,30 @@
315315
//! let content = &mut *b; // And here we have `&mut T` to the same data.
316316
//! }
317317
//! ```
318-
//! This is catastrophic, it means we can first pin the content of the [`RefCell<T>`]
318+
//! This is catastrophic, it means we can first pin the content of the <code>[RefCell]\<T></code>
319319
//! (using `RefCell::get_pin_mut`) and then move that content using the mutable
320320
//! reference we got later.
321321
//!
322322
//! ## Examples
323323
//!
324-
//! For a type like [`Vec<T>`], both possibilities (structural pinning or not) make sense.
325-
//! A [`Vec<T>`] with structural pinning could have `get_pin`/`get_pin_mut` methods to get
324+
//! For a type like <code>[Vec]\<T></code>, both possibilities (structural pinning or not) make sense.
325+
//! A <code>[Vec]\<T></code> with structural pinning could have `get_pin`/`get_pin_mut` methods to get
326326
//! pinned references to elements. However, it could *not* allow calling
327-
//! [`pop`][Vec::pop] on a pinned [`Vec<T>`] because that would move the (structurally pinned)
327+
//! [`pop`][Vec::pop] on a pinned <code>[Vec]\<T></code> because that would move the (structurally pinned)
328328
//! contents! Nor could it allow [`push`][Vec::push], which might reallocate and thus also move the
329329
//! contents.
330330
//!
331-
//! A [`Vec<T>`] without structural pinning could `impl<T> Unpin for Vec<T>`, because the contents
332-
//! are never pinned and the [`Vec<T>`] itself is fine with being moved as well.
331+
//! A <code>[Vec]\<T></code> without structural pinning could `impl<T> Unpin for Vec<T>`, because the contents
332+
//! 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,
336336
//! and thus they do not offer pinning projections. This is why `Box<T>: Unpin` holds for all `T`.
337337
//! It makes sense to do this for pointer types, because moving the `Box<T>`
338-
//! does not actually move the `T`: the [`Box<T>`] can be freely movable (aka `Unpin`) even if
338+
//! does not actually move the `T`: the <code>[Box]\<T></code> can be freely movable (aka `Unpin`) even if
339339
//! 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
341-
//! pointers themselves can be moved without moving the pinned data. For both [`Box<T>`] and
341+
//! 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
343343
//! pointer is pinned, meaning pinning is *not* structural.
344344
//!
@@ -353,17 +353,15 @@
353353
//! [`DerefMut`]: crate::ops::DerefMut
354354
//! [`mem::swap`]: crate::mem::swap
355355
//! [`mem::forget`]: crate::mem::forget
356-
//! [`Box<T>`]: ../../std/boxed/struct.Box.html
357-
//! [`Vec<T>`]: ../../std/vec/struct.Vec.html
356+
//! [Vec]: ../../std/vec/struct.Vec.html
358357
//! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len
359358
//! [Box]: ../../std/boxed/struct.Box.html
360359
//! [Vec::pop]: ../../std/vec/struct.Vec.html#method.pop
361360
//! [Vec::push]: ../../std/vec/struct.Vec.html#method.push
362361
//! [Rc]: ../../std/rc/struct.Rc.html
363-
//! [`RefCell<T>`]: crate::cell::RefCell
362+
//! [RefCell]: crate::cell::RefCell
364363
//! [`drop`]: Drop::drop
365-
//! [`VecDeque<T>`]: ../../std/collections/struct.VecDeque.html
366-
//! [`Some(v)`]: Some
364+
//! [VecDeque]: ../../std/collections/struct.VecDeque.html
367365
//! [`ptr::write`]: crate::ptr::write
368366
//! [`Future`]: crate::future::Future
369367
//! [drop-impl]: #drop-implementation

0 commit comments

Comments
 (0)