Trait Unpin

1.33.0 · Source
pub auto trait Unpin { }
Expand description

Types that do not require any pinning guarantees.

For information on what “pinning” is, see the pin module documentation.

Implementing the Unpin trait for T expresses the fact that T is pinning-agnostic: it shall not expose nor rely on any pinning guarantees. This, in turn, means that a Pin-wrapped pointer to such a type can feature a fully unrestricted API. In other words, if T: Unpin, a value of type T will not be bound by the invariants which pinning otherwise offers, even when “pinned” by a Pin<Ptr> pointing at it. When a value of type T is pointed at by a Pin<Ptr>, Pin will not restrict access to the pointee value like it normally would, thus allowing the user to do anything that they normally could with a non-Pin-wrapped Ptr to that value.

The idea of this trait is to alleviate the reduced ergonomics of APIs that require the use of Pin for soundness for some types, but which also want to be used by other types that don’t care about pinning. The prime example of such an API is Future::poll. There are many Future types that don’t care about pinning. These futures can implement Unpin and therefore get around the pinning related restrictions in the API, while still allowing the subset of Futures which do require pinning to be implemented soundly.

For more discussion on the consequences of Unpin within the wider scope of the pinning system, see the section about Unpin in the pin module.

Unpin has no consequence at all for non-pinned data. In particular, mem::replace happily moves !Unpin data, which would be immovable when pinned (mem::replace works for any &mut T, not just when T: Unpin).

However, you cannot use mem::replace on !Unpin data which is pinned by being wrapped inside a Pin<Ptr> pointing at it. This is because you cannot (safely) use a Pin<Ptr> to get a &mut T to its pointee value, which you would need to call mem::replace, 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());

This trait is automatically implemented for almost every type. The compiler is free to take the conservative stance of marking types as Unpin so long as all of the types that compose its fields are also Unpin. This is because if a type implements Unpin, then it is unsound for that type’s implementation to rely on pinning-related guarantees for soundness, even when viewed through a “pinning” pointer! It is the responsibility of the implementor of a type that relies upon pinning for soundness to ensure that type is not marked as Unpin by adding PhantomPinned field. For more details, see the pin module docs.

Implementors§

1.33.0 · Source§

impl !Unpin for PhantomPinned

Source§

impl Unpin for LocalWaker

1.36.0 · Source§

impl Unpin for Waker

Source§

impl<Dyn> Unpin for DynMetadata<Dyn>
where Dyn: ?Sized,

1.64.0 · Source§

impl<F> Unpin for PollFn<F>
where F: Unpin,

Source§

impl<I> Unpin for FromIter<I>

Source§

impl<T> !Unpin for UnsafePinned<T>
where T: ?Sized,

When this type is used, that almost certainly means safe APIs need to use pinning to avoid the aliases from becoming invalidated. Therefore let’s mark this as !Unpin. You can always opt back in to Unpin with an impl block, provided your API is still sound while unpinned.

1.38.0 · Source§

impl<T> Unpin for *const T
where T: ?Sized,

1.38.0 · Source§

impl<T> Unpin for *mut T
where T: ?Sized,

1.33.0 · Source§

impl<T> Unpin for &T
where T: ?Sized,

1.33.0 · Source§

impl<T> Unpin for &mut T
where T: ?Sized,

1.48.0 · Source§

impl<T> Unpin for Ready<T>

1.28.0 · Source§

impl<T> Unpin for NonZero<T>

1.33.0 · Source§

impl<T, A> Unpin for Rc<T, A>
where A: Allocator, T: ?Sized,

Source§

impl<T, A> Unpin for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

1.33.0 · Source§

impl<T, A> Unpin for Arc<T, A>
where A: Allocator, T: ?Sized,

Source§

impl<T, A> Unpin for UniqueArc<T, A>
where A: Allocator, T: ?Sized,

1.33.0 · Source§

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

Auto implementors§

§

impl Unpin for LogLevel

§

impl Unpin for Ordering

§

impl Unpin for SearchStep

§

impl Unpin for H160

§

impl Unpin for H256

§

impl Unpin for ParseBoolError

§

impl Unpin for Utf8Error

§

impl Unpin for FromUtf8Error

§

impl Unpin for FromUtf16Error

§

impl Unpin for IntoChars

§

impl Unpin for String

§

impl<'a> Unpin for Utf8Pattern<'a>

§

impl<'a> Unpin for CharSearcher<'a>

§

impl<'a> Unpin for Bytes<'a>

§

impl<'a> Unpin for CharIndices<'a>

§

impl<'a> Unpin for Chars<'a>

§

impl<'a> Unpin for EncodeUtf16<'a>

§

impl<'a> Unpin for EscapeDebug<'a>

§

impl<'a> Unpin for EscapeDefault<'a>

§

impl<'a> Unpin for EscapeUnicode<'a>

§

impl<'a> Unpin for Lines<'a>

§

impl<'a> Unpin for LinesAny<'a>

§

impl<'a> Unpin for SplitAsciiWhitespace<'a>

§

impl<'a> Unpin for SplitWhitespace<'a>

§

impl<'a> Unpin for Utf8Chunk<'a>

§

impl<'a> Unpin for Utf8Chunks<'a>

§

impl<'a> Unpin for fvm_std::prelude::string::Drain<'a>

§

impl<'a, 'b> Unpin for CharSliceSearcher<'a, 'b>

§

impl<'a, 'b> Unpin for StrSearcher<'a, 'b>

§

impl<'a, 'b, const N: usize> Unpin for CharArrayRefSearcher<'a, 'b, N>

§

impl<'a, F> Unpin for CharPredicateSearcher<'a, F>
where F: Unpin,

§

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

§

impl<'a, P> Unpin for MatchIndices<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

impl<'a, P> Unpin for Matches<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

impl<'a, P> Unpin for RMatchIndices<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

impl<'a, P> Unpin for RMatches<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

impl<'a, P> Unpin for RSplit<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

impl<'a, P> Unpin for RSplitN<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

impl<'a, P> Unpin for RSplitTerminator<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

impl<'a, P> Unpin for Split<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

impl<'a, P> Unpin for SplitInclusive<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

impl<'a, P> Unpin for SplitN<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

impl<'a, P> Unpin for SplitTerminator<'a, P>
where <P as Pattern>::Searcher<'a>: Unpin,

§

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

§

impl<'a, T, A> Unpin for fvm_std::prelude::vec::Drain<'a, T, A>

§

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

§

impl<'a, const N: usize> Unpin for CharArraySearcher<'a, N>

§

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

§

impl<T> Unpin for Option<T>
where T: Unpin,

§

impl<T> Unpin for HyperList<T>

§

impl<T> Unpin for Event<T>
where T: Unpin,

§

impl<T> Unpin for Reverse<T>
where T: Unpin,

§

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

§

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

§

impl<T, E> Unpin for Result<T, E>
where T: Unpin, E: Unpin,