rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: "Benno Lossin" <benno.lossin@proton.me>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"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>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: [RFC PATCH 1/6] rust: pin-init: internal: add syn version of the `Zeroable` derive macro
Date: Tue, 04 Mar 2025 22:56:51 +0000	[thread overview]
Message-ID: <20250304225536.2033853-2-benno.lossin@proton.me> (raw)
In-Reply-To: <20250304225536.2033853-1-benno.lossin@proton.me>

Implement the `Zeroable` derive macro using syn to simplify parsing by
not going through an additional declarative macro.

The syn version is only enabled in the user-space version and disabled
in the kernel until syn becomes available there.

Signed-off-by: Benno Lossin <benno.lossin@proton.me>
---
 rust/pin-init/internal/src/lib.rs          |  5 ++
 rust/pin-init/internal/src/syn_zeroable.rs | 63 ++++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 rust/pin-init/internal/src/syn_zeroable.rs

diff --git a/rust/pin-init/internal/src/lib.rs b/rust/pin-init/internal/src/lib.rs
index babe5e878550..5019efe22662 100644
--- a/rust/pin-init/internal/src/lib.rs
+++ b/rust/pin-init/internal/src/lib.rs
@@ -30,6 +30,11 @@
 mod helpers;
 mod pin_data;
 mod pinned_drop;
+#[cfg(kernel)]
+mod zeroable;
+
+#[cfg(not(kernel))]
+#[path = "syn_zeroable.rs"]
 mod zeroable;
 
 #[proc_macro_attribute]
diff --git a/rust/pin-init/internal/src/syn_zeroable.rs b/rust/pin-init/internal/src/syn_zeroable.rs
new file mode 100644
index 000000000000..90ea8fa63c83
--- /dev/null
+++ b/rust/pin-init/internal/src/syn_zeroable.rs
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: Apache-2.0 OR MIT
+
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::{
+    parse_macro_input, parse_quote, Data, DataStruct, DeriveInput, Error, GenericParam, Result,
+    TypeParam, WherePredicate,
+};
+
+pub(crate) fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
+    let raw = input.clone().into();
+    do_derive(parse_macro_input!(input as DeriveInput), raw)
+        .unwrap_or_else(|e| e.into_compile_error())
+        .into()
+}
+
+fn do_derive(
+    DeriveInput {
+        ident,
+        mut generics,
+        data,
+        ..
+    }: DeriveInput,
+    raw_input: TokenStream,
+) -> Result<TokenStream> {
+    let Data::Struct(DataStruct { fields, .. }) = data else {
+        return Err(Error::new_spanned(
+            raw_input,
+            "`Zeroable` can only be derived for structs.",
+        ));
+    };
+    let field_ty = fields.iter().map(|f| &f.ty);
+    let zeroable_bounds = generics
+        .params
+        .iter()
+        .filter_map(|p| match p {
+            GenericParam::Type(TypeParam { ident, .. }) => {
+                Some(parse_quote!(#ident: ::pin_init::Zeroable))
+            }
+            _ => None,
+        })
+        .collect::<Vec<WherePredicate>>();
+    generics
+        .make_where_clause()
+        .predicates
+        .extend(zeroable_bounds);
+    let (impl_generics, ty_generics, whr) = generics.split_for_impl();
+    Ok(quote! {
+        // SAFETY: Every field type implements `Zeroable` and padding bytes may be zero.
+        #[automatically_derived]
+        unsafe impl #impl_generics ::pin_init::Zeroable for #ident #ty_generics
+            #whr
+        {}
+        const _: () = {
+            fn assert_zeroable<T: ?::core::marker::Sized + ::pin_init::Zeroable>() {}
+            fn ensure_zeroable #impl_generics ()
+                #whr
+            {
+                #(assert_zeroable::<#field_ty>();)*
+            }
+        };
+    })
+}
-- 
2.48.1



  reply	other threads:[~2025-03-04 22:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-04 22:56 [RFC PATCH 0/6] add `syn` versions of pin-init macros Benno Lossin
2025-03-04 22:56 ` Benno Lossin [this message]
2025-03-04 22:57 ` [RFC PATCH 2/6] rust: pin-init: internal: add syn version of `pinned_drop` proc macro Benno Lossin
2025-03-04 22:57 ` [RFC PATCH 3/6] rust: pin-init: internal: add syn version of the `pin_data` " Benno Lossin
2025-03-04 22:57 ` [RFC PATCH 4/6] rust: pin-init: add `?Sized` bounds to traits in `#[pin_data]` macro Benno Lossin
2025-03-04 22:57 ` [RFC PATCH 5/6] rust: pin-init: allow doctests to refer to the pin-init crate Benno Lossin
2025-03-04 22:57 ` [RFC PATCH 6/6] rust: pin-init: internal: add syn version of `[try_][pin_]init!` macros 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=20250304225536.2033853-2-benno.lossin@proton.me \
    --to=benno.lossin@proton.me \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).