mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] rust: impl_flags: add method to return underlying integer
@ 2026-11-04 13:43 Andreas Hindborg
  0 siblings, 0 replies; only message in thread
From: Andreas Hindborg @ 2026-11-04 13:43 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan
  Cc: rust-for-linux, linux-kernel, Andreas Hindborg

Add a `bits` method to return a copy of the underlying integer used to
represent the flags. This is useful when passing flag values to C APIs.

The macro already generates a public `From<$flags> for $ty` conversion, so
this does not expose anything that was not already reachable by users of
the generated types.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
Changes in v3:
- Make `bits()` unconditionally public and drop the inner-field visibility
  specifier from v2. The bit pattern is already reachable through the
  generated `From` implementation, so gating the accessor hid
  nothing (Gary, Miguel).
- Leave the bitmask struct declaration in the doctest unchanged and only
  add a line exercising `bits()` (Miguel).
- Rebase on v7.4-rc1.
- Link to v2: https://msgid.link/20260605-impl-flags-inner-v2-1-ad1ff3f7ad2b@kernel.org

Changes in v2:
- Rename the accessor from `into_inner` to `bits`, matching the
  name used by the `bitflags` crate (Gary).
- Tie the visibility of `bits` to the visibility of the inner
  field of the bitmask struct, so callers must explicitly write
  `pub` to expose the bit pattern outside the defining module (Gary).
- Extend the macro's doctest to declare the inner field `pub`
  and exercise the `bits` accessor.
- Link to v1: https://msgid.link/20260212-impl-flags-inner-v1-1-1e2edc96e470@kernel.org
---
 rust/kernel/impl_flags.rs | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/rust/kernel/impl_flags.rs b/rust/kernel/impl_flags.rs
index fdf44d5eea9c..6051487f6e28 100644
--- a/rust/kernel/impl_flags.rs
+++ b/rust/kernel/impl_flags.rs
@@ -68,6 +68,11 @@
 /// let negated = !read_only;
 /// assert!(negated.contains(Permission::Write));
 /// assert!(!negated.contains(Permission::Read));
+///
+/// // Obtain the raw integer representation with `bits()`, which is
+/// // useful for passing the value to a C API.
+/// let raw: u32 = read_only.bits();
+/// assert_eq!(raw, Permission::Read as u32);
 /// ```
 #[macro_export]
 macro_rules! impl_flags {
@@ -270,6 +275,12 @@ pub fn contains_any(self, flags: $flags) -> bool {
             pub fn contains_all(self, flags: $flags) -> bool {
                 (self.0 & flags.0) == flags.0
             }
+
+            /// Return a copy of the inner representation of the flags.
+            #[inline]
+            pub fn bits(self) -> $ty {
+                self.0
+            }
         }
     };
 }

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260212-impl-flags-inner-c61974b27b18

Best regards,
--  
Andreas Hindborg <a.hindborg@kernel.org>



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-04 13:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04 13:43 [PATCH v3] rust: impl_flags: add method to return underlying integer Andreas Hindborg

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®