taiki-e / pin-project-lite

A lightweight version of pin-project written with declarative macros.
https://docs.rs/pin-project-lite
Apache License 2.0
216 stars 15 forks source link

Enum support for pin_project! macro #28

Closed taiki-e closed 3 years ago

taiki-e commented 4 years ago

Like pin-project, by passing an argument with the same name as the method to the attribute, you can name the projection type returned from the method. This allows you to use pattern matching on the projected types.

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

pin_project! {
    // The `#[project]` (and `#[project_ref]`) attribute must precede the other attributes except for `#[doc]`:
    #[project = EnumProj] 
    #[project_ref = EnumProjRef]
    #[derive(Debug)]
    enum Enum<T, U> {
        Struct {
            #[pin]
            pinned: T,
            unpinned: U,
        },
        Unit,
    }
}

impl<T, U> Enum<T, U> {
    fn method(self: Pin<&mut Self>) {
        match self.project() {
            EnumProj::Struct { pinned, unpinned } => {
                let _: Pin<&mut T> = pinned;
                let _: &mut U = unpinned;
            }
            EnumProj::Unit => {}
        }
    }
}

Also, this does not provide support to tuple variants for the same reason that it does not support tuple structs.

taiki-e commented 3 years ago

I'm wondering what syntax is preferable, so I would appreciate any feedback.

ghost commented 3 years ago

What if the attribute syntax was used, similar to the one in pin-project?

So perhaps something like #[project = EnumProj] above the enum definition.

taiki-e commented 3 years ago

Ah, that looks like a good idea. There may be a limitation like #3, but I'll try it.

taiki-e commented 3 years ago

I've tried the attribute syntax and it looks good overall. (Thanks @stjepang for the suggestion!)

Limitations of the current implementation I know of are:

ghost commented 3 years ago

It's okay if there are a few limitations with known workarounds. I think it's amazing that you managed to pull this off at all without procedural macros :)

taiki-e commented 3 years ago

I think the implementation is now basically complete, but I'll look into limitations and minor bugs before the merge.

taiki-e commented 3 years ago

Filed #36 to track diagnostics issues. I'm okay with merge this.

bors r+

bors[bot] commented 3 years ago

Build succeeded:

taiki-e commented 3 years ago

Published in 0.2.0.