From: Andreas Hindborg <a.hindborg@kernel.org>
To: "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>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
Andreas Hindborg <a.hindborg@kernel.org>
Subject: [PATCH 1/2] rust: impl_flags: add conversion functions
Date: Sun, 15 Feb 2026 21:22:57 +0100 [thread overview]
Message-ID: <20260215-impl-flags-additions-v1-1-6538c8fb841c@kernel.org> (raw)
In-Reply-To: <20260215-impl-flags-additions-v1-0-6538c8fb841c@kernel.org>
Add two conversion functions to the `impl_flags!` macro:
- A `From<_>` implementation to convert from the flag value enum to
underlying type.
- A `TryFrom<_> implementation to convert from the underlying
representation to flag container type.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/impl_flags.rs | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/rust/kernel/impl_flags.rs b/rust/kernel/impl_flags.rs
index 9160e8ce2e126..f34ecbe870d80 100644
--- a/rust/kernel/impl_flags.rs
+++ b/rust/kernel/impl_flags.rs
@@ -102,6 +102,26 @@ fn from(value: $flags) -> Self {
}
}
+ impl ::core::convert::From<$flag> for $ty {
+ #[inline]
+ fn from(value: $flag) -> Self {
+ // SAFETY: All `$flag` values are valid for use as `$ty`.
+ unsafe { core::mem::transmute::<$flag, $ty>(value) }
+ }
+ }
+
+ impl ::core::convert::TryFrom<$ty> for $flags {
+ type Error = ::kernel::error::Error;
+
+ fn try_from(value: $ty) -> Result<Self, Self::Error> {
+ match value & !$flags::all_bits() == 0 {
+ // SAFETY: We checked above that `value` is a valid bit pattern for `$flags`.
+ true => Ok(unsafe {::core::mem::transmute::<$ty, $flags>(value)}),
+ false => Err(::kernel::error::code::EINVAL),
+ }
+ }
+ }
+
impl ::core::ops::BitOr for $flags {
type Output = Self;
#[inline]
--
2.51.2
next prev parent reply other threads:[~2026-02-15 20:23 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-15 20:22 [PATCH 0/2] rust: impl_flags: add convenience functions Andreas Hindborg
2026-02-15 20:22 ` Andreas Hindborg [this message]
2026-02-15 20:53 ` [PATCH 1/2] rust: impl_flags: add conversion functions Miguel Ojeda
2026-02-15 20:22 ` [PATCH 2/2] rust: impl_flags: add bitwise operations with the underlying type Andreas Hindborg
2026-02-27 14:20 ` [PATCH 0/2] rust: impl_flags: add convenience functions Gary Guo
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=20260215-impl-flags-additions-v1-1-6538c8fb841c@kernel.org \
--to=a.hindborg@kernel.org \
--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=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