-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
11 changed files
with
464 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use pin_project_lite::pin_project; | ||
use std::pin::Pin; | ||
enum Enum<T, U> { | ||
Struct { pinned: T, unpinned: U }, | ||
Unit, | ||
} | ||
#[allow(dead_code)] | ||
#[allow(single_use_lifetimes)] | ||
#[allow(clippy::unknown_clippy_lints)] | ||
#[allow(clippy::mut_mut)] | ||
#[allow(clippy::redundant_pub_crate)] | ||
#[allow(clippy::ref_option_ref)] | ||
#[allow(clippy::type_repetition_in_bounds)] | ||
enum EnumProj<'__pin, T, U> | ||
where | ||
Enum<T, U>: '__pin, | ||
{ | ||
Struct { | ||
pinned: ::pin_project_lite::__private::Pin<&'__pin mut (T)>, | ||
unpinned: &'__pin mut (U), | ||
}, | ||
Unit, | ||
} | ||
#[allow(dead_code)] | ||
#[allow(single_use_lifetimes)] | ||
#[allow(clippy::unknown_clippy_lints)] | ||
#[allow(clippy::mut_mut)] | ||
#[allow(clippy::redundant_pub_crate)] | ||
#[allow(clippy::ref_option_ref)] | ||
#[allow(clippy::type_repetition_in_bounds)] | ||
enum EnumProjRef<'__pin, T, U> | ||
where | ||
Enum<T, U>: '__pin, | ||
{ | ||
Struct { | ||
pinned: ::pin_project_lite::__private::Pin<&'__pin (T)>, | ||
unpinned: &'__pin (U), | ||
}, | ||
Unit, | ||
} | ||
#[allow(single_use_lifetimes)] | ||
#[allow(clippy::unknown_clippy_lints)] | ||
#[allow(clippy::used_underscore_binding)] | ||
const _: () = { | ||
impl<T, U> Enum<T, U> { | ||
fn project<'__pin>( | ||
self: ::pin_project_lite::__private::Pin<&'__pin mut Self>, | ||
) -> EnumProj<'__pin, T, U> { | ||
unsafe { | ||
match self.get_unchecked_mut() { | ||
Self::Struct { pinned, unpinned } => EnumProj::Struct { | ||
pinned: ::pin_project_lite::__private::Pin::new_unchecked(pinned), | ||
unpinned: unpinned, | ||
}, | ||
Self::Unit => EnumProj::Unit, | ||
} | ||
} | ||
} | ||
fn project_ref<'__pin>( | ||
self: ::pin_project_lite::__private::Pin<&'__pin Self>, | ||
) -> EnumProjRef<'__pin, T, U> { | ||
unsafe { | ||
match self.get_ref() { | ||
Self::Struct { pinned, unpinned } => EnumProjRef::Struct { | ||
pinned: ::pin_project_lite::__private::Pin::new_unchecked(pinned), | ||
unpinned: unpinned, | ||
}, | ||
Self::Unit => EnumProjRef::Unit, | ||
} | ||
} | ||
} | ||
} | ||
#[allow(non_snake_case)] | ||
struct __Origin<'__pin, T, U> { | ||
__dummy_lifetime: ::pin_project_lite::__private::PhantomData<&'__pin ()>, | ||
Struct: (T, ::pin_project_lite::__private::AlwaysUnpin<U>), | ||
Unit: (), | ||
} | ||
impl<'__pin, T, U> ::pin_project_lite::__private::Unpin for Enum<T, U> where | ||
__Origin<'__pin, T, U>: ::pin_project_lite::__private::Unpin | ||
{ | ||
} | ||
impl<T, U> ::pin_project_lite::__private::Drop for Enum<T, U> { | ||
fn drop(&mut self) { | ||
fn __drop_inner<T, U>(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) }; | ||
__drop_inner(pinned_self); | ||
} | ||
} | ||
}; | ||
fn main() {} |
Oops, something went wrong.