[][src]Macro pin_project_lite::pin_project

macro_rules! pin_project {
    ($($tt:tt)*) => { ... };

A macro that creates a projection type covering all the fields of struct.

This macro creates a projection type 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.

And the following methods are implemented on the original type:

fn project(self: Pin<&mut Self>) -> Projection<'_>;
fn project_ref(self: Pin<&Self>) -> ProjectionRef<'_>;

The visibility of the projected type and projection method is based on the original type. However, if the visibility of the original type is pub, the visibility of the projected type and the projection method is pub(crate).


pin_project! macro guarantees safety in much the same way as pin-project crate. Both are completely safe unless you write other unsafe code.

See pin-project crate for more details.


use pin_project_lite::pin_project;
use std::pin::Pin;

pin_project! {
    struct Struct<T, U> {
        pinned: T,
        unpinned: U,

impl<T, U> Struct<T, U> {
    fn method(self: Pin<&mut Self>) {
        let this = self.project();
        let _: Pin<&mut T> = this.pinned; // Pinned reference to the field
        let _: &mut U = this.unpinned; // Normal reference to the field

If you want to call the project() method multiple times or later use the original Pin type, it needs to use .as_mut() to avoid consuming the Pin.

If you want to ensure that Unpin is not implemented, use #[pin] attribute for a PhantomPinned field.

use pin_project_lite::pin_project;
use std::marker::PhantomPinned;

pin_project! {
    struct Struct<T> {
        field: T,
        _pin: PhantomPinned,

Note that using PhantomPinned without #[pin] attribute has no effect.