Skip to contentSkip to conversation

rust: add new macro for common bitflag operations

PATCH Superseded Filipe Xavier 5 participants 13 messages Jan 11 – Jan 16 Rust

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v8 1/1 rust: add new macro for common bitflag operations

We have seen a proliferation of mod_whatever::foo::Flags being defined with essentially the same implementation for BitAnd, BitOr, contains and etc.

This macro aims to bring a solution for this, allowing to generate these methods for user-defined structs. With some use cases in KMS and upcoming GPU drivers.

---
Changes in v8:
- Doc: Fix some terms and improving the macro documentation.
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Mask invalid bits: add all_bits mask to preserve only valid bits in Not and BitXor ops.
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- New methods: add new methods contains_any and contains_all.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Docs: improve macro documentation.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Use enum: changed flag type from struct to enum.
- Minor fix: airect casting (flag as $ty) instead of field access (.0).
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- New Feat: added support to declare flags inside macro use.
- Minor fixes: used absolute paths to refer to items, fix rustdoc and fix example cases.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- rename: change macro and file name to impl_flags.
- negation sign: change char for negation to !.
- transpose docs: add support to transpose user provided docs.
- visibility: add support to use user defined visibility.
- operations: add new operations for flag,
to support use between bit and bitmap, eg: flag & flags.
- code style: small fixes to remove warnings.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 rust/kernel/impl_flags.rs | 260 ++++++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs        |   2 +
 rust/kernel/prelude.rs    |   1 +
 3 files changed, 263 insertions(+)
