2019-02-09 16:16:58 -06:00
|
|
|
//! Types that pin data to its location in memory.
|
2018-08-09 12:10:30 -05:00
|
|
|
//!
|
2019-06-06 14:42:15 -05:00
|
|
|
//! It is sometimes useful to have objects that are guaranteed not to move,
|
2018-08-31 23:12:10 -05:00
|
|
|
//! in the sense that their placement in memory does not change, and can thus be relied upon.
|
2018-11-09 22:12:46 -06:00
|
|
|
//! A prime example of such a scenario would be building self-referential structs,
|
2019-06-06 14:42:15 -05:00
|
|
|
//! as moving an object with pointers to itself will invalidate them, which could cause undefined
|
|
|
|
//! behavior.
|
2018-08-31 23:12:10 -05:00
|
|
|
//!
|
2020-08-13 07:41:04 -05:00
|
|
|
//! At a high level, a [`Pin<P>`] ensures that the pointee of any pointer type
|
|
|
|
//! `P` has a stable location in memory, meaning it cannot be moved elsewhere
|
|
|
|
//! and its memory cannot be deallocated until it gets dropped. We say that the
|
|
|
|
//! pointee is "pinned". Things get more subtle when discussing types that
|
|
|
|
//! combine pinned with non-pinned data; [see below](#projections-and-structural-pinning)
|
|
|
|
//! for more details.
|
2019-02-19 13:50:16 -06:00
|
|
|
//!
|
2018-11-15 17:46:17 -06:00
|
|
|
//! By default, all types in Rust are movable. Rust allows passing all types by-value,
|
2019-07-04 11:46:48 -05:00
|
|
|
//! and common smart-pointer types such as [`Box<T>`] and `&mut T` allow replacing and
|
|
|
|
//! moving the values they contain: you can move out of a [`Box<T>`], or you can use [`mem::swap`].
|
|
|
|
//! [`Pin<P>`] wraps a pointer type `P`, so [`Pin`]`<`[`Box`]`<T>>` functions much like a regular
|
|
|
|
//! [`Box<T>`]: when a [`Pin`]`<`[`Box`]`<T>>` gets dropped, so do its contents, and the memory gets
|
|
|
|
//! deallocated. Similarly, [`Pin`]`<&mut T>` is a lot like `&mut T`. However, [`Pin<P>`] does
|
|
|
|
//! not let clients actually obtain a [`Box<T>`] or `&mut T` to pinned data, which implies that you
|
|
|
|
//! cannot use operations such as [`mem::swap`]:
|
2019-06-06 14:42:15 -05:00
|
|
|
//!
|
2019-02-19 13:50:16 -06:00
|
|
|
//! ```
|
2019-02-19 14:12:48 -06:00
|
|
|
//! use std::pin::Pin;
|
2019-02-19 13:50:16 -06:00
|
|
|
//! fn swap_pins<T>(x: Pin<&mut T>, y: Pin<&mut T>) {
|
|
|
|
//! // `mem::swap` needs `&mut T`, but we cannot get it.
|
|
|
|
//! // We are stuck, we cannot swap the contents of these references.
|
|
|
|
//! // We could use `Pin::get_unchecked_mut`, but that is unsafe for a reason:
|
|
|
|
//! // we are not allowed to use it for moving things out of the `Pin`.
|
|
|
|
//! }
|
|
|
|
//! ```
|
2018-11-15 17:46:17 -06:00
|
|
|
//!
|
2019-02-21 08:28:46 -06:00
|
|
|
//! It is worth reiterating that [`Pin<P>`] does *not* change the fact that a Rust compiler
|
2019-07-04 11:46:48 -05:00
|
|
|
//! considers all types movable. [`mem::swap`] remains callable for any `T`. Instead, [`Pin<P>`]
|
|
|
|
//! prevents certain *values* (pointed to by pointers wrapped in [`Pin<P>`]) from being
|
2019-02-20 02:45:28 -06:00
|
|
|
//! moved by making it impossible to call methods that require `&mut T` on them
|
|
|
|
//! (like [`mem::swap`]).
|
2018-11-15 17:46:17 -06:00
|
|
|
//!
|
2019-02-21 08:28:46 -06:00
|
|
|
//! [`Pin<P>`] can be used to wrap any pointer type `P`, and as such it interacts with
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`Deref`] and [`DerefMut`]. A [`Pin<P>`] where `P: Deref` should be considered
|
|
|
|
//! as a "`P`-style pointer" to a pinned `P::Target` -- so, a [`Pin`]`<`[`Box`]`<T>>` is
|
|
|
|
//! an owned pointer to a pinned `T`, and a [`Pin`]`<`[`Rc`]`<T>>` is a reference-counted
|
2019-02-19 14:12:48 -06:00
|
|
|
//! pointer to a pinned `T`.
|
2019-06-06 14:42:15 -05:00
|
|
|
//! For correctness, [`Pin<P>`] relies on the implementations of [`Deref`] and
|
|
|
|
//! [`DerefMut`] not to move out of their `self` parameter, and only ever to
|
|
|
|
//! return a pointer to pinned data when they are called on a pinned pointer.
|
2019-02-19 14:12:48 -06:00
|
|
|
//!
|
2019-02-19 06:08:46 -06:00
|
|
|
//! # `Unpin`
|
2018-08-31 23:12:10 -05:00
|
|
|
//!
|
2019-06-06 14:42:15 -05:00
|
|
|
//! Many types are always freely movable, even when pinned, because they do not
|
|
|
|
//! rely on having a stable address. This includes all the basic types (like
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`bool`], [`i32`], and references) as well as types consisting solely of these
|
2019-06-06 14:42:15 -05:00
|
|
|
//! types. Types that do not care about pinning implement the [`Unpin`]
|
|
|
|
//! auto-trait, which cancels the effect of [`Pin<P>`]. For `T: Unpin`,
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`Pin`]`<`[`Box`]`<T>>` and [`Box<T>`] function identically, as do [`Pin`]`<&mut T>` and
|
2019-06-06 14:42:15 -05:00
|
|
|
//! `&mut T`.
|
2019-02-19 06:08:46 -06:00
|
|
|
//!
|
2019-07-04 11:46:48 -05:00
|
|
|
//! Note that pinning and [`Unpin`] only affect the pointed-to type `P::Target`, not the pointer
|
|
|
|
//! type `P` itself that got wrapped in [`Pin<P>`]. For example, whether or not [`Box<T>`] is
|
|
|
|
//! [`Unpin`] has no effect on the behavior of [`Pin`]`<`[`Box`]`<T>>` (here, `T` is the
|
2019-02-19 06:08:46 -06:00
|
|
|
//! pointed-to type).
|
2018-08-31 23:12:10 -05:00
|
|
|
//!
|
2019-02-19 13:50:16 -06:00
|
|
|
//! # Example: self-referential struct
|
2018-08-31 23:12:10 -05:00
|
|
|
//!
|
2020-08-13 07:41:04 -05:00
|
|
|
//! Before we go into more details to explain the guarantees and choices
|
|
|
|
//! associated with `Pin<T>`, we discuss some examples for how it might be used.
|
|
|
|
//! Feel free to [skip to where the theoretical discussion continues](#drop-guarantee).
|
|
|
|
//!
|
2018-08-31 23:12:10 -05:00
|
|
|
//! ```rust
|
|
|
|
//! use std::pin::Pin;
|
2018-11-15 17:49:16 -06:00
|
|
|
//! use std::marker::PhantomPinned;
|
2018-08-31 23:12:10 -05:00
|
|
|
//! use std::ptr::NonNull;
|
|
|
|
//!
|
2019-06-06 14:42:15 -05:00
|
|
|
//! // This is a self-referential struct because the slice field points to the data field.
|
2018-08-31 23:12:10 -05:00
|
|
|
//! // We cannot inform the compiler about that with a normal reference,
|
2019-06-06 14:42:15 -05:00
|
|
|
//! // as this pattern cannot be described with the usual borrowing rules.
|
|
|
|
//! // Instead we use a raw pointer, though one which is known not to be null,
|
|
|
|
//! // as we know it's pointing at the string.
|
2018-08-31 23:12:10 -05:00
|
|
|
//! struct Unmovable {
|
|
|
|
//! data: String,
|
|
|
|
//! slice: NonNull<String>,
|
2018-11-15 17:49:16 -06:00
|
|
|
//! _pin: PhantomPinned,
|
2018-08-31 23:12:10 -05:00
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! impl Unmovable {
|
|
|
|
//! // To ensure the data doesn't move when the function returns,
|
|
|
|
//! // we place it in the heap where it will stay for the lifetime of the object,
|
|
|
|
//! // and the only way to access it would be through a pointer to it.
|
|
|
|
//! fn new(data: String) -> Pin<Box<Self>> {
|
|
|
|
//! let res = Unmovable {
|
|
|
|
//! data,
|
|
|
|
//! // we only create the pointer once the data is in place
|
|
|
|
//! // otherwise it will have already moved before we even started
|
|
|
|
//! slice: NonNull::dangling(),
|
2018-11-15 17:49:16 -06:00
|
|
|
//! _pin: PhantomPinned,
|
2018-08-31 23:12:10 -05:00
|
|
|
//! };
|
2018-12-18 12:25:02 -06:00
|
|
|
//! let mut boxed = Box::pin(res);
|
2018-08-31 23:12:10 -05:00
|
|
|
//!
|
|
|
|
//! let slice = NonNull::from(&boxed.data);
|
|
|
|
//! // we know this is safe because modifying a field doesn't move the whole struct
|
2018-09-14 19:40:52 -05:00
|
|
|
//! unsafe {
|
2018-08-31 23:12:10 -05:00
|
|
|
//! let mut_ref: Pin<&mut Self> = Pin::as_mut(&mut boxed);
|
2018-12-18 12:20:53 -06:00
|
|
|
//! Pin::get_unchecked_mut(mut_ref).slice = slice;
|
2018-08-31 23:12:10 -05:00
|
|
|
//! }
|
|
|
|
//! boxed
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! let unmoved = Unmovable::new("hello".to_string());
|
|
|
|
//! // The pointer should point to the correct location,
|
|
|
|
//! // so long as the struct hasn't moved.
|
|
|
|
//! // Meanwhile, we are free to move the pointer around.
|
|
|
|
//! # #[allow(unused_mut)]
|
|
|
|
//! let mut still_unmoved = unmoved;
|
|
|
|
//! assert_eq!(still_unmoved.slice, NonNull::from(&still_unmoved.data));
|
|
|
|
//!
|
|
|
|
//! // Since our type doesn't implement Unpin, this will fail to compile:
|
2019-03-05 08:08:01 -06:00
|
|
|
//! // let mut new_unmoved = Unmovable::new("world".to_string());
|
2018-08-31 23:12:10 -05:00
|
|
|
//! // std::mem::swap(&mut *still_unmoved, &mut *new_unmoved);
|
|
|
|
//! ```
|
2019-02-19 06:08:46 -06:00
|
|
|
//!
|
2019-02-19 13:17:20 -06:00
|
|
|
//! # Example: intrusive doubly-linked list
|
|
|
|
//!
|
|
|
|
//! In an intrusive doubly-linked list, the collection does not actually allocate
|
|
|
|
//! the memory for the elements itself. Allocation is controlled by the clients,
|
|
|
|
//! and elements can live on a stack frame that lives shorter than the collection does.
|
|
|
|
//!
|
|
|
|
//! To make this work, every element has pointers to its predecessor and successor in
|
2019-02-21 08:28:46 -06:00
|
|
|
//! the list. Elements can only be added when they are pinned, because moving the elements
|
2019-07-04 11:46:48 -05:00
|
|
|
//! around would invalidate the pointers. Moreover, the [`Drop`] implementation of a linked
|
2019-02-19 13:17:20 -06:00
|
|
|
//! list element will patch the pointers of its predecessor and successor to remove itself
|
|
|
|
//! from the list.
|
|
|
|
//!
|
2019-07-04 11:46:48 -05:00
|
|
|
//! Crucially, we have to be able to rely on [`drop`] being called. If an element
|
|
|
|
//! could be deallocated or otherwise invalidated without calling [`drop`], the pointers into it
|
2020-07-07 19:48:15 -05:00
|
|
|
//! from its neighboring elements would become invalid, which would break the data structure.
|
2019-02-19 14:23:53 -06:00
|
|
|
//!
|
2019-07-04 11:46:48 -05:00
|
|
|
//! Therefore, pinning also comes with a [`drop`]-related guarantee.
|
2019-02-19 13:17:20 -06:00
|
|
|
//!
|
2019-02-19 06:08:46 -06:00
|
|
|
//! # `Drop` guarantee
|
|
|
|
//!
|
|
|
|
//! The purpose of pinning is to be able to rely on the placement of some data in memory.
|
2019-02-21 08:28:46 -06:00
|
|
|
//! To make this work, not just moving the data is restricted; deallocating, repurposing, or
|
2019-02-19 12:46:33 -06:00
|
|
|
//! otherwise invalidating the memory used to store the data is restricted, too.
|
|
|
|
//! Concretely, for pinned data you have to maintain the invariant
|
2019-06-15 16:56:42 -05:00
|
|
|
//! that *its memory will not get invalidated or repurposed from the moment it gets pinned until
|
2020-04-27 07:45:37 -05:00
|
|
|
//! when [`drop`] is called*. Only once [`drop`] returns or panics, the memory may be reused.
|
|
|
|
//!
|
|
|
|
//! Memory can be "invalidated" by deallocation, but also by
|
2019-02-21 08:28:46 -06:00
|
|
|
//! replacing a [`Some(v)`] by [`None`], or calling [`Vec::set_len`] to "kill" some elements
|
2019-06-15 16:56:42 -05:00
|
|
|
//! off of a vector. It can be repurposed by using [`ptr::write`] to overwrite it without
|
2020-04-27 07:45:37 -05:00
|
|
|
//! calling the destructor first. None of this is allowed for pinned data without calling [`drop`].
|
2019-02-19 06:08:46 -06:00
|
|
|
//!
|
2019-02-19 13:17:20 -06:00
|
|
|
//! This is exactly the kind of guarantee that the intrusive linked list from the previous
|
2019-02-19 14:23:53 -06:00
|
|
|
//! section needs to function correctly.
|
2019-02-19 06:08:46 -06:00
|
|
|
//!
|
|
|
|
//! Notice that this guarantee does *not* mean that memory does not leak! It is still
|
2019-07-04 11:46:48 -05:00
|
|
|
//! completely okay not ever to call [`drop`] on a pinned element (e.g., you can still
|
|
|
|
//! call [`mem::forget`] on a [`Pin`]`<`[`Box`]`<T>>`). In the example of the doubly-linked
|
2019-02-19 13:17:20 -06:00
|
|
|
//! list, that element would just stay in the list. However you may not free or reuse the storage
|
2019-07-04 11:46:48 -05:00
|
|
|
//! *without calling [`drop`]*.
|
2019-02-19 06:08:46 -06:00
|
|
|
//!
|
|
|
|
//! # `Drop` implementation
|
|
|
|
//!
|
2019-02-19 13:17:20 -06:00
|
|
|
//! If your type uses pinning (such as the two examples above), you have to be careful
|
2019-07-04 11:46:48 -05:00
|
|
|
//! when implementing [`Drop`]. The [`drop`] function takes `&mut self`, but this
|
2019-02-19 12:50:43 -06:00
|
|
|
//! is called *even if your type was previously pinned*! It is as if the
|
2019-07-04 11:46:48 -05:00
|
|
|
//! compiler automatically called [`Pin::get_unchecked_mut`].
|
2019-02-19 13:17:20 -06:00
|
|
|
//!
|
2019-04-11 21:21:19 -05:00
|
|
|
//! This can never cause a problem in safe code because implementing a type that
|
|
|
|
//! relies on pinning requires unsafe code, but be aware that deciding to make
|
|
|
|
//! use of pinning in your type (for example by implementing some operation on
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`Pin`]`<&Self>` or [`Pin`]`<&mut Self>`) has consequences for your [`Drop`]
|
2019-04-11 21:21:19 -05:00
|
|
|
//! implementation as well: if an element of your type could have been pinned,
|
2019-07-04 11:46:48 -05:00
|
|
|
//! you must treat [`Drop`] as implicitly taking [`Pin`]`<&mut Self>`.
|
2019-02-19 06:08:46 -06:00
|
|
|
//!
|
2019-06-15 16:51:42 -05:00
|
|
|
//! For example, you could implement `Drop` as follows:
|
2019-07-04 11:46:48 -05:00
|
|
|
//!
|
2019-06-16 03:19:22 -05:00
|
|
|
//! ```rust,no_run
|
|
|
|
//! # use std::pin::Pin;
|
|
|
|
//! # struct Type { }
|
2019-06-15 16:51:42 -05:00
|
|
|
//! impl Drop for Type {
|
|
|
|
//! fn drop(&mut self) {
|
|
|
|
//! // `new_unchecked` is okay because we know this value is never used
|
|
|
|
//! // again after being dropped.
|
|
|
|
//! inner_drop(unsafe { Pin::new_unchecked(self)});
|
|
|
|
//! fn inner_drop(this: Pin<&mut Type>) {
|
|
|
|
//! // Actual drop code goes here.
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
2019-07-04 11:46:48 -05:00
|
|
|
//!
|
|
|
|
//! The function `inner_drop` has the type that [`drop`] *should* have, so this makes sure that
|
2019-06-15 16:51:42 -05:00
|
|
|
//! you do not accidentally use `self`/`this` in a way that is in conflict with pinning.
|
|
|
|
//!
|
|
|
|
//! Moreover, if your type is `#[repr(packed)]`, the compiler will automatically
|
2019-08-08 14:02:11 -05:00
|
|
|
//! move fields around to be able to drop them. It might even do
|
2019-08-08 03:01:41 -05:00
|
|
|
//! that for fields that happen to be sufficiently aligned. As a consequence, you cannot use
|
2019-02-19 13:26:42 -06:00
|
|
|
//! pinning with a `#[repr(packed)]` type.
|
|
|
|
//!
|
2019-02-19 06:08:46 -06:00
|
|
|
//! # Projections and Structural Pinning
|
|
|
|
//!
|
2019-06-15 16:51:42 -05:00
|
|
|
//! When working with pinned structs, the question arises how one can access the
|
2019-07-04 11:46:48 -05:00
|
|
|
//! fields of that struct in a method that takes just [`Pin`]`<&mut Struct>`.
|
2019-06-15 16:51:42 -05:00
|
|
|
//! The usual approach is to write helper methods (so called *projections*)
|
2019-07-04 11:46:48 -05:00
|
|
|
//! that turn [`Pin`]`<&mut Struct>` into a reference to the field, but what
|
|
|
|
//! type should that reference have? Is it [`Pin`]`<&mut Field>` or `&mut Field`?
|
2019-06-19 08:02:50 -05:00
|
|
|
//! The same question arises with the fields of an `enum`, and also when considering
|
2019-06-15 16:56:42 -05:00
|
|
|
//! container/wrapper types such as [`Vec<T>`], [`Box<T>`], or [`RefCell<T>`].
|
2019-06-15 17:05:17 -05:00
|
|
|
//! (This question applies to both mutable and shared references, we just
|
|
|
|
//! use the more common case of mutable references here for illustration.)
|
2019-06-15 16:51:42 -05:00
|
|
|
//!
|
|
|
|
//! It turns out that it is actually up to the author of the data structure
|
|
|
|
//! to decide whether the pinned projection for a particular field turns
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`Pin`]`<&mut Struct>` into [`Pin`]`<&mut Field>` or `&mut Field`. There are some
|
2019-06-15 16:51:42 -05:00
|
|
|
//! constraints though, and the most important constraint is *consistency*:
|
|
|
|
//! every field can be *either* projected to a pinned reference, *or* have
|
|
|
|
//! pinning removed as part of the projection. If both are done for the same field,
|
|
|
|
//! that will likely be unsound!
|
|
|
|
//!
|
2019-06-19 08:02:50 -05:00
|
|
|
//! As the author of a data structure you get to decide for each field whether pinning
|
2019-06-15 16:51:42 -05:00
|
|
|
//! "propagates" to this field or not. Pinning that propagates is also called "structural",
|
|
|
|
//! because it follows the structure of the type.
|
2019-06-19 08:11:54 -05:00
|
|
|
//! In the following subsections, we describe the considerations that have to be made
|
|
|
|
//! for either choice.
|
2019-06-15 16:51:42 -05:00
|
|
|
//!
|
|
|
|
//! ## Pinning *is not* structural for `field`
|
|
|
|
//!
|
2019-06-15 17:05:17 -05:00
|
|
|
//! It may seem counter-intuitive that the field of a pinned struct might not be pinned,
|
2019-07-04 11:46:48 -05:00
|
|
|
//! but that is actually the easiest choice: if a [`Pin`]`<&mut Field>` is never created,
|
2019-06-15 16:51:42 -05:00
|
|
|
//! nothing can go wrong! So, if you decide that some field does not have structural pinning,
|
|
|
|
//! all you have to ensure is that you never create a pinned reference to that field.
|
|
|
|
//!
|
2019-06-19 08:11:54 -05:00
|
|
|
//! Fields without structural pinning may have a projection method that turns
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`Pin`]`<&mut Struct>` into `&mut Field`:
|
|
|
|
//!
|
2019-06-16 03:19:22 -05:00
|
|
|
//! ```rust,no_run
|
|
|
|
//! # use std::pin::Pin;
|
|
|
|
//! # type Field = i32;
|
|
|
|
//! # struct Struct { field: Field }
|
2019-06-15 16:51:42 -05:00
|
|
|
//! impl Struct {
|
2019-09-16 18:39:34 -05:00
|
|
|
//! fn pin_get_field(self: Pin<&mut Self>) -> &mut Field {
|
2019-06-15 16:51:42 -05:00
|
|
|
//! // This is okay because `field` is never considered pinned.
|
|
|
|
//! unsafe { &mut self.get_unchecked_mut().field }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
2019-04-13 15:54:57 -05:00
|
|
|
//!
|
2019-06-15 17:05:17 -05:00
|
|
|
//! You may also `impl Unpin for Struct` *even if* the type of `field`
|
2019-07-04 11:46:48 -05:00
|
|
|
//! is not [`Unpin`]. What that type thinks about pinning is not relevant
|
|
|
|
//! when no [`Pin`]`<&mut Field>` is ever created.
|
2019-02-19 06:08:46 -06:00
|
|
|
//!
|
2019-06-15 16:51:42 -05:00
|
|
|
//! ## Pinning *is* structural for `field`
|
2019-02-21 02:57:29 -06:00
|
|
|
//!
|
2019-06-15 16:51:42 -05:00
|
|
|
//! The other option is to decide that pinning is "structural" for `field`,
|
|
|
|
//! meaning that if the struct is pinned then so is the field.
|
2019-02-19 06:08:46 -06:00
|
|
|
//!
|
2019-07-04 11:46:48 -05:00
|
|
|
//! This allows writing a projection that creates a [`Pin`]`<&mut Field>`, thus
|
2019-06-15 16:51:42 -05:00
|
|
|
//! witnessing that the field is pinned:
|
2019-07-04 11:46:48 -05:00
|
|
|
//!
|
2019-06-16 03:19:22 -05:00
|
|
|
//! ```rust,no_run
|
|
|
|
//! # use std::pin::Pin;
|
|
|
|
//! # type Field = i32;
|
|
|
|
//! # struct Struct { field: Field }
|
2019-06-15 16:51:42 -05:00
|
|
|
//! impl Struct {
|
2019-09-16 18:39:34 -05:00
|
|
|
//! fn pin_get_field(self: Pin<&mut Self>) -> Pin<&mut Field> {
|
2019-06-15 16:51:42 -05:00
|
|
|
//! // This is okay because `field` is pinned when `self` is.
|
|
|
|
//! unsafe { self.map_unchecked_mut(|s| &mut s.field) }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! However, structural pinning comes with a few extra requirements:
|
|
|
|
//!
|
|
|
|
//! 1. The struct must only be [`Unpin`] if all the structural fields are
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`Unpin`]. This is the default, but [`Unpin`] is a safe trait, so as the author of
|
2019-06-15 17:05:17 -05:00
|
|
|
//! the struct it is your responsibility *not* to add something like
|
2019-06-15 16:51:42 -05:00
|
|
|
//! `impl<T> Unpin for Struct<T>`. (Notice that adding a projection operation
|
2019-07-04 11:46:48 -05:00
|
|
|
//! requires unsafe code, so the fact that [`Unpin`] is a safe trait does not break
|
2019-02-20 12:34:10 -06:00
|
|
|
//! the principle that you only have to worry about any of this if you use `unsafe`.)
|
2019-06-15 16:51:42 -05:00
|
|
|
//! 2. The destructor of the struct must not move structural fields out of its argument. This
|
2019-02-21 02:57:29 -06:00
|
|
|
//! is the exact point that was raised in the [previous section][drop-impl]: `drop` takes
|
2019-06-15 16:51:42 -05:00
|
|
|
//! `&mut self`, but the struct (and hence its fields) might have been pinned before.
|
2019-07-04 11:46:48 -05:00
|
|
|
//! You have to guarantee that you do not move a field inside your [`Drop`] implementation.
|
2019-06-15 16:51:42 -05:00
|
|
|
//! In particular, as explained previously, this means that your struct must *not*
|
2019-02-20 12:34:10 -06:00
|
|
|
//! be `#[repr(packed)]`.
|
2019-07-04 11:46:48 -05:00
|
|
|
//! See that section for how to write [`drop`] in a way that the compiler can help you
|
2019-06-15 16:51:42 -05:00
|
|
|
//! not accidentally break pinning.
|
2019-02-20 12:34:10 -06:00
|
|
|
//! 3. You must make sure that you uphold the [`Drop` guarantee][drop-guarantee]:
|
2019-06-15 16:51:42 -05:00
|
|
|
//! once your struct is pinned, the memory that contains the
|
2019-02-20 12:34:10 -06:00
|
|
|
//! content is not overwritten or deallocated without calling the content's destructors.
|
2019-07-04 11:46:48 -05:00
|
|
|
//! This can be tricky, as witnessed by [`VecDeque<T>`]: the destructor of [`VecDeque<T>`]
|
|
|
|
//! can fail to call [`drop`] on all elements if one of the destructors panics. This violates
|
|
|
|
//! the [`Drop`] guarantee, because it can lead to elements being deallocated without
|
|
|
|
//! their destructor being called. ([`VecDeque<T>`] has no pinning projections, so this
|
2019-02-20 12:34:10 -06:00
|
|
|
//! does not cause unsoundness.)
|
|
|
|
//! 4. You must not offer any other operations that could lead to data being moved out of
|
2019-06-15 16:51:42 -05:00
|
|
|
//! the structural fields when your type is pinned. For example, if the struct contains an
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`Option<T>`] and there is a `take`-like operation with type
|
2019-06-15 16:51:42 -05:00
|
|
|
//! `fn(Pin<&mut Struct<T>>) -> Option<T>`,
|
|
|
|
//! that operation can be used to move a `T` out of a pinned `Struct<T>` -- which means
|
|
|
|
//! pinning cannot be structural for the field holding this data.
|
2019-02-20 12:34:10 -06:00
|
|
|
//!
|
2019-06-15 16:56:42 -05:00
|
|
|
//! For a more complex example of moving data out of a pinned type, imagine if [`RefCell<T>`]
|
2019-02-20 12:34:10 -06:00
|
|
|
//! had a method `fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T>`.
|
|
|
|
//! Then we could do the following:
|
|
|
|
//! ```compile_fail
|
2019-02-28 15:34:03 -06:00
|
|
|
//! fn exploit_ref_cell<T>(rc: Pin<&mut RefCell<T>>) {
|
2019-02-21 08:28:46 -06:00
|
|
|
//! { let p = rc.as_mut().get_pin_mut(); } // Here we get pinned access to the `T`.
|
2019-02-20 12:34:10 -06:00
|
|
|
//! let rc_shr: &RefCell<T> = rc.into_ref().get_ref();
|
|
|
|
//! let b = rc_shr.borrow_mut();
|
2019-02-21 08:28:46 -06:00
|
|
|
//! let content = &mut *b; // And here we have `&mut T` to the same data.
|
2019-02-20 12:34:10 -06:00
|
|
|
//! }
|
|
|
|
//! ```
|
2019-07-04 11:46:48 -05:00
|
|
|
//! This is catastrophic, it means we can first pin the content of the [`RefCell<T>`]
|
2019-02-20 12:34:10 -06:00
|
|
|
//! (using `RefCell::get_pin_mut`) and then move that content using the mutable
|
|
|
|
//! reference we got later.
|
2019-02-19 06:08:46 -06:00
|
|
|
//!
|
2019-06-15 16:51:42 -05:00
|
|
|
//! ## Examples
|
|
|
|
//!
|
2020-03-06 05:13:55 -06:00
|
|
|
//! For a type like [`Vec<T>`], both possibilities (structural pinning or not) make sense.
|
2019-07-04 11:46:48 -05:00
|
|
|
//! A [`Vec<T>`] with structural pinning could have `get_pin`/`get_pin_mut` methods to get
|
2019-06-15 16:51:42 -05:00
|
|
|
//! pinned references to elements. However, it could *not* allow calling
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`pop`][Vec::pop] on a pinned [`Vec<T>`] because that would move the (structurally pinned)
|
|
|
|
//! contents! Nor could it allow [`push`][Vec::push], which might reallocate and thus also move the
|
|
|
|
//! contents.
|
|
|
|
//!
|
|
|
|
//! A [`Vec<T>`] without structural pinning could `impl<T> Unpin for Vec<T>`, because the contents
|
|
|
|
//! are never pinned and the [`Vec<T>`] itself is fine with being moved as well.
|
2019-06-15 16:51:42 -05:00
|
|
|
//! At that point pinning just has no effect on the vector at all.
|
2019-02-21 03:21:59 -06:00
|
|
|
//!
|
|
|
|
//! In the standard library, pointer types generally do not have structural pinning,
|
|
|
|
//! and thus they do not offer pinning projections. This is why `Box<T>: Unpin` holds for all `T`.
|
2019-02-19 12:50:43 -06:00
|
|
|
//! It makes sense to do this for pointer types, because moving the `Box<T>`
|
2019-07-04 11:46:48 -05:00
|
|
|
//! does not actually move the `T`: the [`Box<T>`] can be freely movable (aka `Unpin`) even if
|
|
|
|
//! the `T` is not. In fact, even [`Pin`]`<`[`Box`]`<T>>` and [`Pin`]`<&mut T>` are always
|
|
|
|
//! [`Unpin`] themselves, for the same reason: their contents (the `T`) are pinned, but the
|
|
|
|
//! pointers themselves can be moved without moving the pinned data. For both [`Box<T>`] and
|
|
|
|
//! [`Pin`]`<`[`Box`]`<T>>`, whether the content is pinned is entirely independent of whether the
|
|
|
|
//! pointer is pinned, meaning pinning is *not* structural.
|
2019-02-20 11:28:12 -06:00
|
|
|
//!
|
2019-06-15 16:56:42 -05:00
|
|
|
//! When implementing a [`Future`] combinator, you will usually need structural pinning
|
2019-07-04 11:46:48 -05:00
|
|
|
//! for the nested futures, as you need to get pinned references to them to call [`poll`].
|
2019-06-15 16:51:42 -05:00
|
|
|
//! But if your combinator contains any other data that does not need to be pinned,
|
|
|
|
//! you can make those fields not structural and hence freely access them with a
|
2019-07-04 11:46:48 -05:00
|
|
|
//! mutable reference even when you just have [`Pin`]`<&mut Self>` (such as in your own
|
|
|
|
//! [`poll`] implementation).
|
2019-06-15 16:51:42 -05:00
|
|
|
//!
|
2020-08-22 15:15:17 -05:00
|
|
|
//! [`Pin<P>`]: Pin
|
|
|
|
//! [`Deref`]: crate::ops::Deref
|
|
|
|
//! [`DerefMut`]: crate::ops::DerefMut
|
|
|
|
//! [`mem::swap`]: crate::mem::swap
|
|
|
|
//! [`mem::forget`]: crate::mem::forget
|
2019-02-21 08:28:46 -06:00
|
|
|
//! [`Box<T>`]: ../../std/boxed/struct.Box.html
|
2019-06-15 16:56:42 -05:00
|
|
|
//! [`Vec<T>`]: ../../std/vec/struct.Vec.html
|
2019-02-21 08:28:46 -06:00
|
|
|
//! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`Box`]: ../../std/boxed/struct.Box.html
|
|
|
|
//! [Vec::pop]: ../../std/vec/struct.Vec.html#method.pop
|
|
|
|
//! [Vec::push]: ../../std/vec/struct.Vec.html#method.push
|
|
|
|
//! [`Rc`]: ../../std/rc/struct.Rc.html
|
2020-08-22 15:15:17 -05:00
|
|
|
//! [`RefCell<T>`]: crate::cell::RefCell
|
|
|
|
//! [`drop`]: Drop::drop
|
2019-07-04 11:46:48 -05:00
|
|
|
//! [`VecDeque<T>`]: ../../std/collections/struct.VecDeque.html
|
2020-08-22 15:15:17 -05:00
|
|
|
//! [`Option<T>`]: Option
|
|
|
|
//! [`Some(v)`]: Some
|
|
|
|
//! [`ptr::write`]: crate::ptr::write
|
|
|
|
//! [`Future`]: crate::future::Future
|
2019-02-19 06:08:46 -06:00
|
|
|
//! [drop-impl]: #drop-implementation
|
|
|
|
//! [drop-guarantee]: #drop-guarantee
|
2020-08-22 15:15:17 -05:00
|
|
|
//! [`poll`]: crate::future::Future::poll
|
2018-08-09 10:20:22 -05:00
|
|
|
|
2018-12-17 20:14:07 -06:00
|
|
|
#![stable(feature = "pin", since = "1.33.0")]
|
2018-08-09 10:20:22 -05:00
|
|
|
|
2019-11-24 03:43:32 -06:00
|
|
|
use crate::cmp::{self, PartialEq, PartialOrd};
|
2019-04-14 21:23:21 -05:00
|
|
|
use crate::fmt;
|
2019-12-04 17:01:03 -06:00
|
|
|
use crate::hash::{Hash, Hasher};
|
2019-04-14 21:23:21 -05:00
|
|
|
use crate::marker::{Sized, Unpin};
|
2019-11-24 03:43:32 -06:00
|
|
|
use crate::ops::{CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Receiver};
|
2018-08-09 10:20:22 -05:00
|
|
|
|
2018-08-31 23:12:10 -05:00
|
|
|
/// A pinned pointer.
|
2018-08-09 10:20:22 -05:00
|
|
|
///
|
2018-08-31 23:12:10 -05:00
|
|
|
/// This is a wrapper around a kind of pointer which makes that pointer "pin" its
|
|
|
|
/// value in place, preventing the value referenced by that pointer from being moved
|
|
|
|
/// unless it implements [`Unpin`].
|
2018-08-14 11:45:39 -05:00
|
|
|
///
|
2019-05-08 10:20:43 -05:00
|
|
|
/// *See the [`pin` module] documentation for an explanation of pinning.*
|
2018-08-14 11:45:39 -05:00
|
|
|
///
|
2020-08-22 15:15:17 -05:00
|
|
|
/// [`pin` module]: self
|
2018-09-14 19:40:52 -05:00
|
|
|
//
|
2019-12-04 17:01:03 -06:00
|
|
|
// Note: the `Clone` derive below causes unsoundness as it's possible to implement
|
|
|
|
// `Clone` for mutable references.
|
|
|
|
// See <https://internals.rust-lang.org/t/unsoundness-in-pin/11311> for more details.
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2019-02-27 17:58:12 -06:00
|
|
|
#[lang = "pin"]
|
2018-08-09 10:20:22 -05:00
|
|
|
#[fundamental]
|
2018-12-17 19:19:32 -06:00
|
|
|
#[repr(transparent)]
|
2019-12-04 17:01:03 -06:00
|
|
|
#[derive(Copy, Clone)]
|
2018-08-31 23:12:10 -05:00
|
|
|
pub struct Pin<P> {
|
|
|
|
pointer: P,
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
|
|
|
|
2019-12-05 03:28:11 -06:00
|
|
|
// The following implementations aren't derived in order to avoid soundness
|
|
|
|
// issues. `&self.pointer` should not be accessible to untrusted trait
|
|
|
|
// implementations.
|
|
|
|
//
|
|
|
|
// See <https://internals.rust-lang.org/t/unsoundness-in-pin/11311/73> for more details.
|
|
|
|
|
2019-12-04 17:01:03 -06:00
|
|
|
#[stable(feature = "pin_trait_impls", since = "1.41.0")]
|
|
|
|
impl<P: Deref, Q: Deref> PartialEq<Pin<Q>> for Pin<P>
|
2019-01-16 20:10:18 -06:00
|
|
|
where
|
2019-12-04 17:01:03 -06:00
|
|
|
P::Target: PartialEq<Q::Target>,
|
2019-01-16 20:10:18 -06:00
|
|
|
{
|
|
|
|
fn eq(&self, other: &Pin<Q>) -> bool {
|
2019-12-07 09:23:43 -06:00
|
|
|
P::Target::eq(self, other)
|
2019-01-16 20:10:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ne(&self, other: &Pin<Q>) -> bool {
|
2019-12-07 09:23:43 -06:00
|
|
|
P::Target::ne(self, other)
|
2019-01-16 20:10:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:01:03 -06:00
|
|
|
#[stable(feature = "pin_trait_impls", since = "1.41.0")]
|
|
|
|
impl<P: Deref<Target: Eq>> Eq for Pin<P> {}
|
|
|
|
|
|
|
|
#[stable(feature = "pin_trait_impls", since = "1.41.0")]
|
|
|
|
impl<P: Deref, Q: Deref> PartialOrd<Pin<Q>> for Pin<P>
|
2019-01-16 20:10:18 -06:00
|
|
|
where
|
2019-12-04 17:01:03 -06:00
|
|
|
P::Target: PartialOrd<Q::Target>,
|
2019-01-16 20:10:18 -06:00
|
|
|
{
|
|
|
|
fn partial_cmp(&self, other: &Pin<Q>) -> Option<cmp::Ordering> {
|
2019-12-07 09:23:43 -06:00
|
|
|
P::Target::partial_cmp(self, other)
|
2019-01-16 20:10:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lt(&self, other: &Pin<Q>) -> bool {
|
2019-12-07 09:23:43 -06:00
|
|
|
P::Target::lt(self, other)
|
2019-01-16 20:10:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn le(&self, other: &Pin<Q>) -> bool {
|
2019-12-07 09:23:43 -06:00
|
|
|
P::Target::le(self, other)
|
2019-01-16 20:10:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn gt(&self, other: &Pin<Q>) -> bool {
|
2019-12-07 09:23:43 -06:00
|
|
|
P::Target::gt(self, other)
|
2019-01-16 20:10:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ge(&self, other: &Pin<Q>) -> bool {
|
2019-12-07 09:23:43 -06:00
|
|
|
P::Target::ge(self, other)
|
2019-12-04 17:01:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "pin_trait_impls", since = "1.41.0")]
|
|
|
|
impl<P: Deref<Target: Ord>> Ord for Pin<P> {
|
|
|
|
fn cmp(&self, other: &Self) -> cmp::Ordering {
|
2019-12-07 09:23:43 -06:00
|
|
|
P::Target::cmp(self, other)
|
2019-12-04 17:01:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "pin_trait_impls", since = "1.41.0")]
|
|
|
|
impl<P: Deref<Target: Hash>> Hash for Pin<P> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
2019-12-07 09:23:43 -06:00
|
|
|
P::Target::hash(self, state);
|
2019-01-16 20:10:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 14:00:35 -05:00
|
|
|
impl<P: Deref<Target: Unpin>> Pin<P> {
|
2019-02-21 08:28:46 -06:00
|
|
|
/// Construct a new `Pin<P>` around a pointer to some data of a type that
|
2019-02-19 06:08:46 -06:00
|
|
|
/// implements [`Unpin`].
|
|
|
|
///
|
|
|
|
/// Unlike `Pin::new_unchecked`, this method is safe because the pointer
|
2019-02-21 08:28:46 -06:00
|
|
|
/// `P` dereferences to an [`Unpin`] type, which cancels the pinning guarantees.
|
2018-09-14 19:40:52 -05:00
|
|
|
#[inline(always)]
|
2020-09-12 18:55:34 -05:00
|
|
|
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
|
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
|
|
|
pub const fn new(pointer: P) -> Pin<P> {
|
2020-09-08 21:26:44 -05:00
|
|
|
// SAFETY: the value pointed to is `Unpin`, and so has no requirements
|
2018-09-14 19:40:52 -05:00
|
|
|
// around pinning.
|
2018-08-31 23:12:10 -05:00
|
|
|
unsafe { Pin::new_unchecked(pointer) }
|
|
|
|
}
|
2019-04-21 06:27:36 -05:00
|
|
|
|
|
|
|
/// Unwraps this `Pin<P>` returning the underlying pointer.
|
|
|
|
///
|
|
|
|
/// This requires that the data inside this `Pin` is [`Unpin`] so that we
|
|
|
|
/// can ignore the pinning invariants when unwrapping it.
|
|
|
|
#[inline(always)]
|
2020-09-12 18:55:34 -05:00
|
|
|
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
|
|
|
|
#[stable(feature = "pin_into_inner", since = "1.39.0")]
|
|
|
|
pub const fn into_inner(pin: Pin<P>) -> P {
|
2019-04-21 06:27:36 -05:00
|
|
|
pin.pointer
|
|
|
|
}
|
2018-08-31 23:12:10 -05:00
|
|
|
}
|
|
|
|
|
2018-09-14 19:40:52 -05:00
|
|
|
impl<P: Deref> Pin<P> {
|
2019-02-21 08:28:46 -06:00
|
|
|
/// Construct a new `Pin<P>` around a reference to some data of a type that
|
2018-08-31 23:12:10 -05:00
|
|
|
/// may or may not implement `Unpin`.
|
|
|
|
///
|
2019-02-19 14:12:48 -06:00
|
|
|
/// If `pointer` dereferences to an `Unpin` type, `Pin::new` should be used
|
|
|
|
/// instead.
|
|
|
|
///
|
2018-08-31 23:12:10 -05:00
|
|
|
/// # Safety
|
|
|
|
///
|
2018-09-14 19:40:52 -05:00
|
|
|
/// This constructor is unsafe because we cannot guarantee that the data
|
2019-02-19 13:50:16 -06:00
|
|
|
/// pointed to by `pointer` is pinned, meaning that the data will not be moved or
|
|
|
|
/// its storage invalidated until it gets dropped. If the constructed `Pin<P>` does
|
2019-02-21 08:33:55 -06:00
|
|
|
/// not guarantee that the data `P` points to is pinned, that is a violation of
|
|
|
|
/// the API contract and may lead to undefined behavior in later (safe) operations.
|
2018-09-14 19:40:52 -05:00
|
|
|
///
|
2019-02-19 06:08:46 -06:00
|
|
|
/// By using this method, you are making a promise about the `P::Deref` and
|
|
|
|
/// `P::DerefMut` implementations, if they exist. Most importantly, they
|
|
|
|
/// must not move out of their `self` arguments: `Pin::as_mut` and `Pin::as_ref`
|
|
|
|
/// will call `DerefMut::deref_mut` and `Deref::deref` *on the pinned pointer*
|
|
|
|
/// and expect these methods to uphold the pinning invariants.
|
|
|
|
/// Moreover, by calling this method you promise that the reference `P`
|
|
|
|
/// dereferences to will not be moved out of again; in particular, it
|
|
|
|
/// must not be possible to obtain a `&mut P::Target` and then
|
2019-02-19 12:46:33 -06:00
|
|
|
/// move out of that reference (using, for example [`mem::swap`]).
|
2019-02-19 06:08:46 -06:00
|
|
|
///
|
2019-02-20 02:45:28 -06:00
|
|
|
/// For example, calling `Pin::new_unchecked` on an `&'a mut T` is unsafe because
|
|
|
|
/// while you are able to pin it for the given lifetime `'a`, you have no control
|
|
|
|
/// over whether it is kept pinned once `'a` ends:
|
2019-02-19 06:08:46 -06:00
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
/// use std::pin::Pin;
|
|
|
|
///
|
2019-02-19 14:12:48 -06:00
|
|
|
/// fn move_pinned_ref<T>(mut a: T, mut b: T) {
|
2019-02-21 16:13:49 -06:00
|
|
|
/// unsafe {
|
|
|
|
/// let p: Pin<&mut T> = Pin::new_unchecked(&mut a);
|
|
|
|
/// // This should mean the pointee `a` can never move again.
|
|
|
|
/// }
|
2019-02-19 12:46:33 -06:00
|
|
|
/// mem::swap(&mut a, &mut b);
|
2019-02-21 08:28:46 -06:00
|
|
|
/// // The address of `a` changed to `b`'s stack slot, so `a` got moved even
|
2019-02-21 16:13:49 -06:00
|
|
|
/// // though we have previously pinned it! We have violated the pinning API contract.
|
2019-02-19 06:08:46 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-02-19 14:12:48 -06:00
|
|
|
/// A value, once pinned, must remain pinned forever (unless its type implements `Unpin`).
|
2019-02-19 06:08:46 -06:00
|
|
|
///
|
2020-03-06 05:13:55 -06:00
|
|
|
/// Similarly, calling `Pin::new_unchecked` on an `Rc<T>` is unsafe because there could be
|
2019-02-19 14:12:48 -06:00
|
|
|
/// aliases to the same data that are not subject to the pinning restrictions:
|
|
|
|
/// ```
|
|
|
|
/// use std::rc::Rc;
|
|
|
|
/// use std::pin::Pin;
|
|
|
|
///
|
|
|
|
/// fn move_pinned_rc<T>(mut x: Rc<T>) {
|
2020-08-30 15:14:17 -05:00
|
|
|
/// let pinned = unsafe { Pin::new_unchecked(Rc::clone(&x)) };
|
2019-02-21 16:13:49 -06:00
|
|
|
/// {
|
|
|
|
/// let p: Pin<&T> = pinned.as_ref();
|
|
|
|
/// // This should mean the pointee can never move again.
|
|
|
|
/// }
|
2019-02-19 14:12:48 -06:00
|
|
|
/// drop(pinned);
|
|
|
|
/// let content = Rc::get_mut(&mut x).unwrap();
|
|
|
|
/// // Now, if `x` was the only reference, we have a mutable reference to
|
|
|
|
/// // data that we pinned above, which we could use to move it as we have
|
2019-02-21 16:13:49 -06:00
|
|
|
/// // seen in the previous example. We have violated the pinning API contract.
|
2019-02-19 14:12:48 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-02-19 06:08:46 -06:00
|
|
|
///
|
2020-08-22 15:15:17 -05:00
|
|
|
/// [`mem::swap`]: crate::mem::swap
|
2020-08-26 03:17:31 -05:00
|
|
|
#[lang = "new_unchecked"]
|
2018-09-14 19:40:52 -05:00
|
|
|
#[inline(always)]
|
2020-09-12 18:55:34 -05:00
|
|
|
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
|
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
|
|
|
pub const unsafe fn new_unchecked(pointer: P) -> Pin<P> {
|
2018-08-31 23:12:10 -05:00
|
|
|
Pin { pointer }
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
|
|
|
|
2019-02-09 16:16:58 -06:00
|
|
|
/// Gets a pinned shared reference from this pinned pointer.
|
2019-02-19 06:08:46 -06:00
|
|
|
///
|
2019-02-20 02:45:28 -06:00
|
|
|
/// This is a generic method to go from `&Pin<Pointer<T>>` to `Pin<&T>`.
|
2019-02-19 06:08:46 -06:00
|
|
|
/// It is safe because, as part of the contract of `Pin::new_unchecked`,
|
2019-02-20 02:45:28 -06:00
|
|
|
/// the pointee cannot move after `Pin<Pointer<T>>` got created.
|
|
|
|
/// "Malicious" implementations of `Pointer::Deref` are likewise
|
2019-02-19 06:08:46 -06:00
|
|
|
/// ruled out by the contract of `Pin::new_unchecked`.
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2018-09-14 19:40:52 -05:00
|
|
|
#[inline(always)]
|
2019-09-16 18:54:30 -05:00
|
|
|
pub fn as_ref(&self) -> Pin<&P::Target> {
|
2019-08-21 12:56:46 -05:00
|
|
|
// SAFETY: see documentation on this function
|
2018-09-18 13:48:03 -05:00
|
|
|
unsafe { Pin::new_unchecked(&*self.pointer) }
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
2019-04-21 06:27:36 -05:00
|
|
|
|
|
|
|
/// Unwraps this `Pin<P>` returning the underlying pointer.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// This function is unsafe. You must guarantee that you will continue to
|
|
|
|
/// treat the pointer `P` as pinned after you call this function, so that
|
|
|
|
/// the invariants on the `Pin` type can be upheld. If the code using the
|
|
|
|
/// resulting `P` does not continue to maintain the pinning invariants that
|
|
|
|
/// is a violation of the API contract and may lead to undefined behavior in
|
|
|
|
/// later (safe) operations.
|
|
|
|
///
|
|
|
|
/// If the underlying data is [`Unpin`], [`Pin::into_inner`] should be used
|
|
|
|
/// instead.
|
|
|
|
#[inline(always)]
|
2020-09-12 18:55:34 -05:00
|
|
|
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
|
|
|
|
#[stable(feature = "pin_into_inner", since = "1.39.0")]
|
|
|
|
pub const unsafe fn into_inner_unchecked(pin: Pin<P>) -> P {
|
2019-04-21 06:27:36 -05:00
|
|
|
pin.pointer
|
|
|
|
}
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
|
|
|
|
2018-09-14 19:40:52 -05:00
|
|
|
impl<P: DerefMut> Pin<P> {
|
2019-02-09 16:16:58 -06:00
|
|
|
/// Gets a pinned mutable reference from this pinned pointer.
|
2019-02-19 06:08:46 -06:00
|
|
|
///
|
2019-02-20 02:45:28 -06:00
|
|
|
/// This is a generic method to go from `&mut Pin<Pointer<T>>` to `Pin<&mut T>`.
|
2019-02-19 06:08:46 -06:00
|
|
|
/// It is safe because, as part of the contract of `Pin::new_unchecked`,
|
2019-02-20 02:45:28 -06:00
|
|
|
/// the pointee cannot move after `Pin<Pointer<T>>` got created.
|
|
|
|
/// "Malicious" implementations of `Pointer::DerefMut` are likewise
|
2019-02-19 06:08:46 -06:00
|
|
|
/// ruled out by the contract of `Pin::new_unchecked`.
|
2019-09-17 05:41:12 -05:00
|
|
|
///
|
|
|
|
/// This method is useful when doing multiple calls to functions that consume the pinned type.
|
|
|
|
///
|
2019-09-17 06:02:48 -05:00
|
|
|
/// # Example
|
2019-09-17 05:41:12 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::pin::Pin;
|
|
|
|
///
|
|
|
|
/// # struct Type {}
|
|
|
|
/// impl Type {
|
|
|
|
/// fn method(self: Pin<&mut Self>) {
|
|
|
|
/// // do something
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn call_method_twice(mut self: Pin<&mut Self>) {
|
|
|
|
/// // `method` consumes `self`, so reborrow the `Pin<&mut Self>` via `as_mut`.
|
|
|
|
/// self.as_mut().method();
|
|
|
|
/// self.as_mut().method();
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2018-09-14 19:40:52 -05:00
|
|
|
#[inline(always)]
|
2019-09-16 18:54:30 -05:00
|
|
|
pub fn as_mut(&mut self) -> Pin<&mut P::Target> {
|
2019-08-21 12:56:46 -05:00
|
|
|
// SAFETY: see documentation on this function
|
2018-09-14 19:40:52 -05:00
|
|
|
unsafe { Pin::new_unchecked(&mut *self.pointer) }
|
2018-08-31 23:12:10 -05:00
|
|
|
}
|
2018-08-09 10:20:22 -05:00
|
|
|
|
2019-02-19 06:08:46 -06:00
|
|
|
/// Assigns a new value to the memory behind the pinned reference.
|
|
|
|
///
|
|
|
|
/// This overwrites pinned data, but that is okay: its destructor gets
|
|
|
|
/// run before being overwritten, so no pinning guarantee is violated.
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2018-09-14 19:40:52 -05:00
|
|
|
#[inline(always)]
|
2019-09-16 18:54:30 -05:00
|
|
|
pub fn set(&mut self, value: P::Target)
|
2018-09-14 19:40:52 -05:00
|
|
|
where
|
|
|
|
P::Target: Sized,
|
2018-08-31 23:12:10 -05:00
|
|
|
{
|
2019-01-07 13:45:34 -06:00
|
|
|
*(self.pointer) = value;
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
2018-08-31 23:12:10 -05:00
|
|
|
}
|
2018-08-09 10:20:22 -05:00
|
|
|
|
2018-09-14 19:40:52 -05:00
|
|
|
impl<'a, T: ?Sized> Pin<&'a T> {
|
2019-02-19 06:08:46 -06:00
|
|
|
/// Constructs a new pin by mapping the interior value.
|
2018-08-31 23:12:10 -05:00
|
|
|
///
|
|
|
|
/// For example, if you wanted to get a `Pin` of a field of something,
|
|
|
|
/// you could use this to get access to that field in one line of code.
|
2019-02-19 06:08:46 -06:00
|
|
|
/// However, there are several gotchas with these "pinning projections";
|
|
|
|
/// see the [`pin` module] documentation for further details on that topic.
|
2018-08-09 10:20:22 -05:00
|
|
|
///
|
2018-08-31 23:12:10 -05:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// This function is unsafe. You must guarantee that the data you return
|
|
|
|
/// will not move so long as the argument value does not move (for example,
|
|
|
|
/// because it is one of the fields of that value), and also that you do
|
|
|
|
/// not move out of the argument you receive to the interior function.
|
2019-02-19 06:08:46 -06:00
|
|
|
///
|
2020-09-01 22:39:16 -05:00
|
|
|
/// [`pin` module]: self#projections-and-structural-pinning
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2019-11-24 03:43:32 -06:00
|
|
|
pub unsafe fn map_unchecked<U, F>(self, func: F) -> Pin<&'a U>
|
|
|
|
where
|
2020-01-06 08:41:09 -06:00
|
|
|
U: ?Sized,
|
2018-08-31 23:12:10 -05:00
|
|
|
F: FnOnce(&T) -> &U,
|
|
|
|
{
|
2018-12-17 19:19:32 -06:00
|
|
|
let pointer = &*self.pointer;
|
2018-08-31 23:12:10 -05:00
|
|
|
let new_pointer = func(pointer);
|
2020-06-21 17:54:46 -05:00
|
|
|
|
|
|
|
// SAFETY: the safety contract for `new_unchecked` must be
|
|
|
|
// upheld by the caller.
|
|
|
|
unsafe { Pin::new_unchecked(new_pointer) }
|
2018-08-31 23:12:10 -05:00
|
|
|
}
|
|
|
|
|
2019-02-09 16:16:58 -06:00
|
|
|
/// Gets a shared reference out of a pin.
|
2018-09-14 19:40:52 -05:00
|
|
|
///
|
2019-02-19 06:08:46 -06:00
|
|
|
/// This is safe because it is not possible to move out of a shared reference.
|
|
|
|
/// It may seem like there is an issue here with interior mutability: in fact,
|
|
|
|
/// it *is* possible to move a `T` out of a `&RefCell<T>`. However, this is
|
|
|
|
/// not a problem as long as there does not also exist a `Pin<&T>` pointing
|
2019-02-21 08:28:46 -06:00
|
|
|
/// to the same data, and `RefCell<T>` does not let you create a pinned reference
|
2019-02-19 06:08:46 -06:00
|
|
|
/// to its contents. See the discussion on ["pinning projections"] for further
|
|
|
|
/// details.
|
|
|
|
///
|
2018-09-14 19:40:52 -05:00
|
|
|
/// Note: `Pin` also implements `Deref` to the target, which can be used
|
|
|
|
/// to access the inner value. However, `Deref` only provides a reference
|
|
|
|
/// that lives for as long as the borrow of the `Pin`, not the lifetime of
|
|
|
|
/// the `Pin` itself. This method allows turning the `Pin` into a reference
|
|
|
|
/// with the same lifetime as the original `Pin`.
|
2019-02-19 06:08:46 -06:00
|
|
|
///
|
2020-09-01 22:39:16 -05:00
|
|
|
/// ["pinning projections"]: self#projections-and-structural-pinning
|
2018-09-14 19:40:52 -05:00
|
|
|
#[inline(always)]
|
2020-09-12 18:55:34 -05:00
|
|
|
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
|
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
|
|
|
pub const fn get_ref(self) -> &'a T {
|
2018-12-17 19:19:32 -06:00
|
|
|
self.pointer
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
2018-08-31 23:12:10 -05:00
|
|
|
}
|
2018-08-09 10:20:22 -05:00
|
|
|
|
2018-09-18 13:48:03 -05:00
|
|
|
impl<'a, T: ?Sized> Pin<&'a mut T> {
|
2019-02-09 16:16:58 -06:00
|
|
|
/// Converts this `Pin<&mut T>` into a `Pin<&T>` with the same lifetime.
|
2018-09-14 19:40:52 -05:00
|
|
|
#[inline(always)]
|
2020-09-18 12:23:50 -05:00
|
|
|
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
|
2020-09-12 18:55:34 -05:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2020-09-18 12:23:50 -05:00
|
|
|
pub const fn into_ref(self) -> Pin<&'a T> {
|
2018-12-17 19:19:32 -06:00
|
|
|
Pin { pointer: self.pointer }
|
2018-09-14 19:40:52 -05:00
|
|
|
}
|
|
|
|
|
2019-02-09 16:16:58 -06:00
|
|
|
/// Gets a mutable reference to the data inside of this `Pin`.
|
2018-09-14 19:40:52 -05:00
|
|
|
///
|
|
|
|
/// This requires that the data inside this `Pin` is `Unpin`.
|
|
|
|
///
|
|
|
|
/// Note: `Pin` also implements `DerefMut` to the data, which can be used
|
|
|
|
/// to access the inner value. However, `DerefMut` only provides a reference
|
|
|
|
/// that lives for as long as the borrow of the `Pin`, not the lifetime of
|
|
|
|
/// the `Pin` itself. This method allows turning the `Pin` into a reference
|
|
|
|
/// with the same lifetime as the original `Pin`.
|
|
|
|
#[inline(always)]
|
2020-09-18 12:23:50 -05:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
|
|
|
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
|
|
|
|
pub const fn get_mut(self) -> &'a mut T
|
2019-11-24 03:43:32 -06:00
|
|
|
where
|
|
|
|
T: Unpin,
|
2018-09-14 19:40:52 -05:00
|
|
|
{
|
2018-12-17 19:19:32 -06:00
|
|
|
self.pointer
|
2018-09-14 19:40:52 -05:00
|
|
|
}
|
|
|
|
|
2019-02-09 16:16:58 -06:00
|
|
|
/// Gets a mutable reference to the data inside of this `Pin`.
|
2018-08-31 23:12:10 -05:00
|
|
|
///
|
|
|
|
/// # Safety
|
2018-08-09 10:20:22 -05:00
|
|
|
///
|
|
|
|
/// This function is unsafe. You must guarantee that you will never move
|
|
|
|
/// the data out of the mutable reference you receive when you call this
|
2018-08-31 23:12:10 -05:00
|
|
|
/// function, so that the invariants on the `Pin` type can be upheld.
|
2018-09-14 19:40:52 -05:00
|
|
|
///
|
|
|
|
/// If the underlying data is `Unpin`, `Pin::get_mut` should be used
|
|
|
|
/// instead.
|
|
|
|
#[inline(always)]
|
2020-09-18 12:23:50 -05:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
|
|
|
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
|
|
|
|
pub const unsafe fn get_unchecked_mut(self) -> &'a mut T {
|
2018-12-17 19:19:32 -06:00
|
|
|
self.pointer
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a new pin by mapping the interior value.
|
|
|
|
///
|
2018-08-31 23:12:10 -05:00
|
|
|
/// For example, if you wanted to get a `Pin` of a field of something,
|
2018-08-09 10:20:22 -05:00
|
|
|
/// you could use this to get access to that field in one line of code.
|
2019-02-19 06:08:46 -06:00
|
|
|
/// However, there are several gotchas with these "pinning projections";
|
|
|
|
/// see the [`pin` module] documentation for further details on that topic.
|
2018-08-09 10:20:22 -05:00
|
|
|
///
|
2018-08-31 23:12:10 -05:00
|
|
|
/// # Safety
|
|
|
|
///
|
2018-08-09 10:20:22 -05:00
|
|
|
/// This function is unsafe. You must guarantee that the data you return
|
|
|
|
/// will not move so long as the argument value does not move (for example,
|
|
|
|
/// because it is one of the fields of that value), and also that you do
|
|
|
|
/// not move out of the argument you receive to the interior function.
|
2019-02-19 06:08:46 -06:00
|
|
|
///
|
2020-09-01 22:39:16 -05:00
|
|
|
/// [`pin` module]: self#projections-and-structural-pinning
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2019-11-24 03:43:32 -06:00
|
|
|
pub unsafe fn map_unchecked_mut<U, F>(self, func: F) -> Pin<&'a mut U>
|
|
|
|
where
|
2020-01-06 08:41:09 -06:00
|
|
|
U: ?Sized,
|
2018-08-31 23:12:10 -05:00
|
|
|
F: FnOnce(&mut T) -> &mut U,
|
2018-08-09 10:20:22 -05:00
|
|
|
{
|
2020-06-21 17:54:46 -05:00
|
|
|
// SAFETY: the caller is responsible for not moving the
|
|
|
|
// value out of this reference.
|
|
|
|
let pointer = unsafe { Pin::get_unchecked_mut(self) };
|
2018-08-31 23:12:10 -05:00
|
|
|
let new_pointer = func(pointer);
|
2020-06-21 17:54:46 -05:00
|
|
|
// SAFETY: as the value of `this` is guaranteed to not have
|
|
|
|
// been moved out, this call to `new_unchecked` is safe.
|
|
|
|
unsafe { Pin::new_unchecked(new_pointer) }
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 16:51:56 -05:00
|
|
|
impl<T: ?Sized> Pin<&'static T> {
|
|
|
|
/// Get a pinned reference from a static reference.
|
|
|
|
///
|
|
|
|
/// This is safe, because the `'static` lifetime guarantees the data will
|
|
|
|
/// never be moved.
|
|
|
|
#[unstable(feature = "pin_static_ref", issue = "none")]
|
2020-10-08 17:06:39 -05:00
|
|
|
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
|
|
|
|
pub const fn new_static(r: &'static T) -> Pin<&'static T> {
|
2020-10-08 16:51:56 -05:00
|
|
|
// SAFETY: The 'static lifetime guarantees the data will not be
|
|
|
|
// moved/invalidated until it gets dropped (which is never).
|
|
|
|
unsafe { Pin::new_unchecked(r) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2018-09-14 19:40:52 -05:00
|
|
|
impl<P: Deref> Deref for Pin<P> {
|
|
|
|
type Target = P::Target;
|
|
|
|
fn deref(&self) -> &P::Target {
|
2018-09-18 13:48:03 -05:00
|
|
|
Pin::get_ref(Pin::as_ref(self))
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2019-07-31 14:00:35 -05:00
|
|
|
impl<P: DerefMut<Target: Unpin>> DerefMut for Pin<P> {
|
2018-09-14 19:40:52 -05:00
|
|
|
fn deref_mut(&mut self) -> &mut P::Target {
|
2018-09-18 13:48:03 -05:00
|
|
|
Pin::get_mut(Pin::as_mut(self))
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 05:16:18 -06:00
|
|
|
#[unstable(feature = "receiver_trait", issue = "none")]
|
Stabilize `Rc`, `Arc` and `Pin` as method receivers
This lets you write methods using `self: Rc<Self>`, `self: Arc<Self>`, `self: Pin<&mut Self>`, `self: Pin<Box<Self>`, and other combinations involving `Pin` and another stdlib receiver type, without needing the `arbitrary_self_types`. Other user-created receiver types can be used, but they still require the feature flag to use.
This is implemented by introducing a new trait, `Receiver`, which the method receiver's type must implement if the `arbitrary_self_types` feature is not enabled. To keep composed receiver types such as `&Arc<Self>` unstable, the receiver type is also required to implement `Deref<Target=Self>` when the feature flag is not enabled.
This lets you use `self: Rc<Self>` and `self: Arc<Self>` in stable Rust, which was not allowed previously. It was agreed that they would be stabilized in #55786. `self: Pin<&Self>` and other pinned receiver types do not require the `arbitrary_self_types` feature, but they cannot be used on stable because `Pin` still requires the `pin` feature.
2018-11-20 10:50:50 -06:00
|
|
|
impl<P: Receiver> Receiver for Pin<P> {}
|
|
|
|
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2018-09-26 16:03:05 -05:00
|
|
|
impl<P: fmt::Debug> fmt::Debug for Pin<P> {
|
2019-04-18 18:37:12 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-08-31 23:12:10 -05:00
|
|
|
fmt::Debug::fmt(&self.pointer, f)
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2018-09-26 16:03:05 -05:00
|
|
|
impl<P: fmt::Display> fmt::Display for Pin<P> {
|
2019-04-18 18:37:12 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-08-31 23:12:10 -05:00
|
|
|
fmt::Display::fmt(&self.pointer, f)
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2018-09-26 16:03:05 -05:00
|
|
|
impl<P: fmt::Pointer> fmt::Pointer for Pin<P> {
|
2019-04-18 18:37:12 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-08-31 23:12:10 -05:00
|
|
|
fmt::Pointer::fmt(&self.pointer, f)
|
2018-08-09 10:20:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 19:40:52 -05:00
|
|
|
// Note: this means that any impl of `CoerceUnsized` that allows coercing from
|
|
|
|
// a type that impls `Deref<Target=impl !Unpin>` to a type that impls
|
|
|
|
// `Deref<Target=Unpin>` is unsound. Any such impl would probably be unsound
|
|
|
|
// for other reasons, though, so we just need to take care not to allow such
|
|
|
|
// impls to land in std.
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2019-11-24 03:43:32 -06:00
|
|
|
impl<P, U> CoerceUnsized<Pin<U>> for Pin<P> where P: CoerceUnsized<U> {}
|
2018-08-09 10:20:22 -05:00
|
|
|
|
2018-12-17 20:14:07 -06:00
|
|
|
#[stable(feature = "pin", since = "1.33.0")]
|
2019-11-24 03:43:32 -06:00
|
|
|
impl<P, U> DispatchFromDyn<Pin<U>> for Pin<P> where P: DispatchFromDyn<U> {}
|