Re: [PATCH v5 0/4] rust: add `TryFrom` and `Into` derive macros
From: Alexandre Courbot
Date: Wed Apr 01 2026 - 01:06:58 EST
On Wed Apr 1, 2026 at 1:32 PM JST, Alexandre Courbot wrote:
> Hi Jesung,
>
> On Fri Mar 20, 2026 at 7:04 PM JST, Jesung Yang wrote:
>> Apologies for the delay.
>
> Same. :)
>
>>
>> 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>`.
>
> Sorry, I said the opposite of what I meant - we want to convert a
> `Bounded<u8, 2>` into an enum with 4 variants using a `From`
> implementation.
Forgot to mention: if you want to add a `from` helper attribute, we have
a temporary implementation in nova-core that introduces a couple of
patterns you might want to follow [1].
In particular the `MAX..` arm branch to have to compiler complain if the
enum does not cover the whole range.
[1] https://lore.kernel.org/all/20260325-b4-nova-register-v4-1-bdf172f0f6ca@xxxxxxxxxx/