1#[doc(hidden)]
12pub extern crate core as _core;
13
14#[macro_export(local_inner_macros)]
15macro_rules! bitflags {
16 (
17 $(#[$outer:meta])*
18 $vis:vis struct $BitFlags:ident: $T:ty {
19 $(
20 $(#[$inner:ident $($args:tt)*])*
21 const $Flag:ident = $value:expr;
22 )*
23 }
24
25 $($t:tt)*
26 ) => {
27 $(#[$outer])*
28 #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
29 $vis struct $BitFlags {
30 bits: $T,
31 }
32
33 __impl_bitflags! {
34 $BitFlags: $T {
35 $(
36 $(#[$inner $($args)*])*
37 $Flag = $value;
38 )*
39 }
40 }
41
42 bitflags! {
43 $($t)*
44 }
45 };
46 () => {};
47}
48
49pub use bitflags;
50
51#[macro_export(local_inner_macros)]
53#[doc(hidden)]
54macro_rules! __impl_all_bitflags {
55 (
56 $BitFlags:ident: $T:ty {
57 $(
58 $(#[$attr:ident $($args:tt)*])*
59 $Flag:ident = $value:expr;
60 )+
61 }
62 ) => {
63 #[allow(non_snake_case)]
65 trait __BitFlags {
66 $(
67 const $Flag: $T = 0;
68 )+
69 }
70 #[allow(non_snake_case)]
71 impl __BitFlags for $BitFlags {
72 $(
73 __impl_bitflags! {
74 #[allow(deprecated)]
75 $(? #[$attr $($args)*])*
76 const $Flag: $T = Self::$Flag.bits;
77 }
78 )+
79 }
80 Self { bits: $(<Self as __BitFlags>::$Flag)|+ }
81 };
82 (
83 $BitFlags:ident: $T:ty { }
84 ) => {
85 Self { bits: 0 }
86 };
87}
88
89#[macro_export(local_inner_macros)]
90#[doc(hidden)]
91macro_rules! __impl_bitflags {
92 (
93 $BitFlags:ident: $T:ty {
94 $(
95 $(#[$attr:ident $($args:tt)*])*
96 $Flag:ident = $value:expr;
97 )*
98 }
99 ) => {
100 impl $crate::_core::fmt::Debug for $BitFlags {
101 fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
102 #[allow(non_snake_case)]
111 trait __BitFlags {
112 $(
113 #[inline]
114 fn $Flag(&self) -> bool { false }
115 )*
116 }
117
118 #[allow(non_snake_case)]
121 impl __BitFlags for $BitFlags {
122 $(
123 __impl_bitflags! {
124 #[allow(deprecated)]
125 #[inline]
126 $(? #[$attr $($args)*])*
127 fn $Flag(&self) -> bool {
128 if Self::$Flag.bits == 0 && self.bits != 0 {
129 false
130 } else {
131 self.bits & Self::$Flag.bits == Self::$Flag.bits
132 }
133 }
134 }
135 )*
136 }
137
138 let mut first = true;
139 $(
140 if <Self as __BitFlags>::$Flag(self) {
141 if !first {
142 f.write_str(" | ")?;
143 }
144 first = false;
145 f.write_str($crate::_core::stringify!($Flag))?;
146 }
147 )*
148 let extra_bits = self.bits & !Self::all().bits();
149 if extra_bits != 0 {
150 if !first {
151 f.write_str(" | ")?;
152 }
153 first = false;
154 f.write_str("0x")?;
155 $crate::_core::fmt::LowerHex::fmt(&extra_bits, f)?;
156 }
157 if first {
158 f.write_str("(empty)")?;
159 }
160 Ok(())
161 }
162 }
163 impl $crate::_core::fmt::Binary for $BitFlags {
164 fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
165 $crate::_core::fmt::Binary::fmt(&self.bits, f)
166 }
167 }
168 impl $crate::_core::fmt::Octal for $BitFlags {
169 fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
170 $crate::_core::fmt::Octal::fmt(&self.bits, f)
171 }
172 }
173 impl $crate::_core::fmt::LowerHex for $BitFlags {
174 fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
175 $crate::_core::fmt::LowerHex::fmt(&self.bits, f)
176 }
177 }
178 impl $crate::_core::fmt::UpperHex for $BitFlags {
179 fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
180 $crate::_core::fmt::UpperHex::fmt(&self.bits, f)
181 }
182 }
183
184 #[allow(dead_code)]
185 impl $BitFlags {
186 $(
187 $(#[$attr $($args)*])*
188 pub const $Flag: Self = Self { bits: $value };
189 )*
190
191 #[inline]
193 pub const fn empty() -> Self {
194 Self { bits: 0 }
195 }
196
197 #[inline]
199 pub const fn all() -> Self {
200 __impl_all_bitflags! {
201 $BitFlags: $T {
202 $(
203 $(#[$attr $($args)*])*
204 $Flag = $value;
205 )*
206 }
207 }
208 }
209
210 #[inline]
212 pub const fn bits(&self) -> $T {
213 self.bits
214 }
215
216 #[inline]
219 pub const fn from_bits(bits: $T) -> $crate::_core::option::Option<Self> {
220 if (bits & !Self::all().bits()) == 0 {
221 $crate::_core::option::Option::Some(Self { bits })
222 } else {
223 $crate::_core::option::Option::None
224 }
225 }
226
227 #[inline]
230 pub const fn from_bits_truncate(bits: $T) -> Self {
231 Self { bits: bits & Self::all().bits }
232 }
233
234 #[inline]
246 pub const unsafe fn from_bits_unchecked(bits: $T) -> Self {
247 Self { bits }
248 }
249
250 #[inline]
252 pub const fn is_empty(&self) -> bool {
253 self.bits() == Self::empty().bits()
254 }
255
256 #[inline]
258 pub const fn is_all(&self) -> bool {
259 Self::all().bits | self.bits == self.bits
260 }
261
262 #[inline]
264 pub const fn intersects(&self, other: Self) -> bool {
265 !(Self { bits: self.bits & other.bits}).is_empty()
266 }
267
268 #[inline]
270 pub const fn contains(&self, other: Self) -> bool {
271 (self.bits & other.bits) == other.bits
272 }
273
274 #[inline]
276 pub fn insert(&mut self, other: Self) {
277 self.bits |= other.bits;
278 }
279
280 #[inline]
282 pub fn remove(&mut self, other: Self) {
283 self.bits &= !other.bits;
284 }
285
286 #[inline]
288 pub fn toggle(&mut self, other: Self) {
289 self.bits ^= other.bits;
290 }
291
292 #[inline]
294 pub fn set(&mut self, other: Self, value: bool) {
295 if value {
296 self.insert(other);
297 } else {
298 self.remove(other);
299 }
300 }
301
302 #[inline]
313 #[must_use]
314 pub const fn intersection(self, other: Self) -> Self {
315 Self { bits: self.bits & other.bits }
316 }
317
318 #[inline]
330 #[must_use]
331 pub const fn union(self, other: Self) -> Self {
332 Self { bits: self.bits | other.bits }
333 }
334
335 #[inline]
348 #[must_use]
349 pub const fn difference(self, other: Self) -> Self {
350 Self { bits: self.bits & !other.bits }
351 }
352
353 #[inline]
367 #[must_use]
368 pub const fn symmetric_difference(self, other: Self) -> Self {
369 Self { bits: self.bits ^ other.bits }
370 }
371
372 #[inline]
386 #[must_use]
387 pub const fn complement(self) -> Self {
388 Self::from_bits_truncate(!self.bits)
389 }
390
391 }
392
393 impl $crate::_core::ops::BitOr for $BitFlags {
394 type Output = Self;
395
396 #[inline]
398 fn bitor(self, other: $BitFlags) -> Self {
399 Self { bits: self.bits | other.bits }
400 }
401 }
402
403 impl $crate::_core::ops::BitOrAssign for $BitFlags {
404 #[inline]
406 fn bitor_assign(&mut self, other: Self) {
407 self.bits |= other.bits;
408 }
409 }
410
411 impl $crate::_core::ops::BitXor for $BitFlags {
412 type Output = Self;
413
414 #[inline]
416 fn bitxor(self, other: Self) -> Self {
417 Self { bits: self.bits ^ other.bits }
418 }
419 }
420
421 impl $crate::_core::ops::BitXorAssign for $BitFlags {
422 #[inline]
424 fn bitxor_assign(&mut self, other: Self) {
425 self.bits ^= other.bits;
426 }
427 }
428
429 impl $crate::_core::ops::BitAnd for $BitFlags {
430 type Output = Self;
431
432 #[inline]
434 fn bitand(self, other: Self) -> Self {
435 Self { bits: self.bits & other.bits }
436 }
437 }
438
439 impl $crate::_core::ops::BitAndAssign for $BitFlags {
440 #[inline]
442 fn bitand_assign(&mut self, other: Self) {
443 self.bits &= other.bits;
444 }
445 }
446
447 impl $crate::_core::ops::Sub for $BitFlags {
448 type Output = Self;
449
450 #[inline]
452 fn sub(self, other: Self) -> Self {
453 Self { bits: self.bits & !other.bits }
454 }
455 }
456
457 impl $crate::_core::ops::SubAssign for $BitFlags {
458 #[inline]
460 fn sub_assign(&mut self, other: Self) {
461 self.bits &= !other.bits;
462 }
463 }
464
465 impl $crate::_core::ops::Not for $BitFlags {
466 type Output = Self;
467
468 #[inline]
470 fn not(self) -> Self {
471 Self { bits: !self.bits } & Self::all()
472 }
473 }
474
475 impl $crate::_core::iter::Extend<$BitFlags> for $BitFlags {
476 fn extend<T: $crate::_core::iter::IntoIterator<Item=Self>>(&mut self, iterator: T) {
477 for item in iterator {
478 self.insert(item)
479 }
480 }
481 }
482
483 impl $crate::_core::iter::FromIterator<$BitFlags> for $BitFlags {
484 fn from_iter<T: $crate::_core::iter::IntoIterator<Item=Self>>(iterator: T) -> Self {
485 let mut result = Self::empty();
486 result.extend(iterator);
487 result
488 }
489 }
490 };
491
492 (
511 $(#[$filtered:meta])*
512 ? #[cfg $($cfgargs:tt)*]
513 $(? #[$rest:ident $($restargs:tt)*])*
514 fn $($item:tt)*
515 ) => {
516 __impl_bitflags! {
517 $(#[$filtered])*
518 #[cfg $($cfgargs)*]
519 $(? #[$rest $($restargs)*])*
520 fn $($item)*
521 }
522 };
523 (
524 $(#[$filtered:meta])*
525 ? #[$next:ident $($nextargs:tt)*]
527 $(? #[$rest:ident $($restargs:tt)*])*
528 fn $($item:tt)*
529 ) => {
530 __impl_bitflags! {
531 $(#[$filtered])*
532 $(? #[$rest $($restargs)*])*
534 fn $($item)*
535 }
536 };
537 (
538 $(#[$filtered:meta])*
539 fn $($item:tt)*
540 ) => {
541 $(#[$filtered])*
542 fn $($item)*
543 };
544
545 (
566 $(#[$filtered:meta])*
567 ? #[cfg $($cfgargs:tt)*]
568 $(? #[$rest:ident $($restargs:tt)*])*
569 const $($item:tt)*
570 ) => {
571 __impl_bitflags! {
572 $(#[$filtered])*
573 #[cfg $($cfgargs)*]
574 $(? #[$rest $($restargs)*])*
575 const $($item)*
576 }
577 };
578 (
579 $(#[$filtered:meta])*
580 ? #[$next:ident $($nextargs:tt)*]
582 $(? #[$rest:ident $($restargs:tt)*])*
583 const $($item:tt)*
584 ) => {
585 __impl_bitflags! {
586 $(#[$filtered])*
587 $(? #[$rest $($restargs)*])*
589 const $($item)*
590 }
591 };
592 (
593 $(#[$filtered:meta])*
594 const $($item:tt)*
595 ) => {
596 $(#[$filtered])*
597 const $($item)*
598 };
599}
600
601#[cfg(feature = "example_generated")]
602pub mod example_generated;
603
604#[cfg(test)]
605mod tests {
606 use std::collections::hash_map::DefaultHasher;
607 use std::hash::{Hash, Hasher};
608
609 bitflags! {
610 #[doc = "> The first principle is that you must not fool yourself — and"]
611 #[doc = "> you are the easiest person to fool."]
612 #[doc = "> "]
613 #[doc = "> - Richard Feynman"]
614 #[derive(Default)]
615 struct Flags: u32 {
616 const A = 0b00000001;
617 #[doc = "<pcwalton> macros are way better at generating code than trans is"]
618 const B = 0b00000010;
619 const C = 0b00000100;
620 #[doc = "* cmr bed"]
621 #[doc = "* strcat table"]
622 #[doc = "<strcat> wait what?"]
623 const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
624 }
625
626 struct _CfgFlags: u32 {
627 #[cfg(unix)]
628 const _CFG_A = 0b01;
629 #[cfg(windows)]
630 const _CFG_B = 0b01;
631 #[cfg(unix)]
632 const _CFG_C = Self::_CFG_A.bits | 0b10;
633 }
634
635 struct AnotherSetOfFlags: i8 {
636 const ANOTHER_FLAG = -1_i8;
637 }
638
639 struct LongFlags: u32 {
640 const LONG_A = 0b1111111111111111;
641 }
642 }
643
644 bitflags! {
645 struct EmptyFlags: u32 {
646 }
647 }
648
649 #[test]
650 fn test_bits() {
651 assert_eq!(Flags::empty().bits(), 0b00000000);
652 assert_eq!(Flags::A.bits(), 0b00000001);
653 assert_eq!(Flags::ABC.bits(), 0b00000111);
654
655 assert_eq!(AnotherSetOfFlags::empty().bits(), 0b00);
656 assert_eq!(AnotherSetOfFlags::ANOTHER_FLAG.bits(), !0_i8);
657
658 assert_eq!(EmptyFlags::empty().bits(), 0b00000000);
659 }
660
661 #[test]
662 fn test_from_bits() {
663 assert_eq!(Flags::from_bits(0), Some(Flags::empty()));
664 assert_eq!(Flags::from_bits(0b1), Some(Flags::A));
665 assert_eq!(Flags::from_bits(0b10), Some(Flags::B));
666 assert_eq!(Flags::from_bits(0b11), Some(Flags::A | Flags::B));
667 assert_eq!(Flags::from_bits(0b1000), None);
668
669 assert_eq!(
670 AnotherSetOfFlags::from_bits(!0_i8),
671 Some(AnotherSetOfFlags::ANOTHER_FLAG)
672 );
673
674 assert_eq!(EmptyFlags::from_bits(0), Some(EmptyFlags::empty()));
675 assert_eq!(EmptyFlags::from_bits(0b1), None);
676 }
677
678 #[test]
679 fn test_from_bits_truncate() {
680 assert_eq!(Flags::from_bits_truncate(0), Flags::empty());
681 assert_eq!(Flags::from_bits_truncate(0b1), Flags::A);
682 assert_eq!(Flags::from_bits_truncate(0b10), Flags::B);
683 assert_eq!(Flags::from_bits_truncate(0b11), (Flags::A | Flags::B));
684 assert_eq!(Flags::from_bits_truncate(0b1000), Flags::empty());
685 assert_eq!(Flags::from_bits_truncate(0b1001), Flags::A);
686
687 assert_eq!(
688 AnotherSetOfFlags::from_bits_truncate(0_i8),
689 AnotherSetOfFlags::empty()
690 );
691
692 assert_eq!(EmptyFlags::from_bits_truncate(0), EmptyFlags::empty());
693 assert_eq!(EmptyFlags::from_bits_truncate(0b1), EmptyFlags::empty());
694 }
695
696 #[test]
697 fn test_from_bits_unchecked() {
698 let extra = unsafe { Flags::from_bits_unchecked(0b1000) };
699 assert_eq!(unsafe { Flags::from_bits_unchecked(0) }, Flags::empty());
700 assert_eq!(unsafe { Flags::from_bits_unchecked(0b1) }, Flags::A);
701 assert_eq!(unsafe { Flags::from_bits_unchecked(0b10) }, Flags::B);
702
703 assert_eq!(
704 unsafe { Flags::from_bits_unchecked(0b11) },
705 (Flags::A | Flags::B)
706 );
707 assert_eq!(
708 unsafe { Flags::from_bits_unchecked(0b1000) },
709 (extra | Flags::empty())
710 );
711 assert_eq!(
712 unsafe { Flags::from_bits_unchecked(0b1001) },
713 (extra | Flags::A)
714 );
715
716 let extra = unsafe { EmptyFlags::from_bits_unchecked(0b1000) };
717 assert_eq!(
718 unsafe { EmptyFlags::from_bits_unchecked(0b1000) },
719 (extra | EmptyFlags::empty())
720 );
721 }
722
723 #[test]
724 fn test_is_empty() {
725 assert!(Flags::empty().is_empty());
726 assert!(!Flags::A.is_empty());
727 assert!(!Flags::ABC.is_empty());
728
729 assert!(!AnotherSetOfFlags::ANOTHER_FLAG.is_empty());
730
731 assert!(EmptyFlags::empty().is_empty());
732 assert!(EmptyFlags::all().is_empty());
733 }
734
735 #[test]
736 fn test_is_all() {
737 assert!(Flags::all().is_all());
738 assert!(!Flags::A.is_all());
739 assert!(Flags::ABC.is_all());
740
741 let extra = unsafe { Flags::from_bits_unchecked(0b1000) };
742 assert!(!extra.is_all());
743 assert!(!(Flags::A | extra).is_all());
744 assert!((Flags::ABC | extra).is_all());
745
746 assert!(AnotherSetOfFlags::ANOTHER_FLAG.is_all());
747
748 assert!(EmptyFlags::all().is_all());
749 assert!(EmptyFlags::empty().is_all());
750 }
751
752 #[test]
753 fn test_two_empties_do_not_intersect() {
754 let e1 = Flags::empty();
755 let e2 = Flags::empty();
756 assert!(!e1.intersects(e2));
757
758 assert!(AnotherSetOfFlags::ANOTHER_FLAG.intersects(AnotherSetOfFlags::ANOTHER_FLAG));
759 }
760
761 #[test]
762 fn test_empty_does_not_intersect_with_full() {
763 let e1 = Flags::empty();
764 let e2 = Flags::ABC;
765 assert!(!e1.intersects(e2));
766 }
767
768 #[test]
769 fn test_disjoint_intersects() {
770 let e1 = Flags::A;
771 let e2 = Flags::B;
772 assert!(!e1.intersects(e2));
773 }
774
775 #[test]
776 fn test_overlapping_intersects() {
777 let e1 = Flags::A;
778 let e2 = Flags::A | Flags::B;
779 assert!(e1.intersects(e2));
780 }
781
782 #[test]
783 fn test_contains() {
784 let e1 = Flags::A;
785 let e2 = Flags::A | Flags::B;
786 assert!(!e1.contains(e2));
787 assert!(e2.contains(e1));
788 assert!(Flags::ABC.contains(e2));
789
790 assert!(AnotherSetOfFlags::ANOTHER_FLAG.contains(AnotherSetOfFlags::ANOTHER_FLAG));
791
792 assert!(EmptyFlags::empty().contains(EmptyFlags::empty()));
793 }
794
795 #[test]
796 fn test_insert() {
797 let mut e1 = Flags::A;
798 let e2 = Flags::A | Flags::B;
799 e1.insert(e2);
800 assert_eq!(e1, e2);
801
802 let mut e3 = AnotherSetOfFlags::empty();
803 e3.insert(AnotherSetOfFlags::ANOTHER_FLAG);
804 assert_eq!(e3, AnotherSetOfFlags::ANOTHER_FLAG);
805 }
806
807 #[test]
808 fn test_remove() {
809 let mut e1 = Flags::A | Flags::B;
810 let e2 = Flags::A | Flags::C;
811 e1.remove(e2);
812 assert_eq!(e1, Flags::B);
813
814 let mut e3 = AnotherSetOfFlags::ANOTHER_FLAG;
815 e3.remove(AnotherSetOfFlags::ANOTHER_FLAG);
816 assert_eq!(e3, AnotherSetOfFlags::empty());
817 }
818
819 #[test]
820 fn test_operators() {
821 let e1 = Flags::A | Flags::C;
822 let e2 = Flags::B | Flags::C;
823 assert_eq!((e1 | e2), Flags::ABC); assert_eq!((e1 & e2), Flags::C); assert_eq!((e1 - e2), Flags::A); assert_eq!(!e2, Flags::A); assert_eq!(e1 ^ e2, Flags::A | Flags::B); let mut e3 = e1;
829 e3.toggle(e2);
830 assert_eq!(e3, Flags::A | Flags::B);
831
832 let mut m4 = AnotherSetOfFlags::empty();
833 m4.toggle(AnotherSetOfFlags::empty());
834 assert_eq!(m4, AnotherSetOfFlags::empty());
835 }
836
837 #[test]
838 fn test_operators_unchecked() {
839 let extra = unsafe { Flags::from_bits_unchecked(0b1000) };
840 let e1 = Flags::A | Flags::C | extra;
841 let e2 = Flags::B | Flags::C;
842 assert_eq!((e1 | e2), (Flags::ABC | extra)); assert_eq!((e1 & e2), Flags::C); assert_eq!((e1 - e2), (Flags::A | extra)); assert_eq!(!e2, Flags::A); assert_eq!(!e1, Flags::B); assert_eq!(e1 ^ e2, Flags::A | Flags::B | extra); let mut e3 = e1;
849 e3.toggle(e2);
850 assert_eq!(e3, Flags::A | Flags::B | extra);
851 }
852
853 #[test]
854 fn test_set_ops_basic() {
855 let ab = Flags::A.union(Flags::B);
856 let ac = Flags::A.union(Flags::C);
857 let bc = Flags::B.union(Flags::C);
858 assert_eq!(ab.bits, 0b011);
859 assert_eq!(bc.bits, 0b110);
860 assert_eq!(ac.bits, 0b101);
861
862 assert_eq!(ab, Flags::B.union(Flags::A));
863 assert_eq!(ac, Flags::C.union(Flags::A));
864 assert_eq!(bc, Flags::C.union(Flags::B));
865
866 assert_eq!(ac, Flags::A | Flags::C);
867 assert_eq!(bc, Flags::B | Flags::C);
868 assert_eq!(ab.union(bc), Flags::ABC);
869
870 assert_eq!(ac, Flags::A | Flags::C);
871 assert_eq!(bc, Flags::B | Flags::C);
872
873 assert_eq!(ac.union(bc), ac | bc);
874 assert_eq!(ac.union(bc), Flags::ABC);
875 assert_eq!(bc.union(ac), Flags::ABC);
876
877 assert_eq!(ac.intersection(bc), ac & bc);
878 assert_eq!(ac.intersection(bc), Flags::C);
879 assert_eq!(bc.intersection(ac), Flags::C);
880
881 assert_eq!(ac.difference(bc), ac - bc);
882 assert_eq!(bc.difference(ac), bc - ac);
883 assert_eq!(ac.difference(bc), Flags::A);
884 assert_eq!(bc.difference(ac), Flags::B);
885
886 assert_eq!(bc.complement(), !bc);
887 assert_eq!(bc.complement(), Flags::A);
888 assert_eq!(ac.symmetric_difference(bc), Flags::A.union(Flags::B));
889 assert_eq!(bc.symmetric_difference(ac), Flags::A.union(Flags::B));
890 }
891
892 #[test]
893 fn test_set_ops_const() {
894 const INTERSECT: Flags = Flags::all().intersection(Flags::C);
897 const UNION: Flags = Flags::A.union(Flags::C);
898 const DIFFERENCE: Flags = Flags::all().difference(Flags::A);
899 const COMPLEMENT: Flags = Flags::C.complement();
900 const SYM_DIFFERENCE: Flags = UNION.symmetric_difference(DIFFERENCE);
901 assert_eq!(INTERSECT, Flags::C);
902 assert_eq!(UNION, Flags::A | Flags::C);
903 assert_eq!(DIFFERENCE, Flags::all() - Flags::A);
904 assert_eq!(COMPLEMENT, !Flags::C);
905 assert_eq!(
906 SYM_DIFFERENCE,
907 (Flags::A | Flags::C) ^ (Flags::all() - Flags::A)
908 );
909 }
910
911 #[test]
912 fn test_set_ops_unchecked() {
913 let extra = unsafe { Flags::from_bits_unchecked(0b1000) };
914 let e1 = Flags::A.union(Flags::C).union(extra);
915 let e2 = Flags::B.union(Flags::C);
916 assert_eq!(e1.bits, 0b1101);
917 assert_eq!(e1.union(e2), (Flags::ABC | extra));
918 assert_eq!(e1.intersection(e2), Flags::C);
919 assert_eq!(e1.difference(e2), Flags::A | extra);
920 assert_eq!(e2.difference(e1), Flags::B);
921 assert_eq!(e2.complement(), Flags::A);
922 assert_eq!(e1.complement(), Flags::B);
923 assert_eq!(e1.symmetric_difference(e2), Flags::A | Flags::B | extra); }
925
926 #[test]
927 fn test_set_ops_exhaustive() {
928 bitflags! {
935 struct Test: u16 {
936 const B = 0b000000010;
938 const D = 0b000001000;
940 const E = 0b000010000;
941 const F = 0b000100000;
942 const G = 0b001000000;
943 const I = 0b100000000;
945 }
946 }
947 let iter_test_flags =
948 || (0..=0b111_1111_1111).map(|bits| unsafe { Test::from_bits_unchecked(bits) });
949
950 for a in iter_test_flags() {
951 assert_eq!(
952 a.complement(),
953 Test::from_bits_truncate(!a.bits),
954 "wrong result: !({:?})",
955 a,
956 );
957 assert_eq!(a.complement(), !a, "named != op: !({:?})", a);
958 for b in iter_test_flags() {
959 assert_eq!(
962 a.union(b).bits,
963 a.bits | b.bits,
964 "wrong result: `{:?}` | `{:?}`",
965 a,
966 b,
967 );
968 assert_eq!(
969 a.intersection(b).bits,
970 a.bits & b.bits,
971 "wrong result: `{:?}` & `{:?}`",
972 a,
973 b,
974 );
975 assert_eq!(
976 a.symmetric_difference(b).bits,
977 a.bits ^ b.bits,
978 "wrong result: `{:?}` ^ `{:?}`",
979 a,
980 b,
981 );
982 assert_eq!(
983 a.difference(b).bits,
984 a.bits & !b.bits,
985 "wrong result: `{:?}` - `{:?}`",
986 a,
987 b,
988 );
989 assert_eq!(
991 b.difference(a).bits,
992 b.bits & !a.bits,
993 "wrong result: `{:?}` - `{:?}`",
994 b,
995 a,
996 );
997 assert_eq!(a.union(b), a | b, "named != op: `{:?}` | `{:?}`", a, b,);
1000 assert_eq!(
1001 a.intersection(b),
1002 a & b,
1003 "named != op: `{:?}` & `{:?}`",
1004 a,
1005 b,
1006 );
1007 assert_eq!(
1008 a.symmetric_difference(b),
1009 a ^ b,
1010 "named != op: `{:?}` ^ `{:?}`",
1011 a,
1012 b,
1013 );
1014 assert_eq!(a.difference(b), a - b, "named != op: `{:?}` - `{:?}`", a, b,);
1015 assert_eq!(b.difference(a), b - a, "named != op: `{:?}` - `{:?}`", b, a,);
1017 assert_eq!(a.union(b), b.union(a), "asymmetry: `{:?}` | `{:?}`", a, b,);
1020 assert_eq!(
1021 a.intersection(b),
1022 b.intersection(a),
1023 "asymmetry: `{:?}` & `{:?}`",
1024 a,
1025 b,
1026 );
1027 assert_eq!(
1028 a.symmetric_difference(b),
1029 b.symmetric_difference(a),
1030 "asymmetry: `{:?}` ^ `{:?}`",
1031 a,
1032 b,
1033 );
1034 }
1035 }
1036 }
1037
1038 #[test]
1039 fn test_set() {
1040 let mut e1 = Flags::A | Flags::C;
1041 e1.set(Flags::B, true);
1042 e1.set(Flags::C, false);
1043
1044 assert_eq!(e1, Flags::A | Flags::B);
1045 }
1046
1047 #[test]
1048 fn test_assignment_operators() {
1049 let mut m1 = Flags::empty();
1050 let e1 = Flags::A | Flags::C;
1051 m1 |= Flags::A;
1053 assert_eq!(m1, Flags::A);
1054 m1 &= e1;
1056 assert_eq!(m1, Flags::A);
1057 m1 -= m1;
1059 assert_eq!(m1, Flags::empty());
1060 m1 ^= e1;
1062 assert_eq!(m1, e1);
1063 }
1064
1065 #[test]
1066 fn test_const_fn() {
1067 const _M1: Flags = Flags::empty();
1068
1069 const M2: Flags = Flags::A;
1070 assert_eq!(M2, Flags::A);
1071
1072 const M3: Flags = Flags::C;
1073 assert_eq!(M3, Flags::C);
1074 }
1075
1076 #[test]
1077 fn test_extend() {
1078 let mut flags;
1079
1080 flags = Flags::empty();
1081 flags.extend([].iter().cloned());
1082 assert_eq!(flags, Flags::empty());
1083
1084 flags = Flags::empty();
1085 flags.extend([Flags::A, Flags::B].iter().cloned());
1086 assert_eq!(flags, Flags::A | Flags::B);
1087
1088 flags = Flags::A;
1089 flags.extend([Flags::A, Flags::B].iter().cloned());
1090 assert_eq!(flags, Flags::A | Flags::B);
1091
1092 flags = Flags::B;
1093 flags.extend([Flags::A, Flags::ABC].iter().cloned());
1094 assert_eq!(flags, Flags::ABC);
1095 }
1096
1097 #[test]
1098 fn test_from_iterator() {
1099 assert_eq!([].iter().cloned().collect::<Flags>(), Flags::empty());
1100 assert_eq!(
1101 [Flags::A, Flags::B].iter().cloned().collect::<Flags>(),
1102 Flags::A | Flags::B
1103 );
1104 assert_eq!(
1105 [Flags::A, Flags::ABC].iter().cloned().collect::<Flags>(),
1106 Flags::ABC
1107 );
1108 }
1109
1110 #[test]
1111 fn test_lt() {
1112 let mut a = Flags::empty();
1113 let mut b = Flags::empty();
1114
1115 assert!(!(a < b) && !(b < a));
1116 b = Flags::B;
1117 assert!(a < b);
1118 a = Flags::C;
1119 assert!(!(a < b) && b < a);
1120 b = Flags::C | Flags::B;
1121 assert!(a < b);
1122 }
1123
1124 #[test]
1125 fn test_ord() {
1126 let mut a = Flags::empty();
1127 let mut b = Flags::empty();
1128
1129 assert!(a <= b && a >= b);
1130 a = Flags::A;
1131 assert!(a > b && a >= b);
1132 assert!(b < a && b <= a);
1133 b = Flags::B;
1134 assert!(b > a && b >= a);
1135 assert!(a < b && a <= b);
1136 }
1137
1138 fn hash<T: Hash>(t: &T) -> u64 {
1139 let mut s = DefaultHasher::new();
1140 t.hash(&mut s);
1141 s.finish()
1142 }
1143
1144 #[test]
1145 fn test_hash() {
1146 let mut x = Flags::empty();
1147 let mut y = Flags::empty();
1148 assert_eq!(hash(&x), hash(&y));
1149 x = Flags::all();
1150 y = Flags::ABC;
1151 assert_eq!(hash(&x), hash(&y));
1152 }
1153
1154 #[test]
1155 fn test_default() {
1156 assert_eq!(Flags::empty(), Flags::default());
1157 }
1158
1159 #[test]
1160 fn test_debug() {
1161 assert_eq!(format!("{:?}", Flags::A | Flags::B), "A | B");
1162 assert_eq!(format!("{:?}", Flags::empty()), "(empty)");
1163 assert_eq!(format!("{:?}", Flags::ABC), "A | B | C | ABC");
1164 let extra = unsafe { Flags::from_bits_unchecked(0xb8) };
1165 assert_eq!(format!("{:?}", extra), "0xb8");
1166 assert_eq!(format!("{:?}", Flags::A | extra), "A | 0xb8");
1167
1168 assert_eq!(
1169 format!("{:?}", Flags::ABC | extra),
1170 "A | B | C | ABC | 0xb8"
1171 );
1172
1173 assert_eq!(format!("{:?}", EmptyFlags::empty()), "(empty)");
1174 }
1175
1176 #[test]
1177 fn test_binary() {
1178 assert_eq!(format!("{:b}", Flags::ABC), "111");
1179 assert_eq!(format!("{:#b}", Flags::ABC), "0b111");
1180 let extra = unsafe { Flags::from_bits_unchecked(0b1010000) };
1181 assert_eq!(format!("{:b}", Flags::ABC | extra), "1010111");
1182 assert_eq!(format!("{:#b}", Flags::ABC | extra), "0b1010111");
1183 }
1184
1185 #[test]
1186 fn test_octal() {
1187 assert_eq!(format!("{:o}", LongFlags::LONG_A), "177777");
1188 assert_eq!(format!("{:#o}", LongFlags::LONG_A), "0o177777");
1189 let extra = unsafe { LongFlags::from_bits_unchecked(0o5000000) };
1190 assert_eq!(format!("{:o}", LongFlags::LONG_A | extra), "5177777");
1191 assert_eq!(format!("{:#o}", LongFlags::LONG_A | extra), "0o5177777");
1192 }
1193
1194 #[test]
1195 fn test_lowerhex() {
1196 assert_eq!(format!("{:x}", LongFlags::LONG_A), "ffff");
1197 assert_eq!(format!("{:#x}", LongFlags::LONG_A), "0xffff");
1198 let extra = unsafe { LongFlags::from_bits_unchecked(0xe00000) };
1199 assert_eq!(format!("{:x}", LongFlags::LONG_A | extra), "e0ffff");
1200 assert_eq!(format!("{:#x}", LongFlags::LONG_A | extra), "0xe0ffff");
1201 }
1202
1203 #[test]
1204 fn test_upperhex() {
1205 assert_eq!(format!("{:X}", LongFlags::LONG_A), "FFFF");
1206 assert_eq!(format!("{:#X}", LongFlags::LONG_A), "0xFFFF");
1207 let extra = unsafe { LongFlags::from_bits_unchecked(0xe00000) };
1208 assert_eq!(format!("{:X}", LongFlags::LONG_A | extra), "E0FFFF");
1209 assert_eq!(format!("{:#X}", LongFlags::LONG_A | extra), "0xE0FFFF");
1210 }
1211
1212 mod submodule {
1213 bitflags! {
1214 pub struct PublicFlags: i8 {
1215 const X = 0;
1216 }
1217
1218 struct PrivateFlags: i8 {
1219 const Y = 0;
1220 }
1221 }
1222
1223 #[test]
1224 fn test_private() {
1225 let _ = PrivateFlags::Y;
1226 }
1227 }
1228
1229 #[test]
1230 fn test_public() {
1231 let _ = submodule::PublicFlags::X;
1232 }
1233
1234 mod t1 {
1235 mod foo {
1236 pub type Bar = i32;
1237 }
1238
1239 bitflags! {
1240 struct Flags: foo::Bar {
1242 const A = 0b00000001;
1243 #[cfg(foo)]
1244 const B = 0b00000010;
1245 #[cfg(foo)]
1246 const C = 0b00000010;
1247 }
1248 }
1249 }
1250
1251 #[test]
1252 fn test_in_function() {
1253 bitflags! {
1254 struct Flags: u8 {
1255 const A = 1;
1256 #[cfg(any())] const B = 2;
1258 }
1259 }
1260 assert_eq!(Flags::all(), Flags::A);
1261 assert_eq!(format!("{:?}", Flags::A), "A");
1262 }
1263
1264 #[test]
1265 fn test_deprecated() {
1266 bitflags! {
1267 pub struct TestFlags: u32 {
1268 #[deprecated(note = "Use something else.")]
1269 const ONE = 1;
1270 }
1271 }
1272 }
1273
1274 #[test]
1275 fn test_pub_crate() {
1276 mod module {
1277 bitflags! {
1278 pub (crate) struct Test: u8 {
1279 const FOO = 1;
1280 }
1281 }
1282 }
1283
1284 assert_eq!(module::Test::FOO.bits(), 1);
1285 }
1286
1287 #[test]
1288 fn test_pub_in_module() {
1289 mod module {
1290 mod submodule {
1291 bitflags! {
1292 pub (in super) struct Test: u8 {
1295 const FOO = 1;
1296 }
1297 }
1298 }
1299
1300 mod test {
1301 pub(super) fn value() -> u8 {
1304 super::submodule::Test::FOO.bits()
1305 }
1306 }
1307
1308 pub fn value() -> u8 {
1309 test::value()
1310 }
1311 }
1312
1313 assert_eq!(module::value(), 1)
1314 }
1315
1316 #[test]
1317 fn test_zero_value_flags() {
1318 bitflags! {
1319 struct Flags: u32 {
1320 const NONE = 0b0;
1321 const SOME = 0b1;
1322 }
1323 }
1324
1325 assert!(Flags::empty().contains(Flags::NONE));
1326 assert!(Flags::SOME.contains(Flags::NONE));
1327 assert!(Flags::NONE.is_empty());
1328
1329 assert_eq!(format!("{:?}", Flags::empty()), "NONE");
1330 assert_eq!(format!("{:?}", Flags::SOME), "SOME");
1331 }
1332
1333 #[test]
1334 fn test_empty_bitflags() {
1335 bitflags! {}
1336 }
1337
1338 #[test]
1339 fn test_u128_bitflags() {
1340 bitflags! {
1341 struct Flags128: u128 {
1342 const A = 0x0000_0000_0000_0000_0000_0000_0000_0001;
1343 const B = 0x0000_0000_0000_1000_0000_0000_0000_0000;
1344 const C = 0x8000_0000_0000_0000_0000_0000_0000_0000;
1345 const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
1346 }
1347 }
1348
1349 assert_eq!(Flags128::ABC, Flags128::A | Flags128::B | Flags128::C);
1350 assert_eq!(Flags128::A.bits, 0x0000_0000_0000_0000_0000_0000_0000_0001);
1351 assert_eq!(Flags128::B.bits, 0x0000_0000_0000_1000_0000_0000_0000_0000);
1352 assert_eq!(Flags128::C.bits, 0x8000_0000_0000_0000_0000_0000_0000_0000);
1353 assert_eq!(
1354 Flags128::ABC.bits,
1355 0x8000_0000_0000_1000_0000_0000_0000_0001
1356 );
1357 assert_eq!(format!("{:?}", Flags128::A), "A");
1358 assert_eq!(format!("{:?}", Flags128::B), "B");
1359 assert_eq!(format!("{:?}", Flags128::C), "C");
1360 assert_eq!(format!("{:?}", Flags128::ABC), "A | B | C | ABC");
1361 }
1362
1363 #[test]
1364 fn test_serde_bitflags_serialize() {
1365 let flags = SerdeFlags::A | SerdeFlags::B;
1366
1367 let serialized = serde_json::to_string(&flags).unwrap();
1368
1369 assert_eq!(serialized, r#"{"bits":3}"#);
1370 }
1371
1372 #[test]
1373 fn test_serde_bitflags_deserialize() {
1374 let deserialized: SerdeFlags = serde_json::from_str(r#"{"bits":12}"#).unwrap();
1375
1376 let expected = SerdeFlags::C | SerdeFlags::D;
1377
1378 assert_eq!(deserialized.bits, expected.bits);
1379 }
1380
1381 #[test]
1382 fn test_serde_bitflags_roundtrip() {
1383 let flags = SerdeFlags::A | SerdeFlags::B;
1384
1385 let deserialized: SerdeFlags =
1386 serde_json::from_str(&serde_json::to_string(&flags).unwrap()).unwrap();
1387
1388 assert_eq!(deserialized.bits, flags.bits);
1389 }
1390
1391 bitflags! {
1392 #[derive(serde::Serialize, serde::Deserialize)]
1393 struct SerdeFlags: u32 {
1394 const A = 1;
1395 const B = 2;
1396 const C = 4;
1397 const D = 8;
1398 }
1399 }
1400}