mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Igor Korotin via B4 Relay <devnull+igor.korotin.linux.gmail.com@kernel.org>
To: "Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"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>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Wolfram Sang" <wsa+renesas@sang-engineering.com>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	 linux-i2c@vger.kernel.org, markus.probst@posteo.de,
	 Igor Korotin <igor.korotin.linux@gmail.com>
Subject: [PATCH 2/5] rust: bits: add define_flags macro
Date: Sat, 31 Jan 2026 14:12:44 +0000	[thread overview]
Message-ID: <20260131-i2c-adapter-v1-2-5a436e34cd1a@gmail.com> (raw)
In-Reply-To: <20260131-i2c-adapter-v1-0-5a436e34cd1a@gmail.com>

From: Igor Korotin <igor.korotin.linux@gmail.com>

introduce define_flags macro that incorporates BitOr BitAnd and Not
implementations.

Signed-off-by: Igor Korotin <igor.korotin.linux@gmail.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



  parent reply	other threads:[~2026-01-31 14:12 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-31 14:12 [PATCH 0/5] rust: extend I2C functionality Igor Korotin via B4 Relay
2026-01-31 14:12 ` [PATCH 1/5] rust: i2c: split client and adapter code into separate files Igor Korotin via B4 Relay
2026-01-31 14:12 ` Igor Korotin via B4 Relay [this message]
2026-02-08 13:56   ` [PATCH 2/5] rust: bits: add define_flags macro Daniel Almeida
2026-01-31 14:12 ` [PATCH 3/5] rust: i2c: Add I2C Adapter registration abstractions Igor Korotin via B4 Relay
2026-01-31 14:12 ` [PATCH 4/5] rust: i2c: add I2C wrappers Igor Korotin via B4 Relay
2026-01-31 14:28   ` Markus Probst
2026-02-04 16:49     ` Igor Korotin
2026-02-04 16:59       ` Danilo Krummrich
2026-02-08 12:44         ` Igor Korotin
2026-02-04 17:03       ` Danilo Krummrich
2026-01-31 14:12 ` [PATCH 5/5] samples: rust: add Rust I2C adapter registration sample Igor Korotin via B4 Relay
2026-01-31 14:26 ` [PATCH 0/5] rust: extend I2C functionality Danilo Krummrich
2026-02-08 12:34   ` Igor Korotin
2026-02-08 17:07     ` Danilo Krummrich
2026-02-09 11:31   ` Bartosz Golaszewski

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=20260131-i2c-adapter-v1-2-5a436e34cd1a@gmail.com \
    --to=devnull+igor.korotin.linux.gmail.com@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=igor.korotin.linux@gmail.com \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=markus.probst@posteo.de \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=wsa+renesas@sang-engineering.com \
    /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

all inboxes | Powered by JetHome®