From nobody Mon Feb 9 00:20:50 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3B3AF2874F5; Thu, 29 Jan 2026 14:32:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769697168; cv=none; b=pQG8Z0y0JxGV2CEmN4EN1tHZcldRxVIuZWVKrJ90kjEUd9ur12N4VxwFVB00P/HVeNqkkeXxczPTMNfBcsmCxdxgXhKKpfJ57JTd/6BgOhsRupDdxtGvGGKQJ+5/pGoC6KZrsg60VXtRxSc2Lpf52YOFd2dsDFrua28xxCSNGrY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769697168; c=relaxed/simple; bh=zsJKLouslNIMhVTvOdw5iCz2jJoutLn9tl1l10pprrw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=T4RDNhQglEG4GG0A1PofWPbIAmx8g+3LbfZ9ZLOOzc6GCO88f/ZjYYoDdDRjY2W2gLGa9FzLJGaPmS5k0+g0iEuU//LWDjTlMH8pjxwK5dUdMw3s0JTdabHnpsuoSd30GEfcNhindjKd5FGoSGJQzuJYkdlP0rMH/lzKXjf/Jys= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ijzwrdW/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ijzwrdW/" Received: by smtp.kernel.org (Postfix) with ESMTPS id 021F3C4AF0B; Thu, 29 Jan 2026 14:32:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1769697168; bh=zsJKLouslNIMhVTvOdw5iCz2jJoutLn9tl1l10pprrw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=ijzwrdW/sFAA6DIn8UJ6Y1yU5jLhaqOMk8LePZU0AtU3kAOsCZhy7Mrh4y+TlllAX 4MP8fCrGNombPay1ja6SwYH8mH8f8gMc+D/ZDpec9sujK+bZOftGfQWxofXtvRumzG rh6NbhBHSF7g5LwUVqv9XKwhd+JDqubMIziqpPuohXI1YyriscPNDIvuiJnwUGvr/x JS8WgJeGM6JPhSpCeoTLKnTkdqx4M7fvMPzZ3W6xcrxOLK2bbIm3ENt3FjXBtR/kFW 6XFQyV5o2wUfdtKGRj10juxheir7MBnhQA795806PLxwjeAjw7w4XHvVu71xV4agy1 WPI8+EhTjGawQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7E0CD61020; Thu, 29 Jan 2026 14:32:47 +0000 (UTC) From: Jesung Yang via B4 Relay Date: Thu, 29 Jan 2026 23:32:46 +0900 Subject: [PATCH v5 2/4] rust: macros: add derive macro for `TryFrom` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260129-try-from-into-macro-v5-2-dd011008118c@gmail.com> References: <20260129-try-from-into-macro-v5-0-dd011008118c@gmail.com> In-Reply-To: <20260129-try-from-into-macro-v5-0-dd011008118c@gmail.com> To: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Alexandre Courbot Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, nouveau@lists.freedesktop.org, Jesung Yang X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=10808; i=y.j3ms.n@gmail.com; h=from:subject:message-id; bh=o0ZOJ2u6fulpd6euQbCwZeiZqbQpSA+Y1ClbiBC4Hqs=; b=owJ4nAFtApL9kA0DAAoBhnBYoOPQ2LoByyZiAGl7b47aA5WH7vO27Eax/xs4+3OV8/xJrrBLu hHqXotpVMg9EIkCMwQAAQoAHRYhBNJuBqTTLsbEgOaQ0IZwWKDj0Ni6BQJpe2+OAAoJEIZwWKDj 0Ni6a3UP/3v9YccMDx0NI+IfLLnZQdKyC6kjqKkTFuVFtmyU6+abCyBRfivWFQYEKwv/C2PIOCu 5GWkMvGyD+iHz4VyhYgOd9JBoNL+KjQXI7uF5zQpzWFCA9zvt97gw76K94El2Kwl1SHAtoBsF4e oRHmUAMvwHwoc+Bvq9GIwiAuEcyafDMX8ktwYxUb9U61fqBPKZKpH7jPf2Rc4/bd3OMO/Yw55Pa ivdiSFO7wmXSI3r0OJdja66PWAiga0URV97XZEVTlJZkRgNScrbCxF5nARxrI1DAIddchIZUHb0 +UcaMrnW1s7GoBP0N4kHVPl95jVAgFZiBMTkrk/q0erR3suHOpYYmPkOcAN0wu5xidpfi5io66q TvKnfY/WmGvZGEwYA1zy6/X9XPQEd4U+F8GVsybDP7KebzrbbONMVyNeMw6m7fDTiVMKYrxBDXh Cd/k58ErBtHwAPJaVI4o8P12+diboiAPNUmlPVqEKugfktgftNS/krbpA0Yx1G6NjZD1L/EHD3J k/3ieugATWzJKLNajrchxDi44HIgYfS4TXc0wOUINjBE0kfYoJZSjuO7bB8QSaNxTg1+IHOQv51 YYmCwkQiaPHH4eCGX7Mwe1bM3SIrZFDJUh11uWRHLYo1MANAzrqky2dCCAc6HWvb5hxg2pIW4hs DSgBGXklSMMUG6xxt8fspUw== X-Developer-Key: i=y.j3ms.n@gmail.com; a=openpgp; fpr=D26E06A4D32EC6C480E690D0867058A0E3D0D8BA X-Endpoint-Received: by B4 Relay for y.j3ms.n@gmail.com/default with auth_id=602 X-Original-From: Jesung Yang Reply-To: y.j3ms.n@gmail.com From: Jesung Yang Introduce a procedural macro `TryFrom` to automatically implement the `TryFrom` trait for unit-only enums. This reduces boilerplate in cases where numeric values need to be interpreted as relevant enum variants. This situation often arises when working with low-level data sources. A typical example is the `Chipset` enum in nova-core, where the value read from a GPU register should be mapped to a corresponding variant. The macro not only supports primitive types such as `bool` or `i8`, but also `Bounded`, a wrapper around integer types limiting the number of bits usable for value representation. This accommodates the shift toward more restrictive register field representations in nova-core where values are constrained to specific bit ranges. Signed-off-by: Jesung Yang --- rust/macros/convert.rs | 64 ++++++++++++++++++ rust/macros/lib.rs | 176 +++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 240 insertions(+) diff --git a/rust/macros/convert.rs b/rust/macros/convert.rs index 096e3c9fdc1b..a7a43b1a2caf 100644 --- a/rust/macros/convert.rs +++ b/rust/macros/convert.rs @@ -34,6 +34,10 @@ pub(crate) fn derive_into(input: DeriveInput) -> syn::Re= sult { derive(DeriveTarget::Into, input) } =20 +pub(crate) fn derive_try_from(input: DeriveInput) -> syn::Result { + derive(DeriveTarget::TryFrom, input) +} + fn derive(target: DeriveTarget, input: DeriveInput) -> syn::Result { let data_enum =3D match input.data { Data::Enum(data) =3D> data, @@ -129,18 +133,21 @@ fn derive(target: DeriveTarget, input: DeriveInput) -= > syn::Result #[derive(Clone, Copy, Debug)] enum DeriveTarget { Into, + TryFrom, } =20 impl DeriveTarget { fn get_trait_name(&self) -> &'static str { match self { Self::Into =3D> "Into", + Self::TryFrom =3D> "TryFrom", } } =20 fn get_helper_name(&self) -> &'static str { match self { Self::Into =3D> "into", + Self::TryFrom =3D> "try_from", } } } @@ -186,6 +193,7 @@ fn derive_for_enum( ) -> TokenStream { let impl_fn =3D match target { DeriveTarget::Into =3D> impl_into, + DeriveTarget::TryFrom =3D> impl_try_from, }; =20 let qualified_repr_ty: syn::Path =3D parse_quote! { ::core::primitive:= :#repr_ty }; @@ -238,6 +246,54 @@ fn from(#param: #enum_ident) -> #input_ty { } } =20 + fn impl_try_from( + enum_ident: &Ident, + variants: &[Ident], + repr_ty: &syn::Path, + input_ty: &ValidTy, + ) -> TokenStream { + let param =3D Ident::new("value", Span::call_site()); + + let overflow_assertion =3D emit_overflow_assert(enum_ident, varian= ts, repr_ty, input_ty); + let emit_cast =3D |variant| { + let variant =3D ::quote::quote! { #enum_ident::#variant }; + match input_ty { + ValidTy::Bounded(inner) =3D> { + let base_ty =3D inner.emit_qualified_base_ty(); + let expr =3D parse_quote! { #variant as #base_ty }; + inner.emit_new(&expr) + } + ValidTy::Primitive(ident) if ident =3D=3D "bool" =3D> { + ::quote::quote! { ((#variant as #repr_ty) =3D=3D 1) } + } + qualified @ ValidTy::Primitive(_) =3D> ::quote::quote! { #= variant as #qualified }, + } + }; + + let clauses =3D variants.iter().map(|variant| { + let cast =3D emit_cast(variant); + ::quote::quote! { + if #param =3D=3D #cast { + ::core::result::Result::Ok(#enum_ident::#variant) + } else + } + }); + + ::quote::quote! { + #[automatically_derived] + impl ::core::convert::TryFrom<#input_ty> for #enum_ident { + type Error =3D ::kernel::prelude::Error; + fn try_from(#param: #input_ty) -> Result<#enum_ident, Self= ::Error> { + #overflow_assertion + + #(#clauses)* { + ::core::result::Result::Err(::kernel::prelude::EIN= VAL) + } + } + } + } + } + fn emit_overflow_assert( enum_ident: &Ident, variants: &[Ident], @@ -360,6 +416,14 @@ fn emit_from_expr(&self, expr: &Expr) -> TokenStream { } } =20 + fn emit_new(&self, expr: &Expr) -> TokenStream { + let Self { base_ty, bits, .. } =3D self; + let qualified_name: syn::Path =3D parse_str(Self::QUALIFIED_NAME).= expect("valid path"); + ::quote::quote! { + #qualified_name::<#base_ty, #bits>::new::<{ #expr }>() + } + } + fn emit_qualified_base_ty(&self) -> TokenStream { let base_ty =3D &self.base_ty; ::quote::quote! { ::core::primitive::#base_ty } diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index 8842067d1017..893adecb9080 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -657,3 +657,179 @@ pub fn derive_into(input: TokenStream) -> TokenStream= { .unwrap_or_else(syn::Error::into_compile_error) .into() } + +/// A derive macro for generating an implementation of the [`TryFrom`] tra= it. +/// +/// This macro automatically derives the [`TryFrom`] trait for a given enu= m. Currently, +/// it only supports [unit-only enum]s. +/// +/// [unit-only enum]: https://doc.rust-lang.org/reference/items/enumeratio= ns.html#r-items.enum.unit-only +/// +/// # Notes +/// +/// - The macro generates [`TryFrom`] implementations that: +/// - Return `Ok(VARIANT)` when the input corresponds to a variant. +/// - Return `Err(EINVAL)` when the input does not correspond to any var= iant. +/// (where `EINVAL` is from [`kernel::error::code`]). +/// +/// - The macro uses the `try_from` custom attribute or `repr` attribute t= o generate +/// [`TryFrom`] implementations. `try_from` always takes precedence over= `repr`. +/// +/// - Currently, the macro does not support `repr(C)` fieldless enums sinc= e the actual +/// representation of discriminants is defined by rustc internally, and = documentation +/// around it is not yet settled. See [Rust issue #124403] and [Rust PR = #147017] +/// for more information. +/// +/// - The macro generates a compile-time assertion for every variant to en= sure its +/// discriminant value fits within the type being converted from. +/// +/// [`kernel::error::code`]: ../kernel/error/code/index.html +/// [Rust issue #124403]: https://github.com/rust-lang/rust/issues/124403 +/// [Rust PR #147017]: https://github.com/rust-lang/rust/pull/147017 +/// +/// # Supported types in `#[try_from(...)]` +/// +/// - [`bool`] +/// - Primitive integer types (e.g., [`i8`], [`u8`]) +/// - [`Bounded`] +/// +/// [`Bounded`]: ../kernel/num/bounded/struct.Bounded.html +/// +/// # Examples +/// +/// ## Without Attributes +/// +/// Since [the default `Rust` representation uses `isize` for the discrimi= nant type][repr-rust], +/// the macro implements `TryFrom`: +/// +/// [repr-rust]: https://doc.rust-lang.org/reference/items/enumerations.ht= ml#r-items.enum.discriminant.repr-rust +/// +/// ```rust +/// # use kernel::prelude::*; +/// use kernel::macros::TryFrom; +/// +/// #[derive(Debug, Default, PartialEq, TryFrom)] +/// enum Foo { +/// #[default] +/// A, +/// B =3D 0x7, +/// } +/// +/// assert_eq!(Err(EINVAL), Foo::try_from(-1_isize)); +/// assert_eq!(Ok(Foo::A), Foo::try_from(0_isize)); +/// assert_eq!(Ok(Foo::B), Foo::try_from(0x7_isize)); +/// assert_eq!(Err(EINVAL), Foo::try_from(0x8_isize)); +/// ``` +/// +/// ## With `#[repr(T)]` +/// +/// The macro implements `TryFrom`: +/// +/// ```rust +/// # use kernel::prelude::*; +/// use kernel::macros::TryFrom; +/// +/// #[derive(Debug, Default, PartialEq, TryFrom)] +/// #[repr(u8)] +/// enum Foo { +/// #[default] +/// A, +/// B =3D 0x7, +/// } +/// +/// assert_eq!(Ok(Foo::A), Foo::try_from(0_u8)); +/// assert_eq!(Ok(Foo::B), Foo::try_from(0x7_u8)); +/// assert_eq!(Err(EINVAL), Foo::try_from(0x8_u8)); +/// ``` +/// +/// ## With `#[try_from(...)]` +/// +/// The macro implements `TryFrom` for each `T` specified in `#[try_fro= m(...)]`, +/// which always overrides `#[repr(...)]`: +/// +/// ```rust +/// # use kernel::prelude::*; +/// use kernel::{ +/// macros::TryFrom, +/// num::Bounded, // +/// }; +/// +/// #[derive(Debug, Default, PartialEq, TryFrom)] +/// #[try_from(bool, i16, Bounded)] +/// #[repr(u8)] +/// enum Foo { +/// #[default] +/// A, +/// B, +/// } +/// +/// assert_eq!(Err(EINVAL), Foo::try_from(-1_i16)); +/// assert_eq!(Ok(Foo::A), Foo::try_from(0_i16)); +/// assert_eq!(Ok(Foo::B), Foo::try_from(1_i16)); +/// assert_eq!(Err(EINVAL), Foo::try_from(2_i16)); +/// +/// assert_eq!(Ok(Foo::A), Foo::try_from(false)); +/// assert_eq!(Ok(Foo::B), Foo::try_from(true)); +/// +/// assert_eq!(Ok(Foo::A), Foo::try_from(Bounded::::new::<0>())); +/// assert_eq!(Ok(Foo::B), Foo::try_from(Bounded::::new::<1>())); +/// ``` +/// +/// ## Compile-time Overflow Assertion +/// +/// The following examples do not compile: +/// +/// ```compile_fail +/// # use kernel::macros::TryFrom; +/// #[derive(TryFrom)] +/// #[try_from(u8)] +/// enum Foo { +/// // `256` is larger than `u8::MAX`. +/// A =3D 256, +/// } +/// ``` +/// +/// ```compile_fail +/// # use kernel::macros::TryFrom; +/// #[derive(TryFrom)] +/// #[try_from(u8)] +/// enum Foo { +/// // `-1` cannot be represented with `u8`. +/// A =3D -1, +/// } +/// ``` +/// +/// ## Unsupported Cases +/// +/// The following examples do not compile: +/// +/// ```compile_fail +/// # use kernel::macros::TryFrom; +/// // Tuple-like enums or struct-like enums are not allowed. +/// #[derive(TryFrom)] +/// enum Foo { +/// A(u8), +/// B { inner: u8 }, +/// } +/// ``` +/// +/// ```compile_fail +/// # use kernel::macros::TryFrom; +/// // Structs are not allowed. +/// #[derive(TryFrom)] +/// struct Foo(u8); +/// ``` +/// +/// ```compile_fail +/// # use kernel::macros::TryFrom; +/// // `repr(C)` enums are not allowed. +/// #[derive(TryFrom)] +/// struct Foo(u8) +/// ``` +#[proc_macro_derive(TryFrom, attributes(try_from))] +pub fn derive_try_from(input: TokenStream) -> TokenStream { + let input =3D parse_macro_input!(input as DeriveInput); + convert::derive_try_from(input) + .unwrap_or_else(syn::Error::into_compile_error) + .into() +} --=20 2.52.0