|
6 | 6 | //! as moving an object with pointers to itself will invalidate them, which could cause undefined
|
7 | 7 | //! behavior.
|
8 | 8 | //!
|
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 |
10 | 10 | //! `P` has a stable location in memory, meaning it cannot be moved elsewhere
|
11 | 11 | //! and its memory cannot be deallocated until it gets dropped. We say that the
|
12 | 12 | //! pointee is "pinned". Things get more subtle when discussing types that
|
13 | 13 | //! combine pinned with non-pinned data; [see below](#projections-and-structural-pinning)
|
14 | 14 | //! for more details.
|
15 | 15 | //!
|
16 | 16 | //! 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 |
23 | 23 | //! cannot use operations such as [`mem::swap`]:
|
24 | 24 | //!
|
25 | 25 | //! ```
|
|
32 | 32 | //! }
|
33 | 33 | //! ```
|
34 | 34 | //!
|
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 |
38 | 38 | //! moved by making it impossible to call methods that require `&mut T` on them
|
39 | 39 | //! (like [`mem::swap`]).
|
40 | 40 | //!
|
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 |
43 | 43 | //! as a "`P`-style pointer" to a pinned `P::Target` -- so, a <code>[Pin]<[Box]\<T>></code> is
|
44 | 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 |
| -//! 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 |
47 | 47 | //! [`DerefMut`] not to move out of their `self` parameter, and only ever to
|
48 | 48 | //! return a pointer to pinned data when they are called on a pinned pointer.
|
49 | 49 | //!
|
|
53 | 53 | //! rely on having a stable address. This includes all the basic types (like
|
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 |
| -//! 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 |
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 |
| -//! 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 |
62 | 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 | //!
|
|
149 | 149 | //! when [`drop`] is called*. Only once [`drop`] returns or panics, the memory may be reused.
|
150 | 150 | //!
|
151 | 151 | //! 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 |
153 | 153 | //! off of a vector. It can be repurposed by using [`ptr::write`] to overwrite it without
|
154 | 154 | //! calling the destructor first. None of this is allowed for pinned data without calling [`drop`].
|
155 | 155 | //!
|
|
209 | 209 | //! that turn <code>[Pin]<&mut Struct></code> into a reference to the field, but what
|
210 | 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 |
| -//! 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>. |
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 | //!
|
|
292 | 292 | //! 3. You must make sure that you uphold the [`Drop` guarantee][drop-guarantee]:
|
293 | 293 | //! once your struct is pinned, the memory that contains the
|
294 | 294 | //! 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> |
296 | 296 | //! can fail to call [`drop`] on all elements if one of the destructors panics. This violates
|
297 | 297 | //! 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 |
299 | 299 | //! does not cause unsoundness.)
|
300 | 300 | //! 4. You must not offer any other operations that could lead to data being moved out of
|
301 | 301 | //! 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 |
303 | 303 | //! `fn(Pin<&mut Struct<T>>) -> Option<T>`,
|
304 | 304 | //! that operation can be used to move a `T` out of a pinned `Struct<T>` -- which means
|
305 | 305 | //! pinning cannot be structural for the field holding this data.
|
306 | 306 | //!
|
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> |
308 | 308 | //! had a method `fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T>`.
|
309 | 309 | //! Then we could do the following:
|
310 | 310 | //! ```compile_fail
|
|
315 | 315 | //! let content = &mut *b; // And here we have `&mut T` to the same data.
|
316 | 316 | //! }
|
317 | 317 | //! ```
|
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> |
319 | 319 | //! (using `RefCell::get_pin_mut`) and then move that content using the mutable
|
320 | 320 | //! reference we got later.
|
321 | 321 | //!
|
322 | 322 | //! ## Examples
|
323 | 323 | //!
|
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 |
326 | 326 | //! 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) |
328 | 328 | //! contents! Nor could it allow [`push`][Vec::push], which might reallocate and thus also move the
|
329 | 329 | //! contents.
|
330 | 330 | //!
|
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. |
333 | 333 | //! At that point pinning just has no effect on the vector at all.
|
334 | 334 | //!
|
335 | 335 | //! In the standard library, pointer types generally do not have structural pinning,
|
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 |
| -//! 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 |
339 | 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 |
| -//! 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 |
342 | 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 | //!
|
|
353 | 353 | //! [`DerefMut`]: crate::ops::DerefMut
|
354 | 354 | //! [`mem::swap`]: crate::mem::swap
|
355 | 355 | //! [`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 |
358 | 357 | //! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len
|
359 | 358 | //! [Box]: ../../std/boxed/struct.Box.html
|
360 | 359 | //! [Vec::pop]: ../../std/vec/struct.Vec.html#method.pop
|
361 | 360 | //! [Vec::push]: ../../std/vec/struct.Vec.html#method.push
|
362 | 361 | //! [Rc]: ../../std/rc/struct.Rc.html
|
363 |
| -//! [`RefCell<T>`]: crate::cell::RefCell |
| 362 | +//! [RefCell]: crate::cell::RefCell |
364 | 363 | //! [`drop`]: Drop::drop
|
365 |
| -//! [`VecDeque<T>`]: ../../std/collections/struct.VecDeque.html |
366 |
| -//! [`Some(v)`]: Some |
| 364 | +//! [VecDeque]: ../../std/collections/struct.VecDeque.html |
367 | 365 | //! [`ptr::write`]: crate::ptr::write
|
368 | 366 | //! [`Future`]: crate::future::Future
|
369 | 367 | //! [drop-impl]: #drop-implementation
|
|
0 commit comments