1 //! Types which pin data to its location in memory
3 //! It is sometimes useful to have objects that are guaranteed to not move,
4 //! in the sense that their placement in memory does not change, and can thus be relied upon.
5 //! A prime example of such a scenario would be building self-referential structs,
6 //! since moving an object with pointers to itself will invalidate them,
7 //! which could cause undefined behavior.
9 //! [`Pin`] ensures that the pointee of any pointer type has a stable location in memory,
10 //! meaning it cannot be moved elsewhere and its memory cannot be deallocated
11 //! until it gets dropped. We say that the pointee is "pinned".
13 //! By default, all types in Rust are movable. Rust allows passing all types by-value,
14 //! and common smart-pointer types such as `Box` and `&mut` allow replacing and
15 //! moving the values they contain: you can move out of a `Box`, or you can use [`mem::swap`].
16 //! [`Pin`] wraps a pointer type, so `Pin<Box<T>>` functions much like a regular `Box<T>`
17 //! (when a `Pin<Box<T>>` gets dropped, so do its contents, and the memory gets deallocated).
18 //! Similarily, `Pin<&mut T>` is a lot like `&mut T`. However, [`Pin`] does not let clients actually
19 //! obtain a `Box<T>` or `&mut T` to pinned data, which implies that you cannot use
20 //! operations such as [`mem::swap`]:
22 //! use std::pin::Pin;
23 //! fn swap_pins<T>(x: Pin<&mut T>, y: Pin<&mut T>) {
24 //! // `mem::swap` needs `&mut T`, but we cannot get it.
25 //! // We are stuck, we cannot swap the contents of these references.
26 //! // We could use `Pin::get_unchecked_mut`, but that is unsafe for a reason:
27 //! // we are not allowed to use it for moving things out of the `Pin`.
31 //! It is worth reiterating that [`Pin`] does *not* change the fact that a Rust compiler
32 //! considers all types movable. [`mem::swap`] remains callable for any `T`. Instead, `Pin`
33 //! prevents certain *values* (pointed to by pointers wrapped in `Pin`) from being
34 //! moved by making it impossible to call methods that require `&mut T` on them
35 //! (like [`mem::swap`]).
37 //! [`Pin`] can be used to wrap any pointer type, and as such it interacts with
38 //! [`Deref`] and [`DerefMut`]. A `Pin<P>` where `P: Deref` should be considered
39 //! as a "`P`-style pointer" to a pinned `P::Target` -- so, a `Pin<Box<T>>` is
40 //! an owned pointer to a pinned `T`, and a `Pin<Rc<T>>` is a reference-counted
41 //! pointer to a pinned `T`.
42 //! For correctness, [`Pin`] relies on the [`Deref`] and [`DerefMut`] implementations
43 //! to not move out of their `self` parameter, and to only ever return a pointer
44 //! to pinned data when they are called on a pinned pointer.
48 //! However, these restrictions are usually not necessary. Many types are always freely
49 //! movable, even when pinned, because they do not rely on having a stable address.
50 //! This includes all the basic types (`bool`, `i32` and friends, references)
51 //! as well as types consisting solely of these types.
52 //! Types that do not care about pinning implement the [`Unpin`] auto-trait, which
53 //! nullifies the effect of [`Pin`]. For `T: Unpin`, `Pin<Box<T>>` and `Box<T>` function
54 //! identically, as do `Pin<&mut T>` and `&mut T`.
56 //! Note that pinning and `Unpin` only affect the pointed-to type, not the pointer
57 //! type itself that got wrapped in `Pin`. For example, whether or not `Box<T>` is
58 //! `Unpin` has no effect on the behavior of `Pin<Box<T>>` (here, `T` is the
61 //! # Example: self-referential struct
64 //! use std::pin::Pin;
65 //! use std::marker::PhantomPinned;
66 //! use std::ptr::NonNull;
68 //! // This is a self-referential struct since the slice field points to the data field.
69 //! // We cannot inform the compiler about that with a normal reference,
70 //! // since this pattern cannot be described with the usual borrowing rules.
71 //! // Instead we use a raw pointer, though one which is known to not be null,
72 //! // since we know it's pointing at the string.
73 //! struct Unmovable {
75 //! slice: NonNull<String>,
76 //! _pin: PhantomPinned,
80 //! // To ensure the data doesn't move when the function returns,
81 //! // we place it in the heap where it will stay for the lifetime of the object,
82 //! // and the only way to access it would be through a pointer to it.
83 //! fn new(data: String) -> Pin<Box<Self>> {
84 //! let res = Unmovable {
86 //! // we only create the pointer once the data is in place
87 //! // otherwise it will have already moved before we even started
88 //! slice: NonNull::dangling(),
89 //! _pin: PhantomPinned,
91 //! let mut boxed = Box::pin(res);
93 //! let slice = NonNull::from(&boxed.data);
94 //! // we know this is safe because modifying a field doesn't move the whole struct
96 //! let mut_ref: Pin<&mut Self> = Pin::as_mut(&mut boxed);
97 //! Pin::get_unchecked_mut(mut_ref).slice = slice;
103 //! let unmoved = Unmovable::new("hello".to_string());
104 //! // The pointer should point to the correct location,
105 //! // so long as the struct hasn't moved.
106 //! // Meanwhile, we are free to move the pointer around.
107 //! # #[allow(unused_mut)]
108 //! let mut still_unmoved = unmoved;
109 //! assert_eq!(still_unmoved.slice, NonNull::from(&still_unmoved.data));
111 //! // Since our type doesn't implement Unpin, this will fail to compile:
112 //! // let new_unmoved = Unmovable::new("world".to_string());
113 //! // std::mem::swap(&mut *still_unmoved, &mut *new_unmoved);
116 //! # Example: intrusive doubly-linked list
118 //! In an intrusive doubly-linked list, the collection does not actually allocate
119 //! the memory for the elements itself. Allocation is controlled by the clients,
120 //! and elements can live on a stack frame that lives shorter than the collection does.
122 //! To make this work, every element has pointers to its predecessor and successor in
123 //! the list. Element can only be added when they are pinned, because moving the elements
124 //! around would invalidate the pointers. Moreover, the `Drop` implementation of a linked
125 //! list element will patch the pointers of its predecessor and successor to remove itself
128 //! Crucially, we have to be able to rely on `drop` being called. If an element
129 //! could be deallocated or otherwise invalidated without calling `drop`, the pointers into it
130 //! from its neighbouring elements would become invalid, which would break the data structure.
132 //! This is why pinning also comes with a `drop`-related guarantee.
134 //! # `Drop` guarantee
136 //! The purpose of pinning is to be able to rely on the placement of some data in memory.
137 //! To make this work, not just moving the data is restricted; deallocating, repurposing or
138 //! otherwise invalidating the memory used to store the data is restricted, too.
139 //! Concretely, for pinned data you have to maintain the invariant
140 //! that *its memory will not get invalidated from the moment it gets pinned until
141 //! when `drop` is called*. Memory can be invalidated by deallocation, but also by
142 //! replacing a `Some(v)` by `None`, or calling `Vec::set_len` to "kill" some elements
145 //! This is exactly the kind of guarantee that the intrusive linked list from the previous
146 //! section needs to function correctly.
148 //! Notice that this guarantee does *not* mean that memory does not leak! It is still
149 //! completely okay not to ever call `drop` on a pinned element (e.g., you can still
150 //! call [`mem::forget`] on a `Pin<Box<T>>`). In the example of the doubly-linked
151 //! list, that element would just stay in the list. However you may not free or reuse the storage
152 //! *without calling `drop`*.
154 //! # `Drop` implementation
156 //! If your type uses pinning (such as the two examples above), you have to be careful
157 //! when implementing `Drop`. The `drop` function takes `&mut self`, but this
158 //! is called *even if your type was previously pinned*! It is as if the
159 //! compiler automatically called `get_unchecked_mut`.
161 //! This can never cause a problem in safe code because implementing a type that relies on pinning
162 //! requires unsafe code, but be aware that deciding to make use of pinning
163 //! in your type (for example by implementing some operation on `Pin<&[mut] Self>`)
164 //! has consequences for your `Drop` implementation as well: if an element
165 //! of your type could have been pinned, you must treat Drop as implicitly taking
166 //! `Pin<&mut Self>`.
168 //! In particular, if your type is `#[repr(packed)]`, the compiler will automatically
169 //! move fields around to be able to drop them. As a consequence, you cannot use
170 //! pinning with a `#[repr(packed)]` type.
172 //! # Projections and Structural Pinning
174 //! One interesting question arises when considering the interaction of pinning and
175 //! the fields of a struct. When can a struct have a "pinning projection", i.e.,
176 //! an operation with type `fn(Pin<&[mut] Struct>) -> Pin<&[mut] Field>`?
177 //! In a similar vein, when can a generic wrapper type (such as `Vec`, `Box`, or `RefCell`)
178 //! have an operation with type `fn(Pin<&[mut] Wrapper<T>>) -> Pin<&[mut] T>`?
180 //! This question is closely related to the question of whether pinning is "structural".
181 //! Structural pinning means that when you have pinned a wrapper type, the contents are
182 //! also pinned. Structural pinning thus explains why pinning projections are correct. This means
183 //! that if the type should have pinning projections for some fields, pinning must be structural
184 //! for those fields.
186 //! In general, deciding for which fields pinning is structural (and thus for which fields
187 //! pinning projections could be offered) is entirely up to the author of any given type.
188 //! For many types, both answers are reasonable. For example, there could be a version
189 //! of `Vec` with structural pinning and `get_pin`/`get_pin_mut` projections to access
190 //! the `Vec` elements, and another version where the contents remain movable even when
191 //! the `Vec` is pinned.
193 //! However, structural pinning comes with a few extra requirements:
195 //! 1. The wrapper must only be [`Unpin`] if all the structural fields are
196 //! `Unpin`. This is the default, but `Unpin` is a safe trait, so as the author of
197 //! the wrapper it is your responsibility *not* to add something like
198 //! `impl<T> Unpin for Wrapper<T>`. (Notice that adding a projection operation
199 //! requires unsafe code, so the fact that `Unpin` is a safe trait does not break
200 //! the principle that you only have to worry about any of this if you use `unsafe`.)
201 //! 2. The destructor of the wrapper must not move structural fields out of its argument. This
202 //! is the exact point that was raised in the [previous section][drop-impl]: `drop` takes
203 //! `&mut self`, but the wrapper (and hence its fields) might have been pinned before.
204 //! You have to guarantee that you do not move a field inside your `Drop` implementation.
205 //! In particular, as explained previously, this means that your wrapper type must *not*
206 //! be `#[repr(packed)]`.
207 //! 3. You must make sure that you uphold the [`Drop` guarantee][drop-guarantee]:
208 //! once your wrapper is pinned, the memory that contains the
209 //! content is not overwritten or deallocated without calling the content's destructors.
210 //! This can be tricky, as witnessed by `VecDeque`: the destructor of `VecDeque` can fail
211 //! to call `drop` on all elements if one of the destructors panics. This violates the
212 //! `Drop` guarantee, because it can lead to elements being deallocated without
213 //! their destructor being called. (`VecDeque` has no pinning projections, so this
214 //! does not cause unsoundness.)
215 //! 4. You must not offer any other operations that could lead to data being moved out of
216 //! the fields when your type is pinned. For example, if the wrapper contains an
217 //! `Option<T>` and there is an operation such as `fn(Pin<&mut Wrapper<T>>) -> Option<T>`,
218 //! that operation can be used to move a `T` out of a pinned `Wrapper` -- that means
219 //! pinning cannot be structural.
221 //! For a more complex example of moving data out of a pinnd type, imagine if `RefCell`
222 //! had a method `fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T>`.
223 //! Then we could do the following:
225 //! fn exploit_ref_cell<T>(rc: Pin<&mut RefCell<T>) {
226 //! { let p = rc.as_mut().get_pin_mut(); } // here we get pinned access to the `T`
227 //! let rc_shr: &RefCell<T> = rc.into_ref().get_ref();
228 //! let b = rc_shr.borrow_mut();
229 //! let content = &mut *b; // and here we have `&mut T` to the same data
232 //! This is catastrophic, it means we can first pin the content of the `RefCell`
233 //! (using `RefCell::get_pin_mut`) and then move that content using the mutable
234 //! reference we got later.
236 //! On the other hand, if you decide *not* to offer any pinning projections, you
237 //! do not have to do anything. If your type also does not do any pinning itself,
238 //! you are free to `impl<T> Unpin for Wrapper<T>`. In the standard library,
239 //! this is done for all pointer types: `Box<T>: Unpin` holds for all `T`.
240 //! It makes sense to do this for pointer types, because moving the `Box<T>`
241 //! does not actually move the `T`: the `Box<T>` can be freely movable even if the `T`
242 //! is not. In fact, even `Pin<Box<T>>` and `Pin<&mut T>` are always `Unpin` themselves,
243 //! for the same reason.
245 //! Another case where you might want to have a wrapper without structural pinning is when even
246 //! a pinned wrapper lets its contents move, e.g. with a `take`-like operation. And, finally,
247 //! if it is not possible to satisfy the requirements for structural pinning, it makes sense
248 //! to add the `impl<T> Unpin for Wrapper<T>` to explicitly document this fact, and to let
249 //! library clients benefit from the easier interaction with [`Pin`] that [`Unpin`] types enjoy.
251 //! [`Pin`]: struct.Pin.html
252 //! [`Unpin`]: ../../std/marker/trait.Unpin.html
253 //! [`Deref`]: ../../std/ops/trait.Deref.html
254 //! [`DerefMut`]: ../../std/ops/trait.DerefMut.html
255 //! [`mem::swap`]: ../../std/mem/fn.swap.html
256 //! [`mem::forget`]: ../../std/mem/fn.forget.html
257 //! [`Box`]: ../../std/boxed/struct.Box.html
258 //! [drop-impl]: #drop-implementation
259 //! [drop-guarantee]: #drop-guarantee
261 #![stable(feature = "pin", since = "1.33.0")]
264 use marker::{Sized, Unpin};
265 use cmp::{self, PartialEq, PartialOrd};
266 use ops::{Deref, DerefMut, Receiver, CoerceUnsized, DispatchFromDyn};
268 /// A pinned pointer.
270 /// This is a wrapper around a kind of pointer which makes that pointer "pin" its
271 /// value in place, preventing the value referenced by that pointer from being moved
272 /// unless it implements [`Unpin`].
274 /// See the [`pin` module] documentation for further explanation on pinning.
276 /// [`Unpin`]: ../../std/marker/trait.Unpin.html
277 /// [`pin` module]: ../../std/pin/index.html
279 // Note: the derives below, and the explicit `PartialEq` and `PartialOrd`
280 // implementations, are allowed because they all only use `&P`, so they cannot move
281 // the value behind `pointer`.
282 #[stable(feature = "pin", since = "1.33.0")]
283 #[cfg_attr(not(stage0), lang = "pin")]
286 #[derive(Copy, Clone, Hash, Eq, Ord)]
291 #[stable(feature = "pin_partialeq_partialord_impl_applicability", since = "1.34.0")]
292 impl<P, Q> PartialEq<Pin<Q>> for Pin<P>
296 fn eq(&self, other: &Pin<Q>) -> bool {
297 self.pointer == other.pointer
300 fn ne(&self, other: &Pin<Q>) -> bool {
301 self.pointer != other.pointer
305 #[stable(feature = "pin_partialeq_partialord_impl_applicability", since = "1.34.0")]
306 impl<P, Q> PartialOrd<Pin<Q>> for Pin<P>
310 fn partial_cmp(&self, other: &Pin<Q>) -> Option<cmp::Ordering> {
311 self.pointer.partial_cmp(&other.pointer)
314 fn lt(&self, other: &Pin<Q>) -> bool {
315 self.pointer < other.pointer
318 fn le(&self, other: &Pin<Q>) -> bool {
319 self.pointer <= other.pointer
322 fn gt(&self, other: &Pin<Q>) -> bool {
323 self.pointer > other.pointer
326 fn ge(&self, other: &Pin<Q>) -> bool {
327 self.pointer >= other.pointer
331 impl<P: Deref> Pin<P>
335 /// Construct a new `Pin` around a pointer to some data of a type that
336 /// implements [`Unpin`].
338 /// Unlike `Pin::new_unchecked`, this method is safe because the pointer
339 /// `P` dereferences to an [`Unpin`] type, which nullifies the pinning guarantees.
341 /// [`Unpin`]: ../../std/marker/trait.Unpin.html
342 #[stable(feature = "pin", since = "1.33.0")]
344 pub fn new(pointer: P) -> Pin<P> {
345 // Safety: the value pointed to is `Unpin`, and so has no requirements
347 unsafe { Pin::new_unchecked(pointer) }
351 impl<P: Deref> Pin<P> {
352 /// Construct a new `Pin` around a reference to some data of a type that
353 /// may or may not implement `Unpin`.
355 /// If `pointer` dereferences to an `Unpin` type, `Pin::new` should be used
360 /// This constructor is unsafe because we cannot guarantee that the data
361 /// pointed to by `pointer` is pinned, meaning that the data will not be moved or
362 /// its storage invalidated until it gets dropped. If the constructed `Pin<P>` does
363 /// not guarantee that the data `P` points to is pinned, constructing a
364 /// `Pin<P>` is unsafe.
366 /// By using this method, you are making a promise about the `P::Deref` and
367 /// `P::DerefMut` implementations, if they exist. Most importantly, they
368 /// must not move out of their `self` arguments: `Pin::as_mut` and `Pin::as_ref`
369 /// will call `DerefMut::deref_mut` and `Deref::deref` *on the pinned pointer*
370 /// and expect these methods to uphold the pinning invariants.
371 /// Moreover, by calling this method you promise that the reference `P`
372 /// dereferences to will not be moved out of again; in particular, it
373 /// must not be possible to obtain a `&mut P::Target` and then
374 /// move out of that reference (using, for example [`mem::swap`]).
376 /// For example, calling `Pin::new_unchecked` on an `&'a mut T` is unsafe because
377 /// while you are able to pin it for the given lifetime `'a`, you have no control
378 /// over whether it is kept pinned once `'a` ends:
381 /// use std::pin::Pin;
383 /// fn move_pinned_ref<T>(mut a: T, mut b: T) {
384 /// unsafe { let p = Pin::new_unchecked(&mut a); } // should mean `a` can never move again
385 /// mem::swap(&mut a, &mut b);
386 /// // the address of `a` changed to `b`'s stack slot, so `a` got moved even
387 /// // though we have previously pinned it!
390 /// A value, once pinned, must remain pinned forever (unless its type implements `Unpin`).
392 /// Similarily, calling `Pin::new_unchecked` on a `Rc<T>` is unsafe because there could be
393 /// aliases to the same data that are not subject to the pinning restrictions:
396 /// use std::pin::Pin;
398 /// fn move_pinned_rc<T>(mut x: Rc<T>) {
399 /// let pinned = unsafe { Pin::new_unchecked(x.clone()) };
400 /// { let p: Pin<&T> = pinned.as_ref(); } // should mean the pointee can never move again
402 /// let content = Rc::get_mut(&mut x).unwrap();
403 /// // Now, if `x` was the only reference, we have a mutable reference to
404 /// // data that we pinned above, which we could use to move it as we have
405 /// // seen in the previous example.
409 /// [`mem::swap`]: ../../std/mem/fn.swap.html
410 #[stable(feature = "pin", since = "1.33.0")]
412 pub unsafe fn new_unchecked(pointer: P) -> Pin<P> {
416 /// Gets a pinned shared reference from this pinned pointer.
418 /// This is a generic method to go from `&Pin<Pointer<T>>` to `Pin<&T>`.
419 /// It is safe because, as part of the contract of `Pin::new_unchecked`,
420 /// the pointee cannot move after `Pin<Pointer<T>>` got created.
421 /// "Malicious" implementations of `Pointer::Deref` are likewise
422 /// ruled out by the contract of `Pin::new_unchecked`.
423 #[stable(feature = "pin", since = "1.33.0")]
425 pub fn as_ref(self: &Pin<P>) -> Pin<&P::Target> {
426 unsafe { Pin::new_unchecked(&*self.pointer) }
430 impl<P: DerefMut> Pin<P> {
431 /// Gets a pinned mutable reference from this pinned pointer.
433 /// This is a generic method to go from `&mut Pin<Pointer<T>>` to `Pin<&mut T>`.
434 /// It is safe because, as part of the contract of `Pin::new_unchecked`,
435 /// the pointee cannot move after `Pin<Pointer<T>>` got created.
436 /// "Malicious" implementations of `Pointer::DerefMut` are likewise
437 /// ruled out by the contract of `Pin::new_unchecked`.
438 #[stable(feature = "pin", since = "1.33.0")]
440 pub fn as_mut(self: &mut Pin<P>) -> Pin<&mut P::Target> {
441 unsafe { Pin::new_unchecked(&mut *self.pointer) }
444 /// Assigns a new value to the memory behind the pinned reference.
446 /// This overwrites pinned data, but that is okay: its destructor gets
447 /// run before being overwritten, so no pinning guarantee is violated.
448 #[stable(feature = "pin", since = "1.33.0")]
450 pub fn set(self: &mut Pin<P>, value: P::Target)
454 *(self.pointer) = value;
458 impl<'a, T: ?Sized> Pin<&'a T> {
459 /// Constructs a new pin by mapping the interior value.
461 /// For example, if you wanted to get a `Pin` of a field of something,
462 /// you could use this to get access to that field in one line of code.
463 /// However, there are several gotchas with these "pinning projections";
464 /// see the [`pin` module] documentation for further details on that topic.
468 /// This function is unsafe. You must guarantee that the data you return
469 /// will not move so long as the argument value does not move (for example,
470 /// because it is one of the fields of that value), and also that you do
471 /// not move out of the argument you receive to the interior function.
473 /// [`pin` module]: ../../std/pin/index.html#projections-and-structural-pinning
474 #[stable(feature = "pin", since = "1.33.0")]
475 pub unsafe fn map_unchecked<U, F>(self: Pin<&'a T>, func: F) -> Pin<&'a U> where
478 let pointer = &*self.pointer;
479 let new_pointer = func(pointer);
480 Pin::new_unchecked(new_pointer)
483 /// Gets a shared reference out of a pin.
485 /// This is safe because it is not possible to move out of a shared reference.
486 /// It may seem like there is an issue here with interior mutability: in fact,
487 /// it *is* possible to move a `T` out of a `&RefCell<T>`. However, this is
488 /// not a problem as long as there does not also exist a `Pin<&T>` pointing
489 /// to the same data, and `RefCell` does not let you create a pinned reference
490 /// to its contents. See the discussion on ["pinning projections"] for further
493 /// Note: `Pin` also implements `Deref` to the target, which can be used
494 /// to access the inner value. However, `Deref` only provides a reference
495 /// that lives for as long as the borrow of the `Pin`, not the lifetime of
496 /// the `Pin` itself. This method allows turning the `Pin` into a reference
497 /// with the same lifetime as the original `Pin`.
499 /// ["pinning projections"]: ../../std/pin/index.html#projections-and-structural-pinning
500 #[stable(feature = "pin", since = "1.33.0")]
502 pub fn get_ref(self: Pin<&'a T>) -> &'a T {
507 impl<'a, T: ?Sized> Pin<&'a mut T> {
508 /// Converts this `Pin<&mut T>` into a `Pin<&T>` with the same lifetime.
509 #[stable(feature = "pin", since = "1.33.0")]
511 pub fn into_ref(self: Pin<&'a mut T>) -> Pin<&'a T> {
512 Pin { pointer: self.pointer }
515 /// Gets a mutable reference to the data inside of this `Pin`.
517 /// This requires that the data inside this `Pin` is `Unpin`.
519 /// Note: `Pin` also implements `DerefMut` to the data, which can be used
520 /// to access the inner value. However, `DerefMut` only provides a reference
521 /// that lives for as long as the borrow of the `Pin`, not the lifetime of
522 /// the `Pin` itself. This method allows turning the `Pin` into a reference
523 /// with the same lifetime as the original `Pin`.
524 #[stable(feature = "pin", since = "1.33.0")]
526 pub fn get_mut(self: Pin<&'a mut T>) -> &'a mut T
532 /// Gets a mutable reference to the data inside of this `Pin`.
536 /// This function is unsafe. You must guarantee that you will never move
537 /// the data out of the mutable reference you receive when you call this
538 /// function, so that the invariants on the `Pin` type can be upheld.
540 /// If the underlying data is `Unpin`, `Pin::get_mut` should be used
542 #[stable(feature = "pin", since = "1.33.0")]
544 pub unsafe fn get_unchecked_mut(self: Pin<&'a mut T>) -> &'a mut T {
548 /// Construct a new pin by mapping the interior value.
550 /// For example, if you wanted to get a `Pin` of a field of something,
551 /// you could use this to get access to that field in one line of code.
552 /// However, there are several gotchas with these "pinning projections";
553 /// see the [`pin` module] documentation for further details on that topic.
557 /// This function is unsafe. You must guarantee that the data you return
558 /// will not move so long as the argument value does not move (for example,
559 /// because it is one of the fields of that value), and also that you do
560 /// not move out of the argument you receive to the interior function.
562 /// [`pin` module]: ../../std/pin/index.html#projections-and-structural-pinning
563 #[stable(feature = "pin", since = "1.33.0")]
564 pub unsafe fn map_unchecked_mut<U, F>(self: Pin<&'a mut T>, func: F) -> Pin<&'a mut U> where
565 F: FnOnce(&mut T) -> &mut U,
567 let pointer = Pin::get_unchecked_mut(self);
568 let new_pointer = func(pointer);
569 Pin::new_unchecked(new_pointer)
573 #[stable(feature = "pin", since = "1.33.0")]
574 impl<P: Deref> Deref for Pin<P> {
575 type Target = P::Target;
576 fn deref(&self) -> &P::Target {
577 Pin::get_ref(Pin::as_ref(self))
581 #[stable(feature = "pin", since = "1.33.0")]
582 impl<P: DerefMut> DerefMut for Pin<P>
586 fn deref_mut(&mut self) -> &mut P::Target {
587 Pin::get_mut(Pin::as_mut(self))
591 #[unstable(feature = "receiver_trait", issue = "0")]
592 impl<P: Receiver> Receiver for Pin<P> {}
594 #[stable(feature = "pin", since = "1.33.0")]
595 impl<P: fmt::Debug> fmt::Debug for Pin<P> {
596 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
597 fmt::Debug::fmt(&self.pointer, f)
601 #[stable(feature = "pin", since = "1.33.0")]
602 impl<P: fmt::Display> fmt::Display for Pin<P> {
603 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
604 fmt::Display::fmt(&self.pointer, f)
608 #[stable(feature = "pin", since = "1.33.0")]
609 impl<P: fmt::Pointer> fmt::Pointer for Pin<P> {
610 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
611 fmt::Pointer::fmt(&self.pointer, f)
615 // Note: this means that any impl of `CoerceUnsized` that allows coercing from
616 // a type that impls `Deref<Target=impl !Unpin>` to a type that impls
617 // `Deref<Target=Unpin>` is unsound. Any such impl would probably be unsound
618 // for other reasons, though, so we just need to take care not to allow such
619 // impls to land in std.
620 #[stable(feature = "pin", since = "1.33.0")]
621 impl<P, U> CoerceUnsized<Pin<U>> for Pin<P>
626 #[stable(feature = "pin", since = "1.33.0")]
627 impl<'a, P, U> DispatchFromDyn<Pin<U>> for Pin<P>
629 P: DispatchFromDyn<U>,