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 2/2] rust: impl_flags: add bitwise operations with the underlying type
Date: Sun, 15 Feb 2026 21:22:58 +0100 [thread overview]
Message-ID: <20260215-impl-flags-additions-v1-2-6538c8fb841c@kernel.org> (raw)
In-Reply-To: <20260215-impl-flags-additions-v1-0-6538c8fb841c@kernel.org>
Add bitwise or operations between the flag value enum and the underlying
type. This is useful when manipulating flags from C API without round
tripping into the Rust flag container type:
let mut lim: bindings::queue_limits = unsafe { core::mem::zeroed() };
...
if self.write_cache {
lim.features |= request::Feature::WriteCache;
}
The above code would be needlessly verbose without this direct assignment
option.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/impl_flags.rs | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/rust/kernel/impl_flags.rs b/rust/kernel/impl_flags.rs
index f34ecbe870d80..f829be815885e 100644
--- a/rust/kernel/impl_flags.rs
+++ b/rust/kernel/impl_flags.rs
@@ -245,6 +245,22 @@ fn bitxor(self, rhs: $flag) -> Self::Output {
}
}
+ impl ::core::ops::BitOr<$flag> for $ty {
+ type Output = Self;
+
+ #[inline]
+ fn bitor(self, rhs: $flag) -> Self::Output {
+ self | <$flag as Into<Self>>::into(rhs)
+ }
+ }
+
+ impl ::core::ops::BitOrAssign<$flag> for $ty {
+ #[inline]
+ fn bitor_assign(&mut self, rhs: $flag) {
+ *self = *self | <$flag as Into<Self>>::into(rhs)
+ }
+ }
+
impl $flags {
/// Returns an empty instance of `type` where no flags are set.
#[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 ` [PATCH 1/2] rust: impl_flags: add conversion functions Andreas Hindborg
2026-02-15 20:53 ` Miguel Ojeda
2026-02-15 20:22 ` Andreas Hindborg [this message]
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-2-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