Macro poplar_util::unsafe_pinned

source ·
macro_rules! unsafe_pinned {
    ($v:vis $f:ident: $t:ty) => { ... };
}
Expand description

A pinned projection of a struct field.

To make using this macro safe, 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)].
use poplar_util::unsafe_pinned;
use core::marker::Unpin;
use core::pin::Pin;

struct Foo<T> {
    field: T,
}

impl<T> Foo<T> {
    unsafe_pinned!(field: T);

    fn baz(mut self: Pin<&mut Self>) {
        let _: Pin<&mut T> = self.field();
    }
}

impl<T: Unpin> Unpin for Foo<T> {}

Note that borrowing the field multiple times requires using .as_mut() to avoid consuming the Pin.