Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom Drop implementation #25

Merged
merged 1 commit into from Jun 14, 2021
Merged

Support custom Drop implementation #25

merged 1 commit into from Jun 14, 2021

Conversation

taiki-e
Copy link
Owner

@taiki-e taiki-e commented Jun 22, 2020

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.

@taiki-e taiki-e added C-enhancement Category: A new feature or an improvement for an existing one I-needs-decision labels Jun 22, 2020
@taiki-e taiki-e force-pushed the pinned-drop branch 2 times, most recently from 6f36e08 to 24ea784 Compare September 21, 2020 16:30
@taiki-e taiki-e changed the title [experiment] Support custom Drop implementation Support custom Drop implementation Sep 21, 2020
@taiki-e taiki-e changed the title Support custom Drop implementation [wip] Support custom Drop implementation Sep 21, 2020
@taiki-e taiki-e force-pushed the pinned-drop branch 5 times, most recently from 9ba5c56 to c69d44f Compare October 2, 2020 12:28
Comment on lines 3 to 13
pin_project! {
pub struct S {
#[pin]
field: u8,
}
impl PinnedDrop for S {
fn drop(self: Pin<&mut Self>) {
self.__drop_inner();
}
}
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm..., I expected this to fail to compile, but it seems that it actually compiles.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(resolved by using normal arg instead self)

@taiki-e taiki-e force-pushed the pinned-drop branch 2 times, most recently from ffcdb4a to 2c4f45d Compare October 24, 2020 13:59
@taiki-e taiki-e removed C-enhancement Category: A new feature or an improvement for an existing one relnotes labels Jan 7, 2021
@taiki-e taiki-e force-pushed the pinned-drop branch 2 times, most recently from 36fdbb2 to 9bd49ab Compare January 12, 2021 05:22
@taiki-e taiki-e changed the title [wip] Support custom Drop implementation Support custom Drop implementation Jan 12, 2021
@taiki-e taiki-e force-pushed the pinned-drop branch 2 times, most recently from 95ecba3 to 8161149 Compare January 13, 2021 06:58
Base automatically changed from master to main March 4, 2021 02:33
@taiki-e taiki-e force-pushed the pinned-drop branch 2 times, most recently from 49b091d to 8fbccc7 Compare March 4, 2021 15:30
@jplatte
Copy link

jplatte commented Jun 4, 2021

What's t he current state of this?

@taiki-e
Copy link
Owner Author

taiki-e commented Jun 7, 2021

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.

// FIXME: check $_ident + $_ty_generics == $self_ty

@taiki-e
Copy link
Owner Author

taiki-e commented Jun 14, 2021

bors r+

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

bors bot added a commit that referenced this pull request Jun 14, 2021
25: Support custom Drop implementation r=taiki-e a=taiki-e

```rust
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.


Co-authored-by: Taiki Endo <te316e89@gmail.com>
@taiki-e taiki-e mentioned this pull request Jun 14, 2021
@bors
Copy link
Contributor

bors bot commented Jun 14, 2021

Canceled.

@taiki-e
Copy link
Owner Author

taiki-e commented Jun 14, 2021

bors r+

@bors
Copy link
Contributor

bors bot commented Jun 14, 2021

Build succeeded:

@bors bors bot merged commit 4e93911 into main Jun 14, 2021
@bors bors bot deleted the pinned-drop branch June 14, 2021 01:14
@taiki-e
Copy link
Owner Author

taiki-e commented Jun 26, 2021

Released in v0.2.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants