Skip to content

Commit 619db96

Browse files
committed
rust: pin-init: 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<'_>`. [ Adapt workqueue to use the new projection instead of its own, custom one - Benno ] Reviewed-by: Gary Guo <gary@garyguo.net> Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Benno Lossin <lossin@kernel.org>
1 parent d49c563 commit 619db96

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

rust/kernel/workqueue.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,11 @@ struct ClosureWork<T> {
356356
func: Option<T>,
357357
}
358358

359-
impl<T> ClosureWork<T> {
360-
fn project(self: Pin<&mut Self>) -> &mut Option<T> {
361-
// SAFETY: The `func` field is not structurally pinned.
362-
unsafe { &mut self.get_unchecked_mut().func }
363-
}
364-
}
365-
366359
impl<T: FnOnce()> WorkItem for ClosureWork<T> {
367360
type Pointer = Pin<KBox<Self>>;
368361

369362
fn run(mut this: Pin<KBox<Self>>) {
370-
if let Some(func) = this.as_mut().project().take() {
363+
if let Some(func) = this.as_mut().project().func.take() {
371364
(func)()
372365
}
373366
}

rust/pin-init/src/macros.rs

Lines changed: 61 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,56 @@ 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+
#[inline]
1023+
$vis fn project<'__pin>(
1024+
self: ::core::pin::Pin<&'__pin mut Self>,
1025+
) -> [< $name Projection >] <'__pin, $($ty_generics)*> {
1026+
// SAFETY: we only give access to `&mut` for fields not structurally pinned.
1027+
let this = unsafe { ::core::pin::Pin::get_unchecked_mut(self) };
1028+
[< $name Projection >] {
1029+
$(
1030+
// SAFETY: `$p_field` is structurally pinned.
1031+
$(#[$($p_attr)*])*
1032+
$p_field : unsafe { ::core::pin::Pin::new_unchecked(&mut this.$p_field) },
1033+
)*
1034+
$(
1035+
$(#[$($attr)*])*
1036+
$field : &mut this.$field,
1037+
)*
1038+
___pin_phantom_data: ::core::marker::PhantomData,
1039+
}
1040+
}
1041+
}
1042+
}
1043+
};
9831044
(make_pin_data:
9841045
@pin_data($pin_data:ident),
9851046
@impl_generics($($impl_generics:tt)*),

0 commit comments

Comments
 (0)