Skip to content

Remove pin_project! macro and add #[pinned_drop] attribute#33

Merged
bors[bot] merged 1 commit intomasterfrom
pinned-drop
Aug 10, 2019
Merged

Remove pin_project! macro and add #[pinned_drop] attribute#33
bors[bot] merged 1 commit intomasterfrom
pinned-drop

Conversation

@taiki-e
Copy link
Owner

@taiki-e taiki-e commented Aug 6, 2019

This removes pin_project! block and adds #[pinned_drop] attribute as proc-macro-attribute.
If you want a custom Drop implementation, you need to pass the PinnedDrop argument to pin_project attribute instead of defining items in pin_project! block.

In the previous implementation, #[pinned_drop] implemented Drop directly, but this PR changes it to implement this via a private unsafe trait method.

Also, this renames pin_projectable to pin_project.

cc #21, #26
Related: #18 (comment)

Examples

Before:

use std::fmt::Debug;
use pin_project::{pin_project, pin_projectable};
use std::pin::Pin;

pin_project! {
    #[pin_projectable]
    pub struct Foo<T: Debug, U: Debug> {
        #[pin] pinned_field: T,
        unpin_field: U
    }

    #[pinned_drop]
    fn my_drop_fn<T: Debug, U: Debug>(foo: Pin<&mut Foo<T, U>>) {
        let foo = foo.project();
        println!("Dropping pinned field: {:?}", foo.pinned_field);
        println!("Dropping unpin field: {:?}", foo.unpin_field);
    }
}

After:

use std::fmt::Debug;
use pin_project::{pin_project, pinned_drop};
use std::pin::Pin;

#[pin_project(PinnedDrop)]
pub struct Foo<T: Debug, U: Debug> {
    #[pin] pinned_field: T,
    unpin_field: U
}

#[pinned_drop]
fn my_drop_fn<T: Debug, U: Debug>(foo: Pin<&mut Foo<T, U>>) {
    let foo = foo.project();
    println!("Dropping pinned field: {:?}", foo.pinned_field);
    println!("Dropping unpin field: {:?}", foo.unpin_field);
}

TODO

  • Update docs

@taiki-e taiki-e changed the title Remove pin_project! function-like macro and add #[pinned_drop] attribute Remove pin_project! macro and add #[pinned_drop] attribute Aug 6, 2019
@Aaron1011
Copy link
Collaborator

@taiki-e: Looks good to me!

@taiki-e taiki-e force-pushed the pinned-drop branch 3 times, most recently from 2ab9380 to 3668b71 Compare August 10, 2019 19:10
@taiki-e taiki-e marked this pull request as ready for review August 10, 2019 19:11
@taiki-e taiki-e force-pushed the pinned-drop branch 8 times, most recently from a459362 to e98b9e8 Compare August 10, 2019 20:36
@taiki-e taiki-e added this to the v0.4 milestone Aug 10, 2019
@taiki-e
Copy link
Owner Author

taiki-e commented Aug 10, 2019

bors r+

bors bot added a commit that referenced this pull request Aug 10, 2019
33: Remove pin_project! macro and add #[pinned_drop] attribute r=taiki-e a=taiki-e

This removes `pin_project!` block and adds `#[pinned_drop]` attribute as proc-macro-attribute.
If you want a custom `Drop` implementation, you need to pass the `PinnedDrop` argument to `pin_project` attribute instead of defining items in `pin_project!` block.

In the previous implementation, `#[pinned_drop]` implemented `Drop` directly, but this PR changes it to implement this via a private unsafe trait method.

Also, this renames `pin_projectable` to `pin_project`.

cc #21, #26
Related: #18 (comment)

### Examples
Before:
```rust
use std::fmt::Debug;
use pin_project::{pin_project, pin_projectable};
use std::pin::Pin;

pin_project! {
    #[pin_projectable]
    pub struct Foo<T: Debug, U: Debug> {
        #[pin] pinned_field: T,
        unpin_field: U
    }

    #[pinned_drop]
    fn my_drop_fn<T: Debug, U: Debug>(foo: Pin<&mut Foo<T, U>>) {
        let foo = foo.project();
        println!("Dropping pinned field: {:?}", foo.pinned_field);
        println!("Dropping unpin field: {:?}", foo.unpin_field);
    }
}
```

After:
```rust
use std::fmt::Debug;
use pin_project::{pin_project, pinned_drop};
use std::pin::Pin;

#[pin_project(PinnedDrop)]
pub struct Foo<T: Debug, U: Debug> {
    #[pin] pinned_field: T,
    unpin_field: U
}

#[pinned_drop]
fn my_drop_fn<T: Debug, U: Debug>(foo: Pin<&mut Foo<T, U>>) {
    let foo = foo.project();
    println!("Dropping pinned field: {:?}", foo.pinned_field);
    println!("Dropping unpin field: {:?}", foo.unpin_field);
}
```

### TODO
- [x] Update docs



Co-authored-by: Taiki Endo <te316e89@gmail.com>
@bors
Copy link
Contributor

bors bot commented Aug 10, 2019

Build succeeded

  • taiki-e.pin-project

@bors bors bot merged commit 127c1cf into master Aug 10, 2019
@taiki-e taiki-e deleted the pinned-drop branch August 10, 2019 22:02
@taiki-e taiki-e added the A-drop Area: #[pinned_drop] and Drop label Sep 24, 2019
@taiki-e taiki-e mentioned this pull request Sep 25, 2019
bors bot added a commit that referenced this pull request Sep 25, 2019
109: Release 0.4.0 r=taiki-e a=taiki-e

cc #21

### Changes since the latest 0.3 release:

* **Pin projection has become a safe operation.** In the absence of other unsafe code that you write, it is impossible to cause undefined behavior. (#18)

* `#[unsafe_project]` attribute has been replaced with `#[pin_project]` attribute. (#18, #33)

* The `Unpin` argument has been removed - an `Unpin` impl is now generated by default. (#18)

* Drop impls must be specified with `#[pinned_drop]` instead of via a normal `Drop` impl. (#18, #33, #86)

* `Unpin` impls must be specified with an impl of `UnsafeUnpin`, instead of implementing the normal `Unpin` trait. (#18)

* `#[pin_project]` attribute now determines the visibility of the projection type/method is based on the original type. (#96)

* `#[pin_project]` can now be used for public type with private field types. (#53)

* `#[pin_project]` can now interoperate with `#[cfg()]`. (#77)

* Added `project_ref` method to `#[pin_project]` types. (#93)

* Added `#[project_ref]` attribute. (#93)

* Removed "project_attr" feature and always enable `#[project]` attribute. (#94)

* `#[project]` attribute can now be used for `impl` blocks. (#46)

* `#[project]` attribute can now be used for `use` statements. (#85)

* `#[project]` attribute now supports `match` expressions at the position of the initializer expression of `let` expressions. (#51)

### Changes since the 0.4.0-beta.1 release:

* Fixed an issue that caused an error when using `#[pin_project(UnsafeUnpin)]` and not providing a manual `UnsafeUnpin` implementation on a type with no generics or lifetime. (#107)


Co-authored-by: Taiki Endo <te316e89@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-drop Area: #[pinned_drop] and Drop

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants