Trait std::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

Auto implementors

impl Unpin for Level

impl Unpin for Diagnostic

impl Unpin for IntoIter

impl Unpin for LexError

impl Unpin for Span

impl Unpin for LineColumn

impl Unpin for SourceFile

impl Unpin for TokenTree

impl Unpin for Group

impl Unpin for Delimiter

impl Unpin for Punct

impl Unpin for Spacing

impl Unpin for Ident

impl Unpin for Literal

impl Unpin for TestOpts

impl Unpin for Metric

impl Unpin for MetricMap

impl Unpin for Concurrent

impl Unpin for Options

impl Unpin for RunIgnored

impl Unpin for TestResult

impl Unpin for TestDesc

impl Unpin for TestId

impl Unpin for TestName

impl Unpin for TestType

impl Unpin for Bencher

impl Unpin for Summary

impl Unpin for TestFn