[PATCH 0/4] rust: add `TryFrom` and `Into` derive macros

Jesung Yang posted 4 patches 2 months ago
There is a newer version of this series
rust/macros/convert.rs | 361 +++++++++++++++++++++++++++++++++++++++++
rust/macros/lib.rs     | 239 +++++++++++++++++++++++++++
rust/macros/quote.rs   |  40 ++++-
3 files changed, 638 insertions(+), 2 deletions(-)
create mode 100644 rust/macros/convert.rs
[PATCH 0/4] rust: add `TryFrom` and `Into` derive macros
Posted by Jesung Yang 2 months ago
This patch series introduces derive macros for `TryFrom` and `Into`
traits.

A few enhancements were made to the custom `quote!()` macro to write
the derive macro. These include support for additional punctuation
tokens and a fix for an unused variable warning when quoting simple
forms. Detailed information about these enhancements is provided in the
relevant patches.

This series builds on the previous work [1], where the `FromPrimitive`
trait was considered too heavy for the current use cases. In response
to the emerging need for functionality similar to `ToPrimitive`, this
series also implements the `Into` derive macro.

The original discussion can be found on Zulip [2].

[1] https://lore.kernel.org/rust-for-linux/cover.1750689857.git.y.j3ms.n@gmail.com/
[2] https://rust-for-linux.zulipchat.com/#narrow/channel/288089/topic/x/near/524335626

Jesung Yang (4):
  rust: macros: extend custom `quote!()` macro
  rust: macros: prefix variable `span` with underscore
  rust: macro: add derive macro for `TryFrom`
  rust: macro: add derive macro for `Into`

 rust/macros/convert.rs | 361 +++++++++++++++++++++++++++++++++++++++++
 rust/macros/lib.rs     | 239 +++++++++++++++++++++++++++
 rust/macros/quote.rs   |  40 ++++-
 3 files changed, 638 insertions(+), 2 deletions(-)
 create mode 100644 rust/macros/convert.rs


base-commit: dff64b072708ffef23c117fa1ee1ea59eb417807
-- 
2.39.5
Re: [PATCH 0/4] rust: add `TryFrom` and `Into` derive macros
Posted by Alexandre Courbot 1 month, 4 weeks ago
On Sun Aug 3, 2025 at 11:20 PM JST, Jesung Yang wrote:
> This patch series introduces derive macros for `TryFrom` and `Into`
> traits.
>
> A few enhancements were made to the custom `quote!()` macro to write
> the derive macro. These include support for additional punctuation
> tokens and a fix for an unused variable warning when quoting simple
> forms. Detailed information about these enhancements is provided in the
> relevant patches.
>
> This series builds on the previous work [1], where the `FromPrimitive`
> trait was considered too heavy for the current use cases. In response
> to the emerging need for functionality similar to `ToPrimitive`, this
> series also implements the `Into` derive macro.
>
> The original discussion can be found on Zulip [2].
>
> [1] https://lore.kernel.org/rust-for-linux/cover.1750689857.git.y.j3ms.n@gmail.com/
> [2] https://rust-for-linux.zulipchat.com/#narrow/channel/288089/topic/x/near/524335626

I have tried this series on nova-core and it allowed me to remove 150
lines of boiletplate just like that. As far as nova-core is concerned,
this is a perfect fit.

For the larger kernel, where using the right type is essential and where
we want to avoid error paths whenever possible, I also feel like this is
a much better fit than the initial `FromPrimitive`. But it would be nice
to hear other opinions on the matter.

I will try to review as much as my proc-macro knowledge allows me to,
but for now:

Tested-by: Alexandre Courbot <acourbot@nvidia.com>
Re: [PATCH 0/4] rust: add `TryFrom` and `Into` derive macros
Posted by Miguel Ojeda 5 days, 22 hours ago
On Fri, Aug 8, 2025 at 11:13 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> I have tried this series on nova-core and it allowed me to remove 150
> lines of boiletplate just like that. As far as nova-core is concerned,
> this is a perfect fit.

It would be nice to see the (possibly RFC) patch if you have it around!

Cheers,
Miguel
Re: [PATCH 0/4] rust: add `TryFrom` and `Into` derive macros
Posted by Alexandre Courbot 5 days, 7 hours ago
On Mon Sep 29, 2025 at 11:24 PM JST, Miguel Ojeda wrote:
> On Fri, Aug 8, 2025 at 11:13 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>>
>> I have tried this series on nova-core and it allowed me to remove 150
>> lines of boiletplate just like that. As far as nova-core is concerned,
>> this is a perfect fit.
>
> It would be nice to see the (possibly RFC) patch if you have it around!

Posted it here! Hope this can help push the series forward.

https://lore.kernel.org/rust-for-linux/20250930-nova-tryfrom-v1-1-0cc1bb507047@nvidia.com/T/#u