/
internal.rs
339 lines (285 loc) · 11.5 KB
/
internal.rs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! Generate the internal `bitflags`-facing flags type.
//!
//! The code generated here is owned by `bitflags`, but still part of its public API.
//! Changes to the types generated here need to be considered like any other public API change.
/// Declare the `bitflags`-facing bitflags struct.
///
/// This type is part of the `bitflags` crate's public API, but not part of the user's.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __declare_internal_bitflags {
(
$vis:vis struct $InternalBitFlags:ident: $T:ty;
$iter_vis:vis struct $Iter:ident;
$iter_names_vis:vis struct $IterNames:ident;
) => {
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
$vis struct $InternalBitFlags {
bits: $T,
}
$iter_vis struct $Iter($IterNames);
$iter_names_vis struct $IterNames {
idx: usize,
source: $InternalBitFlags,
state: $InternalBitFlags,
}
};
}
/// Implement functions on the private (bitflags-facing) bitflags type.
///
/// Methods and trait implementations can be freely added here without breaking end-users.
/// If we want to expose new functionality to `#[derive]`, this is the place to do it.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __impl_internal_bitflags {
(
$InternalBitFlags:ident: $T:ty, $BitFlags:ident, $Iter:ident, $IterNames:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
}
) => {
impl $crate::__private::PublicFlags for $BitFlags {
type Internal = $InternalBitFlags;
}
impl $crate::__private::core::default::Default for $InternalBitFlags {
#[inline]
fn default() -> Self {
$InternalBitFlags::empty()
}
}
impl $crate::__private::core::fmt::Debug for $InternalBitFlags {
fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
// Iterate over the valid flags
let mut first = true;
for (name, _) in self.iter_names() {
if !first {
f.write_str(" | ")?;
}
first = false;
f.write_str(name)?;
}
// Append any extra bits that correspond to flags to the end of the format
let extra_bits = self.bits & !Self::all().bits;
if extra_bits != <$T as $crate::__private::Bits>::EMPTY {
if !first {
f.write_str(" | ")?;
}
first = false;
$crate::__private::core::write!(f, "{:#x}", extra_bits)?;
}
if first {
f.write_str("empty")?;
}
$crate::__private::core::fmt::Result::Ok(())
}
}
impl $crate::__private::core::fmt::Binary for $InternalBitFlags {
fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
$crate::__private::core::fmt::Binary::fmt(&self.bits(), f)
}
}
impl $crate::__private::core::fmt::Octal for $InternalBitFlags {
fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
$crate::__private::core::fmt::Octal::fmt(&self.bits(), f)
}
}
impl $crate::__private::core::fmt::LowerHex for $InternalBitFlags {
fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
$crate::__private::core::fmt::LowerHex::fmt(&self.bits(), f)
}
}
impl $crate::__private::core::fmt::UpperHex for $InternalBitFlags {
fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
$crate::__private::core::fmt::UpperHex::fmt(&self.bits(), f)
}
}
impl $InternalBitFlags {
#[inline]
pub const fn empty() -> Self {
Self { bits: <$T as $crate::__private::Bits>::EMPTY }
}
#[inline]
pub const fn all() -> Self {
Self::from_bits_truncate(<$T as $crate::__private::Bits>::ALL)
}
#[inline]
pub const fn bits(&self) -> $T {
self.bits
}
#[inline]
pub fn bits_mut(&mut self) -> &mut $T {
&mut self.bits
}
#[inline]
pub const fn from_bits(bits: $T) -> $crate::__private::core::option::Option<Self> {
let truncated = Self::from_bits_truncate(bits).bits;
if truncated == bits {
$crate::__private::core::option::Option::Some(Self { bits })
} else {
$crate::__private::core::option::Option::None
}
}
#[inline]
pub const fn from_bits_truncate(bits: $T) -> Self {
if bits == <$T as $crate::__private::Bits>::EMPTY {
return Self { bits }
}
let mut truncated = <$T as $crate::__private::Bits>::EMPTY;
$(
$(#[$attr $($args)*])*
if bits & $BitFlags::$Flag.bits() == $BitFlags::$Flag.bits() {
truncated |= $BitFlags::$Flag.bits()
}
)*
Self { bits: truncated }
}
#[inline]
pub const fn from_bits_retain(bits: $T) -> Self {
Self { bits }
}
#[inline]
pub fn from_name(name: &str) -> $crate::__private::core::option::Option<Self> {
match name {
$(
$(#[$attr $($args)*])*
$crate::__private::core::stringify!($Flag) => $crate::__private::core::option::Option::Some(Self { bits: $BitFlags::$Flag.bits() }),
)*
_ => $crate::__private::core::option::Option::None,
}
}
#[inline]
pub const fn iter(&self) -> $Iter {
$Iter(self.iter_names())
}
#[inline]
pub const fn iter_names(&self) -> $IterNames {
$IterNames {
idx: 0,
source: *self,
state: *self,
}
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.bits == Self::empty().bits
}
#[inline]
pub const fn is_all(&self) -> bool {
Self::all().bits | self.bits == self.bits
}
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
!(Self { bits: self.bits & other.bits}).is_empty()
}
#[inline]
pub const fn contains(&self, other: Self) -> bool {
(self.bits & other.bits) == other.bits
}
#[inline]
pub fn insert(&mut self, other: Self) {
self.bits |= other.bits;
}
#[inline]
pub fn remove(&mut self, other: Self) {
self.bits &= !other.bits;
}
#[inline]
pub fn toggle(&mut self, other: Self) {
self.bits ^= other.bits;
}
#[inline]
pub fn set(&mut self, other: Self, value: bool) {
if value {
self.insert(other);
} else {
self.remove(other);
}
}
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self { bits: self.bits & other.bits }
}
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self { bits: self.bits | other.bits }
}
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
Self { bits: self.bits & !other.bits }
}
#[inline]
#[must_use]
pub const fn symmetric_difference(self, other: Self) -> Self {
Self { bits: self.bits ^ other.bits }
}
#[inline]
#[must_use]
pub const fn complement(self) -> Self {
Self::from_bits_truncate(!self.bits)
}
}
impl $crate::__private::core::iter::Iterator for $Iter {
type Item = $BitFlags;
fn next(&mut self) -> $crate::__private::core::option::Option<Self::Item> {
self.0.next().map(|(_, value)| value)
}
}
impl $crate::__private::core::iter::Iterator for $IterNames {
type Item = (&'static str, $BitFlags);
fn next(&mut self) -> $crate::__private::core::option::Option<Self::Item> {
const NUM_FLAGS: usize = {
let mut num_flags = 0;
$(
$(#[$attr $($args)*])*
{
num_flags += 1;
}
)*
num_flags
};
const OPTIONS: [$T; NUM_FLAGS] = [
$(
$(#[$attr $($args)*])*
$BitFlags::$Flag.bits(),
)*
];
const OPTIONS_NAMES: [&'static str; NUM_FLAGS] = [
$(
$(#[$attr $($args)*])*
$crate::__private::core::stringify!($Flag),
)*
];
if self.state.is_empty() || NUM_FLAGS == 0 {
$crate::__private::core::option::Option::None
} else {
for (flag, flag_name) in OPTIONS[self.idx..NUM_FLAGS].iter().copied()
.zip(OPTIONS_NAMES[self.idx..NUM_FLAGS].iter().copied())
{
self.idx += 1;
// NOTE: We check whether the flag exists in self, but remove it from
// a different value. This ensure that overlapping flags are handled
// properly. Take the following example:
//
// const A: 0b00000001;
// const B: 0b00000101;
//
// Given the bits 0b00000101, both A and B are set. But if we removed A
// as we encountered it we'd be left with 0b00000100, which doesn't
// correspond to a valid flag on its own.
if self.source.contains($InternalBitFlags { bits: flag }) {
self.state.remove($InternalBitFlags { bits: flag });
return $crate::__private::core::option::Option::Some((flag_name, $BitFlags::from_bits_retain(flag)))
}
}
$crate::__private::core::option::Option::None
}
}
}
};
}