Trait core::marker::Unpin

1.33.0 · source · []
pub auto trait Unpin { }
Expand description

Types that can be safely moved after being pinned.

Rust itself has no notion of immovable types, and considers moves (e.g., through assignment or mem::replace) to always be safe.

The Pin type is used instead to prevent moves through the type system. Pointers P<T> wrapped in the Pin<P<T>> wrapper can’t be moved out of. See the pin module documentation for more information on pinning.

Implementing the Unpin trait for T lifts the restrictions of pinning off the type, which then allows moving T out of Pin<P<T>> with functions such as mem::replace.

Unpin has no consequence at all for non-pinned data. In particular, mem::replace happily moves !Unpin data (it works for any &mut T, not just when T: Unpin). However, you cannot use mem::replace on data wrapped inside a Pin<P<T>> because you cannot get the &mut T you need for that, and that is what makes this system work.

So this, for example, can only be done on types implementing Unpin:

use std::mem;
use std::pin::Pin;

let mut string = "this".to_string();
let mut pinned_string = Pin::new(&mut string);

// We need a mutable reference to call `mem::replace`.
// We can obtain such a reference by (implicitly) invoking `Pin::deref_mut`,
// but that is only possible because `String` implements `Unpin`.
mem::replace(&mut *pinned_string, "other".to_string());
Run

This trait is automatically implemented for almost every type.

Implementors

impl<T: ?Sized, A: Allocator> Unpin for Box<T, A> where
    A: 'static, 

impl<T: ?Sized> Unpin for Rc<T>

impl<T: ?Sized> Unpin for Arc<T>

Auto implementors

impl Unpin for Global

impl<'a, B: ?Sized> Unpin for Cow<'a, B> where
    <B as ToOwned>::Owned: Unpin

impl<T> Unpin for BinaryHeap<T> where
    T: Unpin

impl<'a, T> Unpin for PeekMut<'a, T>

impl<'a, T> Unpin for Iter<'a, T>

impl<T> Unpin for IntoIter<T> where
    T: Unpin

impl<T> Unpin for IntoIterSorted<T> where
    T: Unpin

impl<'a, T> Unpin for Drain<'a, T>

impl<'a, T> Unpin for DrainSorted<'a, T>

impl<'a, K, V> Unpin for Entry<'a, K, V> where
    K: Unpin

impl<'a, K, V> Unpin for VacantEntry<'a, K, V> where
    K: Unpin

impl<'a, K, V> Unpin for OccupiedEntry<'a, K, V>

impl<'a, K, V> Unpin for OccupiedError<'a, K, V> where
    V: Unpin

impl<K, V> Unpin for BTreeMap<K, V>

impl<'a, K, V> Unpin for Iter<'a, K, V>

impl<'a, K, V> Unpin for IterMut<'a, K, V>

impl<K, V> Unpin for IntoIter<K, V>

impl<'a, K, V> Unpin for Keys<'a, K, V>

impl<'a, K, V> Unpin for Values<'a, K, V>

impl<'a, K, V> Unpin for ValuesMut<'a, K, V>

impl<K, V> Unpin for IntoKeys<K, V>

impl<K, V> Unpin for IntoValues<K, V>

impl<'a, K, V> Unpin for Range<'a, K, V>

impl<'a, K, V> Unpin for RangeMut<'a, K, V>

impl<'a, K, V, F> Unpin for DrainFilter<'a, K, V, F> where
    F: Unpin

impl<T> Unpin for BTreeSet<T>

impl<'a, T> Unpin for Iter<'a, T>

impl<T> Unpin for IntoIter<T>

impl<'a, T> Unpin for Range<'a, T>

impl<'a, T> Unpin for Difference<'a, T>

impl<'a, T> Unpin for SymmetricDifference<'a, T>

impl<'a, T> Unpin for Intersection<'a, T>

impl<'a, T> Unpin for Union<'a, T>

impl<'a, T, F> Unpin for DrainFilter<'a, T, F> where
    F: Unpin

impl<T> Unpin for LinkedList<T>

impl<'a, T> Unpin for Iter<'a, T>

impl<'a, T> Unpin for IterMut<'a, T>

impl<T> Unpin for IntoIter<T>

impl<'a, T> Unpin for Cursor<'a, T>

impl<'a, T> Unpin for CursorMut<'a, T>

impl<'a, T, F> Unpin for DrainFilter<'a, T, F> where
    F: Unpin

impl<'a, T, A> Unpin for Drain<'a, T, A>

impl<'a, T> Unpin for IterMut<'a, T>

impl<T, A> Unpin for IntoIter<T, A> where
    A: Unpin,
    T: Unpin

impl<'a, T> Unpin for Iter<'a, T>

impl<T, A> Unpin for VecDeque<T, A> where
    A: Unpin,
    T: Unpin

impl<T: ?Sized> Unpin for Weak<T>

impl Unpin for String

impl<'a> Unpin for Drain<'a>

impl<T: ?Sized> Unpin for Weak<T>

impl<'a, T, F, A> Unpin for DrainFilter<'a, T, F, A> where
    F: Unpin

impl<'a, I, A> Unpin for Splice<'a, I, A> where
    I: Unpin

impl<'a, T, A> Unpin for Drain<'a, T, A>

impl<T, A> Unpin for IntoIter<T, A> where
    A: Unpin,
    T: Unpin

impl<T, A> Unpin for Vec<T, A> where
    A: Unpin,
    T: Unpin