|
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 <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 |
18 | 18 | //! moving the values they contain: you can move out of a <code>[Box]\<T></code>, or you can use [`mem::swap`].
|
19 | 19 | //! <code>[Pin]\<P></code> wraps a pointer type `P`, so <code>[Pin]<[Box]\<T>></code> functions much like a regular
|
20 | 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 |
| 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 |
23 | 23 | //! cannot use operations such as [`mem::swap`]:
|
24 | 24 | //!
|
25 | 25 | //! ```
|
|
35 | 35 | //! It is worth reiterating that <code>[Pin]\<P></code> does *not* change the fact that a Rust compiler
|
36 | 36 | //! considers all types movable. [`mem::swap`] remains callable for any `T`. Instead, <code>[Pin]\<P></code>
|
37 | 37 | //! 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 |
39 | 39 | //! (like [`mem::swap`]).
|
40 | 40 | //!
|
41 | 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 |
| -//! 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 |
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 | 46 | //! For correctness, <code>[Pin]\<P></code> relies on the implementations of [`Deref`] and
|
|
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 <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>. |
59 | 59 | //!
|
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 |
61 | 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 | //!
|
65 | 65 | //! # Example: self-referential struct
|
66 | 66 | //!
|
67 | 67 | //! 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. |
69 | 69 | //! Feel free to [skip to where the theoretical discussion continues](#drop-guarantee).
|
70 | 70 | //!
|
71 | 71 | //! ```rust
|
|
165 | 165 | //! # `Drop` implementation
|
166 | 166 | //!
|
167 | 167 | //! 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 |
169 | 169 | //! is called *even if your type was previously pinned*! It is as if the
|
170 | 170 | //! compiler automatically called [`Pin::get_unchecked_mut`].
|
171 | 171 | //!
|
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 |
| -//! <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`] |
176 | 176 | //! 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>. |
178 | 178 | //!
|
179 |
| -//! For example, you could implement `Drop` as follows: |
| 179 | +//! For example, you could implement [`Drop`] as follows: |
180 | 180 | //!
|
181 | 181 | //! ```rust,no_run
|
182 | 182 | //! # use std::pin::Pin;
|
|
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 <code>[Pin]<&mut Struct></code>. |
| 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 <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>? |
211 | 211 | //! The same question arises with the fields of an `enum`, and also when considering
|
212 | 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 | //!
|
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 |
| -//! <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 |
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 <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, |
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 |
| -//! <code>[Pin]<&mut Struct></code> into `&mut Field`: |
| 238 | +//! <code>[Pin]<[&mut] Struct></code> into <code>[&mut] Field</code>: |
239 | 239 | //!
|
240 | 240 | //! ```rust,no_run
|
241 | 241 | //! # use std::pin::Pin;
|
|
249 | 249 | //! }
|
250 | 250 | //! ```
|
251 | 251 | //!
|
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` |
253 | 253 | //! 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. |
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 <code>[Pin]<&mut Field></code>, 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
|
|
278 | 278 | //! 1. The struct must only be [`Unpin`] if all the structural fields are
|
279 | 279 | //! [`Unpin`]. This is the default, but [`Unpin`] is a safe trait, so as the author of
|
280 | 280 | //! 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 |
282 | 282 | //! 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`].) |
284 | 284 | //! 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. |
287 | 287 | //! You have to guarantee that you do not move a field inside your [`Drop`] implementation.
|
288 | 288 | //! In particular, as explained previously, this means that your struct must *not*
|
289 | 289 | //! be `#[repr(packed)]`.
|
|
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 |
| -//! <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 |
305 | 305 | //! pinning cannot be structural for the field holding this data.
|
306 | 306 | //!
|
307 | 307 | //! 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>. |
309 | 309 | //! Then we could do the following:
|
310 | 310 | //! ```compile_fail
|
311 | 311 | //! fn exploit_ref_cell<T>(rc: Pin<&mut RefCell<T>>) {
|
|
316 | 316 | //! }
|
317 | 317 | //! ```
|
318 | 318 | //! 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 |
320 | 320 | //! reference we got later.
|
321 | 321 | //!
|
322 | 322 | //! ## Examples
|
|
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 <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 |
332 | 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 |
| -//! 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 |
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 <code>[Box]\<T></code> and
|
342 | 342 | //! <code>[Pin]<[Box]\<T>></code>, whether the content is pinned is entirely independent of whether the
|
|
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 <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 |
350 | 350 | //! [`poll`] implementation).
|
351 | 351 | //!
|
| 352 | +//! [Deref]: crate::ops::Deref |
352 | 353 | //! [`Deref`]: crate::ops::Deref
|
| 354 | +//! [Target]: crate::ops::Deref::Target |
353 | 355 | //! [`DerefMut`]: crate::ops::DerefMut
|
354 | 356 | //! [`mem::swap`]: crate::mem::swap
|
355 | 357 | //! [`mem::forget`]: crate::mem::forget
|
|
367 | 369 | //! [drop-impl]: #drop-implementation
|
368 | 370 | //! [drop-guarantee]: #drop-guarantee
|
369 | 371 | //! [`poll`]: crate::future::Future::poll
|
| 372 | +//! [&]: ../../std/primitive.reference.html |
| 373 | +//! [&mut]: ../../std/primitive.reference.html |
| 374 | +//! [`unsafe`]: ../../std/keyword.unsafe.html |
370 | 375 |
|
371 | 376 | #![stable(feature = "pin", since = "1.33.0")]
|
372 | 377 |
|
|
0 commit comments