]> git.ipfire.org Git - thirdparty/linux.git/commit
rust: add `impl_flags!` macro for defining common bitflag operations
authorFilipe Xavier <felipeaggger@gmail.com>
Sat, 17 Jan 2026 10:41:25 +0000 (07:41 -0300)
committerMiguel Ojeda <ojeda@kernel.org>
Mon, 2 Feb 2026 07:09:11 +0000 (08:09 +0100)
commit0e62e4f3e56cf6c44926db2ee82ff29b4a28ac03
tree951bec16cfad5c0f86c3cd6fc20909855f2b3a0f
parent08afcc38a64cec3d6065b90391afebfde686a69a
rust: add `impl_flags!` macro for defining common bitflag operations

We have seen a proliferation of `mod_whatever::foo::Flags` being
defined with essentially the same implementation for `BitAnd`, `BitOr`,
`.contains()` 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.

Link: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/We.20really.20need.20a.20common.20.60Flags.60.20type
Suggested-by: Daniel Almeida <daniel.almeida@collabora.com>
Suggested-by: Lyude Paul <lyude@redhat.com>
Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Reviewed-by: Lyude Paul <lyude@redhat.com>
Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Signed-off-by: Filipe Xavier <felipeaggger@gmail.com>
Link: https://patch.msgid.link/20260117-feat-add-bitmask-macro-v9-1-45ea1f00f846@gmail.com
[ Implemented missing `BitXorAssign<$flag> for $flags`. Sorted
  `impl`s. Removed prelude addition for now -- I asked the team and they
  also felt it wasn't needed. We can always add it later on if needed.
  Fixed intra-doc link (by removing the sentence since it was superfluous
  anyway). Simplified `empty()` title. Reworded commit slightly. Added
  docs to enum variants in example to avoid 'missing_docs' lint when used
  in actual code. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/impl_flags.rs [new file with mode: 0644]
rust/kernel/lib.rs