Skip to content

Commit

Permalink
self -> this
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 12, 2021
1 parent 3a386f0 commit 9bd49ab
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 51 deletions.
30 changes: 6 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ macro_rules! __pin_project_internal {
),*
)?
{
fn drop($(mut $self1:ident)? $($self2:ident)?: Pin<&mut Self>) {
fn drop($($arg:ident)+: Pin<&mut Self>) {
$($tt:tt)*
}
}
Expand Down Expand Up @@ -1341,36 +1341,18 @@ macro_rules! __pin_project_internal {
//
// Users can implement [`Drop`] safely using `pin_project!` and can drop a
// type that implements `PinnedDrop` using the [`drop`] function safely.
trait __DropInner {
fn __drop_inner(self: $crate::__private::Pin<&mut Self>);
}
impl $(<
$( $lifetime $(: $lifetime_bound)? ,)*
$( $generics
$(: $generics_bound)?
$(: ?$generics_unsized_bound)?
$(: $generics_lifetime_bound)?
),*
>)? __DropInner for $self_ty
$(where
$( $where_clause_ty
$(: $where_clause_bound)?
$(: ?$where_clause_unsized_bound)?
$(: $where_clause_lifetime_bound)?
),*
)?
{
fn __drop_inner($(mut $self1)? $($self2)?: $crate::__private::Pin<&mut Self>) {
$($tt)*
}
fn __drop_inner($($arg)+: $crate::__private::Pin<&mut $self_ty>) {
// A dummy `__drop_inner` function to prevent users call outer `__drop_inner`.
fn __drop_inner() {}
$($tt)*
}

// Safety - we're in 'drop', so we know that 'self' will
// never move again.
let pinned_self = unsafe { $crate::__private::Pin::new_unchecked(self) };
// We call `__drop_inner` only once. Since `__DropInner::__drop_inner`
// is not accessible by the users, it is never called again.
__DropInner::__drop_inner(pinned_self);
__drop_inner(pinned_self);
}
}
};
Expand Down
12 changes: 4 additions & 8 deletions tests/expand/tests/expand/pinned_drop-enum.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,12 @@ const _: () = {
}
impl<T, U> ::pin_project_lite::__private::Drop for Enum<T, U> {
fn drop(&mut self) {
trait __DropInner {
fn __drop_inner(self: ::pin_project_lite::__private::Pin<&mut Self>);
}
impl<T, U> __DropInner for Enum<T, U> {
fn __drop_inner(self: ::pin_project_lite::__private::Pin<&mut Self>) {
let _ = self;
}
fn __drop_inner(this: ::pin_project_lite::__private::Pin<&mut Enum<T, U>>) {
fn __drop_inner() {}
let _ = this;
}
let pinned_self = unsafe { ::pin_project_lite::__private::Pin::new_unchecked(self) };
__DropInner::__drop_inner(pinned_self);
__drop_inner(pinned_self);
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions tests/expand/tests/expand/pinned_drop-enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pin_project! {
Unit,
}
impl<T, U> PinnedDrop for Enum<T, U> {
fn drop(self: Pin<&mut Self>) {
let _ = self;
fn drop(this: Pin<&mut Self>) {
let _ = this;
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions tests/expand/tests/expand/pinned_drop-struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,12 @@ const _: () = {
}
impl<T, U> ::pin_project_lite::__private::Drop for Struct<T, U> {
fn drop(&mut self) {
trait __DropInner {
fn __drop_inner(self: ::pin_project_lite::__private::Pin<&mut Self>);
}
impl<T, U> __DropInner for Struct<T, U> {
fn __drop_inner(self: ::pin_project_lite::__private::Pin<&mut Self>) {
let _ = self;
}
fn __drop_inner(this: ::pin_project_lite::__private::Pin<&mut Struct<T, U>>) {
fn __drop_inner() {}
let _ = this;
}
let pinned_self = unsafe { ::pin_project_lite::__private::Pin::new_unchecked(self) };
__DropInner::__drop_inner(pinned_self);
__drop_inner(pinned_self);
}
}
#[forbid(safe_packed_borrows)]
Expand Down
4 changes: 2 additions & 2 deletions tests/expand/tests/expand/pinned_drop-struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pin_project! {
unpinned: U,
}
impl<T, U> PinnedDrop for Struct<T, U> {
fn drop(self: Pin<&mut Self>) {
let _ = self;
fn drop(this: Pin<&mut Self>) {
let _ = this;
}
}
}
Expand Down
23 changes: 18 additions & 5 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,19 +620,32 @@ fn attrs() {
#[test]
fn pinned_drop() {
pin_project! {
pub struct Struct<'a> {
pub struct Struct1<'a> {
was_dropped: &'a mut bool,
#[pin]
field: u8,
}
impl PinnedDrop for Struct<'_> {
fn drop(self: Pin<&mut Self>) {
**self.project().was_dropped = true;
impl PinnedDrop for Struct1<'_> {
fn drop(this: Pin<&mut Self>) {
**this.project().was_dropped = true;
}
}
}

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

pin_project! {
pub struct Struct2<'a> {
was_dropped: &'a mut bool,
#[pin]
field: u8,
}
impl PinnedDrop for Struct2<'_> {
fn drop(mut this: Pin<&mut Self>) {
**this.as_mut().project().was_dropped = true;
}
}
}
}
4 changes: 2 additions & 2 deletions tests/ui/pinned_drop-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pin_project! {
field: u8,
}
impl PinnedDrop for S {
fn drop(self: Pin<&mut Self>) {
self.__drop_inner();
fn drop(this: Pin<&mut Self>) {
this.__drop_inner();
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/pinned_drop-1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error[E0599]: no method named `__drop_inner` found for struct `Pin<&mut S>` in the current scope
--> $DIR/pinned_drop-1.rs:10:18
|
10 | this.__drop_inner();
| ^^^^^^^^^^^^ method not found in `Pin<&mut S>`

0 comments on commit 9bd49ab

Please sign in to comment.