Attribute Macro pin_project::unsafe_project
source · #[unsafe_project]
Expand description
An attribute that would create a projection struct covering all the fields.
For the field that use #[pin]
attribute, makes the pinned reference to the field.
For the other field, makes the unpinned reference to the field.
Safety
For the field that use #[pin]
attribute, three things need to be ensured:
- If the struct implements
Drop
, thedrop
method is not allowed to move the value of the field. - If the struct wants to implement
Unpin
, it has to do so conditionally: The struct can only implementUnpin
if the field’s type isUnpin
. - The struct must not be
#[repr(packed)]
.
For the other field, need to be ensured that the contained value not pinned in the current context.
Examples
use pin_project::unsafe_project;
use std::marker::Unpin;
use std::pin::Pin;
#[unsafe_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
}
}
impl<T: Unpin, U> Unpin for Foo<T, U> {} // Conditional Unpin impl
Note that borrowing the field where #[pin]
attribute is used multiple
times requires using .as_mut()
to avoid consuming the Pin
.