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

Add support for #[project(!Unpin)] #76

Merged
merged 3 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 78 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,22 @@ pin-project supports this.
///
/// # `!Unpin`
///
/// If you want to ensure that [`Unpin`] is not implemented, use `#[pin]`
/// attribute for a [`PhantomPinned`] field.
/// If you want to make sure `Unpin` is not implemented, use the `#[project(!Unpin)]`
/// attribute.
///
/// ```
/// use pin_project_lite::pin_project;
///
/// pin_project! {
/// #[project(!Unpin)]
/// struct Struct<T> {
/// #[pin]
/// field: T,
/// }
/// }
/// ```
///
/// This is equivalent to using `#[pin]` attribute for a [`PhantomPinned`] field.
///
/// ```rust
/// use std::marker::PhantomPinned;
Expand All @@ -306,13 +320,14 @@ pin-project supports this.
/// pin_project! {
/// struct Struct<T> {
/// field: T,
/// #[pin] // <------ This `#[pin]` is required to make `Struct` to `!Unpin`.
/// #[pin]
/// _pin: PhantomPinned,
/// }
/// }
/// ```
///
/// Note that using [`PhantomPinned`] without `#[pin]` attribute has no effect.
/// Note that using [`PhantomPinned`] without `#[pin]` or `#[project(!Unpin)]`
/// attribute has no effect.
///
/// [`PhantomPinned`]: core::marker::PhantomPinned
/// [`Pin::as_mut`]: core::pin::Pin::as_mut
Expand All @@ -322,7 +337,7 @@ pin-project supports this.
macro_rules! pin_project {
($($tt:tt)*) => {
$crate::__pin_project_internal! {
[][][][]
[][][][][]
$($tt)*
}
};
Expand All @@ -344,6 +359,7 @@ macro_rules! __pin_project_expand {
[$($proj_mut_ident:ident)?]
[$($proj_ref_ident:ident)?]
[$($proj_replace_ident:ident)?]
[$($proj_not_unpin_mark:ident)?]
[$proj_vis:vis]
[$(#[$attrs:meta])* $vis:vis $struct_ty_ident:ident $ident:ident]
[$($def_generics:tt)*]
Expand Down Expand Up @@ -393,6 +409,7 @@ macro_rules! __pin_project_expand {
$crate::__pin_project_constant! {
[$(#[$attrs])* $vis $struct_ty_ident $ident]
[$($proj_mut_ident)?] [$($proj_ref_ident)?] [$($proj_replace_ident)?]
[$($proj_not_unpin_mark)?]
[$proj_vis]
[$($def_generics)*] [$($impl_generics)*]
[$($ty_generics)*] [$(where $($where_clause)*)?]
Expand All @@ -410,6 +427,7 @@ macro_rules! __pin_project_constant {
(
[$(#[$attrs:meta])* $vis:vis struct $ident:ident]
[$($proj_mut_ident:ident)?] [$($proj_ref_ident:ident)?] [$($proj_replace_ident:ident)?]
[$($proj_not_unpin_mark:ident)?]
[$proj_vis:vis]
[$($def_generics:tt)*]
[$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)*)?]
Expand Down Expand Up @@ -497,6 +515,7 @@ macro_rules! __pin_project_constant {
}

$crate::__pin_project_make_unpin_impl! {
[$($proj_not_unpin_mark)?]
[$vis $ident]
[$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?]
$(
Expand Down Expand Up @@ -546,6 +565,7 @@ macro_rules! __pin_project_constant {
(
[$(#[$attrs:meta])* $vis:vis enum $ident:ident]
[$($proj_mut_ident:ident)?] [$($proj_ref_ident:ident)?] [$($proj_replace_ident:ident)?]
[$($proj_not_unpin_mark:ident)?]
[$proj_vis:vis]
[$($def_generics:tt)*]
[$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)*)?]
Expand Down Expand Up @@ -622,6 +642,7 @@ macro_rules! __pin_project_constant {
}

$crate::__pin_project_make_unpin_impl! {
[$($proj_not_unpin_mark)?]
[$vis $ident]
[$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?]
$(
Expand Down Expand Up @@ -1118,6 +1139,7 @@ macro_rules! __pin_project_enum_make_proj_replace_method {
#[macro_export]
macro_rules! __pin_project_make_unpin_impl {
(
[]
[$vis:vis $ident:ident]
[$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)*)?]
$($field:tt)*
Expand Down Expand Up @@ -1162,6 +1184,23 @@ macro_rules! __pin_project_make_unpin_impl {
{
}
};
(
[$proj_not_unpin_mark:ident]
[$vis:vis $ident:ident]
[$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)*)?]
$($field:tt)*
) => {
#[doc(hidden)]
impl <'__pin, $($impl_generics)*> $crate::__private::Unpin for $ident <$($ty_generics)*>
where
(
::core::marker::PhantomData<&'__pin ()>,
::core::marker::PhantomPinned,
): $crate::__private::Unpin
$(, $($where_clause)*)?
{
}
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -1360,6 +1399,7 @@ macro_rules! __pin_project_internal {
[]
[$($proj_ref_ident:ident)?]
[$($proj_replace_ident:ident)?]
[$( ! $proj_not_unpin_mark:ident)?]
[$($attrs:tt)*]

#[project = $proj_mut_ident:ident]
Expand All @@ -1369,6 +1409,7 @@ macro_rules! __pin_project_internal {
[$proj_mut_ident]
[$($proj_ref_ident)?]
[$($proj_replace_ident)?]
[$( ! $proj_not_unpin_mark)?]
[$($attrs)*]
$($tt)*
}
Expand All @@ -1378,6 +1419,7 @@ macro_rules! __pin_project_internal {
[$($proj_mut_ident:ident)?]
[]
[$($proj_replace_ident:ident)?]
[$( ! $proj_not_unpin_mark:ident)?]
[$($attrs:tt)*]

#[project_ref = $proj_ref_ident:ident]
Expand All @@ -1387,6 +1429,7 @@ macro_rules! __pin_project_internal {
[$($proj_mut_ident)?]
[$proj_ref_ident]
[$($proj_replace_ident)?]
[$( ! $proj_not_unpin_mark)?]
[$($attrs)*]
$($tt)*
}
Expand All @@ -1396,6 +1439,7 @@ macro_rules! __pin_project_internal {
[$($proj_mut_ident:ident)?]
[$($proj_ref_ident:ident)?]
[]
[$( ! $proj_not_unpin_mark:ident)?]
[$($attrs:tt)*]

#[project_replace = $proj_replace_ident:ident]
Expand All @@ -1405,6 +1449,27 @@ macro_rules! __pin_project_internal {
[$($proj_mut_ident)?]
[$($proj_ref_ident)?]
[$proj_replace_ident]
[$( ! $proj_not_unpin_mark)?]
[$($attrs)*]
$($tt)*
}
};
// parsing !Unpin
(
[$($proj_mut_ident:ident)?]
[$($proj_ref_ident:ident)?]
[$($proj_replace_ident:ident)?]
[]
[$($attrs:tt)*]

#[project( ! $proj_not_unpin_mark:ident)]
$($tt:tt)*
) => {
$crate::__pin_project_internal! {
[$($proj_mut_ident)?]
[$($proj_ref_ident)?]
[$($proj_replace_ident)?]
[ ! $proj_not_unpin_mark]
[$($attrs)*]
$($tt)*
}
Expand All @@ -1415,6 +1480,7 @@ macro_rules! __pin_project_internal {
[$($proj_mut_ident:ident)?]
[$($proj_ref_ident:ident)?]
[$($proj_replace_ident:ident)?]
[$( ! $proj_not_unpin_mark:ident)?]
[$($attrs:tt)*]

#[$($attr:tt)*]
Expand All @@ -1424,6 +1490,7 @@ macro_rules! __pin_project_internal {
[$($proj_mut_ident)?]
[$($proj_ref_ident)?]
[$($proj_replace_ident)?]
[$( ! $proj_not_unpin_mark)?]
[$($attrs)* #[$($attr)*]]
$($tt)*
}
Expand All @@ -1434,6 +1501,7 @@ macro_rules! __pin_project_internal {
[$($proj_mut_ident:ident)?]
[$($proj_ref_ident:ident)?]
[$($proj_replace_ident:ident)?]
[$( ! $proj_not_unpin_mark:ident)?]
[$($attrs:tt)*]
pub $struct_ty_ident:ident $ident:ident
$($tt:tt)*
Expand All @@ -1442,6 +1510,7 @@ macro_rules! __pin_project_internal {
[$($proj_mut_ident)?]
[$($proj_ref_ident)?]
[$($proj_replace_ident)?]
[$($proj_not_unpin_mark)?]
[$($attrs)*]
[pub $struct_ty_ident $ident pub(crate)]
$($tt)*
Expand All @@ -1451,6 +1520,7 @@ macro_rules! __pin_project_internal {
[$($proj_mut_ident:ident)?]
[$($proj_ref_ident:ident)?]
[$($proj_replace_ident:ident)?]
[$( ! $proj_not_unpin_mark:ident)?]
[$($attrs:tt)*]
$vis:vis $struct_ty_ident:ident $ident:ident
$($tt:tt)*
Expand All @@ -1459,6 +1529,7 @@ macro_rules! __pin_project_internal {
[$($proj_mut_ident)?]
[$($proj_ref_ident)?]
[$($proj_replace_ident)?]
[$($proj_not_unpin_mark)?]
[$($attrs)*]
[$vis $struct_ty_ident $ident $vis]
$($tt)*
Expand All @@ -1473,6 +1544,7 @@ macro_rules! __pin_project_parse_generics {
[$($proj_mut_ident:ident)?]
[$($proj_ref_ident:ident)?]
[$($proj_replace_ident:ident)?]
[$($proj_not_unpin_mark:ident)?]
[$($attrs:tt)*]
[$vis:vis $struct_ty_ident:ident $ident:ident $proj_ty_vis:vis]
$(<
Expand Down Expand Up @@ -1500,6 +1572,7 @@ macro_rules! __pin_project_parse_generics {
[$($proj_mut_ident)?]
[$($proj_ref_ident)?]
[$($proj_replace_ident)?]
[$($proj_not_unpin_mark)?]
[$proj_ty_vis]
[$($attrs)* $vis $struct_ty_ident $ident]
[$(<
Expand Down
95 changes: 95 additions & 0 deletions tests/expand/not_unpin/enum.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use pin_project_lite::pin_project;
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> {
#[inline]
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,
}
}
}
#[inline]
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,
}
}
}
}
#[doc(hidden)]
impl<'__pin, T, U> ::pin_project_lite::__private::Unpin for Enum<T, U>
where
(
::core::marker::PhantomData<&'__pin ()>,
::core::marker::PhantomPinned,
): ::pin_project_lite::__private::Unpin,
{}
trait MustNotImplDrop {}
#[allow(clippy::drop_bounds, drop_bounds)]
impl<T: ::pin_project_lite::__private::Drop> MustNotImplDrop for T {}
impl<T, U> MustNotImplDrop for Enum<T, U> {}
};
fn main() {}
17 changes: 17 additions & 0 deletions tests/expand/not_unpin/enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use pin_project_lite::pin_project;

pin_project! {
#[project(!Unpin)]
#[project = EnumProj]
#[project_ref = EnumProjRef]
enum Enum<T, U> {
Struct {
#[pin]
pinned: T,
unpinned: U,
},
Unit,
}
}

fn main() {}
Loading