|
158 | 158 | //! [`Pin<Ptr>`]. [`Pin<Ptr>`] can wrap any pointer type, forming a promise that the **pointee**
|
159 | 159 | //! will not be *moved* or [otherwise invalidated][self#subtle-details].
|
160 | 160 | //!
|
| 161 | +//! We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning reference, or pinning |
| 162 | +//! `Box`, etc.) because its existince is the thing that is pinning the underlying pointee in place: |
| 163 | +//! it is the metaphorical "pin" securing the data in place on the pinboard (in memory). |
| 164 | +//! |
161 | 165 | //! It is important to stress that the thing in the [`Pin`] is not the value which we want to pin
|
162 | 166 | //! itself, but rather a pointer to that value! A [`Pin<Ptr>`] does not pin the `Ptr` but rather
|
163 | 167 | //! the pointer's ***pointee** value*.
|
|
190 | 194 | //!
|
191 | 195 | //! [`Pin<Ptr>`] also uses the [`<Ptr as Deref>::Target`][Target] type information to modify the
|
192 | 196 | //! interface it is allowed to provide for interacting with that data (for example, when a
|
193 |
| -//! [`Pin`]-wrapped pointer points at pinned data which implements [`Unpin`], as |
| 197 | +//! pinning pointer points at pinned data which implements [`Unpin`], as |
194 | 198 | //! [discussed below][self#unpin]).
|
195 | 199 | //!
|
196 | 200 | //! [`Pin<Ptr>`] requires that implementations of [`Deref`] and [`DerefMut`] on `Ptr` return a
|
|
247 | 251 | //! // 1. Create the value, not yet in an address-sensitive state
|
248 | 252 | //! let tracker = AddrTracker::default();
|
249 | 253 | //!
|
250 |
| -//! // 2. Pin the value by putting it behind a Pin-wrapped pointer, thus putting |
| 254 | +//! // 2. Pin the value by putting it behind a pinning pointer, thus putting |
251 | 255 | //! // it into an address-sensitive state
|
252 | 256 | //! let mut ptr_to_pinned_tracker: Pin<&mut AddrTracker> = pin!(tracker);
|
253 | 257 | //! ptr_to_pinned_tracker.as_mut().check_for_move();
|
|
263 | 267 | //!
|
264 | 268 | //! Note that this invariant is enforced by simply making it impossible to call code that would
|
265 | 269 | //! perform a move on the pinned value. This is the case since the only way to access that pinned
|
266 |
| -//! value is through the [`Pin`]-wrapped [`&mut T`], which in turn restricts our access. |
| 270 | +//! value is through the pinning <code>[Pin]<[&mut] T>></code>, which in turn restricts our access. |
267 | 271 | //!
|
268 | 272 | //! ## [`Unpin`]
|
269 | 273 | //!
|
|
326 | 330 | //! impl Unmovable {
|
327 | 331 | //! /// Create a new `Unmovable`.
|
328 | 332 | //! ///
|
329 |
| -//! /// To ensure the data doesn't move we place it on the heap behind a Pin-wrapped pointer. |
330 |
| -//! /// Note that the data is pinned, but the pointer to that data can itself still be moved, |
331 |
| -//! /// which is important because it means we can return the pointer from the function. |
| 333 | +//! /// To ensure the data doesn't move we place it on the heap behind a pinning Box. |
| 334 | +//! /// Note that the data is pinned, but the which is pinning it can itself still be moved. |
| 335 | +//! /// This is important because it means we can return the pointer from the function, which |
| 336 | +//! /// is itself a kind of move! |
332 | 337 | //! fn new() -> Pin<Box<Self>> {
|
333 | 338 | //! let res = Unmovable {
|
334 | 339 | //! data: [0; 64],
|
335 | 340 | //! // We only create the pointer once the data is in place
|
336 | 341 | //! // otherwise it will have already moved before we even started.
|
337 |
| -//! slice: NonNull::dangling(), |
| 342 | +//! slice: NonNull::from(&[]), |
338 | 343 | //! _pin: PhantomPinned,
|
339 | 344 | //! };
|
340 | 345 | //! // First we put the data in a box, which will be its final resting place
|
|
344 | 349 | //! // From now on we need to make sure we don't move the boxed data.
|
345 | 350 | //! boxed.slice = NonNull::from(&boxed.data);
|
346 | 351 | //!
|
347 |
| -//! // To do that, we pin the data in place by pointing to it with a Pin-wrapped pointer. |
348 |
| -//! // Box::into_pin wraps the existing Box pointer to the data in-place without moving, |
349 |
| -//! // so we can safely do this now after inserting the slice pointer above, but we have to |
350 |
| -//! // take care that we haven't performed any other semantic moves of `res` in between. |
351 |
| -//! let pinned = Box::into_pin(box); |
| 352 | +//! // To do that, we pin the data in place by pointing to it with a pinning |
| 353 | +//! // (`Pin`-wrapped) pointer. |
| 354 | +//! // |
| 355 | +//! // `Box::into_pin` makes existing `Box` pin the data in-place without moving it, |
| 356 | +//! // so we can safely do this now *after* inserting the slice pointer above, but we have |
| 357 | +//! // to take care that we haven't performed any other semantic moves of `res` in between. |
| 358 | +//! let pin = Box::into_pin(boxed); |
352 | 359 | //!
|
353 |
| -//! // Now we can return the pinned (through a Pin-wrapped box) data |
354 |
| -//! pinned |
| 360 | +//! // Now we can return the pinned (through a pinning Box) data |
| 361 | +//! pin |
355 | 362 | //! }
|
356 | 363 | //! }
|
357 | 364 | //!
|
|
360 | 367 | //! // The inner pointee `Unmovable` struct will now never be allowed to move.
|
361 | 368 | //! // Meanwhile, we are free to move the pointer around.
|
362 | 369 | //! # #[allow(unused_mut)]
|
363 |
| -//! let mut still_unmoved = unmoved; |
| 370 | +//! let mut still_unmoved = unmovable; |
364 | 371 | //! assert_eq!(still_unmoved.slice, NonNull::from(&still_unmoved.data));
|
365 | 372 | //!
|
| 373 | +//! // We cannot mutably dereference a `Pin<Ptr>` unless the pointee is `Unpin` or we use unsafe. |
366 | 374 | //! // Since our type doesn't implement `Unpin`, this will fail to compile.
|
367 |
| -//! // We cannot mutably dereference a `Pin` to `!Unpin` data (without using `unsafe`) |
368 | 375 | //! // let mut new_unmoved = Unmovable::new();
|
369 | 376 | //! // std::mem::swap(&mut *still_unmoved, &mut *new_unmoved);
|
370 | 377 | //! ```
|
|
401 | 408 | //! of the pinned data to be valid, on top of the ones that must be upheld for a non-pinned value of
|
402 | 409 | //! the same type:
|
403 | 410 | //!
|
404 |
| -//! From the moment a value is pinned by constructing a [`Pin`]-wrapped pointer to it, that value |
| 411 | +//! From the moment a value is pinned by constructing a [`Pin`]ning pointer to it, that value |
405 | 412 | //! must *remain, **valid***, at that same address in memory, *until its [`drop`] handler is
|
406 | 413 | //! called.*
|
407 | 414 | //!
|
|
436 | 443 | //! said storage.
|
437 | 444 | //!
|
438 | 445 | //! However, reuse can happen even if no storage is (de-)allocated. For example, if we had an
|
439 |
| -//! [`Option`] which contained a [`Some(v)`] where `v` is a pinned value, then `v` would be |
440 |
| -//! invalidated by setting that option to [`None`]. |
| 446 | +//! [`Option`] which contained a `Some(v)` where `v` is a pinned value, then `v` would be |
| 447 | +//! invalidated by setting that option to `None`. |
441 | 448 | //!
|
442 | 449 | //! Similarly, if a [`Vec`] was used to store pinned values and [`Vec::set_len`] is used to manually
|
443 | 450 | //! "kill" some elements of a vector, all value "killed" would become invalidated.
|
|
451 | 458 | //! # use std::mem::ManuallyDrop;
|
452 | 459 | //! # use std::pin::Pin;
|
453 | 460 | //! # struct Type;
|
454 |
| -//! let mut pinned = Box::pin(ManuallyDrop::new(Type)); |
455 |
| -//! let inner = unsafe { |
456 |
| -//! Pin::map_unchecked_mut(Pin::as_mut(&mut pinned), |x| &mut *x) |
| 461 | +//! // Pin something inside a `ManuallyDrop`. This is fine on its own. |
| 462 | +//! let mut pin: Pin<Box<ManuallyDrop<Type>>> = Box::pin(ManuallyDrop::new(Type)); |
| 463 | +//! |
| 464 | +//! // However, creating a pinning mutable reference to the type *inside* |
| 465 | +//! // the `ManuallyDrop` is not! |
| 466 | +//! let inner: Pin<&mut Type> = unsafe { |
| 467 | +//! Pin::map_unchecked_mut(pin.as_mut(), |x| &mut **x) |
457 | 468 | //! };
|
458 | 469 | //! ```
|
459 | 470 | //!
|
460 |
| -//! Because [`mem::ManuallyDrop`] inhibits the destructor of `Type`, it won't get run, even though |
461 |
| -//! normally [`Box<T>`] drops the `T` before freeing the storage. |
| 471 | +//! Because [`mem::ManuallyDrop`] inhibits the destructor of `Type`, it won't get run when the |
| 472 | +//! <code>[Box]<[ManuallyDrop]\<Type>></code> is dropped, thus violating the drop guarantee of the |
| 473 | +//! <code>[Pin]<[&mut] Type>></code>. |
462 | 474 | //!
|
463 | 475 | //! Of course, *leaking* memory in such a way that its underlying storage will never get invalidated
|
464 | 476 | //! or re-used is still fine: [`mem::forget`]ing a [`Box<T>`] prevents its storage from ever getting
|
465 |
| -//! re-used, so the [`drop`] guarantee does not apply. |
| 477 | +//! re-used, so the [`drop`] guarantee is still satisfied. |
466 | 478 | //!
|
467 | 479 | //! # Implementing an address-sensitive type.
|
468 | 480 | //!
|
|
478 | 490 | //! indeed in an address-sensitive state before [`drop`] was called, it is as if the compiler
|
479 | 491 | //! automatically called [`Pin::get_unchecked_mut`].
|
480 | 492 | //!
|
481 |
| -//! This can never cause a problem in safe code because implementing a type which has an |
482 |
| -//! address-sensitive state which relies on pinning for validity (for example by |
483 |
| -//! implementing some operation on <code>[Pin]<[&]Self></code> or <code>[Pin]<[&mut] Self></code> |
484 |
| -//! which would be invalid if that `self` was not pinned) has consequences for your |
| 493 | +//! This can never cause a problem in purely safe code because creating a pinning pointer to |
| 494 | +//! a type which has an address-sensitive (thus does not implement `Unpin`) requires `unsafe`, |
| 495 | +//! but it is important to note that choosing to take advantage of pinning-related guarantees |
| 496 | +//! to justify validity in the implementation of your type has consequences for that type's |
485 | 497 | //! [`Drop`][Drop] implementation as well: if an element of your type could have been pinned,
|
486 |
| -//! you must treat [`Drop`][Drop] as implicitly taking <code>[Pin]<[&mut] Self></code>. |
| 498 | +//! you must treat [`Drop`][Drop] as implicitly taking <code>self: [Pin]<[&mut] Self></code>. |
487 | 499 | //!
|
488 | 500 | //! You should implement [`Drop`] as follows:
|
489 | 501 | //!
|
|
511 | 523 | //! that for fields that happen to be sufficiently aligned. As a consequence, you cannot use
|
512 | 524 | //! pinning with a [`#[repr(packed)]`][packed] type.
|
513 | 525 | //!
|
| 526 | +//! ### Implementing [`Drop`] for pointer types which will be used as [`Pin`]ning pointers |
| 527 | +//! |
| 528 | +//! It should further be noted that creating a pinning pointer of some type `Ptr` to some underlying |
| 529 | +//! `T` which is not `Unpin` *also* carries with it implications on the way that `Ptr` type must |
| 530 | +//! implement [`Drop`] (as well as [`Deref`] and [`DerefMut`])! When implementing a pointer type |
| 531 | +//! that may be used as a pinning pointer, you must also take the same care described above not to |
| 532 | +//! *move* out of or otherwise invalidate the pointee during [`Drop`], [`Deref`], or [`DerefMut`] |
| 533 | +//! implementations. |
| 534 | +//! |
514 | 535 | //! ## "Assigning" pinned data
|
515 | 536 | //!
|
516 | 537 | //! Although in general it is not valid to swap data through a [`Pin<Ptr>`], or assign from
|
|
600 | 621 | //! ### Choosing pinning *not to be* structural for `field`...
|
601 | 622 | //!
|
602 | 623 | //! While counter-intuitive, it's actually the easier choice: if a
|
603 |
| -//! <code>[Pin]<[`&mut Field`]></code> is never created, nothing can go wrong (well, so long as no |
| 624 | +//! <code>[Pin]<[&mut] Field></code> is never created, nothing can go wrong (well, so long as no |
604 | 625 | //! unsound `unsafe` code is written which expects the invariants of such a [`Pin`] to be upheld
|
605 | 626 | //! without actually using pinning to guarantee them)! So, if you decide that some field does not
|
606 |
| -//! have structural pinning, all you have to ensure is that you never create [`Pin`]-wrapped |
| 627 | +//! have structural pinning, all you have to ensure is that you never create pinning |
607 | 628 | //! reference to that field.
|
608 | 629 | //!
|
609 | 630 | //! Fields without structural pinning may have a projection method that turns
|
610 |
| -//! <code>[Pin]<[&mut Struct][&mut]></code> into [`&mut Field`]: |
| 631 | +//! <code>[Pin]<[&mut] Struct></code> into [`&mut Field`]: |
611 | 632 | //!
|
612 | 633 | //! ```rust,no_run
|
613 | 634 | //! # use std::pin::Pin;
|
|
706 | 727 | //!
|
707 | 728 | //! For a type like [`Vec<T>`], both possibilities (structural pinning or not) make
|
708 | 729 | //! sense. A [`Vec<T>`] with structural pinning could have `get_pin`/`get_pin_mut`
|
709 |
| -//! methods to get [`Pin`]-wrapped references to elements. However, it could *not* allow calling |
| 730 | +//! methods to get pinning references to elements. However, it could *not* allow calling |
710 | 731 | //! [`pop`][Vec::pop] on a pinned [`Vec<T>`] because that would move the (structurally
|
711 | 732 | //! pinned) contents! Nor could it allow [`push`][Vec::push], which might reallocate and thus also
|
712 | 733 | //! move the contents.
|
|
728 | 749 | //! pointer is pinned, meaning pinning is *not* structural.
|
729 | 750 | //!
|
730 | 751 | //! When implementing a [`Future`] combinator, you will usually need structural pinning
|
731 |
| -//! for the nested futures, as you need to get [`Pin`]-wrapped references to them to call [`poll`]. |
732 |
| -//! But if your combinator contains any other data that does not need to be pinned, |
| 752 | +//! for the nested futures, as you need to get pinning ([`Pin`]-wrapped) references to them to |
| 753 | +//! call [`poll`]. But if your combinator contains any other data that does not need to be pinned, |
733 | 754 | //! you can make those fields not structural and hence freely access them with a
|
734 | 755 | //! mutable reference even when you just have <code>[Pin]<[`&mut Self`]></code>
|
735 | 756 | //! (such as in your own [`poll`] implementation).
|
|
744 | 765 | //! [`DerefMut`]: crate::ops::DerefMut "ops::DerefMut"
|
745 | 766 | //! [`mem::swap`]: crate::mem::swap "mem::swap"
|
746 | 767 | //! [`mem::forget`]: crate::mem::forget "mem::forget"
|
| 768 | +//! [ManuallyDrop]: crate::mem::ManuallyDrop "ManuallyDrop" |
747 | 769 | //! [RefCell]: crate::cell::RefCell "cell::RefCell"
|
748 | 770 | //! [`drop`]: Drop::drop
|
749 | 771 | //! [`ptr::write`]: crate::ptr::write "ptr::write"
|
@@ -785,17 +807,32 @@ use crate::{
|
785 | 807 | mem, ptr,
|
786 | 808 | };
|
787 | 809 |
|
788 |
| -/// A pointer to a pinned value. |
| 810 | +/// A pointer which pins its pointee in place. |
| 811 | +/// |
| 812 | +/// [`Pin`] is a wrapper around some kind of pointer `Ptr` which makes that pointer "pin" its |
| 813 | +/// pointee value in place, thus preventing the value referenced by that pointer from being moved or |
| 814 | +/// otherwise invalidated at that place in memory unless it implements [`Unpin`]. |
789 | 815 | ///
|
790 |
| -/// This is a wrapper around any kind of pointer `P` which makes that pointer "pin" its pointee |
791 |
| -/// value in place, thus preventing the value referenced by that pointer from being moved or |
792 |
| -/// otherwise invalidated unless it implements [`Unpin`]. |
| 816 | +/// ## Pinning values with [`Pin<Ptr>`] |
| 817 | +/// |
| 818 | +/// In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a |
| 819 | +/// [`Pin<Ptr>`]. [`Pin<Ptr>`] can wrap any pointer type, forming a promise that the **pointee** |
| 820 | +/// will not be *moved* or [otherwise invalidated][self#subtle-details]. |
| 821 | +/// |
| 822 | +/// We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning ref, or pinning |
| 823 | +/// [`Box`], etc.) because its existince is the thing that is pinning the underlying pointee in |
| 824 | +/// place: it is the metaphorical "pin" securing the data in place on the pinboard (in memory). |
| 825 | +/// |
| 826 | +/// It is important to stress that the thing in the [`Pin`] is not the value which we want to pin |
| 827 | +/// itself, but rather a pointer to that value! A [`Pin<Ptr>`] does not pin the `Ptr` but rather |
| 828 | +/// the pointer's ***pointee** value*. |
793 | 829 | ///
|
794 | 830 | /// `Pin<P>` is guaranteed to have the same memory layout and ABI as `P`.
|
795 | 831 | ///
|
796 |
| -/// *See the [`pin` module] documentation for an overview and explanation of pinning.* |
| 832 | +/// *See the [`pin` module] documentation for a more thorough exploration of pinning.* |
797 | 833 | ///
|
798 | 834 | /// [`pin` module]: self
|
| 835 | +/// [`Box`]: ../../std/boxed/struct.Box.html |
799 | 836 | //
|
800 | 837 | // Note: the `Clone` derive below causes unsoundness as it's possible to implement
|
801 | 838 | // `Clone` for mutable references.
|
@@ -982,12 +1019,14 @@ impl<P: Deref> Pin<P> {
|
982 | 1019 | /// use std::pin::Pin;
|
983 | 1020 | ///
|
984 | 1021 | /// fn move_pinned_rc<T>(mut x: Rc<T>) {
|
985 |
| - /// let pinned = unsafe { Pin::new_unchecked(Rc::clone(&x)) }; |
| 1022 | + /// // This should mean the pointee can never move again. |
| 1023 | + /// let pin = unsafe { Pin::new_unchecked(Rc::clone(&x)) }; |
986 | 1024 | /// {
|
987 |
| - /// let p: Pin<&T> = pinned.as_ref(); |
988 |
| - /// // This should mean the pointee can never move again. |
| 1025 | + /// let p: Pin<&T> = pin.as_ref(); |
| 1026 | + /// // ... |
989 | 1027 | /// }
|
990 |
| - /// drop(pinned); |
| 1028 | + /// drop(pin); |
| 1029 | +
|
991 | 1030 | /// let content = Rc::get_mut(&mut x).unwrap(); // Potential UB down the road ⚠️
|
992 | 1031 | /// // Now, if `x` was the only reference, we have a mutable reference to
|
993 | 1032 | /// // data that we pinned above, which we could use to move it as we have
|
@@ -1101,7 +1140,7 @@ impl<P: DerefMut> Pin<P> {
|
1101 | 1140 | /// ruled out by the contract of `Pin::new_unchecked`.
|
1102 | 1141 | ///
|
1103 | 1142 | /// This method is useful when doing multiple calls to functions that consume the
|
1104 |
| - /// [`Pin`]-wrapped pointer. |
| 1143 | + /// pinning pointer. |
1105 | 1144 | ///
|
1106 | 1145 | /// # Example
|
1107 | 1146 | ///
|
@@ -1293,7 +1332,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
|
1293 | 1332 | }
|
1294 | 1333 |
|
1295 | 1334 | impl<T: ?Sized> Pin<&'static T> {
|
1296 |
| - /// Get a `Pin`-wrapped reference from a `&'static` reference. |
| 1335 | + /// Get a pinning reference from a `&'static` reference. |
1297 | 1336 | ///
|
1298 | 1337 | /// This is safe because `T` is borrowed immutably for the `'static` lifetime, which
|
1299 | 1338 | /// never ends.
|
@@ -1346,7 +1385,7 @@ impl<'a, P: DerefMut> Pin<&'a mut Pin<P>> {
|
1346 | 1385 | }
|
1347 | 1386 |
|
1348 | 1387 | impl<T: ?Sized> Pin<&'static mut T> {
|
1349 |
| - /// Get a `Pin`-wrapped mutable reference from a static mutable reference. |
| 1388 | + /// Get a pinning mutable reference from a static mutable reference. |
1350 | 1389 | ///
|
1351 | 1390 | /// This is safe because `T` is borrowed for the `'static` lifetime, which
|
1352 | 1391 | /// never ends.
|
|
0 commit comments