From: Gary Guo <gary@garyguo.net>
To: "Benno Lossin" <lossin@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
Gary Guo <gary@garyguo.net>
Subject: [PATCH 19/20] rust: pin-init: internal: pin_data: support shared projection
Date: Thu, 08 Oct 2026 14:24:06 +0200 [thread overview]
Message-ID: <20261008-dev-selfref-v1-19-6c1eb269fe57@garyguo.net> (raw)
In-Reply-To: <20261008-dev-selfref-v1-0-6c1eb269fe57@garyguo.net>
For fields that self-references, it is desirable that projection can
happen on shared references too, so lifetime between different fields can
be correlated. Add support for that with `with_project_ref`.
Signed-off-by: Gary Guo <gary@garyguo.net>
---
rust/pin-init/internal/src/pin_data.rs | 128 +++++++++++++++++++++++++++++++++
1 file changed, 128 insertions(+)
diff --git a/rust/pin-init/internal/src/pin_data.rs b/rust/pin-init/internal/src/pin_data.rs
index b79710ba7059..f7dc76849ce3 100644
--- a/rust/pin-init/internal/src/pin_data.rs
+++ b/rust/pin-init/internal/src/pin_data.rs
@@ -1149,10 +1149,17 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
&info.field_lts_outlive_chain,
generics,
]);
+ let generics_with_this_field_ref_lt = CombinedGenerics(vec![
+ &this_lt_generics,
+ &info.field_lts_split_variance_outlive_chain,
+ generics,
+ ]);
let (impl_generics, ty_generics, whr) = generics.split_for_impl();
let (_, field_lt_ty_generics, _) = field_lts.split_for_impl();
let (_, ty_generics_with_this_lt, _) = generics_with_this_lt.split_for_impl();
let (_, ty_generics_with_this_field_lt, _) = generics_with_this_field_lt.split_for_impl();
+ let (_, ty_generics_with_this_field_ref_lt, _) =
+ generics_with_this_field_ref_lt.split_for_impl();
let this = format_ident!("this");
@@ -1466,6 +1473,103 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
}
}
+ let (fields_ref_decl_lt, fields_ref_proj_lt): (Vec<_>, Vec<_>) = info
+ .fields
+ .iter()
+ .filter(|f| {
+ // Mutably referenced fields cannot be accessed by user at all for aliasing reasons.
+ !matches!(
+ f.borrowed,
+ Some(BorrowedInfo {
+ kind: BorrowedKind::Mutable,
+ ..
+ })
+ )
+ })
+ .map(|f| {
+ let vis = &f.field.vis;
+ let ident = f.member.as_ident();
+ let member = &f.member;
+ let name = (!info.is_tuple_struct).then(|| quote!(#ident:));
+
+ let ty = &f.field.ty;
+
+ let mut accessor = quote!(&#this.#member);
+ if !f.captures.is_empty() || f.borrowed.is_some() {
+ accessor = quote!(
+ // SAFETY: we have `Erase<..>` which we know is layout compatible with `f.ty`.
+ // We cannot include explicit type name here as the field lifetimes are nameable
+ // in this context.
+ unsafe { ::core::mem::transmute(#accessor) }
+ )
+ }
+
+ // In `with_project`, borrowed fields have their field lifetime available, so use it
+ // instead of `'__this`.
+ let lt = if f.borrowed.is_some() {
+ Lifetime::from_ident(&ident)
+ } else {
+ this_lt.clone()
+ };
+
+ if f.pinned {
+ (
+ quote!(
+ #vis #name ::core::pin::Pin<&#lt #ty>,
+ ),
+ quote!(
+ // SAFETY: this field is structurally pinned.
+ #name unsafe { ::core::pin::Pin::new_unchecked(#accessor) },
+ ),
+ )
+ } else {
+ (
+ quote!(
+ #vis #name &#lt #ty,
+ ),
+ quote!(
+ #name #accessor,
+ ),
+ )
+ }
+ })
+ .collect();
+
+ let projection_ref_lt = format_ident!("__ProjectionRef");
+ let (projection_ref_lt_def, projection_ref_lt_init) = if info.is_tuple_struct {
+ (
+ quote!(
+ #vis struct #projection_ref_lt #generics_with_this_field_ref_lt(
+ #(#fields_ref_decl_lt)*
+ ::core::marker::PhantomData<&'__this #ident #ty_generics>,
+ ) #whr;
+ ),
+ quote!(
+ #projection_ref_lt(
+ #(#fields_ref_proj_lt)*
+ ::core::marker::PhantomData,
+ )
+ ),
+ )
+ } else {
+ (
+ quote! {
+ #vis struct #projection_ref_lt #generics_with_this_field_ref_lt
+ #whr
+ {
+ #(#fields_ref_decl_lt)*
+ ___pin_phantom_data: ::core::marker::PhantomData<&'__this #ident #ty_generics>,
+ }
+ },
+ quote! {
+ #projection_ref_lt {
+ #(#fields_ref_proj_lt)*
+ ___pin_phantom_data: ::core::marker::PhantomData,
+ }
+ },
+ )
+ };
+
quote! {
#[doc = #docs]
// Allow `non_snake_case` since the same warning will be emitted on
@@ -1481,6 +1585,12 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
#[doc(hidden)]
#projection_lt_def
+ // Allow `non_snake_case` since the same warning will be emitted on
+ // the struct definition.
+ #[allow(dead_code, non_snake_case)]
+ #[doc(hidden)]
+ #projection_ref_lt_def
+
impl #impl_generics #ident #ty_generics
#whr
{
@@ -1519,6 +1629,24 @@ impl #impl_generics #ident #ty_generics
f(#projection_lt_init)
}
+ /// Pin-projects all fields of `Self` from a shared reference with proper lifetime.
+ ///
+ /// These fields are structurally pinned:
+ #(#[doc = #structurally_pinned_fields_docs])*
+ ///
+ /// These fields are **not** structurally pinned:
+ #(#[doc = #not_structurally_pinned_fields_docs])*
+ #[inline]
+ #vis fn with_project_ref<'__this, R>(
+ self: ::core::pin::Pin<&'__this Self>,
+ f: impl for #field_lt_ty_generics ::core::ops::FnOnce(
+ #projection_ref_lt #ty_generics_with_this_field_ref_lt
+ ) -> R,
+ ) -> R {
+ let #this = ::core::pin::Pin::get_ref(self);
+ f(#projection_ref_lt_init)
+ }
+
#(#accessors)*
}
}
--
2.54.0
next prev parent reply other threads:[~2026-10-08 12:25 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-08 12:23 [PATCH 00/20] rust: pin-init: create self references safely Gary Guo
2026-10-08 12:23 ` [PATCH 01/20] kbuild: rust: allow `clippy::comparison_chain` globally Gary Guo
2026-10-08 12:23 ` [PATCH 02/20] rust: pin-init: internal: pin_data: infer self-referential struct Gary Guo
2026-10-08 12:23 ` [PATCH 03/20] rust: pin-init: internal: pin_data: rewrite fields that borrow others Gary Guo
2026-10-08 12:23 ` [PATCH 04/20] rust: pin-init: internal: pin_data: pin borrowed fields with wrapper Gary Guo
2026-10-08 12:23 ` [PATCH 05/20] rust: pin-init: internal: pin_data: teach drop check about generics that cannot dangle Gary Guo
2026-10-08 12:23 ` [PATCH 06/20] rust: pin-init: internal: pin_data: self-referential drop order checks Gary Guo
2026-10-08 12:23 ` [PATCH 07/20] rust: pin-init: internal: pin_data: check covariance of self-referential fields Gary Guo
2026-10-08 12:23 ` [PATCH 08/20] rust: pin-init: internal: pin_data: implement initialization of borrowed structs Gary Guo
2026-10-08 12:23 ` [PATCH 09/20] rust: pin-init: internal: pin_data: project self-referential fields Gary Guo
2026-10-08 12:23 ` [PATCH 10/20] rust: pin-init: internal: pin_data: add `with_project` method Gary Guo
2026-10-08 12:23 ` [PATCH 11/20] rust: pin-init: internal: pin_data: enable self-referential support Gary Guo
2026-10-08 12:23 ` [PATCH 12/20] rust: pin-init: internal: pin_data: allow lifetime to be shortened per field drop order Gary Guo
2026-10-08 12:24 ` [PATCH 13/20] rust: pin-init: internal: pin_data: parse explicit `#[borrowed]` annotation Gary Guo
2026-10-08 12:24 ` [PATCH 14/20] rust: pin-init: internal: pin_data: support mutable borrows Gary Guo
2026-10-08 12:24 ` [PATCH 15/20] rust: pin-init: internal: pin_data: parse explicit `#[uses]` annotation Gary Guo
2026-10-08 12:24 ` [PATCH 16/20] rust: pin-init: internal: pin_data: make field lifetime invariance imply type invariance Gary Guo
[not found] ` <sashiko-outbox-164164@kernel.org>
2026-10-08 15:52 ` Gary Guo
2026-10-08 12:24 ` [PATCH 17/20] rust: pin-init: internal: pin_data: complete invariant borrow support Gary Guo
2026-10-08 12:24 ` [PATCH 18/20] rust: pin-init: internal: pin_data: perform AST lifetime replacement if possible Gary Guo
2026-10-08 12:24 ` Gary Guo [this message]
2026-10-08 12:24 ` [PATCH 20/20] rust: pin-init: internal: pin_data: support existential lifetimes Gary Guo
2026-10-08 16:20 ` [PATCH 00/20] rust: pin-init: create self references safely Benno Lossin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261008-dev-selfref-v1-19-6c1eb269fe57@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.