From: Maurice Hieronymus <mhi@mailbox.org>
To: dakr@kernel.org
Cc: aliceryhl@google.com, acourbot@nvidia.com, airlied@gmail.com,
simona@ffwll.ch, nouveau@lists.freedesktop.org,
dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
ojeda@kernel.org, boqun.feng@gmail.com, gary@garyguo.net,
bjorn3_gh@protonmail.com, lossin@kernel.org,
a.hindborg@kernel.org, tmgross@umich.edu, mhi@mailbox.org,
rust-for-linux@vger.kernel.org
Subject: [PATCH v2 1/2] rust: macros: Add derive Display for enums
Date: Sun, 4 Jan 2026 21:07:31 +0100 [thread overview]
Message-ID: <20260104200733.190494-2-mhi@mailbox.org> (raw)
In-Reply-To: <20260104200733.190494-1-mhi@mailbox.org>
Add a derive macro that implements kernel::fmt::Display for enums.
The macro outputs the exact variant name as written, preserving case.
This supports all enum variant types: unit, tuple, and struct variants.
For variants with data, only the variant name is displayed.
Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
---
rust/macros/display.rs | 52 ++++++++++++++++++++++++++++++++++++++++++
rust/macros/lib.rs | 42 ++++++++++++++++++++++++++++++++++
2 files changed, 94 insertions(+)
create mode 100644 rust/macros/display.rs
diff --git a/rust/macros/display.rs b/rust/macros/display.rs
new file mode 100644
index 000000000000..5cd396d3900e
--- /dev/null
+++ b/rust/macros/display.rs
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Derive macro for `Display` on enums.
+//!
+//! This module provides a derive macro that implements `kernel::fmt::Display`
+//! for enums, outputting the exact variant name as written.
+
+use proc_macro::TokenStream;
+
+pub(crate) fn derive_display(input: TokenStream) -> TokenStream {
+ let input: syn::DeriveInput = syn::parse(input).expect("failed to parse input");
+
+ let data = match &input.data {
+ syn::Data::Enum(data) => data,
+ syn::Data::Struct(_) => {
+ panic!("derive(Display) only supports enums, not structs");
+ }
+ syn::Data::Union(_) => {
+ panic!("derive(Display) only supports enums, not unions");
+ }
+ };
+
+ // Generate match arms for each variant.
+ let match_arms = data.variants.iter().map(|variant| {
+ let variant_ident = &variant.ident;
+ let variant_name = variant_ident.to_string();
+
+ // Handle different variant types: unit, tuple, and struct.
+ let pattern = match &variant.fields {
+ syn::Fields::Unit => quote::quote! { Self::#variant_ident },
+ syn::Fields::Unnamed(_) => quote::quote! { Self::#variant_ident(..) },
+ syn::Fields::Named(_) => quote::quote! { Self::#variant_ident { .. } },
+ };
+
+ quote::quote! {
+ #pattern => f.write_str(#variant_name)
+ }
+ });
+
+ let name = &input.ident;
+ let expanded = quote::quote! {
+ impl ::kernel::fmt::Display for #name {
+ fn fmt(&self, f: &mut ::kernel::fmt::Formatter<'_>) -> ::kernel::fmt::Result {
+ match self {
+ #(#match_arms),*
+ }
+ }
+ }
+ };
+
+ expanded.into()
+}
diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
index b38002151871..4c95a132fefe 100644
--- a/rust/macros/lib.rs
+++ b/rust/macros/lib.rs
@@ -14,6 +14,7 @@
#[macro_use]
mod quote;
mod concat_idents;
+mod display;
mod export;
mod fmt;
mod helpers;
@@ -475,3 +476,44 @@ pub fn paste(input: TokenStream) -> TokenStream {
pub fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
kunit::kunit_tests(attr, ts)
}
+
+/// Derives the [`Display`] trait for enums.
+///
+/// This macro generates an implementation of [`kernel::fmt::Display`] for enums
+/// that outputs the exact variant name as written (case-preserved).
+///
+/// # Requirements
+///
+/// - Can only be applied to enums (not structs or unions).
+/// - Supports unit variants, tuple variants, and struct variants.
+/// - For variants with data, only the variant name is displayed.
+///
+/// # Examples
+///
+/// ```
+/// use kernel::fmt::Adapter;
+/// use kernel::macros::Display;
+///
+/// #[allow(non_camel_case_types)]
+/// #[derive(Display)]
+/// enum TestEnum {
+/// Foo,
+/// bAr(u8),
+/// baZ { value: u8 },
+/// }
+///
+/// let foo = TestEnum::Foo;
+/// let bar = TestEnum::bAr(42);
+/// let baz = TestEnum::baZ { value: 0 };
+///
+/// assert!(format!("{}", Adapter(&foo)) == "Foo");
+/// assert!(format!("{}", Adapter(&bar)) == "bAr");
+/// assert!(format!("{}", Adapter(&baz)) == "baZ");
+/// ```
+///
+/// [`Display`]: ../kernel/fmt/trait.Display.html
+/// [`kernel::fmt::Display`]: ../kernel/fmt/trait.Display.html
+#[proc_macro_derive(Display)]
+pub fn derive_display(input: TokenStream) -> TokenStream {
+ display::derive_display(input)
+}
--
2.51.2
next prev parent reply other threads:[~2026-01-04 20:07 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-04 20:07 [PATCH v2 0/2] rust: macros: Add derive Display for enums Maurice Hieronymus
2026-01-04 20:07 ` Maurice Hieronymus [this message]
2026-01-05 9:02 ` [PATCH v2 1/2] " Benno Lossin
2026-01-05 10:29 ` Danilo Krummrich
2026-01-05 14:42 ` Benno Lossin
2026-01-05 15:00 ` Danilo Krummrich
2026-01-05 15:23 ` Maurice Hieronymus
2026-01-05 16:11 ` Gary Guo
2026-01-05 21:11 ` Maurice Hieronymus
2026-01-05 22:03 ` Danilo Krummrich
2026-01-06 5:56 ` Maurice Hieronymus
2026-01-06 12:56 ` Benno Lossin
2026-01-04 20:07 ` [PATCH v2 2/2] gpu: nova-core: Use derive Display for Chipset enum Maurice Hieronymus
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=20260104200733.190494-2-mhi@mailbox.org \
--to=mhi@mailbox.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=nouveau@lists.freedesktop.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--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