[−][src]Crate pin_project
A crate for safe and ergonomic pin-projection.
This crate provides the following attribute macros:
pin_project
- An attribute that creates a projection struct covering all the fields.pinned_drop
- An attribute for annotating a function that implementsDrop
.project
- An attribute to support pattern matching.
NOTE: While this crate supports stable Rust, it currently requires
nightly Rust in order for rustdoc to correctly document auto-generated
Unpin
impls. This does not affect the runtime functionality of this crate,
nor does it affect the safety of the api provided by this crate.
Examples
pin_project
attribute creates a projection struct covering all the fields.
use pin_project::pin_project; use std::pin::Pin; #[pin_project] struct Foo<T, U> { #[pin] future: T, field: U, } impl<T, U> Foo<T, U> { fn baz(mut self: Pin<&mut Self>) { let this = self.project(); let _: Pin<&mut T> = this.future; // Pinned reference to the field let _: &mut U = this.field; // Normal reference to the field } }
Code like this will be generated:
struct Foo<T, U> { future: T, field: U, } struct __FooProjection<'__a, T, U> { future: ::core::pin::Pin<&'__a mut T>, field: &'__a mut U, } impl<T, U> Foo<T, U> { fn project<'__a>(self: ::core::pin::Pin<&'__a mut Self>) -> __FooProjection<'__a, T, U> { unsafe { let this = ::core::pin::Pin::get_unchecked_mut(self); __FooProjection { future: ::core::pin::Pin::new_unchecked(&mut this.future), field: &mut this.field, } } } } // Automatically create the Drop implementation. impl<T, U> Drop for Foo<T, U> { fn drop(&mut self) { // Do nothing. The precense of this Drop // impl ensures that the user can't provide one of their own } } // Automatically create the appropriate conditional Unpin implementation. impl<T, U> Unpin for Foo<T, U> where T: Unpin {}
See pin_project
attribute for more details.
Traits
UnsafeUnpin | A trait used for custom implementations of |