From: Jesung Yang <y.j3ms.n@gmail.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"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>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
nouveau@lists.freedesktop.org, Jesung Yang <y.j3ms.n@gmail.com>
Subject: [PATCH 0/4] rust: add `FromPrimitive` support
Date: Mon, 23 Jun 2025 15:14:26 +0000 [thread overview]
Message-ID: <cover.1750689857.git.y.j3ms.n@gmail.com> (raw)
This patch series introduces a new `FromPrimitive` trait along with its
corresponding derive macro.
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.
While cleaning up the implementations, I came across an alternative
form of the `FromPrimitive` trait that might better suit the current
use case. Since types that implement this trait may often rely on just
one `from_*` method, the following design could be a simpler fit:
trait FromPrimitive: Sized {
type Primitive;
fn from_bool(b: bool) -> Option<Self>
where
<Self as FromPrimitive>::Primitive: From<bool>,
{
Self::from_primitive(b.into())
}
fn from_primitive(n: Self::Primitive) -> Option<Self>;
}
This is just a thought and not something I feel strongly about, but I
wanted to share it in case others find the idea useful. Feedback or
suggestions are very welcome.
The original discussion of FromPrimitive can be found on Zulip [1].
[1] https://rust-for-linux.zulipchat.com/#narrow/channel/288089/topic/x/near/524427350
Jesung Yang (4):
rust: introduce `FromPrimitive` trait
rust: macros: extend custom `quote!` macro
rust: macros: prefix variable `span` with underscore
rust: macros: add derive macro for `FromPrimitive`
rust/kernel/convert.rs | 154 +++++++++++++++++++++++++++++
rust/kernel/lib.rs | 1 +
rust/macros/convert.rs | 217 +++++++++++++++++++++++++++++++++++++++++
rust/macros/lib.rs | 71 ++++++++++++++
rust/macros/quote.rs | 46 ++++++++-
5 files changed, 487 insertions(+), 2 deletions(-)
create mode 100644 rust/kernel/convert.rs
create mode 100644 rust/macros/convert.rs
base-commit: dc35ddcf97e99b18559d0855071030e664aae44d
--
2.39.5
next reply other threads:[~2025-12-13 12:43 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-23 15:14 Jesung Yang [this message]
2025-06-23 15:14 ` [PATCH 1/4] rust: introduce `FromPrimitive` trait Jesung Yang
2025-06-23 15:14 ` [PATCH 2/4] rust: macros: extend custom `quote!` macro Jesung Yang
2025-06-23 15:14 ` [PATCH 3/4] rust: macros: prefix variable `span` with underscore Jesung Yang
2025-06-23 15:14 ` [PATCH 4/4] rust: macros: add derive macro for `FromPrimitive` Jesung Yang
2025-06-25 14:07 ` [PATCH 0/4] rust: add `FromPrimitive` support Alexandre Courbot
2025-06-26 14:23 ` 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=cover.1750689857.git.y.j3ms.n@gmail.com \
--to=y.j3ms.n@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.