rust: pin-init: internal: synchronize with user-space version
authorBenno Lossin <benno.lossin@proton.me>
Sat, 8 Mar 2025 11:05:22 +0000 (11:05 +0000)
committerMiguel Ojeda <ojeda@kernel.org>
Sun, 16 Mar 2025 20:59:19 +0000 (21:59 +0100)
Synchronize the internal macros crate with the user-space version that
uses the quote crate [1] instead of a custom `quote!` macro. The imports
in the different version are achieved using `cfg` on the kernel config
value. This cfg is always set in the kernel and never set in the
user-space version.

Since the quote crate requires the proc_macro2 crate, imports also need
to be adjusted and `.into()` calls have to be inserted.

Link: https://crates.io/crates/quote
Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Fiona Behrens <me@Kloenk.dev>
Link: https://lore.kernel.org/r/20250308110339.2997091-19-benno.lossin@proton.me
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/pin-init/internal/src/helpers.rs
rust/pin-init/internal/src/lib.rs
rust/pin-init/internal/src/pin_data.rs
rust/pin-init/internal/src/pinned_drop.rs
rust/pin-init/internal/src/zeroable.rs

index 78521ba19d0b705a3f7da404146fd690ec1c1e6d..236f989a50f2f057fffbf95ca10b62c88f326221 100644 (file)
@@ -1,5 +1,8 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
+#[cfg(not(kernel))]
+use proc_macro2 as proc_macro;
+
 use proc_macro::{TokenStream, TokenTree};
 
 /// Parsed generics.
index c201b8a53915e3377567bd142c70d3489a6ae62d..30e145f80bc0531ed609a348508b51c12388350e 100644 (file)
@@ -7,6 +7,13 @@
 //! `pin-init` proc macros.
 
 #![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
+// Allow `.into()` to convert
+// - `proc_macro2::TokenStream` into `proc_macro::TokenStream` in the user-space version.
+// - `proc_macro::TokenStream` into `proc_macro::TokenStream` in the kernel version.
+//   Clippy warns on this conversion, but it's required by the user-space version.
+//
+// Remove once we have `proc_macro2` in the kernel.
+#![allow(clippy::useless_conversion)]
 
 use proc_macro::TokenStream;
 
@@ -14,6 +21,9 @@ use proc_macro::TokenStream;
 #[path = "../../../macros/quote.rs"]
 #[macro_use]
 mod quote;
+#[cfg(not(kernel))]
+#[macro_use]
+extern crate quote;
 
 mod helpers;
 mod pin_data;
@@ -23,17 +33,17 @@ mod zeroable;
 #[allow(missing_docs)]
 #[proc_macro_attribute]
 pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
-    pin_data::pin_data(inner, item)
+    pin_data::pin_data(inner.into(), item.into()).into()
 }
 
 #[allow(missing_docs)]
 #[proc_macro_attribute]
 pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
-    pinned_drop::pinned_drop(args, input)
+    pinned_drop::pinned_drop(args.into(), input.into()).into()
 }
 
 #[allow(missing_docs)]
 #[proc_macro_derive(Zeroable)]
 pub fn derive_zeroable(input: TokenStream) -> TokenStream {
-    zeroable::derive(input)
+    zeroable::derive(input.into()).into()
 }
index 9b974498f4a853163605e9d32a5d9d1d8af05efd..87d4a7eb1d35e7243b7665b850be006a4db2153b 100644 (file)
@@ -1,5 +1,8 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
+#[cfg(not(kernel))]
+use proc_macro2 as proc_macro;
+
 use crate::helpers::{parse_generics, Generics};
 use proc_macro::{Group, Punct, Spacing, TokenStream, TokenTree};
 
index 386f52f73c060ebadc57448622e34195b218f064..c824dd8b436dfbfa93ae96134e13fcd425b77b46 100644 (file)
@@ -1,5 +1,8 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
+#[cfg(not(kernel))]
+use proc_macro2 as proc_macro;
+
 use proc_macro::{TokenStream, TokenTree};
 
 pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream {
index 0cf6732f27dc0a44874366963b2a56915dd05b65..acc94008c152998424048011756c0b0e2a90c757 100644 (file)
@@ -1,5 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#[cfg(not(kernel))]
+use proc_macro2 as proc_macro;
+
 use crate::helpers::{parse_generics, Generics};
 use proc_macro::{TokenStream, TokenTree};