From: "Jesung Yang" <y.j3ms.n@gmail.com>
To: "Charalampos Mitrodimas" <charmitro@posteo.net>, <y.j3ms.n@gmail.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
nouveau@lists.freedesktop.org
Subject: Re: [PATCH v5 2/4] rust: macros: add derive macro for `TryFrom`
Date: Fri, 06 Feb 2026 06:06:24 +0900 [thread overview]
Message-ID: <DG7C1LYQ4SOQ.1HBJ8WH923JHW@gmail.com> (raw)
In-Reply-To: <87h5rxuw1n.fsf@posteo.net>
On Wed Feb 4, 2026 at 10:39 AM KST, Charalampos Mitrodimas wrote:
> Jesung Yang via B4 Relay <devnull+y.j3ms.n.gmail.com@kernel.org> writes:
[...]
>> + fn impl_try_from(
>> + enum_ident: &Ident,
>> + variants: &[Ident],
>> + repr_ty: &syn::Path,
>> + input_ty: &ValidTy,
>> + ) -> TokenStream {
>> + let param = Ident::new("value", Span::call_site());
>> +
>> + let overflow_assertion = emit_overflow_assert(enum_ident, variants, repr_ty, input_ty);
>> + let emit_cast = |variant| {
>> + let variant = ::quote::quote! { #enum_ident::#variant };
>> + match input_ty {
>> + ValidTy::Bounded(inner) => {
>> + let base_ty = inner.emit_qualified_base_ty();
>> + let expr = parse_quote! { #variant as #base_ty };
>> + inner.emit_new(&expr)
>> + }
>> + ValidTy::Primitive(ident) if ident == "bool" => {
>> + ::quote::quote! { ((#variant as #repr_ty) == 1) }
>> + }
>> + qualified @ ValidTy::Primitive(_) => ::quote::quote! { #variant as #qualified },
>> + }
>> + };
>> +
>> + let clauses = variants.iter().map(|variant| {
>> + let cast = emit_cast(variant);
>> + ::quote::quote! {
>> + if #param == #cast {
>> + ::core::result::Result::Ok(#enum_ident::#variant)
>> + } else
>> + }
>> + });
>> +
>> + ::quote::quote! {
>> + #[automatically_derived]
>> + impl ::core::convert::TryFrom<#input_ty> for #enum_ident {
>> + type Error = ::kernel::prelude::Error;
>> + fn try_from(#param: #input_ty) -> Result<#enum_ident, Self::Error> {
>> + #overflow_assertion
>> +
>> + #(#clauses)* {
>> + ::core::result::Result::Err(::kernel::prelude::EINVAL)
>
> What happens if we need a different error type here? For example, a
> quick look around in nova-core's "Chipset" enum, an unrecognized chipset
> ID warrants ENODEV rather than EINVAL, since it's about device
> identification.
>
> Not sure if it fits the design, just wondering if this flexibility would
> be useful, but would something like an optional
>
> error = <ERROR>
>
> in the
>
> #[try_from(...)]
>
> attribute make sense? e.g.
>
> #[try_from(u32, error = ENODEV)]
>
> defaulting ofcourse to EINVAL if unspecified.
I believe this is indeed a desired change.
Back in September, an RFC [1] using the same API (i.e., without error
customization) was sent; I took a quick look at the time and felt
everything was OK, but in hindsight, the need for this flexibility is
clear.
Your proposed API looks good to me. Unless there are objections, I'll
move forward with this approach.
Thanks!
[1] https://lore.kernel.org/rust-for-linux/20250930-nova-tryfrom-v1-1-0cc1bb507047@nvidia.com/
Best regards,
Jesung
next prev parent reply other threads:[~2026-02-05 21:06 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-29 14:32 [PATCH v5 0/4] rust: add `TryFrom` and `Into` derive macros Jesung Yang via B4 Relay
2026-01-29 14:32 ` [PATCH v5 1/4] rust: macros: add derive macro for `Into` Jesung Yang via B4 Relay
2026-01-29 16:11 ` Charalampos Mitrodimas
2026-01-30 10:03 ` Jesung Yang
2026-01-29 14:32 ` [PATCH v5 2/4] rust: macros: add derive macro for `TryFrom` Jesung Yang via B4 Relay
2026-02-04 1:39 ` Charalampos Mitrodimas
2026-02-05 21:06 ` Jesung Yang [this message]
2026-02-17 1:55 ` Alexandre Courbot
2026-02-17 2:45 ` Charalampos Mitrodimas
2026-01-29 14:32 ` [PATCH v5 3/4] rust: macros: add private doctests for `Into` derive macro Jesung Yang via B4 Relay
2026-01-29 14:32 ` [PATCH v5 4/4] rust: macros: add private doctests for `TryFrom` " Jesung Yang via B4 Relay
2026-02-03 12:48 ` [PATCH v5 0/4] rust: add `TryFrom` and `Into` derive macros shivam kalra
2026-02-28 5:31 ` Alexandre Courbot
2026-02-28 5:33 ` Alexandre Courbot
2026-03-20 10:04 ` Jesung Yang
2026-03-22 6:14 ` Jesung Yang
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=DG7C1LYQ4SOQ.1HBJ8WH923JHW@gmail.com \
--to=y.j3ms.n@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=charmitro@posteo.net \
--cc=dakr@kernel.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=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