Attribute Macro pin_project::unsafe_project

source ·
#[unsafe_project]
Expand description

An attribute that would create a projection struct covering all the fields.

This attribute creates a struct according to the following rules:

  • For the field that uses #[pin] attribute, makes the pinned reference to the field.
  • For the other fields, makes the unpinned reference to the field.

Safety

For the field that uses #[pin] attribute, three things need to be ensured:

  • If the struct implements Drop, the drop 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 implement Unpin if the field’s type is Unpin.
  • The struct must not be #[repr(packed)].

For the other fields, 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.