1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
macro_rules! define_bitflag {
(
$(#[$outer_meta:meta])*
$vis:vis struct $name:ident: $int:ty {
$(
$(#[$inner_meta:meta])*
$field:ident: $value:expr
),*
$(,)?
}
) => {
$(#[$outer_meta])*
$vis struct $name {
$(
$(#[$inner_meta])*
pub $field: bool,
)*
}
const _: () = {
#[allow(non_upper_case_globals)]
mod __internal_field_values {
$(
pub const $field: $int = $value as _;
)*
}
impl $name {
/// ビットフラグから構造体を生成します。
pub fn from_bits(bits: $int) -> Self {
Self {
$(
$field: (bits & __internal_field_values::$field) != 0,
)*
}
}
/// 構造体をビットフラグに変換します。
pub fn to_bits(&self) -> $int {
let mut bits: $int = 0;
$(
if self.$field {
bits |= __internal_field_values::$field;
}
)*
bits
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut parts = Vec::new();
$(
if self.$field {
parts.push(stringify!($field));
}
)*
write!(f, "{}({})", stringify!($name), parts.join(" | "))
}
}
impl std::ops::BitOr for $name {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
Self {
$(
$field: self.$field | rhs.$field,
)*
}
}
}
impl std::ops::BitAnd for $name {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
Self {
$(
$field: self.$field & rhs.$field,
)*
}
}
}
impl std::ops::Not for $name {
type Output = Self;
fn not(self) -> Self::Output {
Self {
$(
$field: !self.$field,
)*
}
}
}
impl std::ops::BitXor for $name {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self::Output {
Self {
$(
$field: self.$field ^ rhs.$field,
)*
}
}
}
impl std::ops::Sub for $name {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
$(
$field: self.$field & !rhs.$field,
)*
}
}
}
};
};
}