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 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! A typesafe bitmask flag generator useful for sets of C-style flags.
//! It can be used for creating ergonomic wrappers around C APIs.
//!
//! The `bitflags!` macro generates `struct`s that manage a set of flags. The
//! type of those flags must be some primitive integer.
//!
//! # Examples
//!
//! ```
//! use bitflags::bitflags;
//!
//! bitflags! {
//! #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
//! struct Flags: u32 {
//! const A = 0b00000001;
//! const B = 0b00000010;
//! const C = 0b00000100;
//! const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
//! }
//! }
//!
//! fn main() {
//! let e1 = Flags::A | Flags::C;
//! let e2 = Flags::B | Flags::C;
//! assert_eq!((e1 | e2), Flags::ABC); // union
//! assert_eq!((e1 & e2), Flags::C); // intersection
//! assert_eq!((e1 - e2), Flags::A); // set difference
//! assert_eq!(!e2, Flags::A); // set complement
//! }
//! ```
//!
//! See [`example_generated::Flags`](./example_generated/struct.Flags.html) for documentation of code
//! generated by the above `bitflags!` expansion.
//!
//! # Visibility
//!
//! The `bitflags!` macro supports visibility, just like you'd expect when writing a normal
//! Rust `struct`:
//!
//! ```
//! mod example {
//! use bitflags::bitflags;
//!
//! bitflags! {
//! #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
//! pub struct Flags1: u32 {
//! const A = 0b00000001;
//! }
//!
//! #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
//! # pub
//! struct Flags2: u32 {
//! const B = 0b00000010;
//! }
//! }
//! }
//!
//! fn main() {
//! let flag1 = example::Flags1::A;
//! let flag2 = example::Flags2::B; // error: const `B` is private
//! }
//! ```
//!
//! # Attributes
//!
//! Attributes can be attached to the generated flags types and their constants as normal.
//!
//! # Representation
//!
//! It's valid to add a `#[repr(C)]` or `#[repr(transparent)]` attribute to a generated flags type.
//! The generated flags type is always guaranteed to be a newtype where its only field has the same
//! ABI as the underlying integer type.
//!
//! In this example, `Flags` has the same ABI as `u32`:
//!
//! ```
//! use bitflags::bitflags;
//!
//! bitflags! {
//! #[repr(transparent)]
//! #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
//! struct Flags: u32 {
//! const A = 0b00000001;
//! const B = 0b00000010;
//! const C = 0b00000100;
//! }
//! }
//! ```
//!
//! # Extending
//!
//! Generated flags types belong to you, so you can add trait implementations to them outside
//! of what the `bitflags!` macro gives:
//!
//! ```
//! use std::fmt;
//!
//! use bitflags::bitflags;
//!
//! bitflags! {
//! #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
//! struct Flags: u32 {
//! const A = 0b00000001;
//! const B = 0b00000010;
//! }
//! }
//!
//! impl Flags {
//! pub fn clear(&mut self) {
//! *self.0.bits_mut() = 0;
//! }
//! }
//!
//! fn main() {
//! let mut flags = Flags::A | Flags::B;
//!
//! flags.clear();
//! assert!(flags.is_empty());
//!
//! assert_eq!(format!("{:?}", Flags::A | Flags::B), "Flags(A | B)");
//! assert_eq!(format!("{:?}", Flags::B), "Flags(B)");
//! }
//! ```
//!
//! # What's implemented by `bitflags!`
//!
//! The `bitflags!` macro adds some trait implementations and inherent methods
//! to generated flags types, but leaves room for you to choose the semantics
//! of others.
//!
//! ## Iterators
//!
//! The following iterator traits are implemented for generated flags types:
//!
//! - `Extend`: adds the union of the instances iterated over.
//! - `FromIterator`: calculates the union.
//! - `IntoIterator`: iterates over set flag values.
//!
//! ## Formatting
//!
//! The following formatting traits are implemented for generated flags types:
//!
//! - `Binary`.
//! - `LowerHex` and `UpperHex`.
//! - `Octal`.
//!
//! Also see the _Debug and Display_ section for details about standard text
//! representations for flags types.
//!
//! ## Operators
//!
//! The following operator traits are implemented for the generated `struct`s:
//!
//! - `BitOr` and `BitOrAssign`: union
//! - `BitAnd` and `BitAndAssign`: intersection
//! - `BitXor` and `BitXorAssign`: toggle
//! - `Sub` and `SubAssign`: set difference
//! - `Not`: set complement
//!
//! ## Methods
//!
//! The following methods are defined for the generated `struct`s:
//!
//! - `empty`: an empty set of flags
//! - `all`: the set of all defined flags
//! - `bits`: the raw value of the flags currently stored
//! - `from_bits`: convert from underlying bit representation, unless that
//! representation contains bits that do not correspond to a
//! defined flag
//! - `from_bits_truncate`: convert from underlying bit representation, dropping
//! any bits that do not correspond to defined flags
//! - `from_bits_retain`: convert from underlying bit representation, keeping
//! all bits (even those not corresponding to defined
//! flags)
//! - `is_empty`: `true` if no flags are currently stored
//! - `is_all`: `true` if currently set flags exactly equal all defined flags
//! - `intersects`: `true` if there are flags common to both `self` and `other`
//! - `contains`: `true` if all of the flags in `other` are contained within `self`
//! - `insert`: inserts the specified flags in-place
//! - `remove`: removes the specified flags in-place
//! - `toggle`: the specified flags will be inserted if not present, and removed
//! if they are.
//! - `set`: inserts or removes the specified flags depending on the passed value
//! - `intersection`: returns a new set of flags, containing only the flags present
//! in both `self` and `other` (the argument to the function).
//! - `union`: returns a new set of flags, containing any flags present in
//! either `self` or `other` (the argument to the function).
//! - `difference`: returns a new set of flags, containing all flags present in
//! `self` without any of the flags present in `other` (the
//! argument to the function).
//! - `symmetric_difference`: returns a new set of flags, containing all flags
//! present in either `self` or `other` (the argument
//! to the function), but not both.
//! - `complement`: returns a new set of flags, containing all flags which are
//! not set in `self`, but which are allowed for this type.
//!
//! # What's not implemented by `bitflags!`
//!
//! Some functionality is not automatically implemented for generated flags types
//! by the `bitflags!` macro, even when it reasonably could be. This is so callers
//! have more freedom to decide on the semantics of their flags types.
//!
//! ## `Clone` and `Copy`
//!
//! Generated flags types are not automatically copyable, even though they can always
//! derive both `Clone` and `Copy`.
//!
//! ## `Default`
//!
//! The `Default` trait is not automatically implemented for the generated structs.
//!
//! If your default value is equal to `0` (which is the same value as calling `empty()`
//! on the generated struct), you can simply derive `Default`:
//!
//! ```
//! use bitflags::bitflags;
//!
//! bitflags! {
//! // Results in default value with bits: 0
//! #[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash)]
//! struct Flags: u32 {
//! const A = 0b00000001;
//! const B = 0b00000010;
//! const C = 0b00000100;
//! }
//! }
//!
//! fn main() {
//! let derived_default: Flags = Default::default();
//! assert_eq!(derived_default.bits(), 0);
//! }
//! ```
//!
//! If your default value is not equal to `0` you need to implement `Default` yourself:
//!
//! ```
//! use bitflags::bitflags;
//!
//! bitflags! {
//! #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
//! struct Flags: u32 {
//! const A = 0b00000001;
//! const B = 0b00000010;
//! const C = 0b00000100;
//! }
//! }
//!
//! // explicit `Default` implementation
//! impl Default for Flags {
//! fn default() -> Flags {
//! Flags::A | Flags::C
//! }
//! }
//!
//! fn main() {
//! let implemented_default: Flags = Default::default();
//! assert_eq!(implemented_default, (Flags::A | Flags::C));
//! }
//! ```
//!
//! ## `Debug` and `Display`
//!
//! The `Debug` trait can be derived for a reasonable implementation. This library defines a standard
//! text-based representation for flags that generated flags types can use. For details on the exact
//! grammar, see the [`parser`] module.
//!
//! To support formatting and parsing your generated flags types using that representation, you can implement
//! the standard `Display` and `FromStr` traits in this fashion:
//!
//! ```
//! use bitflags::bitflags;
//! use std::{fmt, str};
//!
//! bitflags! {
//! pub struct Flags: u32 {
//! const A = 1;
//! const B = 2;
//! const C = 4;
//! const D = 8;
//! }
//! }
//!
//! impl fmt::Debug for Flags {
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//! fmt::Debug::fmt(&self.0, f)
//! }
//! }
//!
//! impl fmt::Display for Flags {
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//! fmt::Display::fmt(&self.0, f)
//! }
//! }
//!
//! impl str::FromStr for Flags {
//! type Err = bitflags::parser::ParseError;
//!
//! fn from_str(flags: &str) -> Result<Self, Self::Err> {
//! Ok(Self(flags.parse()?))
//! }
//! }
//! ```
//!
//! ## `PartialEq` and `PartialOrd`
//!
//! Equality and ordering can be derived for a reasonable implementation, or implemented manually
//! for different semantics.
//!
//! # Edge cases
//!
//! ## Zero Flags
//!
//! Flags with a value equal to zero will have some strange behavior that one should be aware of.
//!
//! ```
//! use bitflags::bitflags;
//!
//! bitflags! {
//! #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
//! struct Flags: u32 {
//! const NONE = 0b00000000;
//! const SOME = 0b00000001;
//! }
//! }
//!
//! fn main() {
//! let empty = Flags::empty();
//! let none = Flags::NONE;
//! let some = Flags::SOME;
//!
//! // Zero flags are treated as always present
//! assert!(empty.contains(Flags::NONE));
//! assert!(none.contains(Flags::NONE));
//! assert!(some.contains(Flags::NONE));
//!
//! // Zero flags will be ignored when testing for emptiness
//! assert!(none.is_empty());
//! }
//! ```
//!
//! Users should generally avoid defining a flag with a value of zero.
//!
//! ## Multi-bit Flags
//!
//! It is allowed to define a flag with multiple bits set, however such
//! flags are _not_ treated as a set where any of those bits is a valid
//! flag. Instead, each flag is treated as a unit when converting from
//! bits with [`from_bits`] or [`from_bits_truncate`].
//!
//! ```
//! use bitflags::bitflags;
//!
//! bitflags! {
//! #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
//! struct Flags: u8 {
//! const F3 = 0b00000011;
//! }
//! }
//!
//! fn main() {
//! // This bit pattern does not set all the bits in `F3`, so it is rejected.
//! assert!(Flags::from_bits(0b00000001).is_none());
//! assert!(Flags::from_bits_truncate(0b00000001).is_empty());
//! }
//! ```
//!
//! [`from_bits`]: Flags::from_bits
//! [`from_bits_truncate`]: Flags::from_bits_truncate
//!
//! # The `Flags` trait
//!
//! This library defines a `Flags` trait that's implemented by all generated flags types.
//! The trait makes it possible to work with flags types generically:
//!
//! ```
//! fn count_unset_flags<F: bitflags::Flags>(flags: &F) -> usize {
//! // Find out how many flags there are in total
//! let total = F::all().iter().count();
//!
//! // Find out how many flags are set
//! let set = flags.iter().count();
//!
//! total - set
//! }
//!
//! use bitflags::bitflags;
//!
//! bitflags! {
//! #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
//! struct Flags: u32 {
//! const A = 0b00000001;
//! const B = 0b00000010;
//! const C = 0b00000100;
//! }
//! }
//!
//! assert_eq!(2, count_unset_flags(&Flags::B));
//! ```
//!
//! # The internal field
//!
//! This library generates newtypes like:
//!
//! ```
//! # pub struct Field0;
//! pub struct Flags(Field0);
//! ```
//!
//! You can freely use methods and trait implementations on this internal field as `.0`.
//! For details on exactly what's generated for it, see the [`Field0`](example_generated/struct.Field0.html)
//! example docs.
#![cfg_attr(not(any(feature = "std", test)), no_std)]
#![cfg_attr(not(test), forbid(unsafe_code))]
#![cfg_attr(test, allow(mixed_script_confusables))]
#![doc(html_root_url = "https://docs.rs/bitflags/2.3.3")]
#[doc(inline)]
pub use traits::{Bits, Flag, Flags};
pub mod iter;
pub mod parser;
mod traits;
#[doc(hidden)]
pub mod __private {
pub use crate::{external::__private::*, traits::__private::*};
pub use core;
}
#[allow(unused_imports)]
pub use external::*;
#[allow(deprecated)]
pub use traits::BitFlags;
/*
How does the bitflags crate work?
This library generates a `struct` in the end-user's crate with a bunch of constants on it that represent flags.
The difference between `bitflags` and a lot of other libraries is that we don't actually control the generated `struct` in the end.
It's part of the end-user's crate, so it belongs to them. That makes it difficult to extend `bitflags` with new functionality
because we could end up breaking valid code that was already written.
Our 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).
To give you an example, let's say we had a crate that called `bitflags!`:
```rust
bitflags! {
pub struct MyFlags: u32 {
const A = 1;
const B = 2;
}
}
```
What they'd end up with looks something like this:
```rust
pub struct MyFlags(<MyFlags as PublicFlags>::InternalBitFlags);
const _: () = {
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MyInternalBitFlags {
bits: u32,
}
impl PublicFlags for MyFlags {
type Internal = InternalBitFlags;
}
};
```
If we want to expose something like a new trait impl for generated flags types, we add it to our generated `MyInternalBitFlags`,
and let `#[derive]` on `MyFlags` pick up that implementation, if an end-user chooses to add one.
The public API is generated in the `__impl_public_flags!` macro, and the internal API is generated in
the `__impl_internal_flags!` macro.
The macros are split into 3 modules:
- `public`: where the user-facing flags types are generated.
- `internal`: where the `bitflags`-facing flags types are generated.
- `external`: where external library traits are implemented conditionally.
*/
/// The macro used to generate the flag structure.
///
/// See the [crate level docs](../bitflags/index.html) for complete documentation.
///
/// # Example
///
/// ```
/// use bitflags::bitflags;
///
/// bitflags! {
/// #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
/// struct Flags: u32 {
/// const A = 0b00000001;
/// const B = 0b00000010;
/// const C = 0b00000100;
/// const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
/// }
/// }
///
/// let e1 = Flags::A | Flags::C;
/// let e2 = Flags::B | Flags::C;
/// assert_eq!((e1 | e2), Flags::ABC); // union
/// assert_eq!((e1 & e2), Flags::C); // intersection
/// assert_eq!((e1 - e2), Flags::A); // set difference
/// assert_eq!(!e2, Flags::A); // set complement
/// ```
///
/// The generated `struct`s can also be extended with type and trait
/// implementations:
///
/// ```
/// use std::fmt;
///
/// use bitflags::bitflags;
///
/// bitflags! {
/// #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
/// struct Flags: u32 {
/// const A = 0b00000001;
/// const B = 0b00000010;
/// }
/// }
///
/// impl Flags {
/// pub fn clear(&mut self) {
/// *self.0.bits_mut() = 0;
/// }
/// }
///
/// let mut flags = Flags::A | Flags::B;
///
/// flags.clear();
/// assert!(flags.is_empty());
///
/// assert_eq!(format!("{:?}", Flags::A | Flags::B), "Flags(A | B)");
/// assert_eq!(format!("{:?}", Flags::B), "Flags(B)");
/// ```
#[macro_export(local_inner_macros)]
macro_rules! bitflags {
(
$(#[$outer:meta])*
$vis:vis struct $BitFlags:ident: $T:ty {
$(
$(#[$inner:ident $($args:tt)*])*
const $Flag:ident = $value:expr;
)*
}
$($t:tt)*
) => {
// Declared in the scope of the `bitflags!` call
// This type appears in the end-user's API
__declare_public_bitflags! {
$(#[$outer])*
$vis struct $BitFlags
}
// Workaround for: https://github.com/bitflags/bitflags/issues/320
__impl_public_bitflags_consts! {
$BitFlags: $T {
$(
$(#[$inner $($args)*])*
$Flag = $value;
)*
}
}
#[allow(
dead_code,
deprecated,
unused_doc_comments,
unused_attributes,
unused_mut,
unused_imports,
non_upper_case_globals,
clippy::assign_op_pattern
)]
const _: () = {
// Declared in a "hidden" scope that can't be reached directly
// These types don't appear in the end-user's API
__declare_internal_bitflags! {
$vis struct InternalBitFlags: $T
}
__impl_internal_bitflags! {
InternalBitFlags: $T, $BitFlags {
$(
$(#[$inner $($args)*])*
$Flag = $value;
)*
}
}
// This is where new library trait implementations can be added
__impl_external_bitflags! {
InternalBitFlags: $T, $BitFlags {
$(
$(#[$inner $($args)*])*
$Flag;
)*
}
}
__impl_public_bitflags_forward! {
$BitFlags: $T, InternalBitFlags
}
__impl_public_bitflags_ops! {
$BitFlags
}
__impl_public_bitflags_iter! {
$BitFlags: $T, $BitFlags
}
};
bitflags! {
$($t)*
}
};
(
impl $BitFlags:ident: $T:ty {
$(
$(#[$inner:ident $($args:tt)*])*
const $Flag:ident = $value:expr;
)*
}
$($t:tt)*
) => {
__impl_public_bitflags_consts! {
$BitFlags: $T {
$(
$(#[$inner $($args)*])*
$Flag = $value;
)*
}
}
#[allow(
dead_code,
deprecated,
unused_doc_comments,
unused_attributes,
unused_mut,
unused_imports,
non_upper_case_globals,
clippy::assign_op_pattern
)]
const _: () = {
__impl_public_bitflags! {
$BitFlags: $T, $BitFlags {
$(
$(#[$inner $($args)*])*
$Flag;
)*
}
}
__impl_public_bitflags_ops! {
$BitFlags
}
__impl_public_bitflags_iter! {
$BitFlags: $T, $BitFlags
}
};
bitflags! {
$($t)*
}
};
() => {};
}
/// Implement functions on bitflags types.
///
/// We need to be careful about adding new methods and trait implementations here because they
/// could conflict with items added by the end-user.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __impl_bitflags {
(
$PublicBitFlags:ident: $T:ty {
fn empty() $empty:block
fn all() $all:block
fn bits($bits0:ident) $bits:block
fn from_bits($from_bits0:ident) $from_bits:block
fn from_bits_truncate($from_bits_truncate0:ident) $from_bits_truncate:block
fn from_bits_retain($from_bits_retain0:ident) $from_bits_retain:block
fn from_name($from_name0:ident) $from_name:block
fn is_empty($is_empty0:ident) $is_empty:block
fn is_all($is_all0:ident) $is_all:block
fn intersects($intersects0:ident, $intersects1:ident) $intersects:block
fn contains($contains0:ident, $contains1:ident) $contains:block
fn insert($insert0:ident, $insert1:ident) $insert:block
fn remove($remove0:ident, $remove1:ident) $remove:block
fn toggle($toggle0:ident, $toggle1:ident) $toggle:block
fn set($set0:ident, $set1:ident, $set2:ident) $set:block
fn intersection($intersection0:ident, $intersection1:ident) $intersection:block
fn union($union0:ident, $union1:ident) $union:block
fn difference($difference0:ident, $difference1:ident) $difference:block
fn symmetric_difference($symmetric_difference0:ident, $symmetric_difference1:ident) $symmetric_difference:block
fn complement($complement0:ident) $complement:block
}
) => {
#[allow(dead_code, deprecated, unused_attributes)]
impl $PublicBitFlags {
/// Returns an empty set of flags.
#[inline]
pub const fn empty() -> Self {
$empty
}
/// Returns the set containing all flags.
#[inline]
pub const fn all() -> Self {
$all
}
/// Returns the raw value of the flags currently stored.
#[inline]
pub const fn bits(&self) -> $T {
let $bits0 = self;
$bits
}
/// Convert from underlying bit representation, unless that
/// representation contains bits that do not correspond to a flag.
#[inline]
pub const fn from_bits(bits: $T) -> $crate::__private::core::option::Option<Self> {
let $from_bits0 = bits;
$from_bits
}
/// Convert from underlying bit representation, dropping any bits
/// that do not correspond to flags.
#[inline]
pub const fn from_bits_truncate(bits: $T) -> Self {
let $from_bits_truncate0 = bits;
$from_bits_truncate
}
/// Convert from underlying bit representation, preserving all
/// bits (even those not corresponding to a defined flag).
#[inline]
pub const fn from_bits_retain(bits: $T) -> Self {
let $from_bits_retain0 = bits;
$from_bits_retain
}
/// Get the value for a flag from its stringified name.
///
/// Names are _case-sensitive_, so must correspond exactly to
/// the identifier given to the flag.
#[inline]
pub fn from_name(name: &str) -> $crate::__private::core::option::Option<Self> {
let $from_name0 = name;
$from_name
}
/// Returns `true` if no flags are currently stored.
#[inline]
pub const fn is_empty(&self) -> bool {
let $is_empty0 = self;
$is_empty
}
/// Returns `true` if all flags are currently set.
#[inline]
pub const fn is_all(&self) -> bool {
let $is_all0 = self;
$is_all
}
/// Returns `true` if there are flags common to both `self` and `other`.
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
let $intersects0 = self;
let $intersects1 = other;
$intersects
}
/// Returns `true` if all of the flags in `other` are contained within `self`.
#[inline]
pub const fn contains(&self, other: Self) -> bool {
let $contains0 = self;
let $contains1 = other;
$contains
}
/// Inserts the specified flags in-place.
///
/// This method is equivalent to `union`.
#[inline]
pub fn insert(&mut self, other: Self) {
let $insert0 = self;
let $insert1 = other;
$insert
}
/// Removes the specified flags in-place.
///
/// This method is equivalent to `difference`.
#[inline]
pub fn remove(&mut self, other: Self) {
let $remove0 = self;
let $remove1 = other;
$remove
}
/// Toggles the specified flags in-place.
///
/// This method is equivalent to `symmetric_difference`.
#[inline]
pub fn toggle(&mut self, other: Self) {
let $toggle0 = self;
let $toggle1 = other;
$toggle
}
/// Inserts or removes the specified flags depending on the passed value.
#[inline]
pub fn set(&mut self, other: Self, value: bool) {
let $set0 = self;
let $set1 = other;
let $set2 = value;
$set
}
/// Returns the intersection between the flags in `self` and
/// `other`.
///
/// Calculating `self` bitwise and (`&`) other, including
/// any bits that don't correspond to a defined flag.
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
let $intersection0 = self;
let $intersection1 = other;
$intersection
}
/// Returns the union of between the flags in `self` and `other`.
///
/// Calculates `self` bitwise or (`|`) `other`, including
/// any bits that don't correspond to a defined flag.
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
let $union0 = self;
let $union1 = other;
$union
}
/// Returns the difference between the flags in `self` and `other`.
///
/// Calculates `self` bitwise and (`&!`) the bitwise negation of `other`,
/// including any bits that don't correspond to a defined flag.
///
/// This method is _not_ equivalent to `a & !b` when there are bits set that
/// don't correspond to a defined flag. The `!` operator will unset any
/// bits that don't correspond to a flag, so they'll always be unset by `a &! b`,
/// but respected by `a.difference(b)`.
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
let $difference0 = self;
let $difference1 = other;
$difference
}
/// Returns the symmetric difference between the flags
/// in `self` and `other`.
///
/// Calculates `self` bitwise exclusive or (`^`) `other`,
/// including any bits that don't correspond to a defined flag.
#[inline]
#[must_use]
pub const fn symmetric_difference(self, other: Self) -> Self {
let $symmetric_difference0 = self;
let $symmetric_difference1 = other;
$symmetric_difference
}
/// Returns the complement of this set of flags.
///
/// Calculates the bitwise negation (`!`) of `self`,
/// **unsetting** any bits that don't correspond to a defined flag.
#[inline]
#[must_use]
pub const fn complement(self) -> Self {
let $complement0 = self;
$complement
}
}
};
}
/// A macro that processed the input to `bitflags!` and shuffles attributes around
/// based on whether or not they're "expression-safe".
///
/// This macro is a token-tree muncher that works on 2 levels:
///
/// For each attribute, we explicitly match on its identifier, like `cfg` to determine
/// whether or not it should be considered expression-safe.
///
/// If you find yourself with an attribute that should be considered expression-safe
/// and isn't, it can be added here.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __bitflags_expr_safe_attrs {
// Entrypoint: Move all flags and all attributes into `unprocessed` lists
// where they'll be munched one-at-a-time
(
$(#[$inner:ident $($args:tt)*])*
{ $e:expr }
) => {
__bitflags_expr_safe_attrs! {
expr: { $e },
attrs: {
// All attributes start here
unprocessed: [$(#[$inner $($args)*])*],
// Attributes that are safe on expressions go here
processed: [],
},
}
};
// Process the next attribute on the current flag
// `cfg`: The next flag should be propagated to expressions
// NOTE: You can copy this rules block and replace `cfg` with
// your attribute name that should be considered expression-safe
(
expr: { $e:expr },
attrs: {
unprocessed: [
// cfg matched here
#[cfg $($args:tt)*]
$($attrs_rest:tt)*
],
processed: [$($expr:tt)*],
},
) => {
__bitflags_expr_safe_attrs! {
expr: { $e },
attrs: {
unprocessed: [
$($attrs_rest)*
],
processed: [
$($expr)*
// cfg added here
#[cfg $($args)*]
],
},
}
};
// Process the next attribute on the current flag
// `$other`: The next flag should not be propagated to expressions
(
expr: { $e:expr },
attrs: {
unprocessed: [
// $other matched here
#[$other:ident $($args:tt)*]
$($attrs_rest:tt)*
],
processed: [$($expr:tt)*],
},
) => {
__bitflags_expr_safe_attrs! {
expr: { $e },
attrs: {
unprocessed: [
$($attrs_rest)*
],
processed: [
// $other not added here
$($expr)*
],
},
}
};
// Once all attributes on all flags are processed, generate the actual code
(
expr: { $e:expr },
attrs: {
unprocessed: [],
processed: [$(#[$expr:ident $($exprargs:tt)*])*],
},
) => {
$(#[$expr $($exprargs)*])*
{ $e }
}
}
#[macro_use]
mod public;
#[macro_use]
mod internal;
#[macro_use]
mod external;
#[cfg(feature = "example_generated")]
pub mod example_generated;
#[cfg(test)]
mod tests;