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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
macro_rules! bitflags_array_impl {
($impl_name:ident $inner_name:ident $name:ident $op:tt $($struct_names:ident)*) => (
impl core::ops::$impl_name for $name {
type Output = Self;
#[inline]
fn $inner_name(self, other: Self) -> Self {
Self {
$($struct_names: self.$struct_names $op other.$struct_names,)*
}
}
}
)
}
pub(crate) use bitflags_array_impl;
macro_rules! bitflags_array_impl_assign {
($impl_name:ident $inner_name:ident $name:ident $op:tt $($struct_names:ident)*) => (
impl core::ops::$impl_name for $name {
#[inline]
fn $inner_name(&mut self, other: Self) {
$(self.$struct_names $op other.$struct_names;)*
}
}
)
}
pub(crate) use bitflags_array_impl_assign;
macro_rules! bit_array_impl {
($impl_name:ident $inner_name:ident $name:ident $op:tt) => (
impl core::ops::$impl_name for $name {
type Output = Self;
#[inline]
fn $inner_name(mut self, other: Self) -> Self {
for (inner, other) in self.0.iter_mut().zip(other.0.iter()) {
*inner $op *other;
}
self
}
}
)
}
pub(crate) use bit_array_impl;
macro_rules! bitflags_independent_two_arg {
($(#[$meta:meta])* $func_name:ident $($struct_names:ident)*) => (
$(#[$meta])*
pub const fn $func_name(self, other:Self) -> Self {
Self { $($struct_names: self.$struct_names.$func_name(other.$struct_names),)* }
}
)
}
pub(crate) use bitflags_independent_two_arg;
// For the most part this macro should not be modified, most configuration should be possible
// without changing this macro.
/// Macro for creating sets of bitflags, we need this because there are almost more flags than bits
/// in a u64, we can't use a u128 because of FFI, and the number of flags is increasing.
///
/// Alternatively we also want to separate WebGPU features from native WGPU features.
macro_rules! bitflags_array {
(
$(#[$outer:meta])*
pub struct ($name:ident, $name_bits:ident): [$T:ty; $Len:expr];
$(
$(#[$bit_outer:meta])*
$vis:vis struct $inner_name:ident $lower_inner_name:ident {
$(
$(#[doc $($args:tt)*])*
const $Flag:tt = $value:expr;
)*
}
)*
) => {
$(
bitflags::bitflags! {
$(#[$bit_outer])*
$vis struct $inner_name: $T {
$(
$(#[doc $($args)*])*
const $Flag = $value;
)*
}
}
)*
$(#[$outer])*
pub struct $name {
$(
#[expect(missing_docs, reason = "the inner flag types are documented")]
$vis $lower_inner_name: $inner_name,
)*
}
#[doc = concat!("Bits from [`", stringify!($name), "`] in array form")]
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct $name_bits(pub [$T; $Len]);
$crate::bitflags_array_impl! { BitOr bitor $name | $($lower_inner_name)* }
$crate::bitflags_array_impl! { BitAnd bitand $name & $($lower_inner_name)* }
$crate::bitflags_array_impl! { BitXor bitxor $name ^ $($lower_inner_name)* }
impl core::ops::Not for $name {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self {
$($lower_inner_name: !self.$lower_inner_name,)*
}
}
}
$crate::bitflags_array_impl! { Sub sub $name - $($lower_inner_name)* }
#[cfg(feature = "serde")]
impl Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
bitflags::serde::serialize(self, serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
bitflags::serde::deserialize(deserializer)
}
}
impl core::fmt::Display for $name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut iter = self.iter_names();
// simple look ahead
let mut next = iter.next();
while let Some((name, _)) = next {
f.write_str(name)?;
next = iter.next();
if next.is_some() {
f.write_str(" | ")?;
}
}
Ok(())
}
}
$crate::bitflags_array_impl_assign! { BitOrAssign bitor_assign $name |= $($lower_inner_name)* }
$crate::bitflags_array_impl_assign! { BitAndAssign bitand_assign $name &= $($lower_inner_name)* }
$crate::bitflags_array_impl_assign! { BitXorAssign bitxor_assign $name ^= $($lower_inner_name)* }
$crate::bit_array_impl! { BitOr bitor $name_bits |= }
$crate::bit_array_impl! { BitAnd bitand $name_bits &= }
$crate::bit_array_impl! { BitXor bitxor $name_bits ^= }
impl core::ops::Not for $name_bits {
type Output = Self;
#[inline]
fn not(self) -> Self {
let [$($lower_inner_name,)*] = self.0;
Self([$(!$lower_inner_name,)*])
}
}
#[cfg(feature = "serde")]
impl bitflags::parser::WriteHex for $name_bits {
fn write_hex<W: core::fmt::Write>(&self, mut writer: W) -> core::fmt::Result {
let [$($lower_inner_name,)*] = self.0;
let mut wrote = false;
let mut stager = alloc::string::String::with_capacity(size_of::<$T>() * 2);
// we don't want to write it if it's just zero as there may be multiple zeros
// resulting in something like "00" being written out. We do want to write it if
// there has already been something written though.
$(if ($lower_inner_name != 0) || wrote {
// First we write to a staging string, then we add any zeros (e.g if #1
// is f and a u8 and #2 is a then the two combined would be f0a which requires
// a 0 inserted)
$lower_inner_name.write_hex(&mut stager)?;
if (stager.len() != size_of::<$T>() * 2) && wrote {
let zeros_to_write = (size_of::<$T>() * 2) - stager.len();
for _ in 0..zeros_to_write {
writer.write_char('0')?
}
}
writer.write_str(&stager)?;
stager.clear();
wrote = true;
})*
if !wrote {
writer.write_str("0")?;
}
Ok(())
}
}
#[cfg(feature = "serde")]
impl bitflags::parser::ParseHex for $name_bits {
fn parse_hex(input: &str) -> Result<Self, bitflags::parser::ParseError> {
use bitflags::Bits;
let mut unset = Self::EMPTY;
let mut end = input.len();
if end == 0 {
return Err(bitflags::parser::ParseError::empty_flag())
}
// we iterate starting at the least significant places and going up
for (idx, _) in [$(stringify!($lower_inner_name),)*].iter().enumerate().rev() {
// A byte is two hex places - u8 (1 byte) = 0x00 (2 hex places).
let checked_start = end.checked_sub(size_of::<$T>() * 2);
let start = checked_start.unwrap_or(0);
let cur_input = &input[start..end];
unset.0[idx] = <$T>::from_str_radix(cur_input, 16)
.map_err(|_|bitflags::parser::ParseError::invalid_hex_flag(cur_input))?;
end = start;
if let None = checked_start {
break;
}
}
Ok(unset)
}
}
impl bitflags::Bits for $name_bits {
const EMPTY: Self = $name::empty().bits();
const ALL: Self = $name::all().bits();
}
impl bitflags::Flags for $name {
const FLAGS: &'static [bitflags::Flag<Self>] = $name::FLAGS;
type Bits = $name_bits;
fn bits(&self) -> $name_bits {
$name_bits([
$(self.$lower_inner_name.bits(),)*
])
}
fn from_bits_retain(bits: $name_bits) -> Self {
let [$($lower_inner_name,)*] = bits.0;
Self {
$($lower_inner_name: $inner_name::from_bits_retain($lower_inner_name),)*
}
}
fn empty() -> Self {
Self::empty()
}
fn all() -> Self {
Self::all()
}
}
impl $name {
pub(crate) const FLAGS: &'static [bitflags::Flag<Self>] = &[
$(
$(
bitflags::Flag::new(stringify!($Flag), $name::$Flag),
)*
)*
];
/// Gets the set flags as a container holding an array of bits.
pub const fn bits(&self) -> $name_bits {
$name_bits([
$(self.$lower_inner_name.bits(),)*
])
}
/// Returns self with no flags set.
pub const fn empty() -> Self {
Self {
$($lower_inner_name: $inner_name::empty(),)*
}
}
/// Returns self with all flags set.
pub const fn all() -> Self {
Self {
$($lower_inner_name: $inner_name::all(),)*
}
}
/// Whether all the bits set in `other` are all set in `self`
pub const fn contains(self, other:Self) -> bool {
// we need an annoying true to catch the last && >:(
$(self.$lower_inner_name.contains(other.$lower_inner_name) &&)* true
}
/// Returns whether any bit set in `self` matched any bit set in `other`.
pub const fn intersects(self, other:Self) -> bool {
$(self.$lower_inner_name.intersects(other.$lower_inner_name) ||)* false
}
/// Returns whether there is no flag set.
pub const fn is_empty(self) -> bool {
$(self.$lower_inner_name.is_empty() &&)* true
}
/// Returns whether the struct has all flags set.
pub const fn is_all(self) -> bool {
$(self.$lower_inner_name.is_all() &&)* true
}
$crate::bitflags_independent_two_arg! {
/// Bitwise or - `self | other`
union $($lower_inner_name)*
}
$crate::bitflags_independent_two_arg! {
/// Bitwise and - `self & other`
intersection $($lower_inner_name)*
}
$crate::bitflags_independent_two_arg! {
/// Bitwise and of the complement of other - `self & !other`
difference $($lower_inner_name)*
}
$crate::bitflags_independent_two_arg! {
/// Bitwise xor - `self ^ other`
symmetric_difference $($lower_inner_name)*
}
/// Bitwise not - `!self`
pub const fn complement(self) -> Self {
Self {
$($lower_inner_name: self.$lower_inner_name.complement(),)*
}
}
/// Calls [`Self::insert`] if `set` is true and otherwise calls [`Self::remove`].
pub fn set(&mut self, other:Self, set: bool) {
$(self.$lower_inner_name.set(other.$lower_inner_name, set);)*
}
/// Inserts specified flag(s) into self
pub fn insert(&mut self, other:Self) {
$(self.$lower_inner_name.insert(other.$lower_inner_name);)*
}
/// Removes specified flag(s) from self
pub fn remove(&mut self, other:Self) {
$(self.$lower_inner_name.remove(other.$lower_inner_name);)*
}
/// Toggles specified flag(s) in self
pub fn toggle(&mut self, other:Self) {
$(self.$lower_inner_name.toggle(other.$lower_inner_name);)*
}
#[doc = concat!("Takes in [`", stringify!($name_bits), "`] and returns")]
/// None if there are invalid bits or otherwise Self with those bits set
pub const fn from_bits(bits: $name_bits) -> Option<Self> {
let [$($lower_inner_name,)*] = bits.0;
// The ? operator does not work in a const context.
Some(Self {
$(
$lower_inner_name: match $inner_name::from_bits($lower_inner_name) {
Some(some) => some,
None => return None,
},
)*
})
}
#[doc = concat!("Takes in [`", stringify!($name_bits), "`] and returns Self with only valid bits (all other bits removed)")]
pub const fn from_bits_truncate(bits: $name_bits) -> Self {
let [$($lower_inner_name,)*] = bits.0;
Self { $($lower_inner_name: $inner_name::from_bits_truncate($lower_inner_name),)* }
}
#[doc = concat!("Takes in [`", stringify!($name_bits), "`] and returns Self with all bits that were set without removing invalid bits")]
pub const fn from_bits_retain(bits: $name_bits) -> Self {
let [$($lower_inner_name,)*] = bits.0;
Self { $($lower_inner_name: $inner_name::from_bits_retain($lower_inner_name),)* }
}
/// Takes in a bitflags flag name (in `SCREAMING_SNAKE_CASE`) and returns Self
/// if it matches or none if the name does not match the name of any of the
/// flags. Name is capitalisation dependent.
///
/// [`impl FromStr`] can be used to recognize kebab-case names, like are used in
/// the WebGPU spec.
pub fn from_name(name: &str) -> Option<Self> {
match name {
$(
$(
stringify!($Flag) => Some(Self::$Flag),
)*
)*
_ => None,
}
}
/// Combines the flags from the internal flags into the entire flags struct
pub fn from_internal_flags($($lower_inner_name: $inner_name,)*) -> Self {
Self {
$($lower_inner_name,)*
}
}
/// Returns an iterator over the set flags.
pub const fn iter(&self) -> bitflags::iter::Iter<$name> {
bitflags::iter::Iter::__private_const_new($name::FLAGS, *self, *self)
}
/// Returns an iterator over the set flags and their names.
///
/// These are bitflags names in `SCREAMING_SNAKE_CASE`.
pub const fn iter_names(&self) -> bitflags::iter::IterNames<$name> {
bitflags::iter::IterNames::__private_const_new($name::FLAGS, *self, *self)
}
$(
$(
$(#[doc $($args)*])*
pub const $Flag: Self = Self {
$lower_inner_name: $inner_name::from_bits_truncate($value),
..Self::empty()
};
)*
)*
}
$(
impl From<$inner_name> for $name {
fn from($lower_inner_name: $inner_name) -> Self {
Self {
$lower_inner_name,
..Self::empty()
}
}
}
)*
};
}
pub(crate) use bitflags_array;