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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/// Defines a flag set from a compact declaration.
///
/// The macro generates a [`BitFlagsSpec`] implementation for a tag type,
/// along with named constants for individual bits and combined masks.
///
/// This is a convenience wrapper around the manual [`BitFlagsSpec`]
/// implementation.
/// The expansion is small and inspectable.
/// Running `cargo expand` shows code that matches what you would write by hand.
///
/// The macro does not generate a type alias for the flag set. Type aliases
/// are trivial to write in ordinary Rust, and generating one would create a
/// naming asymmetry: the flag set would have a name but the individual flag
/// would not. Write the aliases you need next to the macro invocation:
///
/// ```
/// use bitflags_lite::{BitFlag, BitFlags};
/// bitflags_lite::bitflags! {
/// #![storage(u32)]
/// pub struct PermissionsTag;
/// // ...
/// }
/// pub type Permissions = BitFlags<u32, PermissionsTag>;
/// pub type Permission = BitFlag<u32, PermissionsTag>;
/// ```
///
/// # Syntax
///
/// ```text
/// bitflags! {
/// // Storage type. Required.
/// #![storage( STORAGE )]
///
/// // Optional default name matcher.
/// $( #![name_matcher( EXPR )] )?
///
/// // Tag declaration. The `VIS struct` prefix is optional.
/// $( $( #[$ATTR] )* VIS struct )? TAG;
///
/// // Bit declarations. The name and the name slice are optional.
/// $( #[$ATTR] )* VIS bit NAME = INDEX, &[NAMES];
/// $( #[$ATTR] )* VIS bit NAME = INDEX;
/// $( #[$ATTR] )* VIS bit = INDEX;
///
/// // Mask declarations. The display name is optional.
/// $( #[$ATTR] )* VIS mask NAME = [ A, B, C ], "NAME";
/// $( #[$ATTR] )* VIS mask NAME = EXPR, "NAME";
/// }
/// ```
///
/// where:
///
/// - `STORAGE` is a primitive integer type such as `u32`. It is required.
/// - `VIS` is an optional visibility modifier such as `pub` or `pub(crate)`.
/// - `TAG` is the tag type name. Writing `VIS struct TAG;` generates the tag
/// with standard derives. Writing just `TAG;` reuses an existing one.
/// - `INDEX` is the zero-based bit index.
/// - `&[NAMES]` is a slice of one or more names for the bit. The first is used
/// by [`Display`](core::fmt::Display). All are accepted when parsing.
/// - `bit NAME = INDEX;` declares a bit that has a named constant but no
/// display name.
/// - `bit = INDEX;` declares a valid bit with no constant and no name. It
/// contributes to [`VALID_BITS_MASK`] but not to [`FLAG_NAMES`].
/// - `[ A, B, C ]` computes a union of named bit constants into a multi-bit
/// value. The special syntax is required because the macro runs in const
/// context, where `BitFlag` does not implement `BitOr` in a way that can be
/// folded into a raw integer.
/// - `EXPR` is a bit-mask expression, for example `0` or
/// `FIRST.mask_or_default() | SECOND.mask_or_default()`.
/// - `#![name_matcher(...)]` sets [`BitFlagsSpec::DEFAULT_NAME_MATCHER`] and
/// defaults to [`NameMatcher::Exact`].
/// - `ATTR` is any outer attribute, including doc comments. Attributes are
/// copied to the generated item.
///
/// Indices that are not declared are invalid. They are excluded from
/// [`VALID_BITS_MASK`] and cleared by truncating operations.
///
/// # Tag declaration
///
/// The tag line may be written in two ways:
///
/// ```rust,ignore
/// // Reuse an existing tag type.
/// bitflags! {
/// #![storage(u32)]
/// SomeTag;
/// // ...
/// }
///
/// // Generate the tag type.
/// bitflags! {
/// #![storage(u32)]
/// pub struct SomeTag;
/// // ...
/// }
/// ```
///
/// Attributes on the tag declaration are copied to the generated struct.
///
/// # Generated items
///
/// The macro emits `impl BitFlagsSpec<STORAGE> for TAG`, plus one `const` per
/// named `bit` or `mask`.
/// The tag struct is emitted only when requested.
/// Attributes written on a declaration are copied to the corresponding
/// generated item.
///
/// The generated code is approximately equivalent to the following pattern:
///
/// ```rust,ignore
/// // Tag struct, if requested.
/// #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default, Debug)]
/// pub struct TAG;
///
/// // Spec implementation for the tag.
/// impl BitFlagsSpec<STORAGE> for TAG {
/// const VALID_BITS_MASK: STORAGE = ...;
/// const FLAG_NAMES: &[&[&str]] = ...;
/// const COMBINED_NAMES: &[(STORAGE, &str)] = ...;
/// const DEFAULT_NAME_MATCHER: NameMatcher = ...;
/// }
///
/// // Bit constant, one per named `bit`.
/// pub const NAME: BitFlag<STORAGE, TAG> =
/// BitFlag::<STORAGE, TAG>::from_bit_retain(INDEX);
///
/// // Mask constant, one per named `mask`.
/// pub const NAME: BitFlags<STORAGE, TAG> =
/// BitFlags::<STORAGE, TAG>::from_bits_retain(VALUE);
/// ```
///
/// `FLAG_NAMES` is sized to the highest declared bit index.
/// Gaps are filled with empty slices, so unnamed and undeclared indices
/// coexist.
///
/// # Examples
///
/// Simple example with three consequent bits in bit flags.
///
/// ```rust
/// use bitflags_lite::{BitFlagsSpec, NameMatcher};
///
/// bitflags_lite::bitflags! {
/// #![storage(u32)]
///
/// pub struct PermissionsTag;
///
/// pub bit READ = 0, &["read"];
/// pub bit WRITE = 1, &["write"];
/// pub bit EXEC = 2, &["exec", "execute"];
///
/// pub mask RWX = [ READ, WRITE, EXEC ], "all";
/// pub mask NONE = 0, "none";
/// }
///
/// assert_eq!(PermissionsTag::VALID_BITS_MASK, 0b111);
/// assert_eq!(
/// PermissionsTag::FLAG_NAMES,
/// &[&["read"][..], &["write"], &["exec", "execute"]]
/// );
/// assert_eq!(
/// PermissionsTag::COMBINED_NAMES,
/// &[(0b111, "all"), (0, "none")]
/// );
/// assert_eq!(PermissionsTag::DEFAULT_NAME_MATCHER, NameMatcher::Exact);
///
/// assert_eq!(READ.bit(), 0);
/// assert_eq!(WRITE.bit(), 1);
/// assert_eq!(EXEC.bit(), 2);
/// assert_eq!(RWX.bits(), 0b111);
/// assert_eq!(NONE.bits(), 0);
/// ```
///
/// <details>
/// <summary>Macro expansion (click to show)</summary>
///
/// ```rust
/// use bitflags_lite::{BitFlagsSpec, NameMatcher};
///
/// #[derive(
/// ::core::marker::Copy,
/// ::core::clone::Clone,
/// ::core::cmp::Eq,
/// ::core::cmp::PartialEq,
/// ::core::cmp::Ord,
/// ::core::cmp::PartialOrd,
/// ::core::hash::Hash,
/// ::core::default::Default,
/// ::core::fmt::Debug,
/// )]
/// pub struct PermissionsTag;
///
/// impl bitflags_lite::BitFlagsSpec<u32> for PermissionsTag {
/// const VALID_BITS_MASK: u32 =
/// const { (0 as u32) | (1 as u32) << 0 | (1 as u32) << 1 | (1 as u32) << 2 };
/// const FLAG_NAMES: &[&[&str]] = const {
/// const MAX: usize = bitflags_lite::util::__const_max(&[0, 1, 2]);
/// const FLAG_NAMES: [&'static [&'static str]; MAX + 1] = const {
/// let mut flag_names: [&'static [&'static str]; MAX + 1] = [&[]; MAX + 1];
/// flag_names[0] = (&["read"]);
/// flag_names[1] = (&["write"]);
/// flag_names[2] = (&["exec", "execute"]);
/// flag_names
/// };
/// &FLAG_NAMES
/// };
/// const COMBINED_NAMES: &[(u32, &str)] = &[
/// (
/// (0 | bitflags_lite::BitFlag::<u32, PermissionsTag>::mask_or_default(&READ)
/// | bitflags_lite::BitFlag::<u32, PermissionsTag>::mask_or_default(&WRITE)
/// | bitflags_lite::BitFlag::<u32, PermissionsTag>::mask_or_default(&EXEC)),
/// "all",
/// ),
/// (0, "none"),
/// ];
/// const DEFAULT_NAME_MATCHER: bitflags_lite::NameMatcher =
/// (bitflags_lite::NameMatcher::Exact);
/// }
///
/// pub const READ: bitflags_lite::BitFlag<u32, PermissionsTag> =
/// bitflags_lite::BitFlag::<u32, PermissionsTag>::from_bit_retain(0);
///
/// pub const WRITE: bitflags_lite::BitFlag<u32, PermissionsTag> =
/// bitflags_lite::BitFlag::<u32, PermissionsTag>::from_bit_retain(1);
///
/// pub const EXEC: bitflags_lite::BitFlag<u32, PermissionsTag> =
/// bitflags_lite::BitFlag::<u32, PermissionsTag>::from_bit_retain(2);
///
/// pub const RWX: bitflags_lite::BitFlags<u32, PermissionsTag> =
/// bitflags_lite::BitFlags::<u32, PermissionsTag>::from_bits_retain(
/// (0 | bitflags_lite::BitFlag::<u32, PermissionsTag>::mask_or_default(&READ)
/// | bitflags_lite::BitFlag::<u32, PermissionsTag>::mask_or_default(&WRITE)
/// | bitflags_lite::BitFlag::<u32, PermissionsTag>::mask_or_default(&EXEC)),
/// );
///
/// pub const NONE: bitflags_lite::BitFlags<u32, PermissionsTag> =
/// bitflags_lite::BitFlags::<u32, PermissionsTag>::from_bits_retain(0);
/// ```
///
/// </details>
///
/// Example with invalid and unnamed but valid bits.
///
/// ```rust
/// use bitflags_lite::{BitFlagsSpec, NameMatcher};
///
/// bitflags_lite::bitflags! {
/// #![storage(u32)]
/// #![name_matcher(NameMatcher::AsciiCaseInsensitive)]
///
/// /// Flags tag doc-comment.
/// pub struct FlagsTag;
///
/// /// `FIRST` `BitFlag` doc-comment.
/// pub bit FIRST = 0, &["first", "1st"];
///
/// /// `THIRD` `BitFlag` doc-comment.
/// pub bit THIRD = 2, &["third", "3rd"];
///
/// pub bit = 3;
///
/// /// `TENTH` `BitFlag` doc-comment.
/// pub bit TENTH = 9, &["tenth", "10th"];
///
/// /// `ALL` `BitFlags` doc-comment.
/// pub mask ALL = [ FIRST, THIRD, TENTH ], "all";
///
/// /// `ALL_ALT` `BitFlags` doc-comment.
/// pub mask ALL_ALT = FIRST.mask_or_default()
/// | THIRD.mask_or_default()
/// | TENTH.mask_or_default();
///
/// /// `NONE` `BitFlags` doc-comment.
/// pub mask NONE = 0, "none";
/// }
///
/// assert_eq!(FlagsTag::VALID_BITS_MASK, 0b10_0000_1101);
/// assert_eq!(
/// FlagsTag::FLAG_NAMES,
/// &[
/// &["first", "1st"][..],
/// &[],
/// &["third", "3rd"],
/// &[],
/// &[],
/// &[],
/// &[],
/// &[],
/// &[],
/// &["tenth", "10th"]
/// ]
/// );
/// assert_eq!(
/// FlagsTag::COMBINED_NAMES,
/// &[(0b10_0000_0101, "all"), (0, "none")]
/// );
/// assert_eq!(
/// FlagsTag::DEFAULT_NAME_MATCHER,
/// NameMatcher::AsciiCaseInsensitive
/// );
///
/// assert_eq!(FIRST.bit(), 0);
/// assert_eq!(THIRD.bit(), 2);
/// assert_eq!(TENTH.bit(), 9);
/// assert_eq!(ALL.bits(), 0b10_0000_0101);
/// assert_eq!(NONE.bits(), 0);
/// ```
///
/// <details>
/// <summary>Macro expansion (click to show)</summary>
///
/// ```rust
/// use bitflags_lite::{BitFlagsSpec, NameMatcher};
///
/// #[doc = r" Flags tag doc-comment."]
/// #[derive(
/// ::core::marker::Copy,
/// ::core::clone::Clone,
/// ::core::cmp::Eq,
/// ::core::cmp::PartialEq,
/// ::core::cmp::Ord,
/// ::core::cmp::PartialOrd,
/// ::core::hash::Hash,
/// ::core::default::Default,
/// ::core::fmt::Debug,
/// )]
/// pub struct FlagsTag;
///
/// impl bitflags_lite::BitFlagsSpec<u32> for FlagsTag {
/// const VALID_BITS_MASK: u32 = const {
/// (0 as u32) | (1 as u32) << 0 | (1 as u32) << 2 | (1 as u32) << 3 | (1 as u32) << 9
/// };
/// const FLAG_NAMES: &[&[&str]] = const {
/// const MAX: usize = bitflags_lite::util::__const_max(&[0, 2, 3, 9]);
/// const FLAG_NAMES: [&'static [&'static str]; MAX + 1] = const {
/// let mut flag_names: [&'static [&'static str]; MAX + 1] = [&[]; MAX + 1];
/// flag_names[0] = (&["first", "1st"]);
/// flag_names[2] = (&["third", "3rd"]);
/// flag_names[9] = (&["tenth", "10th"]);
/// flag_names
/// };
/// &FLAG_NAMES
/// };
/// const COMBINED_NAMES: &[(u32, &str)] = &[
/// (
/// (0 | bitflags_lite::BitFlag::<u32, FlagsTag>::mask_or_default(&FIRST)
/// | bitflags_lite::BitFlag::<u32, FlagsTag>::mask_or_default(&THIRD)
/// | bitflags_lite::BitFlag::<u32, FlagsTag>::mask_or_default(&TENTH)),
/// "all",
/// ),
/// (0, "none"),
/// ];
/// const DEFAULT_NAME_MATCHER: bitflags_lite::NameMatcher =
/// (NameMatcher::AsciiCaseInsensitive);
/// }
///
/// #[doc = r" `FIRST` `BitFlag` doc-comment."]
/// pub const FIRST: bitflags_lite::BitFlag<u32, FlagsTag> =
/// bitflags_lite::BitFlag::<u32, FlagsTag>::from_bit_retain(0);
///
/// #[doc = r" `THIRD` `BitFlag` doc-comment."]
/// pub const THIRD: bitflags_lite::BitFlag<u32, FlagsTag> =
/// bitflags_lite::BitFlag::<u32, FlagsTag>::from_bit_retain(2);
///
/// #[doc = r" `TENTH` `BitFlag` doc-comment."]
/// pub const TENTH: bitflags_lite::BitFlag<u32, FlagsTag> =
/// bitflags_lite::BitFlag::<u32, FlagsTag>::from_bit_retain(9);
///
/// #[doc = r" `ALL` `BitFlags` doc-comment."]
/// pub const ALL: bitflags_lite::BitFlags<u32, FlagsTag> =
/// bitflags_lite::BitFlags::<u32, FlagsTag>::from_bits_retain(
/// (0 | bitflags_lite::BitFlag::<u32, FlagsTag>::mask_or_default(&FIRST)
/// | bitflags_lite::BitFlag::<u32, FlagsTag>::mask_or_default(&THIRD)
/// | bitflags_lite::BitFlag::<u32, FlagsTag>::mask_or_default(&TENTH)),
/// );
///
/// #[doc = r" `ALL_ALT` `BitFlags` doc-comment."]
/// pub const ALL_ALT: bitflags_lite::BitFlags<u32, FlagsTag> =
/// bitflags_lite::BitFlags::<u32, FlagsTag>::from_bits_retain(
/// (FIRST.mask_or_default() | THIRD.mask_or_default() | TENTH.mask_or_default()),
/// );
///
/// #[doc = r" `NONE` `BitFlags` doc-comment."]
/// pub const NONE: bitflags_lite::BitFlags<u32, FlagsTag> =
/// bitflags_lite::BitFlags::<u32, FlagsTag>::from_bits_retain(0);
/// ```
///
/// </details>
///
/// [`BitFlagsSpec`]: trait.BitFlagsSpec.html
/// [`BitFlagsSpec::VALID_BITS_MASK`]: trait.BitFlagsSpec.html#associatedconstant.VALID_BITS_MASK
/// [`BitFlagsSpec::FLAG_NAMES`]: trait.BitFlagsSpec.html#associatedconstant.FLAG_NAMES
/// [`BitFlagsSpec::DEFAULT_NAME_MATCHER`]: trait.BitFlagsSpec.html#associatedconstant.DEFAULT_NAME_MATCHER
/// [`VALID_BITS_MASK`]: trait.BitFlagsSpec.html#associatedconstant.VALID_BITS_MASK
/// [`FLAG_NAMES`]: trait.BitFlagsSpec.html#associatedconstant.FLAG_NAMES
/// [`NameMatcher::Exact`]: enum.NameMatcher.html#variant.Exact
};
=> ;
}
/// # Syntax
///
/// ```rust,ignore
/// __bitflags_impl {
/// // Unparsed meta.
/// [ $( #![name_matcher( $name_matcher:expr )] )? ]
///
/// // Unparsed bits and masks.
/// [ $( $vis:vis $name:ident: $kind:tt = $value:expr $(, $display:expr )?; )* ]
///
/// // Parsed bits.
/// [ $( $value:expr $(, $display:expr )?; )* ]
///
/// // Parsed named bits.
/// [ $( $vis:vis $name:ident = $value:expr $(, $display:expr )?; )* ]
///
/// // Parsed masks.
/// [ $( $value:expr $(, $display:expr )?; )* ]
///
/// // Parsed named masks.
/// [ $( $vis:vis $name:ident = $value:expr $(, $display:expr )?; )* ]
///
/// // Storage inner typename.
/// [ $storage:ty ]
///
/// // Tag typename.
/// [ $tag:ty ]
///
/// // Parsed name matcher.
/// [ $( $name_matcher:expr )? ]
/// }
/// ```
};
// Choose name matcher.
=> ;
// Parse unnamed bit item.
=> ;
// Parse named bit item.
=> ;
// Parse mask item with union array syntax.
=> ;
// Parse unnamed mask item.
=> ;
// Parse named mask item.
=> ;
=> ;
}