kernel/lib/macros/bitflags.rs
1// SPDX-License-Identifier: GPL-3.0-only
2//! Bitflags macro for creating bitwise flag definitions
3//!
4//! Authors: MarioS271
5
6macro_rules! bitflags {
7 ($(#[$meta:meta])* $name:ident, $inner:ty) => {
8 $(#[$meta])*
9 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
10 #[repr(transparent)]
11 pub struct $name($inner);
12
13 impl $name {
14 /// Construct an empty bitflag
15 pub const fn empty() -> Self {
16 Self(0)
17 }
18
19 /// Construct a bitflag from the given u8
20 pub const fn from_u8(value: u8) -> Self {
21 Self(value)
22 }
23
24 /// Check if the bitflag contains a specific flag or flag combination
25 pub const fn contains(self, other: Self) -> bool {
26 self.0 & other.0 == other.0
27 }
28
29 /// Returns the inner [`u8`] value of `self`
30 pub const fn as_u8(&self) -> u8 {
31 self.0
32 }
33 }
34
35 impl core::ops::BitOr for $name {
36 type Output = Self;
37
38 /// Apply the bitwise OR operation to the two values
39 fn bitor(self, rhs: Self) -> Self::Output {
40 Self(self.0 | rhs.0)
41 }
42 }
43 impl core::ops::BitOrAssign for $name {
44 /// Mask the inner value using the given value and the bitwise OR operation
45 fn bitor_assign(&mut self, rhs: Self) {
46 self.0 |= rhs.0
47 }
48 }
49 impl core::ops::BitAnd for $name {
50 type Output = Self;
51
52 /// Apply the bitwise AND operation to the two values
53 fn bitand(self, rhs: Self) -> Self::Output {
54 Self(self.0 & rhs.0)
55 }
56 }
57 impl core::ops::BitAndAssign for $name {
58 /// Mask the inner value using the given value and the bitwise AND operation
59 fn bitand_assign(&mut self, rhs: Self) {
60 self.0 &= rhs.0
61 }
62 }
63 };
64}
65pub(crate) use bitflags;