Skip to content

Commit a82c203

Browse files
committed
add pin projections to #[pin_data]
Make the `#[pin_data]` macro generate a `*Projection` struct that holds either `Pin<&mut Field>` or `&mut Field` for every field of the original struct. Which version is chosen depends on weather there is a `#[pin]` or not respectively. Access to this projected version is enabled through generating `fn project(self: Pin<&mut Self>) -> SelfProjection<'_>`. Signed-off-by: Benno Lossin <[email protected]>
1 parent 67fc903 commit a82c203

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- `#[pin_data]` now generates a `*Projection` struct similar to the `pin-project` crate.
13+
1014
## [0.0.10] - 2025-08-19
1115

1216
### Added

src/macros.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,17 @@ macro_rules! __pin_data {
831831
$($fields)*
832832
}
833833

834+
$crate::__pin_data!(make_pin_projections:
835+
@vis($vis),
836+
@name($name),
837+
@impl_generics($($impl_generics)*),
838+
@ty_generics($($ty_generics)*),
839+
@decl_generics($($decl_generics)*),
840+
@where($($whr)*),
841+
@pinned($($pinned)*),
842+
@not_pinned($($not_pinned)*),
843+
);
844+
834845
// We put the rest into this const item, because it then will not be accessible to anything
835846
// outside.
836847
const _: () = {
@@ -980,6 +991,55 @@ macro_rules! __pin_data {
980991
stringify!($($rest)*),
981992
);
982993
};
994+
(make_pin_projections:
995+
@vis($vis:vis),
996+
@name($name:ident),
997+
@impl_generics($($impl_generics:tt)*),
998+
@ty_generics($($ty_generics:tt)*),
999+
@decl_generics($($decl_generics:tt)*),
1000+
@where($($whr:tt)*),
1001+
@pinned($($(#[$($p_attr:tt)*])* $pvis:vis $p_field:ident : $p_type:ty),* $(,)?),
1002+
@not_pinned($($(#[$($attr:tt)*])* $fvis:vis $field:ident : $type:ty),* $(,)?),
1003+
) => {
1004+
$crate::macros::paste! {
1005+
#[doc(hidden)]
1006+
$vis struct [< $name Projection >] <'__pin, $($decl_generics)*> {
1007+
$($(#[$($p_attr)*])* $pvis $p_field : ::core::pin::Pin<&'__pin mut $p_type>,)*
1008+
$($(#[$($attr)*])* $fvis $field : &'__pin mut $type,)*
1009+
___pin_phantom_data: ::core::marker::PhantomData<&'__pin mut ()>,
1010+
}
1011+
1012+
impl<$($impl_generics)*> $name<$($ty_generics)*>
1013+
where $($whr)*
1014+
{
1015+
/// Pin-projects all fields of `Self`.
1016+
///
1017+
/// These fields are structurally pinned:
1018+
$(#[doc = ::core::concat!("- `", ::core::stringify!($p_field), "`")])*
1019+
///
1020+
/// These fields are **not** structurally pinned:
1021+
$(#[doc = ::core::concat!("- `", ::core::stringify!($field), "`")])*
1022+
$vis fn project<'__pin>(
1023+
self: ::core::pin::Pin<&'__pin mut Self>,
1024+
) -> [< $name Projection >] <'__pin, $($ty_generics)*> {
1025+
// SAFETY: we only give access to `&mut` for fields not structurally pinned.
1026+
let this = unsafe { ::core::pin::Pin::get_unchecked_mut(self) };
1027+
[< $name Projection >] {
1028+
$(
1029+
// SAFETY: `$p_field` is structurally pinned.
1030+
$(#[$($p_attr)*])*
1031+
$p_field : unsafe { ::core::pin::Pin::new_unchecked(&mut this.$p_field) },
1032+
)*
1033+
$(
1034+
$(#[$($attr)*])*
1035+
$field : &mut this.$field,
1036+
)*
1037+
___pin_phantom_data: ::core::marker::PhantomData,
1038+
}
1039+
}
1040+
}
1041+
}
1042+
};
9831043
(make_pin_data:
9841044
@pin_data($pin_data:ident),
9851045
@impl_generics($($impl_generics:tt)*),

0 commit comments

Comments
 (0)