[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260131-i2c-adapter-v1-2-5a436e34cd1a@gmail.com>
Date: Sat, 31 Jan 2026 14:12:44 +0000
From: Igor Korotin via B4 Relay <devnull+igor.korotin.linux.gmail.com@...nel.org>
To: Danilo Krummrich <dakr@...nel.org>,
Daniel Almeida <daniel.almeida@...labora.com>,
Miguel Ojeda <ojeda@...nel.org>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>, Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
Wolfram Sang <wsa+renesas@...g-engineering.com>
Cc: linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org,
linux-i2c@...r.kernel.org, markus.probst@...teo.de,
Igor Korotin <igor.korotin.linux@...il.com>
Subject: [PATCH 2/5] rust: bits: add define_flags macro
From: Igor Korotin <igor.korotin.linux@...il.com>
introduce define_flags macro that incorporates BitOr BitAnd and Not
implementations.
Signed-off-by: Igor Korotin <igor.korotin.linux@...il.com>
---
rust/kernel/bits.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/rust/kernel/bits.rs b/rust/kernel/bits.rs
index 553d50265883..6750aef18708 100644
--- a/rust/kernel/bits.rs
+++ b/rust/kernel/bits.rs
@@ -201,3 +201,60 @@ pub const fn [<genmask_ $ty>](range: RangeInclusive<u32>) -> $ty {
/// assert_eq!(genmask_u8(0..=7), u8::MAX);
/// ```
);
+
+/// defines flags
+#[macro_export]
+macro_rules! define_flags {
+ (
+ $gen_name:ident($gen_type:ident),
+ $($variant:ident = $binding:expr,)+
+ ) => {
+ /// Flags that can be combined with the operators `|`, `&`, and `!`.
+ ///
+ /// Values can be used from the [`flags`] module.
+ #[derive(Clone, Copy, PartialEq)]
+ pub struct $gen_name($gen_type);
+
+ impl $gen_name {
+ /// Get the raw representation of this flag.
+ pub(crate) fn as_raw(self) -> $gen_type {
+ self.0
+ }
+
+ /// Check whether `flags` is contained in `self`.
+ pub fn contains(self, flags: $gen_name) -> bool {
+ (self & flags) == flags
+ }
+ }
+
+ impl core::ops::BitOr for $gen_name {
+ type Output = $gen_name;
+ fn bitor(self, rhs: Self) -> Self::Output {
+ Self(self.0 | rhs.0)
+ }
+ }
+
+ impl core::ops::BitAnd for $gen_name {
+ type Output = $gen_name;
+ fn bitand(self, rhs: Self) -> Self::Output {
+ Self(self.0 & rhs.0)
+ }
+ }
+
+ impl core::ops::Not for $gen_name {
+ type Output = $gen_name;
+ fn not(self) -> Self::Output {
+ Self(!self.0)
+ }
+ }
+
+ #[allow(missing_docs)]
+ pub mod flags {
+ use super::$gen_name;
+ $(
+ #[allow(missing_docs)]
+ pub const $variant: $gen_name = $gen_name($binding as $gen_type);
+ )+
+ }
+ }
+}
--
2.43.0
Powered by blists - more mailing lists