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

Support custom Drop implementation #25

Closed taiki-e closed 3 years ago

taiki-e commented 4 years ago
use pin_project_lite::pin_project;

pin_project! {
    pub struct Struct<'a> {
        was_dropped: &'a mut bool,
        #[pin]
        field: u8,
    }
    impl PinnedDrop for Struct<'_> {
        fn drop(this: Pin<&mut Self>) { // <----- NOTE: this is not `self`
            **this.project().was_dropped = true;
        }
    }
}

fn main() {
    let mut was_dropped = false;
    drop(Struct { was_dropped: &mut was_dropped, field: 42 });
    assert!(was_dropped);
}

It's clear how to pass options(=arguments in #[pin_project]), and we don't have to think about how to pass options. So, this is easiest to implement compared to other options. For all other options, we have to start by deciding how to pass the options, which can be a hard task, whether simple or complex to implement.

jplatte commented 3 years ago

What's t he current state of this?

taiki-e commented 3 years ago

The implementation is basically done except for this fixme and lack of documentation. The latter is probably easy, as we can just copy the description of this PR and pin-project's docs.

https://github.com/taiki-e/pin-project-lite/blob/30f66ddd051092ec49756b87233e7dd18c9ae40a/src/lib.rs#L1204

taiki-e commented 3 years ago

bors r+

Well, documentation is not a blocker. Filed #61 to follow-up.

bors[bot] commented 3 years ago

Canceled.

taiki-e commented 3 years ago

bors r+

bors[bot] commented 3 years ago

Build succeeded:

taiki-e commented 3 years ago

Released in v0.2.7