rust/kernel/impl_flags.rs new file +260 -0
new file mode 100644
index 0000000000000000000000000000000000000000..0c50b8404fa795926b4c12c21350b611a2a3d149
@@ -0,0 +1,260 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Common helper for declaring bitflag and bitmask types.
+///
+/// This macro takes as input:
+/// - A struct declaration representing a bitmask type
+///   (e.g., `pub struct Permissions(u32)`).
+/// - An enumeration declaration representing individual bit flags
+///   (e.g., `pub enum Permission { ... }`).
+///
+/// And generates:
+/// - The struct and enum types with appropriate `#[repr]` attributes.
+/// - Implementations of common bitflag operators
+///   ([`::core::ops::BitOr`], [`::core::ops::BitAnd`], etc.).
+/// - Utility methods such as `.contains()` to check flags.
+///
+/// # Examples
+///
+/// Defining and using [`impl_flags!`]:
+///
+/// ```
+/// use kernel::impl_flags;
+///
+/// impl_flags!(
+///     /// Represents multiple permissions.
+///     #[derive(Debug, Clone, Default, Copy, PartialEq, Eq)]
+///     pub struct Permissions(u32);
+///
+///     /// Represents a single permission.
+///     #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+///     pub enum Permission {
+///         Read = 1 << 0,
+///         Write = 1 << 1,
+///         Execute = 1 << 2,
+///     }
+/// );
+///
+/// // Combine multiple permissions using the bitwise OR (`|`) operator.
+/// let mut read_write: Permissions = Permission::Read | Permission::Write;
+/// assert!(read_write.contains(Permission::Read));
+/// assert!(read_write.contains(Permission::Write));
+/// assert!(!read_write.contains(Permission::Execute));
+/// assert!(read_write.contains_any(Permission::Read | Permission::Execute));
+/// assert!(read_write.contains_all(Permission::Read | Permission::Write));
+///
+/// // Using the bitwise OR assignment (`|=`) operator.
+/// read_write |= Permission::Execute;
+/// assert!(read_write.contains(Permission::Execute));
+///
+/// // Masking a permission with the bitwise AND (`&`) operator.
+/// let read_only: Permissions = read_write & Permission::Read;
+/// assert!(read_only.contains(Permission::Read));
+/// assert!(!read_only.contains(Permission::Write));
+///
+/// // Toggling permissions with the bitwise XOR (`^`) operator.
+/// let toggled: Permissions = read_only ^ Permission::Read;
+/// assert!(!toggled.contains(Permission::Read));
+///
+/// // Inverting permissions with the bitwise NOT (`!`) operator.
+/// let negated = !read_only;
+/// assert!(negated.contains(Permission::Write));
+/// assert!(!negated.contains(Permission::Read));
+/// ```
+#[macro_export]
+macro_rules! impl_flags {
+    (
+        $(#[$outer_flags:meta])*
+        $vis_flags:vis struct $flags:ident($ty:ty);
+
+        $(#[$outer_flag:meta])*
+        $vis_flag:vis enum $flag:ident {
+            $(
+                $(#[$inner_flag:meta])*
+                $name:ident = $value:expr
+            ),+ $( , )?
+        }
+    ) => {
+        $(#[$outer_flags])*
+        #[repr(transparent)]
+        $vis_flags struct $flags($ty);
+
+        $(#[$outer_flag])*
+        #[repr($ty)]
+        $vis_flag enum $flag {
+            $(
+                $(#[$inner_flag])*
+                $name = $value
+            ),+
+        }
+
+        impl ::core::convert::From<$flag> for $flags {
+            #[inline]
+            fn from(value: $flag) -> Self {
+                Self(value as $ty)
+            }
+        }
+
+        impl ::core::convert::From<$flags> for $ty {
+            #[inline]
+            fn from(value: $flags) -> Self {
+                value.0
+            }
+        }
+
+        impl ::core::ops::BitOr for $flags {
+            type Output = Self;
+            #[inline]
+            fn bitor(self, rhs: Self) -> Self::Output {
+                Self(self.0 | rhs.0)
+            }
+        }
+
+        impl ::core::ops::BitOrAssign for $flags {
+            #[inline]
+            fn bitor_assign(&mut self, rhs: Self) {
+                *self = *self | rhs;
+            }
+        }
+
+        impl ::core::ops::BitAnd for $flags {
+            type Output = Self;
+            #[inline]
+            fn bitand(self, rhs: Self) -> Self::Output {
+                Self(self.0 & rhs.0)
+            }
+        }
+
+        impl ::core::ops::BitAndAssign for $flags {
+            #[inline]
+            fn bitand_assign(&mut self, rhs: Self) {
+                *self = *self & rhs;
+            }
+        }
+
+        impl ::core::ops::BitOr<$flag> for $flags {
+            type Output = Self;
+            #[inline]
+            fn bitor(self, rhs: $flag) -> Self::Output {
+                self | Self::from(rhs)
+            }
+        }
+
+        impl ::core::ops::BitOrAssign<$flag> for $flags {
+            #[inline]
+            fn bitor_assign(&mut self, rhs: $flag) {
+                *self = *self | rhs;
+            }
+        }
+
+        impl ::core::ops::BitAnd<$flag> for $flags {
+            type Output = Self;
+            #[inline]
+            fn bitand(self, rhs: $flag) -> Self::Output {
+                self & Self::from(rhs)
+            }
+        }
+
+        impl ::core::ops::BitAndAssign<$flag> for $flags {
+            #[inline]
+            fn bitand_assign(&mut self, rhs: $flag) {
+                *self = *self & rhs;
+            }
+        }
+
+        impl ::core::ops::BitXor for $flags {
+            type Output = Self;
+            #[inline]
+            fn bitxor(self, rhs: Self) -> Self::Output {
+                Self((self.0 ^ rhs.0) & Self::all_bits())
+            }
+        }
+
+        impl ::core::ops::BitXorAssign for $flags {
+            #[inline]
+            fn bitxor_assign(&mut self, rhs: Self) {
+                *self = *self ^ rhs;
+            }
+        }
+
+        impl ::core::ops::Not for $flags {
+            type Output = Self;
+            #[inline]
+            fn not(self) -> Self::Output {
+                Self((!self.0) & Self::all_bits())
+            }
+        }
+
+        impl ::core::ops::BitOr for $flag {
+            type Output = $flags;
+            #[inline]
+            fn bitor(self, rhs: Self) -> Self::Output {
+                $flags(self as $ty | rhs as $ty)
+            }
+        }
+
+        impl ::core::ops::BitAnd for $flag {
+            type Output = $flags;
+            #[inline]
+            fn bitand(self, rhs: Self) -> Self::Output {
+                $flags(self as $ty & rhs as $ty)
+            }
+        }
+
+        impl ::core::ops::BitXor for $flag {
+            type Output = $flags;
+            #[inline]
+            fn bitxor(self, rhs: Self) -> Self::Output {
+                $flags((self as $ty ^ rhs as $ty) & $flags::all_bits())
+            }
+        }
+
+        impl ::core::ops::Not for $flag {
+            type Output = $flags;
+            #[inline]
+            fn not(self) -> Self::Output {
+                $flags((!(self as $ty)) & $flags::all_bits())
+            }
+        }
+
+        impl ::core::ops::BitXor<$flag> for $flags {
+            type Output = Self;
+            #[inline]
+            fn bitxor(self, rhs: $flag) -> Self::Output {
+                self ^ Self::from(rhs)
+            }
+        }
+
+        impl $flags {
+            /// Returns an empty instance of `type` where no flags are set.
+            #[inline]
+            pub const fn empty() -> Self {
+                Self(0)
+            }
+
+            /// Returns a mask containing all valid flag bits.
+            #[inline]
+            pub const fn all_bits() -> $ty {
+                0 $( | $value )+
+            }
+
+            /// Checks if a specific flag is set.
+            #[inline]
+            pub fn contains(self, flag: $flag) -> bool {
+                (self.0 & flag as $ty) == flag as $ty
+            }
+
+            /// Checks if at least one of the provided flags is set.
+            #[inline]
+            pub fn contains_any(self, flags: $flags) -> bool {
+                (self.0 & flags.0) != 0
+            }
+
+            /// Checks if all of the provided flags are set.
+            #[inline]
+            pub fn contains_all(self, flags: $flags) -> bool {
+                (self.0 & flags.0) == flags.0
+            }
+        }
+    };
+}
rust/kernel/lib.rs +2 -0
index 28007be98fbad0e875d7e5345e164e2af2c5da32..57b789315deda3d2fa8961af79cad4462fff7fa8 100644
@@ -67,6 +67,8 @@
 #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
 pub mod firmware;
 pub mod fs;
+#[doc(hidden)]
+pub mod impl_flags;
 pub mod init;
 pub mod io;
 pub mod ioctl;
rust/kernel/prelude.rs +1 -0
index baa774a351ceeb995a2a647f78a27b408d9f3834..085f23502544c80c2202eacd18ba190186a74f30 100644
@@ -27,6 +27,7 @@
 #[doc(no_inline)]
 pub use super::dbg;
 pub use super::fmt;
+pub use super::impl_flags;
 pub use super::{dev_alert, dev_crit, dev_dbg, dev_emerg, dev_err, dev_info, dev_notice, dev_warn};
 pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn};
 

---
base-commit: edc5e6e019c99b529b3d1f2801d5cce9924ae79b
change-id: 20250304-feat-add-bitmask-macro-6424b1c317e2

Best regards,

Signature · 1 line
Filipe Xavier <[email protected]>
#1 lore raw

rust/kernel/impl_flags.rs line 5

Quoted 6 lines · Filipe Xavier
rust/kernel/impl_flags.rs new file +5 -0
+// SPDX-License-Identifier: GPL-2.0
+
+//! Common helper for declaring bitflag and bitmask types.
+///
+/// This macro takes as input:

So I didn't mean to convert the first line to //!, but to add documentation to the module between the SPDX line and the documentation for the item.

Please see how other files do it, e.g. rust/kernel/slice.rs and rust/kernel/irq.rs are similar cases (no use blocks between //! and the first item).

But please wait a few days in case there is any other feedback -- hopefully we are getting finally there.

(By the way, in general, it is good to reply to the feedback messages you get, to confirm or not whether you applied a particular suggestion).

Thanks for updating the docs and being persistent!

Cheers,
Miguel

#2 lore raw
Quoted 9 lines · Miguel Ojeda

rust/kernel/impl_flags.rs line 5

Quoted 5 lines · Filipe Xavier
rust/kernel/impl_flags.rs new file +5 -0
+// SPDX-License-Identifier: GPL-2.0
+
+//! Common helper for declaring bitflag and bitmask types.
+///
+/// This macro takes as input:

So I didn't mean to convert the first line to //!, but to add documentation to the module between the SPDX line and the documentation for the item.

Oh, sorry, I misunderstood. I thought it might be redundant; I'll adjust it in the next version.

Quoted 4 lines

Please see how other files do it, e.g. rust/kernel/slice.rs and rust/kernel/irq.rs are similar cases (no use blocks between //! and the first item).

Quoted 8 lines

But please wait a few days in case there is any other feedback -- hopefully we are getting finally there.

(By the way, in general, it is good to reply to the feedback messages you get, to confirm or not whether you applied a particular suggestion).

Thanks for updating the docs and being persistent!

I am the one who is grateful for the feedback and patience.

Xavier

reviewer Tested-by Reviewed-by on v8 1/1
#3 lore raw
In reply to Filipe Xavier
Quoted 8 lines · Filipe Xavier

We have seen a proliferation of mod_whatever::foo::Flags being defined with essentially the same implementation for BitAnd, BitOr, contains and etc.

This macro aims to bring a solution for this, allowing to generate these methods for user-defined structs. With some use cases in KMS and upcoming GPU drivers.

Quoted 6 lines

I think it would be useful to add:

impl Flags {
     unsafe from_raw(value: Repr) -> {
     ...
     }
}
impl TryFrom<Repr> for Flag {
     // Succeed if `value` is a valid `Flag` enum bit pattern.
}
impl TryFrom<Repr> for Flags {
     // Succeed if `value` is the logical OR of valid enum bit patterns.
}

Best regards,
Andreas Hindborg

#4 lore raw
Quoted 29 lines · Andreas Hindborg
Quoted 8 lines · Filipe Xavier

We have seen a proliferation of mod_whatever::foo::Flags being defined with essentially the same implementation for BitAnd, BitOr, contains and etc.

This macro aims to bring a solution for this, allowing to generate these methods for user-defined structs. With some use cases in KMS and upcoming GPU drivers.

Quoted 6 lines

I think it would be useful to add:

impl Flags {
     unsafe from_raw(value: Repr) -> {
     ...
     }
}
Quoted 7 lines
impl TryFrom<Repr> for Flag {
     // Succeed if `value` is a valid `Flag` enum bit pattern.
}
impl TryFrom<Repr> for Flags {
     // Succeed if `value` is the logical OR of valid enum bit patterns.
}

Also bitwise operations on the underlying type such as

  impl BitOrAssign<Flag> for u32 {
  ...
  }

Not sure if that is possible with the orphan rule, but it would be nice. I was trying to

  lim.features |= request::Feature::Rotational;

where lim: bindings::queue_limits.

Best regards,
Andreas Hindborg

#5 lore raw
Quoted 45 lines · Andreas Hindborg
Quoted 29 lines · Andreas Hindborg
Quoted 8 lines · Filipe Xavier

We have seen a proliferation of mod_whatever::foo::Flags being defined with essentially the same implementation for BitAnd, BitOr, contains and etc.

This macro aims to bring a solution for this, allowing to generate these methods for user-defined structs. With some use cases in KMS and upcoming GPU drivers.

Quoted 6 lines

I think it would be useful to add:

impl Flags {
     unsafe from_raw(value: Repr) -> {
     ...
     }
}
Quoted 7 lines
impl TryFrom<Repr> for Flag {
     // Succeed if `value` is a valid `Flag` enum bit pattern.
}
impl TryFrom<Repr> for Flags {
     // Succeed if `value` is the logical OR of valid enum bit patterns.
}

Also bitwise operations on the underlying type such as

  impl BitOrAssign<Flag> for u32 {
  ...
  }
Quoted 6 lines

Not sure if that is possible with the orphan rule, but it would be nice. I was trying to

  lim.features |= request::Feature::Rotational;

where lim: bindings::queue_limits.

Sorry for the spam. This works for me:

rust/kernel/impl_flags.rs +35 -0
index 0c50b8404fa79..e1143e6381d47 100644
@@ -102,6 +102,25 @@ fn from(value: $flags) -> Self {
             }
         }
 
+        impl ::core::convert::From<$flag> for $ty {
+            #[inline]
+            fn from(value: $flag) -> Self {
+                value as _
+            }
+        }
+
+        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(value)}),
+                    false => Err(::kernel::error::code::EINVAL),
+                }
+            }
+        }
+
         impl ::core::ops::BitOr for $flags {
             type Output = Self;
             #[inline]
@@ -225,6 +244,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]

Best regards,
Andreas Hindborg

reviewer
#6 lore raw
In reply to Andreas Hindborg
Quoted 41 lines · Andreas Hindborg
Quoted 29 lines · Andreas Hindborg
Quoted 8 lines · Filipe Xavier

We have seen a proliferation of mod_whatever::foo::Flags being defined with essentially the same implementation for BitAnd, BitOr, contains and etc.

This macro aims to bring a solution for this, allowing to generate these methods for user-defined structs. With some use cases in KMS and upcoming GPU drivers.

Quoted 6 lines

I think it would be useful to add:

impl Flags {
     unsafe from_raw(value: Repr) -> {
     ...
     }
}
Quoted 7 lines
impl TryFrom<Repr> for Flag {
     // Succeed if `value` is a valid `Flag` enum bit pattern.
}
impl TryFrom<Repr> for Flags {
     // Succeed if `value` is the logical OR of valid enum bit patterns.
}

Also bitwise operations on the underlying type such as

Quoted 8 lines
  impl BitOrAssign<Flag> for u32 {
  ...
  }

Not sure if that is possible with the orphan rule, but it would be nice. I was trying to

  lim.features |= request::Feature::Rotational;

This is allowed by orphan rules, in a similar way that you can implement From<Local> for Foreign.

However I don't think this is desirable feature? Why can't a conversion be used if you're using raw reprs?

I think we should discourage the use of raw integers when bitflags can be used, adding that (or even TryFrom) can be a negative incentive.

Best,
Gary

Quoted 6 lines

where lim: bindings::queue_limits.

Best regards,
Andreas Hindborg

#7 lore raw
Quoted 51 lines · Gary Guo
Quoted 41 lines · Andreas Hindborg
Quoted 29 lines · Andreas Hindborg
Quoted 8 lines · Filipe Xavier

We have seen a proliferation of mod_whatever::foo::Flags being defined with essentially the same implementation for BitAnd, BitOr, contains and etc.

This macro aims to bring a solution for this, allowing to generate these methods for user-defined structs. With some use cases in KMS and upcoming GPU drivers.

Quoted 6 lines

I think it would be useful to add:

impl Flags {
     unsafe from_raw(value: Repr) -> {
     ...
     }
}
Quoted 7 lines
impl TryFrom<Repr> for Flag {
     // Succeed if `value` is a valid `Flag` enum bit pattern.
}
impl TryFrom<Repr> for Flags {
     // Succeed if `value` is the logical OR of valid enum bit patterns.
}

Also bitwise operations on the underlying type such as

Quoted 8 lines
  impl BitOrAssign<Flag> for u32 {
  ...
  }

Not sure if that is possible with the orphan rule, but it would be nice. I was trying to

  lim.features |= request::Feature::Rotational;
Quoted 8 lines

This is allowed by orphan rules, in a similar way that you can implement From<Local> for Foreign.

However I don't think this is desirable feature? Why can't a conversion be used if you're using raw reprs?

I think we should discourage the use of raw integers when bitflags can be used, adding that (or even TryFrom) can be a negative incentive.

In this case I have a C struct with a flag field. It would not be ergonomic to read out the field, convert to Flag type, do the operation, and store back again.

This assignment is in abstraction code. I could use the raw bindings constant for the operation, but I would rather use the Rust type.

I do not think there is any risk by allowing operators between flags and integers. What situation do you imagine where there would be a problem?

Best regards
Andreas Hindborg

#8 lore raw
In reply to Gary Guo
Quoted 55 lines · Gary Guo
Quoted 41 lines · Andreas Hindborg
Quoted 29 lines · Andreas Hindborg
Quoted 8 lines · Filipe Xavier

We have seen a proliferation of mod_whatever::foo::Flags being defined with essentially the same implementation for BitAnd, BitOr, contains and etc.

This macro aims to bring a solution for this, allowing to generate these methods for user-defined structs. With some use cases in KMS and upcoming GPU drivers.

Quoted 6 lines

I think it would be useful to add:

impl Flags {
    unsafe from_raw(value: Repr) -> {
    ...
    }
}
Quoted 7 lines
impl TryFrom<Repr> for Flag {
    // Succeed if `value` is a valid `Flag` enum bit pattern.
}
impl TryFrom<Repr> for Flags {
    // Succeed if `value` is the logical OR of valid enum bit patterns.
}

Also bitwise operations on the underlying type such as

Quoted 8 lines

impl BitOrAssign<Flag> for u32 {
...
}

Not sure if that is possible with the orphan rule, but it would be nice. I was trying to

lim.features |= request::Feature::Rotational;

Quoted 8 lines

This is allowed by orphan rules, in a similar way that you can implement From<Local> for Foreign.

However I don't think this is desirable feature? Why can't a conversion be used if you're using raw reprs?

I think we should discourage the use of raw integers when bitflags can be used, adding that (or even TryFrom) can be a negative incentive.

The flags may be in your uapi or they may otherwise come from C. In that case you’re stuck with a TryFrom or a manual conversion.

I think a generated TryFrom shouldn't hurt. Just my 0.02c.

Quoted 10 lines

Best,
Gary

Quoted 6 lines

where lim: bindings::queue_limits.

Best regards,
Andreas Hindborg

#9 lore raw
Quoted 5 lines · Daniel Almeida

The flags may be in your uapi or they may otherwise come from C. In that case you’re stuck with a TryFrom or a manual conversion.

I think a generated TryFrom shouldn't hurt. Just my 0.02c.

I would say let's land this patch as-is (with the doc bit I mentioned) and then we can discuss additions on top independently.

Cheers,
Miguel

#10 lore raw
Quoted 7 lines · Miguel Ojeda
Quoted 1 line · Daniel Almeida
Quoted 7 lines
Quoted 4 lines

The flags may be in your uapi or they may otherwise come from C. In that case you’re stuck with a TryFrom or a manual conversion.

I think a generated TryFrom shouldn't hurt. Just my 0.02c.

I would say let's land this patch as-is (with the doc bit I mentioned) and then we can discuss additions on top independently.

Sure, that is also fine.

Thanks for all the work, Filipe :)

