Skip to content

Commit dc8e9fb

Browse files
committed
Rollup merge of rust-lang#53104 - nivkner:unpin_doc, r=RalfJung
expand the documentation on the `Unpin` trait provides an overview of the Pin API which the trait is for, and show how it can be used in making self referencial structs part of rust-lang#49150
2 parents fed4298 + 6ae915b commit dc8e9fb

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/libcore/marker.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -603,15 +603,35 @@ unsafe impl<T: ?Sized> Freeze for *mut T {}
603603
unsafe impl<'a, T: ?Sized> Freeze for &'a T {}
604604
unsafe impl<'a, T: ?Sized> Freeze for &'a mut T {}
605605

606-
/// Types which can be moved out of a `PinMut`.
606+
/// Types which can be safely moved after being pinned.
607607
///
608-
/// The `Unpin` trait is used to control the behavior of the [`PinMut`] type. If a
609-
/// type implements `Unpin`, it is safe to move a value of that type out of the
610-
/// `PinMut` pointer.
608+
/// Since Rust itself has no notion of immovable types, and will consider moves to always be safe,
609+
/// this trait cannot prevent types from moving by itself.
610+
///
611+
/// Instead it can be used to prevent moves through the type system,
612+
/// by controlling the behavior of special pointer types like [`PinMut`],
613+
/// which "pin" the type in place by not allowing it to be moved out of them.
614+
///
615+
/// Implementing this trait lifts the restrictions of pinning off a type,
616+
/// which then allows it to move out with functions such as [`replace`].
617+
///
618+
/// So this, for example, can only be done on types implementing `Unpin`:
619+
///
620+
/// ```rust
621+
/// #![feature(pin)]
622+
/// use std::mem::{PinMut, replace};
623+
///
624+
/// let mut string = "this".to_string();
625+
/// let mut pinned_string = PinMut::new(&mut string);
626+
///
627+
/// // dereferencing the pointer mutably is only possible because String implements Unpin
628+
/// replace(&mut *pinned_string, "other".to_string());
629+
/// ```
611630
///
612631
/// This trait is automatically implemented for almost every type.
613632
///
614633
/// [`PinMut`]: ../mem/struct.PinMut.html
634+
/// [`replace`]: ../mem/fn.replace.html
615635
#[unstable(feature = "pin", issue = "49150")]
616636
pub auto trait Unpin {}
617637

0 commit comments

Comments
 (0)