From: Gary Guo Date: Mon, 12 Jan 2026 17:07:20 +0000 (+0000) Subject: rust: macros: allow arbitrary types to be used in `module!` macro X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d5731a6be6a43d8c90d766e1404502c44545241;p=thirdparty%2Flinux.git rust: macros: allow arbitrary types to be used in `module!` macro Previously this only accepts an identifier, but now with `syn` it is easy to make it accepts any type. Reviewed-by: Benno Lossin Signed-off-by: Gary Guo Link: https://patch.msgid.link/20260112170919.1888584-10-gary@kernel.org Signed-off-by: Miguel Ojeda --- diff --git a/rust/macros/module.rs b/rust/macros/module.rs index 43ada49525c9d..9e0242d42d51f 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -26,7 +26,8 @@ use syn::{ LitStr, Path, Result, - Token, // + Token, + Type, // }; use crate::helpers::*; @@ -370,7 +371,7 @@ impl Parse for Parameter { } pub(crate) struct ModuleInfo { - type_: Ident, + type_: Type, license: AsciiLitStr, name: AsciiLitStr, authors: Option>, @@ -529,7 +530,6 @@ pub(crate) fn module(info: ModuleInfo) -> Result { // Double nested modules, since then nobody can access the public items inside. mod __module_init { mod __module_init { - use super::super::#type_; use pin_init::PinInit; /// The "Rust loadable module" mark. @@ -541,7 +541,7 @@ pub(crate) fn module(info: ModuleInfo) -> Result { #[used(compiler)] static __IS_RUST_MODULE: () = (); - static mut __MOD: ::core::mem::MaybeUninit<#type_> = + static mut __MOD: ::core::mem::MaybeUninit = ::core::mem::MaybeUninit::uninit(); // Loadable modules need to export the `{init,cleanup}_module` identifiers. @@ -628,8 +628,9 @@ pub(crate) fn module(info: ModuleInfo) -> Result { /// /// This function must only be called once. unsafe fn __init() -> ::kernel::ffi::c_int { - let initer = - <#type_ as ::kernel::InPlaceModule>::init(&super::super::THIS_MODULE); + let initer = ::init( + &super::super::THIS_MODULE + ); // SAFETY: No data race, since `__MOD` can only be accessed by this module // and there only `__init` and `__exit` access it. These functions are only // called once and `__exit` cannot be called before or during `__init`.