bitflags/lib.rs
1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11/*!
12Generate types for C-style flags with ergonomic APIs.
13
14# Getting started
15
16Add `bitflags` to your `Cargo.toml`:
17
18```toml
19[dependencies.bitflags]
20version = "2.12.1"
21```
22
23## Crate features
24
25The `bitflags` library defines a few Cargo features that you can opt-in to:
26
27- `std`: Implement the `Error` trait on error types used by `bitflags`.
28- `serde`: Support deriving `serde` traits on generated flags types.
29- `arbitrary`: Support deriving `arbitrary` traits on generated flags types.
30- `bytemuck`: Support deriving `bytemuck` traits on generated flags types.
31
32## Generating flags types
33
34Use the [`bitflags`] macro to generate flags types:
35
36```rust
37use bitflags::bitflags;
38
39bitflags! {
40 pub struct Flags: u32 {
41 const A = 0b00000001;
42 const B = 0b00000010;
43 const C = 0b00000100;
44 }
45}
46```
47
48See the docs for the `bitflags` macro for the full syntax.
49
50Also see the [`example_generated`](./example_generated/index.html) module for an example of what the `bitflags` macro generates for a flags type.
51
52### Externally defined flags
53
54If you're generating flags types for an external source, such as a C API, you can define
55an extra unnamed flag as a mask of all bits the external source may ever set. Usually this would be all bits (`!0`):
56
57```rust
58# use bitflags::bitflags;
59bitflags! {
60 pub struct Flags: u32 {
61 const A = 0b00000001;
62 const B = 0b00000010;
63 const C = 0b00000100;
64
65 // The source may set any bits
66 const _ = !0;
67 }
68}
69```
70
71Why should you do this? Generated methods like `all` and truncating operators like `!` only consider
72bits in defined flags. Adding an unnamed flag makes those methods consider additional bits,
73without generating additional constants for them. It helps compatibility when the external source
74may start setting additional bits at any time. The [known and unknown bits](#known-and-unknown-bits)
75section has more details on this behavior.
76
77### Custom derives
78
79You can derive some traits on generated flags types if you enable Cargo features. The following
80libraries are currently supported:
81
82- `serde`: Support `#[derive(Serialize, Deserialize)]`, using text for human-readable formats,
83 and a raw number for binary formats.
84- `arbitrary`: Support `#[derive(Arbitrary)]`, only generating flags values with known bits.
85- `bytemuck`: Support `#[derive(Pod, Zeroable)]`, for casting between flags values and their
86 underlying bits values.
87
88You can also define your own flags type outside of the [`bitflags`] macro and then use it to generate methods.
89This can be useful if you need a custom `#[derive]` attribute for a library that `bitflags` doesn't
90natively support:
91
92```rust
93# use std::fmt::Debug as SomeTrait;
94# use bitflags::bitflags;
95#[derive(SomeTrait)]
96pub struct Flags(u32);
97
98bitflags! {
99 impl Flags: u32 {
100 const A = 0b00000001;
101 const B = 0b00000010;
102 const C = 0b00000100;
103 }
104}
105```
106
107### Adding custom methods
108
109The [`bitflags`] macro supports attributes on generated flags types within the macro itself, while
110`impl` blocks can be added outside of it:
111
112```rust
113# use bitflags::bitflags;
114bitflags! {
115 // Attributes can be applied to flags types
116 #[repr(transparent)]
117 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
118 pub struct Flags: u32 {
119 const A = 0b00000001;
120 const B = 0b00000010;
121 const C = 0b00000100;
122 }
123}
124
125// Impl blocks can be added to flags types
126impl Flags {
127 pub fn as_u64(&self) -> u64 {
128 self.bits() as u64
129 }
130}
131```
132
133### Renaming flags
134
135The [`bitflags`] macro recognizes a special `#[bitflags(flag_name = "<value>")]` attribute on flags values to rename them:
136
137```rust
138# use bitflags::bitflags;
139bitflags! {
140 pub struct Flags: u32 {
141 // Add the attribute to a flag to change its name
142 #[bitflags(flag_name = "a")]
143 const A = 0b00000001;
144 #[bitflags(flag_name = "b")]
145 const B = 0b00000010;
146 #[bitflags(flag_name = "c")]
147 const C = 0b00000100;
148 }
149}
150```
151
152When applied to a flag value, instead of using its identifier, like `A` as the name, it'll use the given string. This
153doesn't affect the identifier of the constant itself, just the name recognized when parsing and formatting.
154
155## Working with flags values
156
157Use generated constants and standard bitwise operators to interact with flags values:
158
159```rust
160# use bitflags::bitflags;
161# bitflags! {
162# #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
163# pub struct Flags: u32 {
164# const A = 0b00000001;
165# const B = 0b00000010;
166# const C = 0b00000100;
167# }
168# }
169// union
170let ab = Flags::A | Flags::B;
171
172// intersection
173let a = ab & Flags::A;
174
175// difference
176let b = ab - Flags::A;
177
178// complement
179let c = !ab;
180```
181
182See the docs for the [`Flags`] trait for more details on operators and how they behave.
183
184# Formatting and parsing
185
186`bitflags` defines a text format that can be used to convert any flags value to and from strings.
187
188See the [`parser`] module for more details.
189
190# Specification
191
192The terminology and behavior of generated flags types is
193[specified in the source repository](https://github.com/bitflags/bitflags/blob/main/spec.md).
194Details are repeated in these docs where appropriate, but is exhaustively listed in the spec. Some
195things are worth calling out explicitly here.
196
197## Flags types, flags values, flags
198
199The spec and these docs use consistent terminology to refer to things in the bitflags domain:
200
201- **Bits type**: A type that defines a fixed number of bits at specific locations.
202- **Flag**: A set of bits in a bits type that may have a unique name.
203- **Flags type**: A set of defined flags over a specific bits type.
204- **Flags value**: An instance of a flags type using its specific bits value for storage.
205
206```
207# use bitflags::bitflags;
208bitflags! {
209 struct FlagsType: u8 {
210// -- Bits type
211// --------- Flags type
212 const A = 1;
213// ----- Flag
214 }
215}
216
217let flag = FlagsType::A;
218// ---- Flags value
219```
220
221## Known and unknown bits
222
223Any bits in a flag you define are called _known bits_. Any other bits are _unknown bits_.
224In the following flags type:
225
226```
227# use bitflags::bitflags;
228bitflags! {
229 struct Flags: u8 {
230 const A = 1;
231 const B = 1 << 1;
232 const C = 1 << 2;
233 }
234}
235```
236
237The known bits are `0b0000_0111` and the unknown bits are `0b1111_1000`.
238
239`bitflags` doesn't guarantee that a flags value will only ever have known bits set, but some operators
240will unset any unknown bits they encounter. In a future version of `bitflags`, all operators will
241unset unknown bits.
242
243If you're using `bitflags` for flags types defined externally, such as from C, you probably want all
244bits to be considered known, in case that external source changes. You can do this using an unnamed
245flag, as described in [externally defined flags](#externally-defined-flags).
246
247## Zero-bit flags
248
249Flags with no bits set should be avoided because they interact strangely with [`Flags::contains`]
250and [`Flags::intersects`]. A zero-bit flag is always contained, but is never intersected. The
251names of zero-bit flags can be parsed, but are never formatted.
252
253## Multi-bit flags
254
255Flags that set multiple bits should be avoided unless each bit is also in a single-bit flag.
256Take the following flags type as an example:
257
258```
259# use bitflags::bitflags;
260bitflags! {
261 struct Flags: u8 {
262 const A = 1;
263 const B = 1 | 1 << 1;
264 }
265}
266```
267
268The result of `Flags::A ^ Flags::B` is `0b0000_0010`, which doesn't correspond to either
269`Flags::A` or `Flags::B` even though it's still a known bit.
270*/
271
272#![cfg_attr(not(any(feature = "std", test)), no_std)]
273#![cfg_attr(not(test), forbid(unsafe_code))]
274#![cfg_attr(test, allow(mixed_script_confusables))]
275
276#[doc(inline)]
277pub use traits::{Bits, Flag, Flags};
278
279pub mod iter;
280pub mod parser;
281
282mod traits;
283
284#[doc(hidden)]
285pub mod __private {
286 #[allow(unused_imports)]
287 // Easier than conditionally checking any optional external dependencies
288 pub use crate::{external::__private::*, traits::__private::*};
289
290 pub use core;
291}
292
293#[allow(unused_imports)]
294pub use external::*;
295
296#[allow(deprecated)]
297pub use traits::BitFlags;
298
299/*
300How does the bitflags crate work?
301
302This library generates a `struct` in the end-user's crate with a bunch of constants on it that represent flags.
303The difference between `bitflags` and a lot of other libraries is that we don't actually control the generated `struct` in the end.
304It's part of the end-user's crate, so it belongs to them. That makes it difficult to extend `bitflags` with new functionality
305because we could end up breaking valid code that was already written.
306
307Our solution is to split the type we generate into two: the public struct owned by the end-user, and an internal struct owned by `bitflags` (us).
308To give you an example, let's say we had a crate that called `bitflags!`:
309
310```rust
311bitflags! {
312 pub struct MyFlags: u32 {
313 const A = 1;
314 const B = 2;
315 }
316}
317```
318
319What they'd end up with looks something like this:
320
321```rust
322pub struct MyFlags(<MyFlags as PublicFlags>::InternalBitFlags);
323
324const _: () = {
325 #[repr(transparent)]
326 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
327 pub struct MyInternalBitFlags {
328 bits: u32,
329 }
330
331 impl PublicFlags for MyFlags {
332 type Internal = InternalBitFlags;
333 }
334};
335```
336
337If we want to expose something like a new trait impl for generated flags types, we add it to our generated `MyInternalBitFlags`,
338and let `#[derive]` on `MyFlags` pick up that implementation, if an end-user chooses to add one.
339
340The public API is generated in the `__impl_public_flags!` macro, and the internal API is generated in
341the `__impl_internal_flags!` macro.
342
343The macros are split into 3 modules:
344
345- `public`: where the user-facing flags types are generated.
346- `internal`: where the `bitflags`-facing flags types are generated.
347- `external`: where external library traits are implemented conditionally.
348*/
349
350/**
351Generate a flags type.
352
353# `struct` mode
354
355A declaration that begins with `$vis struct` will generate a `struct` for a flags type, along with
356methods and trait implementations for it. The body of the declaration defines flags as constants,
357where each constant is a flags value of the generated flags type.
358
359## Examples
360
361Generate a flags type using `u8` as the bits type:
362
363```
364# use bitflags::bitflags;
365bitflags! {
366 struct Flags: u8 {
367 const A = 1;
368 const B = 1 << 1;
369 const C = 0b0000_0100;
370 }
371}
372```
373
374Flags types are private by default and accept standard visibility modifiers. Flags themselves
375are always public:
376
377```
378# use bitflags::bitflags;
379bitflags! {
380 pub struct Flags: u8 {
381 // Constants are always `pub`
382 const A = 1;
383 }
384}
385```
386
387Flags may refer to other flags using their [`Flags::bits`] value:
388
389```
390# use bitflags::bitflags;
391bitflags! {
392 struct Flags: u8 {
393 const A = 1;
394 const B = 1 << 1;
395 const AB = Flags::A.bits() | Flags::B.bits();
396 }
397}
398```
399
400A single `bitflags` invocation may include zero or more flags type declarations:
401
402```
403# use bitflags::bitflags;
404bitflags! {}
405
406bitflags! {
407 struct Flags1: u8 {
408 const A = 1;
409 }
410
411 struct Flags2: u8 {
412 const A = 1;
413 }
414}
415```
416
417# `impl` mode
418
419A declaration that begins with `impl` will only generate methods and trait implementations for the
420`struct` defined outside of the `bitflags` macro.
421
422The struct itself must be a newtype using the bits type as its field.
423
424The syntax for `impl` mode is identical to `struct` mode besides the starting token.
425
426## Examples
427
428Implement flags methods and traits for a custom flags type using `u8` as its underlying bits type:
429
430```
431# use bitflags::bitflags;
432struct Flags(u8);
433
434bitflags! {
435 impl Flags: u8 {
436 const A = 1;
437 const B = 1 << 1;
438 const C = 0b0000_0100;
439 }
440}
441```
442
443# Named and unnamed flags
444
445Constants in the body of a declaration are flags. The identifier of the constant is the name of
446the flag. If the identifier is `_`, then the flag is unnamed. Unnamed flags don't appear in the
447generated API, but affect how bits are truncated.
448
449## Examples
450
451Adding an unnamed flag that makes all bits known:
452
453```
454# use bitflags::bitflags;
455bitflags! {
456 struct Flags: u8 {
457 const A = 1;
458 const B = 1 << 1;
459
460 const _ = !0;
461 }
462}
463```
464
465Flags types may define multiple unnamed flags:
466
467```
468# use bitflags::bitflags;
469bitflags! {
470 struct Flags: u8 {
471 const _ = 1;
472 const _ = 1 << 1;
473 }
474}
475```
476*/
477#[macro_export]
478macro_rules! bitflags {
479 (
480 $(#[$outer:meta])*
481 $vis:vis struct $BitFlags:ident: $T:ty {
482 $(
483 $(#[$inner:ident $($args:tt)*])*
484 const $Flag:tt = $value:expr;
485 )*
486 }
487
488 $($t:tt)*
489 ) => {
490 // Declared in the scope of the `bitflags!` call
491 // This type appears in the end-user's API
492 $crate::__declare_public_bitflags! {
493 $(#[$outer])*
494 $vis struct $BitFlags
495 }
496
497 #[allow(
498 dead_code,
499 deprecated,
500 unused_doc_comments,
501 unused_attributes,
502 unused_mut,
503 unused_imports,
504 non_upper_case_globals,
505 clippy::min_ident_chars,
506 clippy::assign_op_pattern,
507 clippy::indexing_slicing,
508 clippy::same_name_method,
509 clippy::iter_without_into_iter,
510 )]
511 const _: () = {
512 // Declared in a "hidden" scope that can't be reached directly
513 // These types don't appear in the end-user's API
514 $crate::__declare_internal_bitflags! {
515 $vis struct InternalBitFlags: $T
516 }
517
518 $crate::__impl_public_bitflags_consts! {
519 $BitFlags: $T {
520 $(
521 $(#[$inner $($args)*])*
522 const $Flag = $value;
523 )*
524 }
525 }
526
527 $crate::__impl_internal_bitflags! {
528 InternalBitFlags: $T, $BitFlags {
529 $(
530 $(#[$inner $($args)*])*
531 const $Flag = $value;
532 )*
533 }
534 }
535
536 // This is where new library trait implementations can be added
537 $crate::__impl_external_bitflags! {
538 InternalBitFlags: $T, $BitFlags {
539 $(
540 $(#[$inner $($args)*])*
541 const $Flag;
542 )*
543 }
544 }
545
546 $crate::__impl_public_bitflags_forward! {
547 $BitFlags: $T, InternalBitFlags
548 }
549
550 $crate::__impl_public_bitflags_ops! {
551 $BitFlags
552 }
553
554 $crate::__impl_public_bitflags_iter! {
555 $BitFlags: $T, $BitFlags
556 }
557 };
558
559 $crate::bitflags! {
560 $($t)*
561 }
562 };
563 (
564 $(#[$outer:meta])*
565 impl $BitFlags:ident: $T:ty {
566 $(
567 $(#[$inner:ident $($args:tt)*])*
568 const $Flag:tt = $value:expr;
569 )*
570 }
571
572 $($t:tt)*
573 ) => {
574 #[allow(
575 dead_code,
576 deprecated,
577 unused_doc_comments,
578 unused_attributes,
579 unused_mut,
580 unused_imports,
581 non_upper_case_globals,
582 clippy::min_ident_chars,
583 clippy::assign_op_pattern,
584 clippy::iter_without_into_iter,
585 )]
586 const _: () = {
587 $crate::__impl_public_bitflags_consts! {
588 $BitFlags: $T {
589 $(
590 $(#[$inner $($args)*])*
591 const $Flag = $value;
592 )*
593 }
594 }
595
596 $crate::__impl_public_bitflags! {
597 $(#[$outer])*
598 $BitFlags: $T, $BitFlags {
599 $(
600 $(#[$inner $($args)*])*
601 const $Flag = $value;
602 )*
603 }
604 }
605
606 $crate::__impl_public_bitflags_ops! {
607 $BitFlags
608 }
609
610 $crate::__impl_public_bitflags_iter! {
611 $BitFlags: $T, $BitFlags
612 }
613 };
614
615 $crate::bitflags! {
616 $($t)*
617 }
618 };
619 () => {};
620}
621
622/// Implement functions on bitflags types.
623///
624/// We need to be careful about adding new methods and trait implementations here because they
625/// could conflict with items added by the end-user.
626#[macro_export]
627#[doc(hidden)]
628macro_rules! __impl_bitflags {
629 (
630 // These param names must be passed in to make the macro work.
631 // Just use `params: self, bits, name, other, value;`.
632 params: $self:ident, $bits:ident, $name:ident, $other:ident, $value:ident;
633 $(#[$outer:meta])*
634 $PublicBitFlags:ident: $T:ty {
635 fn empty() $empty_body:block
636 fn all() $all_body:block
637 fn bits(&self) $bits_body:block
638 fn from_bits(bits) $from_bits_body:block
639 fn from_bits_truncate(bits) $from_bits_truncate_body:block
640 fn from_bits_retain(bits) $from_bits_retain_body:block
641 fn from_name(name) $from_name_body:block
642 fn is_empty(&self) $is_empty_body:block
643 fn is_all(&self) $is_all_body:block
644 fn intersects(&self, other) $intersects_body:block
645 fn contains(&self, other) $contains_body:block
646 fn insert(&mut self, other) $insert_body:block
647 fn remove(&mut self, other) $remove_body:block
648 fn toggle(&mut self, other) $toggle_body:block
649 fn set(&mut self, other, value) $set_body:block
650 fn intersection(self, other) $intersection_body:block
651 fn union(self, other) $union_body:block
652 fn difference(self, other) $difference_body:block
653 fn symmetric_difference(self, other) $symmetric_difference_body:block
654 fn complement(self) $complement_body:block
655 }
656 ) => {
657 $(#[$outer])*
658 impl $PublicBitFlags {
659 /// Get a flags value with all bits unset.
660 #[inline]
661 pub const fn empty() -> Self
662 $empty_body
663
664 /// Get a flags value with all known bits set.
665 #[inline]
666 pub const fn all() -> Self
667 $all_body
668
669 /// Get the underlying bits value.
670 ///
671 /// The returned value is exactly the bits set in this flags value.
672 #[inline]
673 pub const fn bits(&$self) -> $T
674 $bits_body
675
676 /// Convert from a bits value.
677 ///
678 /// This method will return `None` if any unknown bits are set.
679 #[inline]
680 pub const fn from_bits($bits: $T) -> $crate::__private::core::option::Option<Self>
681 $from_bits_body
682
683 /// Convert from a bits value, unsetting any unknown bits.
684 #[inline]
685 pub const fn from_bits_truncate($bits: $T) -> Self
686 $from_bits_truncate_body
687
688 /// Convert from a bits value exactly.
689 #[inline]
690 pub const fn from_bits_retain($bits: $T) -> Self
691 $from_bits_retain_body
692
693 /// Get a flags value with the bits of a flag with the given name set.
694 ///
695 /// This method will return `None` if `name` is empty or doesn't
696 /// correspond to any named flag.
697 #[inline]
698 pub fn from_name($name: &str) -> $crate::__private::core::option::Option<Self>
699 $from_name_body
700
701 /// Whether all bits in `self` are unset.
702 #[inline]
703 pub const fn is_empty(&$self) -> bool
704 $is_empty_body
705
706 /// Whether all known bits in this flags value are set.
707 #[inline]
708 pub const fn is_all(&$self) -> bool
709 $is_all_body
710
711 /// Whether any set bits in `other` are also set in `self`.
712 #[inline]
713 pub const fn intersects(&$self, $other: Self) -> bool
714 $intersects_body
715
716 /// Whether all set bits in `other` are also set in `self`.
717 #[inline]
718 pub const fn contains(&$self, $other: Self) -> bool
719 $contains_body
720
721 /// The bitwise or (`|`) of the bits in `self` and `other`.
722 #[inline]
723 pub fn insert(&mut $self, $other: Self)
724 $insert_body
725
726 /// The intersection of `self` with the complement of `other` (`&!`).
727 ///
728 /// This method is not equivalent to `self & !other` when `other` has unknown bits set.
729 /// `remove` won't truncate `other`, but the `!` operator will.
730 #[inline]
731 pub fn remove(&mut $self, $other: Self)
732 $remove_body
733
734 /// The bitwise exclusive-or (`^`) of the bits in `self` and `other`.
735 #[inline]
736 pub fn toggle(&mut $self, $other: Self)
737 $toggle_body
738
739 /// Call `insert` when `value` is `true` or `remove` when `value` is `false`.
740 #[inline]
741 pub fn set(&mut $self, $other: Self, $value: bool)
742 $set_body
743
744 /// The bitwise and (`&`) of the bits in `self` and `other`.
745 #[inline]
746 #[must_use]
747 pub const fn intersection($self, $other: Self) -> Self
748 $intersection_body
749
750 /// The bitwise or (`|`) of the bits in `self` and `other`.
751 #[inline]
752 #[must_use]
753 pub const fn union($self, $other: Self) -> Self
754 $union_body
755
756 /// The intersection of `self` with the complement of `other` (`&!`).
757 ///
758 /// This method is not equivalent to `self & !other` when `other` has unknown bits set.
759 /// `difference` won't truncate `other`, but the `!` operator will.
760 #[inline]
761 #[must_use]
762 pub const fn difference($self, $other: Self) -> Self
763 $difference_body
764
765 /// The bitwise exclusive-or (`^`) of the bits in `self` and `other`.
766 #[inline]
767 #[must_use]
768 pub const fn symmetric_difference($self, $other: Self) -> Self
769 $symmetric_difference_body
770
771 /// The bitwise negation (`!`) of the bits in `self`, truncating the result.
772 #[inline]
773 #[must_use]
774 pub const fn complement($self) -> Self
775 $complement_body
776 }
777 };
778}
779
780/// A macro that matches flags values, similar to Rust's `match` statement.
781///
782/// In a regular `match` statement, the syntax `Flag::A | Flag::B` is interpreted as an or-pattern,
783/// instead of the bitwise-or of `Flag::A` and `Flag::B`. This can be surprising when combined with flags types
784/// because `Flag::A | Flag::B` won't match the pattern `Flag::A | Flag::B`. This macro is an alternative to
785/// `match` for flags values that doesn't have this issue.
786///
787/// # Syntax
788///
789/// ```ignore
790/// bitflags_match!(expression, {
791/// pattern1 => result1,
792/// pattern2 => result2,
793/// ..
794/// _ => default_result,
795/// })
796/// ```
797///
798/// The final `_ => default_result` arm is required, otherwise the macro will fail to compile.
799///
800/// # Examples
801///
802/// ```rust
803/// use bitflags::{bitflags, bitflags_match};
804///
805/// bitflags! {
806/// #[derive(PartialEq)]
807/// struct Flags: u8 {
808/// const A = 1 << 0;
809/// const B = 1 << 1;
810/// const C = 1 << 2;
811/// }
812/// }
813///
814/// let flags = Flags::A | Flags::B;
815///
816/// // Prints `the value is A and B`
817/// bitflags_match!(flags, {
818/// Flags::A | Flags::B => println!("the value is A and B"),
819/// _ => println!("the value is not A and B"),
820/// });
821///
822/// // Prints `the value is not A`
823/// bitflags_match!(flags, {
824/// Flags::A => println!("the value is A"),
825/// _ => println!("the value is not A"),
826/// });
827/// ```
828///
829/// # How it works
830///
831/// The macro expands to a series of `if` statements, **checking equality** between the input expression
832/// and each pattern. This allows for correct matching of bitflag combinations, which is not possible
833/// with a regular match expression due to the way bitflags are implemented.
834///
835/// Patterns are evaluated in the order they appear in the macro.
836#[macro_export]
837macro_rules! bitflags_match {
838 ($operation:expr, {
839 $($t:tt)*
840 }) => {
841 // Expand to a closure so we can use `return`
842 // This makes it possible to apply attributes to the "match arms"
843 (|| {
844 $crate::__bitflags_match!($operation, { $($t)* })
845 })()
846 };
847}
848
849/// Expand the `bitflags_match` macro
850#[macro_export]
851#[doc(hidden)]
852macro_rules! __bitflags_match {
853 // Eat an optional `,` following a block match arm
854 ($operation:expr, { $pattern:expr => { $($body:tt)* } , $($t:tt)+ }) => {
855 $crate::__bitflags_match!($operation, { $pattern => { $($body)* } $($t)+ })
856 };
857 // Expand a block match arm `A => { .. }`
858 ($operation:expr, { $pattern:expr => { $($body:tt)* } $($t:tt)+ }) => {
859 {
860 if $operation == $pattern {
861 return {
862 $($body)*
863 };
864 }
865
866 $crate::__bitflags_match!($operation, { $($t)+ })
867 }
868 };
869 // Expand an expression match arm `A => x,`
870 ($operation:expr, { $pattern:expr => $body:expr , $($t:tt)+ }) => {
871 {
872 if $operation == $pattern {
873 return $body;
874 }
875
876 $crate::__bitflags_match!($operation, { $($t)+ })
877 }
878 };
879 // Expand the default case
880 ($operation:expr, { _ => $default:expr $(,)? }) => {
881 $default
882 }
883}
884
885/// Implement a flag, which may be a wildcard `_`.
886///
887/// Named flags will emit the `named` block, and unnamed flags will emit the `unnamed` block.
888#[macro_export]
889#[doc(hidden)]
890macro_rules! __bitflags_flag {
891 (
892 {
893 name: _,
894 named: { $($named:tt)* },
895 unnamed: { $($unnamed:tt)* },
896 }
897 ) => {
898 $($unnamed)*
899 };
900 (
901 {
902 name: $Flag:ident,
903 named: { $($named:tt)* },
904 unnamed: { $($unnamed:tt)* },
905 }
906 ) => {
907 $($named)*
908 };
909}
910
911/*
912Attribute inspection macros
913
914The following macros all use the same pattern for searching for specific attributes and transforming
915a target token tree. They're implementations of _token-tree munchers_, where each token from a source
916set is matched one-at-a-time until the input is exhausted, at which point the final result is emitted.
917
918The first match is the entrypoint for the macro with user syntax.
919
920Subsequent matches pull tokens from `unprocessed` and do something with them. That might be moving
921them into `processed` to be emitted later, or manipulating a target item/expression. The logic of
922the macro is implemented in these middle matches.
923
924The final match is the exitpoint, where `unprocessed` is empty.
925*/
926
927/// A macro that processes the input to `bitflags!` and shuffles attributes around
928/// based on whether or not they're "expression-safe".
929///
930/// This macro is a token-tree muncher that works on 2 levels:
931///
932/// For each attribute, we explicitly match on its identifier, like `cfg` to determine
933/// whether or not it should be considered expression-safe.
934///
935/// If you find yourself with an attribute that should be considered expression-safe
936/// and isn't, it can be added here.
937#[macro_export]
938#[doc(hidden)]
939macro_rules! __bitflags_expr_safe_attrs {
940 (
941 $(#[$inner:ident $($args:tt)*])*
942 { $e:expr }
943 ) => {
944 $crate::__bitflags_expr_safe_attrs! {
945 expr: { $e },
946 attrs: {
947 // All attributes start here
948 unprocessed: [$(#[$inner $($args)*])*],
949 // Attributes that are safe on expressions go here
950 processed: [],
951 },
952 }
953 };
954 // `cfg`: propagate
955 (
956 expr: { $e:expr },
957 attrs: {
958 unprocessed: [
959 #[cfg $($args:tt)*]
960 $($attrs_rest:tt)*
961 ],
962 processed: [$($expr:tt)*],
963 },
964 ) => {
965 $crate::__bitflags_expr_safe_attrs! {
966 expr: { $e },
967 attrs: {
968 unprocessed: [
969 $($attrs_rest)*
970 ],
971 processed: [
972 $($expr)*
973 #[cfg $($args)*]
974 ],
975 },
976 }
977 };
978 // Other: discard
979 (
980 expr: { $e:expr },
981 attrs: {
982 unprocessed: [
983 #[$other:ident $($args:tt)*]
984 $($attrs_rest:tt)*
985 ],
986 processed: [$($expr:tt)*],
987 },
988 ) => {
989 $crate::__bitflags_expr_safe_attrs! {
990 expr: { $e },
991 attrs: {
992 unprocessed: [
993 $($attrs_rest)*
994 ],
995 processed: [
996 $($expr)*
997 ],
998 },
999 }
1000 };
1001 // Finished
1002 (
1003 expr: { $e:expr },
1004 attrs: {
1005 unprocessed: [],
1006 processed: [$(#[$expr:ident $($exprargs:tt)*])*],
1007 },
1008 ) => {
1009 $(#[$expr $($exprargs)*])*
1010 { $e }
1011 }
1012}
1013
1014/// A macro that processes the input to `bitflags!` and shuffles attributes around
1015/// based on whether or not they're "item-safe".
1016///
1017/// This macro follows the same pattern as expr-safe above, but assumes all attributes
1018/// are safe on items. It only filters out any `bitflags`-defined attributes.
1019#[macro_export]
1020#[doc(hidden)]
1021macro_rules! __bitflags_item_safe_attrs {
1022 (
1023 $(#[$inner:ident $($args:tt)*])*
1024 { $i:item }
1025 ) => {
1026 $crate::__bitflags_item_safe_attrs! {
1027 item: { $i },
1028 attrs: {
1029 // All attributes start here
1030 unprocessed: [$(#[$inner $($args)*])*],
1031 // Attributes that are safe on items go here
1032 processed: [],
1033 },
1034 }
1035 };
1036 // `bitflags`: discard
1037 (
1038 item: { $i:item },
1039 attrs: {
1040 unprocessed: [
1041 #[bitflags $($args:tt)*]
1042 $($attrs_rest:tt)*
1043 ],
1044 processed: [$($item:tt)*],
1045 },
1046 ) => {
1047 $crate::__bitflags_item_safe_attrs! {
1048 item: { $i },
1049 attrs: {
1050 unprocessed: [
1051 $($attrs_rest)*
1052 ],
1053 processed: [
1054 $($item)*
1055 ],
1056 },
1057 }
1058 };
1059 // Other: propagate
1060 (
1061 item: { $i:item },
1062 attrs: {
1063 unprocessed: [
1064 // $other matched here
1065 #[$other:ident $($args:tt)*]
1066 $($attrs_rest:tt)*
1067 ],
1068 processed: [$($item:tt)*],
1069 },
1070 ) => {
1071 $crate::__bitflags_item_safe_attrs! {
1072 item: { $i },
1073 attrs: {
1074 unprocessed: [
1075 $($attrs_rest)*
1076 ],
1077 processed: [
1078 $($item)*
1079 #[$other $($args)*]
1080 ],
1081 },
1082 }
1083 };
1084 // Finished
1085 (
1086 item: { $i:item },
1087 attrs: {
1088 unprocessed: [],
1089 processed: [$(#[$item:ident $($itemargs:tt)*])*],
1090 },
1091 ) => {
1092 $(#[$item $($itemargs)*])*
1093 $i
1094 }
1095}
1096
1097/// Determine the name to assign to a flag.
1098#[macro_export]
1099#[doc(hidden)]
1100macro_rules! __bitflags_flag_name {
1101 // Unnamed
1102 (
1103 $(#[$inner:ident $($args:tt)*])*
1104 { $vis:vis const _ = _ }
1105 ) => {
1106
1107 };
1108 (
1109 $(#[$inner:ident $($args:tt)*])*
1110 { $vis:vis const $binding:ident = $name:expr }
1111 ) => {
1112 $crate::__bitflags_flag_name! {
1113 item: { $vis const $binding = $crate::__private::core::stringify!($name) },
1114 attrs: {
1115 // All attributes start here
1116 unprocessed: [$(#[$inner $($args)*])*],
1117 // Attributes that are safe on the flag name go here
1118 processed: [],
1119 },
1120 }
1121 };
1122 // `bitflags(flag_name)`: set the name
1123 (
1124 item: { $vis:vis const $binding:ident = $name:expr },
1125 attrs: {
1126 unprocessed: [
1127 #[bitflags(flag_name = $flag_name:expr)]
1128 $($attrs_rest:tt)*
1129 ],
1130 processed: [$($item:tt)*],
1131 },
1132 ) => {
1133 $crate::__bitflags_flag_name! {
1134 item: { $vis const $binding = $flag_name },
1135 attrs: {
1136 unprocessed: [
1137 $($attrs_rest)*
1138 ],
1139 processed: [
1140 $($item)*
1141 ],
1142 },
1143 }
1144 };
1145 // `cfg`: propagate
1146 (
1147 item: { $vis:vis const $binding:ident = $name:expr },
1148 attrs: {
1149 unprocessed: [
1150 #[cfg $($args:tt)*]
1151 $($attrs_rest:tt)*
1152 ],
1153 processed: [$($item:tt)*],
1154 },
1155 ) => {
1156 $crate::__bitflags_flag_name! {
1157 item: { $vis const $binding = $name },
1158 attrs: {
1159 unprocessed: [
1160 $($attrs_rest)*
1161 ],
1162 processed: [
1163 $($item)*
1164 #[cfg $($args)*]
1165 ],
1166 },
1167 }
1168 };
1169 // Other: discard
1170 (
1171 item: { $vis:vis const $binding:ident = $name:expr },
1172 attrs: {
1173 unprocessed: [
1174 #[$other:ident $($args:tt)*]
1175 $($attrs_rest:tt)*
1176 ],
1177 processed: [$($item:tt)*],
1178 },
1179 ) => {
1180 $crate::__bitflags_flag_name! {
1181 item: { $vis const $binding = $name },
1182 attrs: {
1183 unprocessed: [
1184 $($attrs_rest)*
1185 ],
1186 processed: [$($item)*],
1187 },
1188 }
1189 };
1190 // Finished
1191 (
1192 item: { $vis:vis const $binding:ident = $name:expr },
1193 attrs: {
1194 unprocessed: [],
1195 processed: [$(#[$item:ident $($itemargs:tt)*])*],
1196 },
1197 ) => {
1198 $(#[$item $($itemargs)*])*
1199 $vis const $binding: &'static str = $name;
1200 }
1201}
1202
1203#[macro_use]
1204mod public;
1205#[macro_use]
1206mod internal;
1207#[macro_use]
1208mod external;
1209
1210#[cfg(feature = "example_generated")]
1211pub mod example_generated;
1212
1213#[cfg(test)]
1214mod tests;