mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "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: Sat, 28 Feb 2026 14:31:51 +0900	[thread overview]
Message-ID: <DGQCKLKCHRD1.25W93SR8LJ87H@nvidia.com> (raw)
In-Reply-To: <20260129-try-from-into-macro-v5-0-dd011008118c@gmail.com>

On Thu Jan 29, 2026 at 11:32 PM JST, Jesung Yang via B4 Relay wrote:
> This patch series introduces derive macros for the `TryFrom` and `Into`
> traits.

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.
- 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).
- 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. :)

<snip>
> 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.

  parent reply	other threads:[~2026-02-28  5:32 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-29 14:32 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 [this message]
2026-02-28  5:33   ` Alexandre Courbot
2026-03-20 10:04   ` Jesung Yang
2026-03-22  6:14     ` Jesung Yang
2026-04-01  5:19       ` Alexandre Courbot
2026-04-02 13:07         ` Jesung Yang
2026-04-02 13:36           ` Alexandre Courbot
2026-04-01  4:32     ` Alexandre Courbot
2026-04-01  5:06       ` Alexandre Courbot

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=DGQCKLKCHRD1.25W93SR8LJ87H@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --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 \
    --cc=y.j3ms.n@gmail.com \
    /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

Powered by JetHome