From nobody Wed Jan 7 23:01:13 2026 Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5BAE2E6CDF; Sun, 4 Jan 2026 20:07:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.161 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767557279; cv=none; b=jtvpjjCWr+ybI2N7zhp9Q274JsCD0iPT++GmSnXMOPEp3aRZAvZZFNTTKcAYctaYogYNor30D5KS250UoNPqJKE7Th7+mk75BfMjrR1barSJxUfEI2qAU03NKDv23Vw0bfbScNoqvfUKsSMtmt6RWj8PeUeBfXELLiU2mxoVlng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767557279; c=relaxed/simple; bh=kIVztsDOXv0Lu3DKyWEgCoE4Bh0NOawWkK3BaYkO/Bk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T9cUDb7nvAC1ABkCumupGy9ELf6Wm9bFaKudckq1YU3EBtlO7acXST9QgdU7wk4aeZnMRZ1+tbGBBy3rM/LC56Nd0FHkrUEF8z5itu5FRup7YPpO5lBbyorDRbaeM6acdKg3O8QQeYkLYoT+fjDdR1/Q1zFVhfSIsECbK2lVIlo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mailbox.org; spf=pass smtp.mailfrom=mailbox.org; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b=ItHY2sxt; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b=H9sW+hTB; arc=none smtp.client-ip=80.241.56.161 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mailbox.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mailbox.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b="ItHY2sxt"; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b="H9sW+hTB" Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4dkpQ40J97z9scL; Sun, 4 Jan 2026 21:07:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1767557276; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EI5J4p+axLP7z2aq6eHJ9D//w05BSzabU6OGTBUhdsk=; b=ItHY2sxtHXnRFL9qRwooDxKpsr2KnZiFtqX5LDtK1d+NmS7qffnU75C5ewSf+LqcpN53zD rJq6ab8a7N+TvXYs5Nn3e+CmSFHqN2k2SxTY3K1aCakvVE23Z4/QZSTg6UoVYsHFSx068f vtC2fJCaKKhzgakRjPEcvigvPQZTGoX9WVn4DiBXF+T2I3/5UBag0uczVhRFHNtatTyrAc jczDXBfT5jYVKGgcSz3cyh9WtYze56SYMCNBSuoYxtVq/TgmwBJKMLqoROZYDntWmuUmX2 6zarqhMvyc4HnS03gdocOroKFnw/53x14+WU2KK/RbV3WxVbJgNC6Cbg/8kqyA== Authentication-Results: outgoing_mbo_mout; dkim=pass header.d=mailbox.org header.s=mail20150812 header.b=H9sW+hTB; spf=pass (outgoing_mbo_mout: domain of mhi@mailbox.org designates 2001:67c:2050:b231:465::2 as permitted sender) smtp.mailfrom=mhi@mailbox.org From: Maurice Hieronymus DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1767557274; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EI5J4p+axLP7z2aq6eHJ9D//w05BSzabU6OGTBUhdsk=; b=H9sW+hTBOqmcGWmi+ll1NQbUVpWG1jeuxVQeRsGv5CZt+yHHPIrvGCgY5mS7R3pYPOzrQU XGiXSQ6e5bx7udKePQlhQMLgVTlHAqr3407YHne+6vnAERszXHxuP3mdDbobC5IVTDyVrB dZKm8mGHxIpLpe+1m7ZwCLCifHp39T73OkKeX3snz/KNj5sx1oNcglbqnzJ5D5qdj5rLSz xA9UYFyhHi7o1EiMm3DzlqPl0Bo933z0y1w9VLqkKYz8cD4o39ZRk9SANLSN4wawXQ17xA aKvnHkByX69kKedLsa9dz9noCcRycDrDj7bh25oVQbBZbCD9tpiRK3O9koBysg== 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 Message-ID: <20260104200733.190494-2-mhi@mailbox.org> In-Reply-To: <20260104200733.190494-1-mhi@mailbox.org> References: <20260104200733.190494-1-mhi@mailbox.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MBO-RS-ID: a68a25a06434902dc54 X-MBO-RS-META: rmu7uzej3bqujr7x1kmbtarytfmx9si5 X-Rspamd-Queue-Id: 4dkpQ40J97z9scL Content-Type: text/plain; charset="utf-8" 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 --- 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::Disp= lay` +//! 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 =3D syn::parse(input).expect("failed to pa= rse input"); + + let data =3D match &input.data { + syn::Data::Enum(data) =3D> data, + syn::Data::Struct(_) =3D> { + panic!("derive(Display) only supports enums, not structs"); + } + syn::Data::Union(_) =3D> { + panic!("derive(Display) only supports enums, not unions"); + } + }; + + // Generate match arms for each variant. + let match_arms =3D data.variants.iter().map(|variant| { + let variant_ident =3D &variant.ident; + let variant_name =3D variant_ident.to_string(); + + // Handle different variant types: unit, tuple, and struct. + let pattern =3D match &variant.fields { + syn::Fields::Unit =3D> quote::quote! { Self::#variant_ident }, + syn::Fields::Unnamed(_) =3D> quote::quote! { Self::#variant_id= ent(..) }, + syn::Fields::Named(_) =3D> quote::quote! { Self::#variant_iden= t { .. } }, + }; + + quote::quote! { + #pattern =3D> f.write_str(#variant_name) + } + }); + + let name =3D &input.ident; + let expanded =3D quote::quote! { + impl ::kernel::fmt::Display for #name { + fn fmt(&self, f: &mut ::kernel::fmt::Formatter<'_>) -> ::kerne= l::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 =3D TestEnum::Foo; +/// let bar =3D TestEnum::bAr(42); +/// let baz =3D TestEnum::baZ { value: 0 }; +/// +/// assert!(format!("{}", Adapter(&foo)) =3D=3D "Foo"); +/// assert!(format!("{}", Adapter(&bar)) =3D=3D "bAr"); +/// assert!(format!("{}", Adapter(&baz)) =3D=3D "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) +} --=20 2.51.2 From nobody Wed Jan 7 23:01:13 2026 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BAF6C2D3ECF; Sun, 4 Jan 2026 20:08:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767557289; cv=none; b=pjPSMjFWadAqXkYX5TJCDWgHJnyfY3Irx7n8t8FTIR3Eml+4kqgCBbncV3Q26Lze5ftjmRUitSJWNyZgRInXteDYmLLRC9dQWt+KlUiNfI4EeMCnAtAKkFmmVNOzX4HlIGdFl/f3GeIOuP0wWkqFv1Fa0jSHY/PSve5UA2jwQds= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767557289; c=relaxed/simple; bh=zx+SwXeUY/mRzsEeW+qHs5tewQiDWfZM5/tDt+5m3wI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KR6ANxZfuciSukZ71VcD2JPcAexzo+eTMomp7zs/jlpiKVG3sBmQrzYP7+q0LJwKxXZLnttP+8DUXE0sjU0CNayQKej8eVmzveCzu+GtbEWL7p2FGN8U7XKxQmBU6HQT+nhTOssm/f9tTPd9kpLKG7HaQ9T1wMpYt1C1wJC61B4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mailbox.org; spf=pass smtp.mailfrom=mailbox.org; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b=iBRImHm3; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b=UFXy0n8Z; arc=none smtp.client-ip=80.241.56.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mailbox.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mailbox.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b="iBRImHm3"; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b="UFXy0n8Z" Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4dkpQ834tVz9snJ; Sun, 4 Jan 2026 21:08:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1767557280; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e1bFVgbKd2bGX2uopKiOokyPDAQ+3U4m878ja6n9ICc=; b=iBRImHm3YZKOSDLxCmKXIMmJGvBjZ1NNNHjSd7boqJT4IDXal4nqsANtNJTS5Hl0JqeDdE QC0FZTq79Lpta5jXrGtNSsSSd1vjgvsItpONmp8OTXRZFnNovolff/NVkbU1DUlVOWw7pu vMZos0A+hURWe+ghPEtHCMG1Ftm2TAKNDF3mmxpyfmVv9+/frcr+ky2d8pNMLsDa2GS4TO 5VGNWJgay5ioIx8M7UXMO54rZpfrWaYAot5TSDK0GITk63LfhWQNo38kLrx1qglJyHw6mg 5C6VHlMg8R6EKL2n2IiBazQfvvTmGd+eSSi7jQkKsPDD4vFj1yfGU+eshVcQBw== From: Maurice Hieronymus DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1767557278; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e1bFVgbKd2bGX2uopKiOokyPDAQ+3U4m878ja6n9ICc=; b=UFXy0n8ZUlf+8CebJcoSFv4UsJbeRaURhXYI1Q7cFe4UBaVBx2sivR3wShdYcj2OKf9KmX VFuhG0WyQDkP223RgNDC02xuV7EqsPry3+mfXEsh5mNhmR9IfUu9JzJmY/KCUooWBHxqKv dxYdC43A6EYzWZ+XWDSr6YNhuVeiCbPx58c5sF0fG07JlJyq2wNI8fiyDkF15X4sbjrveM ac6WlqvWYXwJxDyS88X498SzQ0aBqu1COaj6AbGHou73hb0+dOMTyvrWQmyEnlUdHk9WI7 cYadxzFM0Zv69BcRfzx/hzXeLc0ZSgj1szu0+Yb0yEasqxPFjVFBMKZETWzWaw== 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 2/2] gpu: nova-core: Use derive Display for Chipset enum Date: Sun, 4 Jan 2026 21:07:32 +0100 Message-ID: <20260104200733.190494-3-mhi@mailbox.org> In-Reply-To: <20260104200733.190494-1-mhi@mailbox.org> References: <20260104200733.190494-1-mhi@mailbox.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MBO-RS-ID: d7f6dc98ca0715cbf2a X-MBO-RS-META: 41iyczy5oti6zqpyf4g16pc5jha3nowi Content-Type: text/plain; charset="utf-8" Use the derive macro for implementing fmt::Display on the Chipset enum instead of relying on the compiler-generated Display output. This ensures stable display strings that won't change unexpectedly. Signed-off-by: Maurice Hieronymus --- drivers/gpu/nova-core/gpu.rs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/drivers/gpu/nova-core/gpu.rs b/drivers/gpu/nova-core/gpu.rs index 629c9d2dc994..db2e913f85bf 100644 --- a/drivers/gpu/nova-core/gpu.rs +++ b/drivers/gpu/nova-core/gpu.rs @@ -4,6 +4,7 @@ device, devres::Devres, fmt, + macros, pci, prelude::*, sync::Arc, // @@ -26,7 +27,7 @@ macro_rules! define_chipset { ({ $($variant:ident =3D $value:expr),* $(,)* }) =3D> { /// Enum representation of the GPU chipset. - #[derive(fmt::Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)] + #[derive(macros::Display, fmt::Debug, Copy, Clone, PartialOrd, Ord= , PartialEq, Eq)] pub(crate) enum Chipset { $($variant =3D $value),*, } @@ -107,20 +108,6 @@ pub(crate) fn arch(&self) -> Architecture { } } =20 -// TODO -// -// The resulting strings are used to generate firmware paths, hence the -// generated strings have to be stable. -// -// Hence, replace with something like strum_macros derive(Display). -// -// For now, redirect to fmt::Debug for convenience. -impl fmt::Display for Chipset { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{self:?}") - } -} - /// Enum representation of the GPU generation. /// /// TODO: remove the `Default` trait implementation, and the `#[default]` --=20 2.51.2