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.
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.
11 //! A typesafe bitmask flag generator useful for sets of C-style bitmask flags.
12 //! It can be used for creating typesafe wrappers around C APIs.
14 //! The `bitflags!` macro generates a `struct` that manages a set of flags. The
15 //! flags should only be defined for integer types, otherwise unexpected type
16 //! errors may occur at compile time.
22 //! extern crate bitflags;
25 //! struct Flags: u32 {
26 //! const FLAG_A = 0b00000001;
27 //! const FLAG_B = 0b00000010;
28 //! const FLAG_C = 0b00000100;
29 //! const FLAG_ABC = FLAG_A.bits
36 //! let e1 = FLAG_A | FLAG_C;
37 //! let e2 = FLAG_B | FLAG_C;
38 //! assert_eq!((e1 | e2), FLAG_ABC); // union
39 //! assert_eq!((e1 & e2), FLAG_C); // intersection
40 //! assert_eq!((e1 - e2), FLAG_A); // set difference
41 //! assert_eq!(!e2, FLAG_A); // set complement
45 //! See [`example_generated::Flags`](./example_generated/struct.Flags.html) for documentation of code
46 //! generated by the above `bitflags!` expansion.
48 //! The generated `struct`s can also be extended with type and trait
53 //! extern crate bitflags;
58 //! struct Flags: u32 {
59 //! const FLAG_A = 0b00000001;
60 //! const FLAG_B = 0b00000010;
65 //! pub fn clear(&mut self) {
66 //! self.bits = 0; // The `bits` field can be accessed from within the
67 //! // same module where the `bitflags!` macro was invoked.
71 //! impl fmt::Display for Flags {
72 //! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78 //! let mut flags = FLAG_A | FLAG_B;
80 //! assert!(flags.is_empty());
81 //! assert_eq!(format!("{}", flags), "hi!");
82 //! assert_eq!(format!("{:?}", FLAG_A | FLAG_B), "FLAG_A | FLAG_B");
83 //! assert_eq!(format!("{:?}", FLAG_B), "FLAG_B");
89 //! The generated struct and its associated flag constants are not exported
90 //! out of the current module by default. A definition can be exported out of
91 //! the current module by adding `pub` before `flags`:
95 //! extern crate bitflags;
99 //! pub struct Flags1: u32 {
100 //! const FLAG_A = 0b00000001;
104 //! struct Flags2: u32 {
105 //! const FLAG_B = 0b00000010;
111 //! let flag1 = example::FLAG_A;
112 //! let flag2 = example::FLAG_B; // error: const `FLAG_B` is private
118 //! Attributes can be attached to the generated `struct` by placing them
119 //! before the `flags` keyword.
121 //! # Trait implementations
123 //! The `Copy`, `Clone`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash`
124 //! traits automatically derived for the `struct` using the `derive` attribute.
125 //! Additional traits can be derived by providing an explicit `derive`
126 //! attribute on `flags`.
128 //! The `Extend` and `FromIterator` traits are implemented for the `struct`,
129 //! too: `Extend` adds the union of the instances of the `struct` iterated over,
130 //! while `FromIterator` calculates the union.
132 //! The `Binary`, `Debug`, `LowerExp`, `Octal` and `UpperExp` trait is also
133 //! implemented by displaying the bits value of the internal struct.
137 //! The following operator traits are implemented for the generated `struct`:
139 //! - `BitOr` and `BitOrAssign`: union
140 //! - `BitAnd` and `BitAndAssign`: intersection
141 //! - `BitXor` and `BitXorAssign`: toggle
142 //! - `Sub` and `SubAssign`: set difference
143 //! - `Not`: set complement
147 //! The following methods are defined for the generated `struct`:
149 //! - `empty`: an empty set of flags
150 //! - `all`: the set of all flags
151 //! - `bits`: the raw value of the flags currently stored
152 //! - `from_bits`: convert from underlying bit representation, unless that
153 //! representation contains bits that do not correspond to a flag
154 //! - `from_bits_truncate`: convert from underlying bit representation, dropping
155 //! any bits that do not correspond to flags
156 //! - `is_empty`: `true` if no flags are currently stored
157 //! - `is_all`: `true` if all flags are currently set
158 //! - `intersects`: `true` if there are flags common to both `self` and `other`
159 //! - `contains`: `true` all of the flags in `other` are contained within `self`
160 //! - `insert`: inserts the specified flags in-place
161 //! - `remove`: removes the specified flags in-place
162 //! - `toggle`: the specified flags will be inserted if not present, and removed
167 //! The `Default` trait is not automatically implemented for the generated struct.
169 //! If your default value is equal to `0` (which is the same value as calling `empty()`
170 //! on the generated struct), you can simply derive `Default`:
174 //! extern crate bitflags;
177 //! // Results in default value with bits: 0
178 //! #[derive(Default)]
179 //! struct Flags: u32 {
180 //! const FLAG_A = 0b00000001;
181 //! const FLAG_B = 0b00000010;
182 //! const FLAG_C = 0b00000100;
187 //! let derived_default: Flags = Default::default();
188 //! assert_eq!(derived_default.bits(), 0);
192 //! If your default value is not equal to `0` you need to implement `Default` yourself:
196 //! extern crate bitflags;
199 //! struct Flags: u32 {
200 //! const FLAG_A = 0b00000001;
201 //! const FLAG_B = 0b00000010;
202 //! const FLAG_C = 0b00000100;
206 //! // explicit `Default` implementation
207 //! impl Default for Flags {
208 //! fn default() -> Flags {
214 //! let implemented_default: Flags = Default::default();
215 //! assert_eq!(implemented_default, (FLAG_A | FLAG_C));
221 #![doc(html_root_url = "https://docs.rs/bitflags/0.9.1")]
222 // When compiled for the rustc compiler itself we want to make sure that this is
223 // an unstable crate.
224 #![cfg_attr(rustbuild, feature(staged_api))]
225 #![cfg_attr(rustbuild, unstable(feature = "rustc_private", issue = "27812"))]
231 // Re-export libstd/libcore using an alias so that the macros can work in no_std
232 // crates while remaining compatible with normal crates.
234 pub extern crate core
as _core
;
236 /// The macro used to generate the flag structure.
238 /// See the [crate level docs](../bitflags/index.html) for complete documentation.
244 /// extern crate bitflags;
247 /// struct Flags: u32 {
248 /// const FLAG_A = 0b00000001;
249 /// const FLAG_B = 0b00000010;
250 /// const FLAG_C = 0b00000100;
251 /// const FLAG_ABC = FLAG_A.bits
258 /// let e1 = FLAG_A | FLAG_C;
259 /// let e2 = FLAG_B | FLAG_C;
260 /// assert_eq!((e1 | e2), FLAG_ABC); // union
261 /// assert_eq!((e1 & e2), FLAG_C); // intersection
262 /// assert_eq!((e1 - e2), FLAG_A); // set difference
263 /// assert_eq!(!e2, FLAG_A); // set complement
267 /// The generated `struct`s can also be extended with type and trait
272 /// extern crate bitflags;
277 /// struct Flags: u32 {
278 /// const FLAG_A = 0b00000001;
279 /// const FLAG_B = 0b00000010;
284 /// pub fn clear(&mut self) {
285 /// self.bits = 0; // The `bits` field can be accessed from within the
286 /// // same module where the `bitflags!` macro was invoked.
290 /// impl fmt::Display for Flags {
291 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
297 /// let mut flags = FLAG_A | FLAG_B;
299 /// assert!(flags.is_empty());
300 /// assert_eq!(format!("{}", flags), "hi!");
301 /// assert_eq!(format!("{:?}", FLAG_A | FLAG_B), "FLAG_A | FLAG_B");
302 /// assert_eq!(format!("{:?}", FLAG_B), "FLAG_B");
306 macro_rules
! bitflags
{
307 ($
(#[$attr:meta])* pub struct $BitFlags:ident: $T:ty {
308 $
($
(#[$Flag_attr:meta])* const $Flag:ident = $value:expr;)+
310 #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
312 pub struct $BitFlags
{
316 $
($
(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+
319 struct $BitFlags
: $T
{
320 $
($
(#[$Flag_attr])* const $Flag = $value;)+
324 ($
(#[$attr:meta])* struct $BitFlags:ident: $T:ty {
325 $
($
(#[$Flag_attr:meta])* const $Flag:ident = $value:expr;)+
327 #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
333 $
($
(#[$Flag_attr])* const $Flag: $BitFlags = $BitFlags { bits: $value };)+
336 struct $BitFlags
: $T
{
337 $
($
(#[$Flag_attr])* const $Flag = $value;)+
346 macro_rules
! __impl_bitflags
{
347 (struct $BitFlags
:ident
: $T
:ty
{
348 $
($
(#[$Flag_attr:meta])* const $Flag:ident = $value:expr;)+
350 impl $
crate::_core
::fmt
::Debug
for $BitFlags
{
351 fn fmt(&self, f
: &mut $
crate::_core
::fmt
::Formatter
) -> $
crate::_core
::fmt
::Result
{
352 // This convoluted approach is to handle #[cfg]-based flag
353 // omission correctly. For example it needs to support:
355 // #[cfg(unix)] const A: Flag = /* ... */;
356 // #[cfg(windows)] const B: Flag = /* ... */;
358 // Unconditionally define a check for every flag, even disabled
360 #[allow(non_snake_case)]
363 fn $
Flag(&self) -> bool { false }
367 // Conditionally override the check for just those flags that
368 // are not #[cfg]ed away.
369 impl __BitFlags
for $BitFlags
{
372 fn $
Flag(&self) -> bool
{
373 self.bits
& $Flag
.bits
== $Flag
.bits
378 let mut first
= true;
380 if <$BitFlags
as __BitFlags
>::$
Flag(self) {
382 try
!(f
.write_str(" | "));
385 try
!(f
.write_str(stringify
!($Flag
)));
389 try
!(f
.write_str("(empty)"));
394 impl $
crate::_core
::fmt
::Binary
for $BitFlags
{
395 fn fmt(&self, f
: &mut $
crate::_core
::fmt
::Formatter
) -> $
crate::_core
::fmt
::Result
{
396 $
crate::_core
::fmt
::Binary
::fmt(&self.bits
, f
)
399 impl $
crate::_core
::fmt
::Octal
for $BitFlags
{
400 fn fmt(&self, f
: &mut $
crate::_core
::fmt
::Formatter
) -> $
crate::_core
::fmt
::Result
{
401 $
crate::_core
::fmt
::Octal
::fmt(&self.bits
, f
)
404 impl $
crate::_core
::fmt
::LowerHex
for $BitFlags
{
405 fn fmt(&self, f
: &mut $
crate::_core
::fmt
::Formatter
) -> $
crate::_core
::fmt
::Result
{
406 $
crate::_core
::fmt
::LowerHex
::fmt(&self.bits
, f
)
409 impl $
crate::_core
::fmt
::UpperHex
for $BitFlags
{
410 fn fmt(&self, f
: &mut $
crate::_core
::fmt
::Formatter
) -> $
crate::_core
::fmt
::Result
{
411 $
crate::_core
::fmt
::UpperHex
::fmt(&self.bits
, f
)
417 /// Returns an empty set of flags.
419 pub fn empty() -> $BitFlags
{
420 $BitFlags { bits: 0 }
423 /// Returns the set containing all flags.
425 pub fn all() -> $BitFlags
{
426 // See `Debug::fmt` for why this approach is taken.
427 #[allow(non_snake_case)]
430 fn $
Flag() -> $T { 0 }
433 impl __BitFlags
for $BitFlags
{
436 fn $
Flag() -> $T { $Flag.bits }
439 $BitFlags { bits: $(<$BitFlags as __BitFlags>::$Flag())|+ }
442 /// Returns the raw value of the flags currently stored.
444 pub fn bits(&self) -> $T
{
448 /// Convert from underlying bit representation, unless that
449 /// representation contains bits that do not correspond to a flag.
451 pub fn from_bits(bits
: $T
) -> $
crate::_core
::option
::Option
<$BitFlags
> {
452 if (bits
& !$BitFlags
::all().bits()) == 0 {
453 $
crate::_core
::option
::Option
::Some($BitFlags { bits: bits }
)
455 $
crate::_core
::option
::Option
::None
459 /// Convert from underlying bit representation, dropping any bits
460 /// that do not correspond to flags.
462 pub fn from_bits_truncate(bits
: $T
) -> $BitFlags
{
463 $BitFlags { bits: bits }
& $BitFlags
::all()
466 /// Returns `true` if no flags are currently stored.
468 pub fn is_empty(&self) -> bool
{
469 *self == $BitFlags
::empty()
472 /// Returns `true` if all flags are currently set.
474 pub fn is_all(&self) -> bool
{
475 *self == $BitFlags
::all()
478 /// Returns `true` if there are flags common to both `self` and `other`.
480 pub fn intersects(&self, other
: $BitFlags
) -> bool
{
481 !(*self & other
).is_empty()
484 /// Returns `true` all of the flags in `other` are contained within `self`.
486 pub fn contains(&self, other
: $BitFlags
) -> bool
{
487 (*self & other
) == other
490 /// Inserts the specified flags in-place.
492 pub fn insert(&mut self, other
: $BitFlags
) {
493 self.bits
|= other
.bits
;
496 /// Removes the specified flags in-place.
498 pub fn remove(&mut self, other
: $BitFlags
) {
499 self.bits
&= !other
.bits
;
502 /// Toggles the specified flags in-place.
504 pub fn toggle(&mut self, other
: $BitFlags
) {
505 self.bits ^
= other
.bits
;
508 /// Inserts or removes the specified flags depending on the passed value.
510 pub fn set(&mut self, other
: $BitFlags
, value
: bool
) {
519 impl $
crate::_core
::ops
::BitOr
for $BitFlags
{
520 type Output
= $BitFlags
;
522 /// Returns the union of the two sets of flags.
524 fn bitor(self, other
: $BitFlags
) -> $BitFlags
{
525 $BitFlags { bits: self.bits | other.bits }
529 impl $
crate::_core
::ops
::BitOrAssign
for $BitFlags
{
531 /// Adds the set of flags.
533 fn bitor_assign(&mut self, other
: $BitFlags
) {
534 self.bits
|= other
.bits
;
538 impl $
crate::_core
::ops
::BitXor
for $BitFlags
{
539 type Output
= $BitFlags
;
541 /// Returns the left flags, but with all the right flags toggled.
543 fn bitxor(self, other
: $BitFlags
) -> $BitFlags
{
544 $BitFlags { bits: self.bits ^ other.bits }
548 impl $
crate::_core
::ops
::BitXorAssign
for $BitFlags
{
550 /// Toggles the set of flags.
552 fn bitxor_assign(&mut self, other
: $BitFlags
) {
553 self.bits ^
= other
.bits
;
557 impl $
crate::_core
::ops
::BitAnd
for $BitFlags
{
558 type Output
= $BitFlags
;
560 /// Returns the intersection between the two sets of flags.
562 fn bitand(self, other
: $BitFlags
) -> $BitFlags
{
563 $BitFlags { bits: self.bits & other.bits }
567 impl $
crate::_core
::ops
::BitAndAssign
for $BitFlags
{
569 /// Disables all flags disabled in the set.
571 fn bitand_assign(&mut self, other
: $BitFlags
) {
572 self.bits
&= other
.bits
;
576 impl $
crate::_core
::ops
::Sub
for $BitFlags
{
577 type Output
= $BitFlags
;
579 /// Returns the set difference of the two sets of flags.
581 fn sub(self, other
: $BitFlags
) -> $BitFlags
{
582 $BitFlags { bits: self.bits & !other.bits }
586 impl $
crate::_core
::ops
::SubAssign
for $BitFlags
{
588 /// Disables all flags enabled in the set.
590 fn sub_assign(&mut self, other
: $BitFlags
) {
591 self.bits
&= !other
.bits
;
595 impl $
crate::_core
::ops
::Not
for $BitFlags
{
596 type Output
= $BitFlags
;
598 /// Returns the complement of this set of flags.
600 fn not(self) -> $BitFlags
{
601 $BitFlags { bits: !self.bits }
& $BitFlags
::all()
605 impl $
crate::_core
::iter
::Extend
<$BitFlags
> for $BitFlags
{
606 fn extend
<T
: $
crate::_core
::iter
::IntoIterator
<Item
=$BitFlags
>>(&mut self, iterator
: T
) {
607 for item
in iterator
{
613 impl $
crate::_core
::iter
::FromIterator
<$BitFlags
> for $BitFlags
{
614 fn from_iter
<T
: $
crate::_core
::iter
::IntoIterator
<Item
=$BitFlags
>>(iterator
: T
) -> $BitFlags
{
615 let mut result
= Self::empty();
616 result
.extend(iterator
);
623 #[cfg(feature = "example_generated")]
624 pub mod example_generated
;
627 #[allow(non_upper_case_globals, dead_code)]
629 use std
::hash
::{Hash, Hasher}
;
630 use std
::collections
::hash_map
::DefaultHasher
;
633 #[doc = "> The first principle is that you must not fool yourself — and"]
634 #[doc = "> you are the easiest person to fool."]
636 #[doc = "> - Richard Feynman"]
638 const FlagA
= 0b00000001;
639 #[doc = "<pcwalton> macros are way better at generating code than trans is"]
640 const FlagB
= 0b00000010;
641 const FlagC
= 0b00000100;
643 #[doc = "* strcat table"]
644 #[doc = "<strcat> wait what?"]
645 const FlagABC
= FlagA
.bits
652 struct _CfgFlags
: u32 {
658 const _CfgC
= _CfgA
.bits
| 0b10;
663 struct AnotherSetOfFlags
: i8 {
664 const AnotherFlag
= -1_i8;
669 struct LongFlags
: u32 {
670 const LongFlagA
= 0b1111111111111111;
676 assert_eq
!(Flags
::empty().bits(), 0b00000000);
677 assert_eq
!(FlagA
.bits(), 0b00000001);
678 assert_eq
!(FlagABC
.bits(), 0b00000111);
680 assert_eq
!(AnotherSetOfFlags
::empty().bits(), 0b00);
681 assert_eq
!(AnotherFlag
.bits(), !0_i8);
685 fn test_from_bits() {
686 assert_eq
!(Flags
::from_bits(0), Some(Flags
::empty()));
687 assert_eq
!(Flags
::from_bits(0b1), Some(FlagA
));
688 assert_eq
!(Flags
::from_bits(0b10), Some(FlagB
));
689 assert_eq
!(Flags
::from_bits(0b11), Some(FlagA
| FlagB
));
690 assert_eq
!(Flags
::from_bits(0b1000), None
);
692 assert_eq
!(AnotherSetOfFlags
::from_bits(!0_i8), Some(AnotherFlag
));
696 fn test_from_bits_truncate() {
697 assert_eq
!(Flags
::from_bits_truncate(0), Flags
::empty());
698 assert_eq
!(Flags
::from_bits_truncate(0b1), FlagA
);
699 assert_eq
!(Flags
::from_bits_truncate(0b10), FlagB
);
700 assert_eq
!(Flags
::from_bits_truncate(0b11), (FlagA
| FlagB
));
701 assert_eq
!(Flags
::from_bits_truncate(0b1000), Flags
::empty());
702 assert_eq
!(Flags
::from_bits_truncate(0b1001), FlagA
);
704 assert_eq
!(AnotherSetOfFlags
::from_bits_truncate(0_i8), AnotherSetOfFlags
::empty());
709 assert
!(Flags
::empty().is_empty());
710 assert
!(!FlagA
.is_empty());
711 assert
!(!FlagABC
.is_empty());
713 assert
!(!AnotherFlag
.is_empty());
718 assert
!(Flags
::all().is_all());
719 assert
!(!FlagA
.is_all());
720 assert
!(FlagABC
.is_all());
722 assert
!(AnotherFlag
.is_all());
726 fn test_two_empties_do_not_intersect() {
727 let e1
= Flags
::empty();
728 let e2
= Flags
::empty();
729 assert
!(!e1
.intersects(e2
));
731 assert
!(AnotherFlag
.intersects(AnotherFlag
));
735 fn test_empty_does_not_intersect_with_full() {
736 let e1
= Flags
::empty();
738 assert
!(!e1
.intersects(e2
));
742 fn test_disjoint_intersects() {
745 assert
!(!e1
.intersects(e2
));
749 fn test_overlapping_intersects() {
751 let e2
= FlagA
| FlagB
;
752 assert
!(e1
.intersects(e2
));
758 let e2
= FlagA
| FlagB
;
759 assert
!(!e1
.contains(e2
));
760 assert
!(e2
.contains(e1
));
761 assert
!(FlagABC
.contains(e2
));
763 assert
!(AnotherFlag
.contains(AnotherFlag
));
769 let e2
= FlagA
| FlagB
;
773 let mut e3
= AnotherSetOfFlags
::empty();
774 e3
.insert(AnotherFlag
);
775 assert_eq
!(e3
, AnotherFlag
);
780 let mut e1
= FlagA
| FlagB
;
781 let e2
= FlagA
| FlagC
;
783 assert_eq
!(e1
, FlagB
);
785 let mut e3
= AnotherFlag
;
786 e3
.remove(AnotherFlag
);
787 assert_eq
!(e3
, AnotherSetOfFlags
::empty());
791 fn test_operators() {
792 let e1
= FlagA
| FlagC
;
793 let e2
= FlagB
| FlagC
;
794 assert_eq
!((e1
| e2
), FlagABC
); // union
795 assert_eq
!((e1
& e2
), FlagC
); // intersection
796 assert_eq
!((e1
- e2
), FlagA
); // set difference
797 assert_eq
!(!e2
, FlagA
); // set complement
798 assert_eq
!(e1 ^ e2
, FlagA
| FlagB
); // toggle
801 assert_eq
!(e3
, FlagA
| FlagB
);
803 let mut m4
= AnotherSetOfFlags
::empty();
804 m4
.toggle(AnotherSetOfFlags
::empty());
805 assert_eq
!(m4
, AnotherSetOfFlags
::empty());
810 let mut e1
= FlagA
| FlagC
;
812 e1
.set(FlagC
, false);
814 assert_eq
!(e1
, FlagA
| FlagB
);
818 fn test_assignment_operators() {
819 let mut m1
= Flags
::empty();
820 let e1
= FlagA
| FlagC
;
823 assert_eq
!(m1
, FlagA
);
826 assert_eq
!(m1
, FlagA
);
829 assert_eq
!(m1
, Flags
::empty());
839 flags
= Flags
::empty();
840 flags
.extend([].iter().cloned());
841 assert_eq
!(flags
, Flags
::empty());
843 flags
= Flags
::empty();
844 flags
.extend([FlagA
, FlagB
].iter().cloned());
845 assert_eq
!(flags
, FlagA
| FlagB
);
848 flags
.extend([FlagA
, FlagB
].iter().cloned());
849 assert_eq
!(flags
, FlagA
| FlagB
);
852 flags
.extend([FlagA
, FlagABC
].iter().cloned());
853 assert_eq
!(flags
, FlagABC
);
857 fn test_from_iterator() {
858 assert_eq
!([].iter().cloned().collect
::<Flags
>(), Flags
::empty());
859 assert_eq
!([FlagA
, FlagB
].iter().cloned().collect
::<Flags
>(), FlagA
| FlagB
);
860 assert_eq
!([FlagA
, FlagABC
].iter().cloned().collect
::<Flags
>(), FlagABC
);
865 let mut a
= Flags
::empty();
866 let mut b
= Flags
::empty();
868 assert
!(!(a
< b
) && !(b
< a
));
872 assert
!(!(a
< b
) && b
< a
);
879 let mut a
= Flags
::empty();
880 let mut b
= Flags
::empty();
882 assert
!(a
<= b
&& a
>= b
);
884 assert
!(a
> b
&& a
>= b
);
885 assert
!(b
< a
&& b
<= a
);
887 assert
!(b
> a
&& b
>= a
);
888 assert
!(a
< b
&& a
<= b
);
891 fn hash
<T
: Hash
>(t
: &T
) -> u64 {
892 let mut s
= DefaultHasher
::new();
899 let mut x
= Flags
::empty();
900 let mut y
= Flags
::empty();
901 assert_eq
!(hash(&x
), hash(&y
));
904 assert_eq
!(hash(&x
), hash(&y
));
909 assert_eq
!(format
!("{:?}", FlagA
| FlagB
), "FlagA | FlagB");
910 assert_eq
!(format
!("{:?}", Flags
::empty()), "(empty)");
911 assert_eq
!(format
!("{:?}", FlagABC
), "FlagA | FlagB | FlagC | FlagABC");
916 assert_eq
!(format
!("{:b}", FlagABC
), "111");
917 assert_eq
!(format
!("{:#b}", FlagABC
), "0b111");
922 assert_eq
!(format
!("{:o}", LongFlagA
), "177777");
923 assert_eq
!(format
!("{:#o}", LongFlagA
), "0o177777");
928 assert_eq
!(format
!("{:x}", LongFlagA
), "ffff");
929 assert_eq
!(format
!("{:#x}", LongFlagA
), "0xffff");
934 assert_eq
!(format
!("{:X}", LongFlagA
), "FFFF");
935 assert_eq
!(format
!("{:#X}", LongFlagA
), "0xFFFF");
940 pub struct PublicFlags
: i8 {
945 struct PrivateFlags
: i8 {
958 let _
= submodule
::FlagX
;
968 struct Flags
: foo
::Bar
{
969 const A
= 0b00000001;
971 const B
= 0b00000010;
973 const C
= 0b00000010;
979 fn test_in_function() {
983 #[cfg(any())] // false
987 assert_eq
!(Flags
::all(), A
);
988 assert_eq
!(format
!("{:?}", A
), "A");