public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Jesung Yang" <y.j3ms.n@gmail.com>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
	"Jesung Yang via B4 Relay"
	<devnull+y.j3ms.n.gmail.com@kernel.org>
Cc: y.j3ms.n@gmail.com, "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>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	nouveau@lists.freedesktop.org
Subject: Re: [PATCH v5 0/4] rust: add `TryFrom` and `Into` derive macros
Date: Fri, 20 Mar 2026 19:04:01 +0900	[thread overview]
Message-ID: <DH7IVVURBJ8G.QP1WRFTGS556@gmail.com> (raw)
In-Reply-To: <DGQCKLKCHRD1.25W93SR8LJ87H@nvidia.com>

Apologies for the delay.

On Sat Feb 28, 2026 at 2:31 PM KST, Alexandre Courbot wrote:
[...]
> FWIW I have converted nova-core to use this, and this results in -200LoC
> delta. Obviously I like this very much. :)
>
> A few pieces of feedback for things I noticed while doing the
> conversion:
>
> - Very often we need to convert a type from and into the same primitive.
>   Not having to repeat the same thing in `#[try_from(foo)]` and
>   `#[into(foo)]` for these cases would be nice.

I think I can add a common attribute that works for both `TryFrom` and
`Into`, e.g. `#[convert(foo)]`.

> - In some rare cases, we want to convert an enum with 4 variants into
>   e.g. a `Bounded<u8, 2>`. This can be done using a `From`
>   implementation, and that's what the register macro expects. These
>   cases are not covered by the current macro (they are few however).

I think you can just do the following?:

    #[derive(Into)]
    #[into(Bounded<u8, 2>)]
    enum Enum {
        A,
        B,
        C,
        D,
    }

    let a = Bounded::<u8, 2>::from(Enum::A);
    // or let a: Bounded<u8, 2> = Enum::A.into();

This works because `Into` actually generates the `From<Enum>`
implementation for `Bounded<u8, 2>`.

> - When converting from/into boundeds, the number of used bits is the
>   only thing that counts - the backing type (u8, u32, ...) is
>   irrelevant. I thought it would be cool to be able to have a generic
>   TryFrom/Into implementation that works irrespective of the backing
>   type, but as far as I can tell this would be difficult to achieve.
>   Just throwing the idea in case others are more inspired than I am. :)

Yeah, it seems difficult to me as well since those macros need the
full type information to generate the trait implementations.

>> One last point: the current `Into` implementation relies on
>> `Bounded::from_expr()`, which utilizes `build_assert!()`. So the
>> expanded result looks roughly like this:
>>
>>   impl From<Enum> for Bounded<u8, 4> {
>>       fn from(value: Enum) -> Bounded<u8, 4> {
>>           // Compile-time assertions to guarantee `value` fits within
>>           // `u8` (Omitted)
>>
>>           Bounded::<u8, 4>::from_expr(value as u8)
>>       }
>>   }
>>
>> After some experimentation, it appears that the compiler correctly
>> optimizes out the assertion if (and only if) the `Bounded` type covers
>> all enum discriminants, though I'm not 100% certain of this behavior in
>> all cases. Can we approach this better?
>
> I think you should also be able to have a match arm for all variants and
> call the `Bounded::new` const method - hopefully the compiler will
> optimize that as well. That or rely on unsafe code and a (hypothetical)
> `new_unchecked` constructor for Bounded, since the macro can infer that
> its invariants are respected.

This looks much better. Thanks for the idea!

I'll update the implementation based on your feedback within this week,
but I'd like to ping Benno first to check whether he's satisfied with
this v5 before I send those new changes.

Thanks for your patience.

Best regards,
Jesung

  parent reply	other threads:[~2026-03-20 10:04 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
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 [this message]
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=DH7IVVURBJ8G.QP1WRFTGS556@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=dakr@kernel.org \
    --cc=devnull+y.j3ms.n.gmail.com@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