mirror of
https://github.com/torvalds/linux.git
synced 2026-11-07 06:17:02 +01:00
rust: pin-init: internal: pin_data: support shared projection
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`. Acked-by: Benno Lossin <lossin@kernel.org> Link: https://patch.msgid.link/20261008-dev-selfref-v2-19-e280b3c8fba5@garyguo.net/ Signed-off-by: Gary Guo <gary@garyguo.net>
This commit is contained in:
parent
e2b2e01365
commit
e2ab80587d
|
|
@ -1145,10 +1145,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");
|
||||
|
||||
|
|
@ -1462,6 +1469,109 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
|
|||
}
|
||||
}
|
||||
|
||||
let (fields_ref_decl_lt, fields_ref_proj_lt): (Vec<_>, Vec<_>) = info
|
||||
.fields
|
||||
.iter()
|
||||
.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 matches!(
|
||||
f.borrowed,
|
||||
Some(BorrowedInfo {
|
||||
kind: BorrowedKind::Mutable,
|
||||
..
|
||||
})
|
||||
) {
|
||||
// Mutable borrow must be omitted for aliasing reason.
|
||||
(
|
||||
quote!(
|
||||
#vis #name ::pin_init::__internal::NotVisible<&#lt #ty>,
|
||||
),
|
||||
quote!(
|
||||
#name ::pin_init::__internal::NotVisible::new(),
|
||||
),
|
||||
)
|
||||
} else 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
|
||||
|
|
@ -1477,6 +1587,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
|
||||
{
|
||||
|
|
@ -1515,6 +1631,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)*
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user