— Daniel

#11 lore raw
Quoted 8 lines · Daniel Almeida
Quoted 8 lines · Miguel Ojeda
Quoted 4 lines · Daniel Almeida

The flags may be in your uapi or they may otherwise come from C. In that case you’re stuck with a TryFrom or a manual conversion.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I think a generated TryFrom shouldn't hurt. Just my 0.02c.

I would say let's land this patch as-is (with the doc bit I mentioned) and then we can discuss additions on top independently.

Sure, that is also fine.

Thanks for all the work, Filipe :)

Absolutely, I did not intend to stall this series. Just suggestions for future work.

Best regards,
Andreas Hindborg

#12 lore raw
In reply to Andreas Hindborg
Quoted 37 lines · Andreas Hindborg
Quoted 8 lines · Filipe Xavier

We have seen a proliferation of mod_whatever::foo::Flags being defined with essentially the same implementation for BitAnd, BitOr, contains and etc.

This macro aims to bring a solution for this, allowing to generate these methods for user-defined structs. With some use cases in KMS and upcoming GPU drivers.

Quoted 6 lines

I think it would be useful to add:

impl Flags {
    unsafe from_raw(value: Repr) -> {
    ...
    }
}
impl TryFrom<Repr> for Flag {
    // Succeed if `value` is a valid `Flag` enum bit pattern.
}
Quoted 8 lines
impl TryFrom<Repr> for Flags {
    // Succeed if `value` is the logical OR of valid enum bit patterns.
}

Best regards,
Andreas Hindborg

Ah, true. There is a use-case for that in Tyr as well where we ingest the raw values from userspace and need to validate and build valid flags from. This may be somewhat common for others too.