|
16 | 16 | //! By default, all types in Rust are movable. Rust allows passing all types by-value,
|
17 | 17 | //! and common smart-pointer types such as [`Box<T>`] and `&mut T` allow replacing and
|
18 | 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 [`Pin`]`<`[`Box`]`<T>>` functions much like a regular |
20 |
| -//! [`Box<T>`]: when a [`Pin`]`<`[`Box`]`<T>>` gets dropped, so do its contents, and the memory gets |
21 |
| -//! deallocated. Similarly, [`Pin`]`<&mut T>` is a lot like `&mut T`. However, [`Pin<P>`] does |
| 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 | 22 | //! not let clients actually obtain a [`Box<T>`] or `&mut T` to pinned data, which implies that you
|
23 | 23 | //! cannot use operations such as [`mem::swap`]:
|
24 | 24 | //!
|
|
40 | 40 | //!
|
41 | 41 | //! [`Pin<P>`] can be used to wrap any pointer type `P`, and as such it interacts with
|
42 | 42 | //! [`Deref`] and [`DerefMut`]. A [`Pin<P>`] where `P: Deref` should be considered
|
43 |
| -//! as a "`P`-style pointer" to a pinned `P::Target` -- so, a [`Pin`]`<`[`Box`]`<T>>` is |
44 |
| -//! an owned pointer to a pinned `T`, and a [`Pin`]`<`[`Rc`]`<T>>` is a reference-counted |
| 43 | +//! as a "`P`-style pointer" to a pinned `P::Target` -- so, a <code>[Pin]<[Box]\<T>></code> is |
| 44 | +//! an owned pointer to a pinned `T`, and a <code>[Pin]<[Rc]\<T>></code> is a reference-counted |
45 | 45 | //! pointer to a pinned `T`.
|
46 | 46 | //! For correctness, [`Pin<P>`] relies on the implementations of [`Deref`] and
|
47 | 47 | //! [`DerefMut`] not to move out of their `self` parameter, and only ever to
|
|
54 | 54 | //! [`bool`], [`i32`], and references) as well as types consisting solely of these
|
55 | 55 | //! types. Types that do not care about pinning implement the [`Unpin`]
|
56 | 56 | //! auto-trait, which cancels the effect of [`Pin<P>`]. For `T: Unpin`,
|
57 |
| -//! [`Pin`]`<`[`Box`]`<T>>` and [`Box<T>`] function identically, as do [`Pin`]`<&mut T>` and |
| 57 | +//! <code>[Pin]<[Box]\<T>></code> and [`Box<T>`] function identically, as do <code>[Pin]<&mut T></code> and |
58 | 58 | //! `&mut T`.
|
59 | 59 | //!
|
60 | 60 | //! Note that pinning and [`Unpin`] only affect the pointed-to type `P::Target`, not the pointer
|
61 | 61 | //! type `P` itself that got wrapped in [`Pin<P>`]. For example, whether or not [`Box<T>`] is
|
62 |
| -//! [`Unpin`] has no effect on the behavior of [`Pin`]`<`[`Box`]`<T>>` (here, `T` is the |
| 62 | +//! [`Unpin`] has no effect on the behavior of <code>[Pin]<[Box]\<T>></code> (here, `T` is the |
63 | 63 | //! pointed-to type).
|
64 | 64 | //!
|
65 | 65 | //! # Example: self-referential struct
|
|
158 | 158 | //!
|
159 | 159 | //! Notice that this guarantee does *not* mean that memory does not leak! It is still
|
160 | 160 | //! completely okay not ever to call [`drop`] on a pinned element (e.g., you can still
|
161 |
| -//! call [`mem::forget`] on a [`Pin`]`<`[`Box`]`<T>>`). In the example of the doubly-linked |
| 161 | +//! call [`mem::forget`] on a <code>[Pin]<[Box]\<T>></code>). In the example of the doubly-linked |
162 | 162 | //! list, that element would just stay in the list. However you may not free or reuse the storage
|
163 | 163 | //! *without calling [`drop`]*.
|
164 | 164 | //!
|
|
172 | 172 | //! This can never cause a problem in safe code because implementing a type that
|
173 | 173 | //! relies on pinning requires unsafe code, but be aware that deciding to make
|
174 | 174 | //! use of pinning in your type (for example by implementing some operation on
|
175 |
| -//! [`Pin`]`<&Self>` or [`Pin`]`<&mut Self>`) has consequences for your [`Drop`] |
| 175 | +//! <code>[Pin]<&Self></code> or <code>[Pin]<&mut Self></code>) has consequences for your [`Drop`] |
176 | 176 | //! implementation as well: if an element of your type could have been pinned,
|
177 |
| -//! you must treat [`Drop`] as implicitly taking [`Pin`]`<&mut Self>`. |
| 177 | +//! you must treat [`Drop`] as implicitly taking <code>[Pin]<&mut Self></code>. |
178 | 178 | //!
|
179 | 179 | //! For example, you could implement `Drop` as follows:
|
180 | 180 | //!
|
|
204 | 204 | //! # Projections and Structural Pinning
|
205 | 205 | //!
|
206 | 206 | //! When working with pinned structs, the question arises how one can access the
|
207 |
| -//! fields of that struct in a method that takes just [`Pin`]`<&mut Struct>`. |
| 207 | +//! fields of that struct in a method that takes just <code>[Pin]<&mut Struct></code>. |
208 | 208 | //! The usual approach is to write helper methods (so called *projections*)
|
209 |
| -//! that turn [`Pin`]`<&mut Struct>` into a reference to the field, but what |
210 |
| -//! type should that reference have? Is it [`Pin`]`<&mut Field>` 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 `&mut Field`? |
211 | 211 | //! The same question arises with the fields of an `enum`, and also when considering
|
212 | 212 | //! container/wrapper types such as [`Vec<T>`], [`Box<T>`], or [`RefCell<T>`].
|
213 | 213 | //! (This question applies to both mutable and shared references, we just
|
214 | 214 | //! use the more common case of mutable references here for illustration.)
|
215 | 215 | //!
|
216 | 216 | //! It turns out that it is actually up to the author of the data structure
|
217 | 217 | //! to decide whether the pinned projection for a particular field turns
|
218 |
| -//! [`Pin`]`<&mut Struct>` into [`Pin`]`<&mut Field>` or `&mut Field`. There are some |
| 218 | +//! <code>[Pin]<&mut Struct></code> into <code>[Pin]<&mut Field></code> or `&mut Field`. There are some |
219 | 219 | //! constraints though, and the most important constraint is *consistency*:
|
220 | 220 | //! every field can be *either* projected to a pinned reference, *or* have
|
221 | 221 | //! pinning removed as part of the projection. If both are done for the same field,
|
|
230 | 230 | //! ## Pinning *is not* structural for `field`
|
231 | 231 | //!
|
232 | 232 | //! 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 [`Pin`]`<&mut Field>` is never created, |
| 233 | +//! but that is actually the easiest choice: if a <code>[Pin]<&mut Field></code> is never created, |
234 | 234 | //! nothing can go wrong! So, if you decide that some field does not have structural pinning,
|
235 | 235 | //! all you have to ensure is that you never create a pinned reference to that field.
|
236 | 236 | //!
|
237 | 237 | //! Fields without structural pinning may have a projection method that turns
|
238 |
| -//! [`Pin`]`<&mut Struct>` into `&mut Field`: |
| 238 | +//! <code>[Pin]<&mut Struct></code> into `&mut Field`: |
239 | 239 | //!
|
240 | 240 | //! ```rust,no_run
|
241 | 241 | //! # use std::pin::Pin;
|
|
251 | 251 | //!
|
252 | 252 | //! You may also `impl Unpin for Struct` *even if* the type of `field`
|
253 | 253 | //! is not [`Unpin`]. What that type thinks about pinning is not relevant
|
254 |
| -//! when no [`Pin`]`<&mut Field>` is ever created. |
| 254 | +//! when no <code>[Pin]<&mut Field></code> is ever created. |
255 | 255 | //!
|
256 | 256 | //! ## Pinning *is* structural for `field`
|
257 | 257 | //!
|
258 | 258 | //! The other option is to decide that pinning is "structural" for `field`,
|
259 | 259 | //! meaning that if the struct is pinned then so is the field.
|
260 | 260 | //!
|
261 |
| -//! This allows writing a projection that creates a [`Pin`]`<&mut Field>`, thus |
| 261 | +//! This allows writing a projection that creates a <code>[Pin]<&mut Field></code>, thus |
262 | 262 | //! witnessing that the field is pinned:
|
263 | 263 | //!
|
264 | 264 | //! ```rust,no_run
|
|
336 | 336 | //! and thus they do not offer pinning projections. This is why `Box<T>: Unpin` holds for all `T`.
|
337 | 337 | //! It makes sense to do this for pointer types, because moving the `Box<T>`
|
338 | 338 | //! does not actually move the `T`: the [`Box<T>`] can be freely movable (aka `Unpin`) even if
|
339 |
| -//! the `T` is not. In fact, even [`Pin`]`<`[`Box`]`<T>>` and [`Pin`]`<&mut T>` are always |
| 339 | +//! the `T` is not. In fact, even <code>[Pin]<[Box]\<T>></code> and <code>[Pin]<&mut T></code> are always |
340 | 340 | //! [`Unpin`] themselves, for the same reason: their contents (the `T`) are pinned, but the
|
341 | 341 | //! pointers themselves can be moved without moving the pinned data. For both [`Box<T>`] and
|
342 |
| -//! [`Pin`]`<`[`Box`]`<T>>`, whether the content is pinned is entirely independent of whether the |
| 342 | +//! <code>[Pin]<[Box]\<T>></code>, whether the content is pinned is entirely independent of whether the |
343 | 343 | //! pointer is pinned, meaning pinning is *not* structural.
|
344 | 344 | //!
|
345 | 345 | //! When implementing a [`Future`] combinator, you will usually need structural pinning
|
346 | 346 | //! for the nested futures, as you need to get pinned references to them to call [`poll`].
|
347 | 347 | //! But if your combinator contains any other data that does not need to be pinned,
|
348 | 348 | //! you can make those fields not structural and hence freely access them with a
|
349 |
| -//! mutable reference even when you just have [`Pin`]`<&mut Self>` (such as in your own |
| 349 | +//! mutable reference even when you just have <code>[Pin]<&mut Self></code> (such as in your own |
350 | 350 | //! [`poll`] implementation).
|
351 | 351 | //!
|
352 | 352 | //! [`Deref`]: crate::ops::Deref
|
|
356 | 356 | //! [`Box<T>`]: ../../std/boxed/struct.Box.html
|
357 | 357 | //! [`Vec<T>`]: ../../std/vec/struct.Vec.html
|
358 | 358 | //! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len
|
359 |
| -//! [`Box`]: ../../std/boxed/struct.Box.html |
| 359 | +//! [Box]: ../../std/boxed/struct.Box.html |
360 | 360 | //! [Vec::pop]: ../../std/vec/struct.Vec.html#method.pop
|
361 | 361 | //! [Vec::push]: ../../std/vec/struct.Vec.html#method.push
|
362 |
| -//! [`Rc`]: ../../std/rc/struct.Rc.html |
| 362 | +//! [Rc]: ../../std/rc/struct.Rc.html |
363 | 363 | //! [`RefCell<T>`]: crate::cell::RefCell
|
364 | 364 | //! [`drop`]: Drop::drop
|
365 | 365 | //! [`VecDeque<T>`]: ../../std/collections/struct.VecDeque.html
|
|
0 commit comments