]> git.proxmox.com Git - rustc.git/blob - vendor/bitflags/src/lib.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / vendor / bitflags / src / 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 //! 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.
13 //!
14 //! The `bitflags!` macro generates `struct`s that manage a set of flags. The
15 //! flags should only be defined for integer types, otherwise unexpected type
16 //! errors may occur at compile time.
17 //!
18 //! # Example
19 //!
20 //! ```
21 //! use bitflags::bitflags;
22 //!
23 //! bitflags! {
24 //! struct Flags: u32 {
25 //! const A = 0b00000001;
26 //! const B = 0b00000010;
27 //! const C = 0b00000100;
28 //! const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
29 //! }
30 //! }
31 //!
32 //! fn main() {
33 //! let e1 = Flags::A | Flags::C;
34 //! let e2 = Flags::B | Flags::C;
35 //! assert_eq!((e1 | e2), Flags::ABC); // union
36 //! assert_eq!((e1 & e2), Flags::C); // intersection
37 //! assert_eq!((e1 - e2), Flags::A); // set difference
38 //! assert_eq!(!e2, Flags::A); // set complement
39 //! }
40 //! ```
41 //!
42 //! See [`example_generated::Flags`](./example_generated/struct.Flags.html) for documentation of code
43 //! generated by the above `bitflags!` expansion.
44 //!
45 //! The generated `struct`s can also be extended with type and trait
46 //! implementations:
47 //!
48 //! ```
49 //! use std::fmt;
50 //!
51 //! use bitflags::bitflags;
52 //!
53 //! bitflags! {
54 //! struct Flags: u32 {
55 //! const A = 0b00000001;
56 //! const B = 0b00000010;
57 //! }
58 //! }
59 //!
60 //! impl Flags {
61 //! pub fn clear(&mut self) {
62 //! self.bits = 0; // The `bits` field can be accessed from within the
63 //! // same module where the `bitflags!` macro was invoked.
64 //! }
65 //! }
66 //!
67 //! impl fmt::Display for Flags {
68 //! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 //! write!(f, "hi!")
70 //! }
71 //! }
72 //!
73 //! fn main() {
74 //! let mut flags = Flags::A | Flags::B;
75 //! flags.clear();
76 //! assert!(flags.is_empty());
77 //! assert_eq!(format!("{}", flags), "hi!");
78 //! assert_eq!(format!("{:?}", Flags::A | Flags::B), "A | B");
79 //! assert_eq!(format!("{:?}", Flags::B), "B");
80 //! }
81 //! ```
82 //!
83 //! # Visibility
84 //!
85 //! The generated structs and their associated flag constants are not exported
86 //! out of the current module by default. A definition can be exported out of
87 //! the current module by adding `pub` before `struct`:
88 //!
89 //! ```
90 //! mod example {
91 //! use bitflags::bitflags;
92 //!
93 //! bitflags! {
94 //! pub struct Flags1: u32 {
95 //! const A = 0b00000001;
96 //! }
97 //!
98 //! # pub
99 //! struct Flags2: u32 {
100 //! const B = 0b00000010;
101 //! }
102 //! }
103 //! }
104 //!
105 //! fn main() {
106 //! let flag1 = example::Flags1::A;
107 //! let flag2 = example::Flags2::B; // error: const `B` is private
108 //! }
109 //! ```
110 //!
111 //! # Attributes
112 //!
113 //! Attributes can be attached to the generated `struct`s by placing them
114 //! before the `struct` keyword.
115 //!
116 //! ## Representations
117 //!
118 //! It's valid to add a `#[repr(C)]` or `#[repr(transparent)]` attribute to a type
119 //! generated by `bitflags!`. In these cases, the type is guaranteed to be a newtype.
120 //!
121 //! ```
122 //! use bitflags::bitflags;
123 //!
124 //! bitflags! {
125 //! #[repr(transparent)]
126 //! struct Flags: u32 {
127 //! const A = 0b00000001;
128 //! const B = 0b00000010;
129 //! const C = 0b00000100;
130 //! }
131 //! }
132 //! ```
133 //!
134 //! # Trait implementations
135 //!
136 //! The `Copy`, `Clone`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash`
137 //! traits are automatically derived for the `struct`s using the `derive` attribute.
138 //! Additional traits can be derived by providing an explicit `derive`
139 //! attribute on `struct`.
140 //!
141 //! The `Extend` and `FromIterator` traits are implemented for the `struct`s,
142 //! too: `Extend` adds the union of the instances of the `struct` iterated over,
143 //! while `FromIterator` calculates the union.
144 //!
145 //! The `Binary`, `Debug`, `LowerHex`, `Octal` and `UpperHex` traits are also
146 //! implemented by displaying the bits value of the internal struct.
147 //!
148 //! ## Operators
149 //!
150 //! The following operator traits are implemented for the generated `struct`s:
151 //!
152 //! - `BitOr` and `BitOrAssign`: union
153 //! - `BitAnd` and `BitAndAssign`: intersection
154 //! - `BitXor` and `BitXorAssign`: toggle
155 //! - `Sub` and `SubAssign`: set difference
156 //! - `Not`: set complement
157 //!
158 //! # Methods
159 //!
160 //! The following methods are defined for the generated `struct`s:
161 //!
162 //! - `empty`: an empty set of flags
163 //! - `all`: the set of all defined flags
164 //! - `bits`: the raw value of the flags currently stored
165 //! - `from_bits`: convert from underlying bit representation, unless that
166 //! representation contains bits that do not correspond to a
167 //! defined flag
168 //! - `from_bits_truncate`: convert from underlying bit representation, dropping
169 //! any bits that do not correspond to defined flags
170 //! - `from_bits_unchecked`: convert from underlying bit representation, keeping
171 //! all bits (even those not corresponding to defined
172 //! flags)
173 //! - `is_empty`: `true` if no flags are currently stored
174 //! - `is_all`: `true` if currently set flags exactly equal all defined flags
175 //! - `intersects`: `true` if there are flags common to both `self` and `other`
176 //! - `contains`: `true` if all of the flags in `other` are contained within `self`
177 //! - `insert`: inserts the specified flags in-place
178 //! - `remove`: removes the specified flags in-place
179 //! - `toggle`: the specified flags will be inserted if not present, and removed
180 //! if they are.
181 //! - `set`: inserts or removes the specified flags depending on the passed value
182 //! - `intersection`: returns a new set of flags, containing only the flags present
183 //! in both `self` and `other` (the argument to the function).
184 //! - `union`: returns a new set of flags, containing any flags present in
185 //! either `self` or `other` (the argument to the function).
186 //! - `difference`: returns a new set of flags, containing all flags present in
187 //! `self` without any of the flags present in `other` (the
188 //! argument to the function).
189 //! - `symmetric_difference`: returns a new set of flags, containing all flags
190 //! present in either `self` or `other` (the argument
191 //! to the function), but not both.
192 //! - `complement`: returns a new set of flags, containing all flags which are
193 //! not set in `self`, but which are allowed for this type.
194 //!
195 //! ## Default
196 //!
197 //! The `Default` trait is not automatically implemented for the generated structs.
198 //!
199 //! If your default value is equal to `0` (which is the same value as calling `empty()`
200 //! on the generated struct), you can simply derive `Default`:
201 //!
202 //! ```
203 //! use bitflags::bitflags;
204 //!
205 //! bitflags! {
206 //! // Results in default value with bits: 0
207 //! #[derive(Default)]
208 //! struct Flags: u32 {
209 //! const A = 0b00000001;
210 //! const B = 0b00000010;
211 //! const C = 0b00000100;
212 //! }
213 //! }
214 //!
215 //! fn main() {
216 //! let derived_default: Flags = Default::default();
217 //! assert_eq!(derived_default.bits(), 0);
218 //! }
219 //! ```
220 //!
221 //! If your default value is not equal to `0` you need to implement `Default` yourself:
222 //!
223 //! ```
224 //! use bitflags::bitflags;
225 //!
226 //! bitflags! {
227 //! struct Flags: u32 {
228 //! const A = 0b00000001;
229 //! const B = 0b00000010;
230 //! const C = 0b00000100;
231 //! }
232 //! }
233 //!
234 //! // explicit `Default` implementation
235 //! impl Default for Flags {
236 //! fn default() -> Flags {
237 //! Flags::A | Flags::C
238 //! }
239 //! }
240 //!
241 //! fn main() {
242 //! let implemented_default: Flags = Default::default();
243 //! assert_eq!(implemented_default, (Flags::A | Flags::C));
244 //! }
245 //! ```
246 //!
247 //! # Zero Flags
248 //!
249 //! Flags with a value equal to zero will have some strange behavior that one should be aware of.
250 //!
251 //! ```
252 //! use bitflags::bitflags;
253 //!
254 //! bitflags! {
255 //! struct Flags: u32 {
256 //! const NONE = 0b00000000;
257 //! const SOME = 0b00000001;
258 //! }
259 //! }
260 //!
261 //! fn main() {
262 //! let empty = Flags::empty();
263 //! let none = Flags::NONE;
264 //! let some = Flags::SOME;
265 //!
266 //! // Zero flags are treated as always present
267 //! assert!(empty.contains(Flags::NONE));
268 //! assert!(none.contains(Flags::NONE));
269 //! assert!(some.contains(Flags::NONE));
270 //!
271 //! // Zero flags will be ignored when testing for emptiness
272 //! assert!(none.is_empty());
273 //! }
274 //! ```
275 //!
276 //! Users should generally avoid defining a flag with a value of zero.
277
278 #![cfg_attr(not(test), no_std)]
279 #![doc(html_root_url = "https://docs.rs/bitflags/1.3.1")]
280
281 #[doc(hidden)]
282 pub extern crate core as _core;
283
284 /// The macro used to generate the flag structures.
285 ///
286 /// See the [crate level docs](../bitflags/index.html) for complete documentation.
287 ///
288 /// # Example
289 ///
290 /// ```
291 /// use bitflags::bitflags;
292 ///
293 /// bitflags! {
294 /// struct Flags: u32 {
295 /// const A = 0b00000001;
296 /// const B = 0b00000010;
297 /// const C = 0b00000100;
298 /// const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
299 /// }
300 /// }
301 ///
302 /// fn main() {
303 /// let e1 = Flags::A | Flags::C;
304 /// let e2 = Flags::B | Flags::C;
305 /// assert_eq!((e1 | e2), Flags::ABC); // union
306 /// assert_eq!((e1 & e2), Flags::C); // intersection
307 /// assert_eq!((e1 - e2), Flags::A); // set difference
308 /// assert_eq!(!e2, Flags::A); // set complement
309 /// }
310 /// ```
311 ///
312 /// The generated `struct`s can also be extended with type and trait
313 /// implementations:
314 ///
315 /// ```
316 /// use std::fmt;
317 ///
318 /// use bitflags::bitflags;
319 ///
320 /// bitflags! {
321 /// struct Flags: u32 {
322 /// const A = 0b00000001;
323 /// const B = 0b00000010;
324 /// }
325 /// }
326 ///
327 /// impl Flags {
328 /// pub fn clear(&mut self) {
329 /// self.bits = 0; // The `bits` field can be accessed from within the
330 /// // same module where the `bitflags!` macro was invoked.
331 /// }
332 /// }
333 ///
334 /// impl fmt::Display for Flags {
335 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
336 /// write!(f, "hi!")
337 /// }
338 /// }
339 ///
340 /// fn main() {
341 /// let mut flags = Flags::A | Flags::B;
342 /// flags.clear();
343 /// assert!(flags.is_empty());
344 /// assert_eq!(format!("{}", flags), "hi!");
345 /// assert_eq!(format!("{:?}", Flags::A | Flags::B), "A | B");
346 /// assert_eq!(format!("{:?}", Flags::B), "B");
347 /// }
348 /// ```
349 #[macro_export(local_inner_macros)]
350 macro_rules! bitflags {
351 (
352 $(#[$outer:meta])*
353 $vis:vis struct $BitFlags:ident: $T:ty {
354 $(
355 $(#[$inner:ident $($args:tt)*])*
356 const $Flag:ident = $value:expr;
357 )*
358 }
359
360 $($t:tt)*
361 ) => {
362 $(#[$outer])*
363 #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
364 $vis struct $BitFlags {
365 bits: $T,
366 }
367
368 __impl_bitflags! {
369 $BitFlags: $T {
370 $(
371 $(#[$inner $($args)*])*
372 $Flag = $value;
373 )*
374 }
375 }
376
377 bitflags! {
378 $($t)*
379 }
380 };
381 () => {};
382 }
383
384 // A helper macro to implement the `all` function.
385 #[macro_export(local_inner_macros)]
386 #[doc(hidden)]
387 macro_rules! __impl_all_bitflags {
388 (
389 $BitFlags:ident: $T:ty {
390 $(
391 $(#[$attr:ident $($args:tt)*])*
392 $Flag:ident = $value:expr;
393 )+
394 }
395 ) => {
396 // See `Debug::fmt` for why this approach is taken.
397 #[allow(non_snake_case)]
398 trait __BitFlags {
399 $(
400 const $Flag: $T = 0;
401 )+
402 }
403 impl __BitFlags for $BitFlags {
404 $(
405 __impl_bitflags! {
406 #[allow(deprecated)]
407 $(? #[$attr $($args)*])*
408 const $Flag: $T = Self::$Flag.bits;
409 }
410 )+
411 }
412 Self { bits: $(<Self as __BitFlags>::$Flag)|+ }
413 };
414 (
415 $BitFlags:ident: $T:ty { }
416 ) => {
417 Self { bits: 0 }
418 };
419 }
420
421 #[macro_export(local_inner_macros)]
422 #[doc(hidden)]
423 macro_rules! __impl_bitflags {
424 (
425 $BitFlags:ident: $T:ty {
426 $(
427 $(#[$attr:ident $($args:tt)*])*
428 $Flag:ident = $value:expr;
429 )*
430 }
431 ) => {
432 impl $crate::_core::fmt::Debug for $BitFlags {
433 fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
434 // This convoluted approach is to handle #[cfg]-based flag
435 // omission correctly. For example it needs to support:
436 //
437 // #[cfg(unix)] const A: Flag = /* ... */;
438 // #[cfg(windows)] const B: Flag = /* ... */;
439
440 // Unconditionally define a check for every flag, even disabled
441 // ones.
442 #[allow(non_snake_case)]
443 trait __BitFlags {
444 $(
445 #[inline]
446 fn $Flag(&self) -> bool { false }
447 )*
448 }
449
450 // Conditionally override the check for just those flags that
451 // are not #[cfg]ed away.
452 impl __BitFlags for $BitFlags {
453 $(
454 __impl_bitflags! {
455 #[allow(deprecated)]
456 #[inline]
457 $(? #[$attr $($args)*])*
458 fn $Flag(&self) -> bool {
459 if Self::$Flag.bits == 0 && self.bits != 0 {
460 false
461 } else {
462 self.bits & Self::$Flag.bits == Self::$Flag.bits
463 }
464 }
465 }
466 )*
467 }
468
469 let mut first = true;
470 $(
471 if <Self as __BitFlags>::$Flag(self) {
472 if !first {
473 f.write_str(" | ")?;
474 }
475 first = false;
476 f.write_str($crate::_core::stringify!($Flag))?;
477 }
478 )*
479 let extra_bits = self.bits & !Self::all().bits();
480 if extra_bits != 0 {
481 if !first {
482 f.write_str(" | ")?;
483 }
484 first = false;
485 f.write_str("0x")?;
486 $crate::_core::fmt::LowerHex::fmt(&extra_bits, f)?;
487 }
488 if first {
489 f.write_str("(empty)")?;
490 }
491 Ok(())
492 }
493 }
494 impl $crate::_core::fmt::Binary for $BitFlags {
495 fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
496 $crate::_core::fmt::Binary::fmt(&self.bits, f)
497 }
498 }
499 impl $crate::_core::fmt::Octal for $BitFlags {
500 fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
501 $crate::_core::fmt::Octal::fmt(&self.bits, f)
502 }
503 }
504 impl $crate::_core::fmt::LowerHex for $BitFlags {
505 fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
506 $crate::_core::fmt::LowerHex::fmt(&self.bits, f)
507 }
508 }
509 impl $crate::_core::fmt::UpperHex for $BitFlags {
510 fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
511 $crate::_core::fmt::UpperHex::fmt(&self.bits, f)
512 }
513 }
514
515 #[allow(dead_code)]
516 impl $BitFlags {
517 $(
518 $(#[$attr $($args)*])*
519 pub const $Flag: Self = Self { bits: $value };
520 )*
521
522 /// Returns an empty set of flags.
523 #[inline]
524 pub const fn empty() -> Self {
525 Self { bits: 0 }
526 }
527
528 /// Returns the set containing all flags.
529 #[inline]
530 pub const fn all() -> Self {
531 __impl_all_bitflags! {
532 $BitFlags: $T {
533 $(
534 $(#[$attr $($args)*])*
535 $Flag = $value;
536 )*
537 }
538 }
539 }
540
541 /// Returns the raw value of the flags currently stored.
542 #[inline]
543 pub const fn bits(&self) -> $T {
544 self.bits
545 }
546
547 /// Convert from underlying bit representation, unless that
548 /// representation contains bits that do not correspond to a flag.
549 #[inline]
550 pub const fn from_bits(bits: $T) -> $crate::_core::option::Option<Self> {
551 if (bits & !Self::all().bits()) == 0 {
552 $crate::_core::option::Option::Some(Self { bits })
553 } else {
554 $crate::_core::option::Option::None
555 }
556 }
557
558 /// Convert from underlying bit representation, dropping any bits
559 /// that do not correspond to flags.
560 #[inline]
561 pub const fn from_bits_truncate(bits: $T) -> Self {
562 Self { bits: bits & Self::all().bits }
563 }
564
565 /// Convert from underlying bit representation, preserving all
566 /// bits (even those not corresponding to a defined flag).
567 ///
568 /// # Safety
569 ///
570 /// The caller of the `bitflags!` macro can chose to allow or
571 /// disallow extra bits for their bitflags type.
572 ///
573 /// The caller of `from_bits_unchecked()` has to ensure that
574 /// all bits correspond to a defined flag or that extra bits
575 /// are valid for this bitflags type.
576 #[inline]
577 pub const unsafe fn from_bits_unchecked(bits: $T) -> Self {
578 Self { bits }
579 }
580
581 /// Returns `true` if no flags are currently stored.
582 #[inline]
583 pub const fn is_empty(&self) -> bool {
584 self.bits() == Self::empty().bits()
585 }
586
587 /// Returns `true` if all flags are currently set.
588 #[inline]
589 pub const fn is_all(&self) -> bool {
590 Self::all().bits | self.bits == self.bits
591 }
592
593 /// Returns `true` if there are flags common to both `self` and `other`.
594 #[inline]
595 pub const fn intersects(&self, other: Self) -> bool {
596 !(Self { bits: self.bits & other.bits}).is_empty()
597 }
598
599 /// Returns `true` if all of the flags in `other` are contained within `self`.
600 #[inline]
601 pub const fn contains(&self, other: Self) -> bool {
602 (self.bits & other.bits) == other.bits
603 }
604
605 /// Inserts the specified flags in-place.
606 #[inline]
607 pub fn insert(&mut self, other: Self) {
608 self.bits |= other.bits;
609 }
610
611 /// Removes the specified flags in-place.
612 #[inline]
613 pub fn remove(&mut self, other: Self) {
614 self.bits &= !other.bits;
615 }
616
617 /// Toggles the specified flags in-place.
618 #[inline]
619 pub fn toggle(&mut self, other: Self) {
620 self.bits ^= other.bits;
621 }
622
623 /// Inserts or removes the specified flags depending on the passed value.
624 #[inline]
625 pub fn set(&mut self, other: Self, value: bool) {
626 if value {
627 self.insert(other);
628 } else {
629 self.remove(other);
630 }
631 }
632
633 /// Returns the intersection between the flags in `self` and
634 /// `other`.
635 ///
636 /// Specifically, the returned set contains only the flags which are
637 /// present in *both* `self` *and* `other`.
638 ///
639 /// This is equivalent to using the `&` operator (e.g.
640 /// [`ops::BitAnd`]), as in `flags & other`.
641 ///
642 /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
643 #[inline]
644 #[must_use]
645 pub const fn intersection(self, other: Self) -> Self {
646 Self { bits: self.bits & other.bits }
647 }
648
649 /// Returns the union of between the flags in `self` and `other`.
650 ///
651 /// Specifically, the returned set contains all flags which are
652 /// present in *either* `self` *or* `other`, including any which are
653 /// present in both (see [`Self::symmetric_difference`] if that
654 /// is undesirable).
655 ///
656 /// This is equivalent to using the `|` operator (e.g.
657 /// [`ops::BitOr`]), as in `flags | other`.
658 ///
659 /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
660 #[inline]
661 #[must_use]
662 pub const fn union(self, other: Self) -> Self {
663 Self { bits: self.bits | other.bits }
664 }
665
666 /// Returns the difference between the flags in `self` and `other`.
667 ///
668 /// Specifically, the returned set contains all flags present in
669 /// `self`, except for the ones present in `other`.
670 ///
671 /// It is also conceptually equivalent to the "bit-clear" operation:
672 /// `flags & !other` (and this syntax is also supported).
673 ///
674 /// This is equivalent to using the `-` operator (e.g.
675 /// [`ops::Sub`]), as in `flags - other`.
676 ///
677 /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
678 #[inline]
679 #[must_use]
680 pub const fn difference(self, other: Self) -> Self {
681 Self { bits: self.bits & !other.bits }
682 }
683
684 /// Returns the [symmetric difference][sym-diff] between the flags
685 /// in `self` and `other`.
686 ///
687 /// Specifically, the returned set contains the flags present which
688 /// are present in `self` or `other`, but that are not present in
689 /// both. Equivalently, it contains the flags present in *exactly
690 /// one* of the sets `self` and `other`.
691 ///
692 /// This is equivalent to using the `^` operator (e.g.
693 /// [`ops::BitXor`]), as in `flags ^ other`.
694 ///
695 /// [sym-diff]: https://en.wikipedia.org/wiki/Symmetric_difference
696 /// [`ops::BitXor`]: https://doc.rust-lang.org/std/ops/trait.BitXor.html
697 #[inline]
698 #[must_use]
699 pub const fn symmetric_difference(self, other: Self) -> Self {
700 Self { bits: self.bits ^ other.bits }
701 }
702
703 /// Returns the complement of this set of flags.
704 ///
705 /// Specifically, the returned set contains all the flags which are
706 /// not set in `self`, but which are allowed for this type.
707 ///
708 /// Alternatively, it can be thought of as the set difference
709 /// between [`Self::all()`] and `self` (e.g. `Self::all() - self`)
710 ///
711 /// This is equivalent to using the `!` operator (e.g.
712 /// [`ops::Not`]), as in `!flags`.
713 ///
714 /// [`Self::all()`]: Self::all
715 /// [`ops::Not`]: https://doc.rust-lang.org/std/ops/trait.Not.html
716 #[inline]
717 #[must_use]
718 pub const fn complement(self) -> Self {
719 Self::from_bits_truncate(!self.bits)
720 }
721
722 }
723
724 impl $crate::_core::ops::BitOr for $BitFlags {
725 type Output = Self;
726
727 /// Returns the union of the two sets of flags.
728 #[inline]
729 fn bitor(self, other: $BitFlags) -> Self {
730 Self { bits: self.bits | other.bits }
731 }
732 }
733
734 impl $crate::_core::ops::BitOrAssign for $BitFlags {
735 /// Adds the set of flags.
736 #[inline]
737 fn bitor_assign(&mut self, other: Self) {
738 self.bits |= other.bits;
739 }
740 }
741
742 impl $crate::_core::ops::BitXor for $BitFlags {
743 type Output = Self;
744
745 /// Returns the left flags, but with all the right flags toggled.
746 #[inline]
747 fn bitxor(self, other: Self) -> Self {
748 Self { bits: self.bits ^ other.bits }
749 }
750 }
751
752 impl $crate::_core::ops::BitXorAssign for $BitFlags {
753 /// Toggles the set of flags.
754 #[inline]
755 fn bitxor_assign(&mut self, other: Self) {
756 self.bits ^= other.bits;
757 }
758 }
759
760 impl $crate::_core::ops::BitAnd for $BitFlags {
761 type Output = Self;
762
763 /// Returns the intersection between the two sets of flags.
764 #[inline]
765 fn bitand(self, other: Self) -> Self {
766 Self { bits: self.bits & other.bits }
767 }
768 }
769
770 impl $crate::_core::ops::BitAndAssign for $BitFlags {
771 /// Disables all flags disabled in the set.
772 #[inline]
773 fn bitand_assign(&mut self, other: Self) {
774 self.bits &= other.bits;
775 }
776 }
777
778 impl $crate::_core::ops::Sub for $BitFlags {
779 type Output = Self;
780
781 /// Returns the set difference of the two sets of flags.
782 #[inline]
783 fn sub(self, other: Self) -> Self {
784 Self { bits: self.bits & !other.bits }
785 }
786 }
787
788 impl $crate::_core::ops::SubAssign for $BitFlags {
789 /// Disables all flags enabled in the set.
790 #[inline]
791 fn sub_assign(&mut self, other: Self) {
792 self.bits &= !other.bits;
793 }
794 }
795
796 impl $crate::_core::ops::Not for $BitFlags {
797 type Output = Self;
798
799 /// Returns the complement of this set of flags.
800 #[inline]
801 fn not(self) -> Self {
802 Self { bits: !self.bits } & Self::all()
803 }
804 }
805
806 impl $crate::_core::iter::Extend<$BitFlags> for $BitFlags {
807 fn extend<T: $crate::_core::iter::IntoIterator<Item=Self>>(&mut self, iterator: T) {
808 for item in iterator {
809 self.insert(item)
810 }
811 }
812 }
813
814 impl $crate::_core::iter::FromIterator<$BitFlags> for $BitFlags {
815 fn from_iter<T: $crate::_core::iter::IntoIterator<Item=Self>>(iterator: T) -> Self {
816 let mut result = Self::empty();
817 result.extend(iterator);
818 result
819 }
820 }
821 };
822
823 // Every attribute that the user writes on a const is applied to the
824 // corresponding const that we generate, but within the implementation of
825 // Debug and all() we want to ignore everything but #[cfg] attributes. In
826 // particular, including a #[deprecated] attribute on those items would fail
827 // to compile.
828 // https://github.com/bitflags/bitflags/issues/109
829 //
830 // Input:
831 //
832 // ? #[cfg(feature = "advanced")]
833 // ? #[deprecated(note = "Use something else.")]
834 // ? #[doc = r"High quality documentation."]
835 // fn f() -> i32 { /* ... */ }
836 //
837 // Output:
838 //
839 // #[cfg(feature = "advanced")]
840 // fn f() -> i32 { /* ... */ }
841 (
842 $(#[$filtered:meta])*
843 ? #[cfg $($cfgargs:tt)*]
844 $(? #[$rest:ident $($restargs:tt)*])*
845 fn $($item:tt)*
846 ) => {
847 __impl_bitflags! {
848 $(#[$filtered])*
849 #[cfg $($cfgargs)*]
850 $(? #[$rest $($restargs)*])*
851 fn $($item)*
852 }
853 };
854 (
855 $(#[$filtered:meta])*
856 // $next != `cfg`
857 ? #[$next:ident $($nextargs:tt)*]
858 $(? #[$rest:ident $($restargs:tt)*])*
859 fn $($item:tt)*
860 ) => {
861 __impl_bitflags! {
862 $(#[$filtered])*
863 // $next filtered out
864 $(? #[$rest $($restargs)*])*
865 fn $($item)*
866 }
867 };
868 (
869 $(#[$filtered:meta])*
870 fn $($item:tt)*
871 ) => {
872 $(#[$filtered])*
873 fn $($item)*
874 };
875
876 // Every attribute that the user writes on a const is applied to the
877 // corresponding const that we generate, but within the implementation of
878 // Debug and all() we want to ignore everything but #[cfg] attributes. In
879 // particular, including a #[deprecated] attribute on those items would fail
880 // to compile.
881 // https://github.com/bitflags/bitflags/issues/109
882 //
883 // const version
884 //
885 // Input:
886 //
887 // ? #[cfg(feature = "advanced")]
888 // ? #[deprecated(note = "Use something else.")]
889 // ? #[doc = r"High quality documentation."]
890 // const f: i32 { /* ... */ }
891 //
892 // Output:
893 //
894 // #[cfg(feature = "advanced")]
895 // const f: i32 { /* ... */ }
896 (
897 $(#[$filtered:meta])*
898 ? #[cfg $($cfgargs:tt)*]
899 $(? #[$rest:ident $($restargs:tt)*])*
900 const $($item:tt)*
901 ) => {
902 __impl_bitflags! {
903 $(#[$filtered])*
904 #[cfg $($cfgargs)*]
905 $(? #[$rest $($restargs)*])*
906 const $($item)*
907 }
908 };
909 (
910 $(#[$filtered:meta])*
911 // $next != `cfg`
912 ? #[$next:ident $($nextargs:tt)*]
913 $(? #[$rest:ident $($restargs:tt)*])*
914 const $($item:tt)*
915 ) => {
916 __impl_bitflags! {
917 $(#[$filtered])*
918 // $next filtered out
919 $(? #[$rest $($restargs)*])*
920 const $($item)*
921 }
922 };
923 (
924 $(#[$filtered:meta])*
925 const $($item:tt)*
926 ) => {
927 $(#[$filtered])*
928 const $($item)*
929 };
930 }
931
932 #[cfg(feature = "example_generated")]
933 pub mod example_generated;
934
935 #[cfg(test)]
936 mod tests {
937 use std::collections::hash_map::DefaultHasher;
938 use std::hash::{Hash, Hasher};
939
940 bitflags! {
941 #[doc = "> The first principle is that you must not fool yourself — and"]
942 #[doc = "> you are the easiest person to fool."]
943 #[doc = "> "]
944 #[doc = "> - Richard Feynman"]
945 #[derive(Default)]
946 struct Flags: u32 {
947 const A = 0b00000001;
948 #[doc = "<pcwalton> macros are way better at generating code than trans is"]
949 const B = 0b00000010;
950 const C = 0b00000100;
951 #[doc = "* cmr bed"]
952 #[doc = "* strcat table"]
953 #[doc = "<strcat> wait what?"]
954 const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
955 }
956
957 struct _CfgFlags: u32 {
958 #[cfg(unix)]
959 const _CFG_A = 0b01;
960 #[cfg(windows)]
961 const _CFG_B = 0b01;
962 #[cfg(unix)]
963 const _CFG_C = Self::_CFG_A.bits | 0b10;
964 }
965
966 struct AnotherSetOfFlags: i8 {
967 const ANOTHER_FLAG = -1_i8;
968 }
969
970 struct LongFlags: u32 {
971 const LONG_A = 0b1111111111111111;
972 }
973 }
974
975 bitflags! {
976 struct EmptyFlags: u32 {
977 }
978 }
979
980 #[test]
981 fn test_bits() {
982 assert_eq!(Flags::empty().bits(), 0b00000000);
983 assert_eq!(Flags::A.bits(), 0b00000001);
984 assert_eq!(Flags::ABC.bits(), 0b00000111);
985
986 assert_eq!(AnotherSetOfFlags::empty().bits(), 0b00);
987 assert_eq!(AnotherSetOfFlags::ANOTHER_FLAG.bits(), !0_i8);
988
989 assert_eq!(EmptyFlags::empty().bits(), 0b00000000);
990 }
991
992 #[test]
993 fn test_from_bits() {
994 assert_eq!(Flags::from_bits(0), Some(Flags::empty()));
995 assert_eq!(Flags::from_bits(0b1), Some(Flags::A));
996 assert_eq!(Flags::from_bits(0b10), Some(Flags::B));
997 assert_eq!(Flags::from_bits(0b11), Some(Flags::A | Flags::B));
998 assert_eq!(Flags::from_bits(0b1000), None);
999
1000 assert_eq!(
1001 AnotherSetOfFlags::from_bits(!0_i8),
1002 Some(AnotherSetOfFlags::ANOTHER_FLAG)
1003 );
1004
1005 assert_eq!(EmptyFlags::from_bits(0), Some(EmptyFlags::empty()));
1006 assert_eq!(EmptyFlags::from_bits(0b1), None);
1007 }
1008
1009 #[test]
1010 fn test_from_bits_truncate() {
1011 assert_eq!(Flags::from_bits_truncate(0), Flags::empty());
1012 assert_eq!(Flags::from_bits_truncate(0b1), Flags::A);
1013 assert_eq!(Flags::from_bits_truncate(0b10), Flags::B);
1014 assert_eq!(Flags::from_bits_truncate(0b11), (Flags::A | Flags::B));
1015 assert_eq!(Flags::from_bits_truncate(0b1000), Flags::empty());
1016 assert_eq!(Flags::from_bits_truncate(0b1001), Flags::A);
1017
1018 assert_eq!(
1019 AnotherSetOfFlags::from_bits_truncate(0_i8),
1020 AnotherSetOfFlags::empty()
1021 );
1022
1023 assert_eq!(EmptyFlags::from_bits_truncate(0), EmptyFlags::empty());
1024 assert_eq!(EmptyFlags::from_bits_truncate(0b1), EmptyFlags::empty());
1025 }
1026
1027 #[test]
1028 fn test_from_bits_unchecked() {
1029 let extra = unsafe { Flags::from_bits_unchecked(0b1000) };
1030 assert_eq!(unsafe { Flags::from_bits_unchecked(0) }, Flags::empty());
1031 assert_eq!(unsafe { Flags::from_bits_unchecked(0b1) }, Flags::A);
1032 assert_eq!(unsafe { Flags::from_bits_unchecked(0b10) }, Flags::B);
1033
1034 assert_eq!(
1035 unsafe { Flags::from_bits_unchecked(0b11) },
1036 (Flags::A | Flags::B)
1037 );
1038 assert_eq!(
1039 unsafe { Flags::from_bits_unchecked(0b1000) },
1040 (extra | Flags::empty())
1041 );
1042 assert_eq!(
1043 unsafe { Flags::from_bits_unchecked(0b1001) },
1044 (extra | Flags::A)
1045 );
1046
1047 let extra = unsafe { EmptyFlags::from_bits_unchecked(0b1000) };
1048 assert_eq!(
1049 unsafe { EmptyFlags::from_bits_unchecked(0b1000) },
1050 (extra | EmptyFlags::empty())
1051 );
1052 }
1053
1054 #[test]
1055 fn test_is_empty() {
1056 assert!(Flags::empty().is_empty());
1057 assert!(!Flags::A.is_empty());
1058 assert!(!Flags::ABC.is_empty());
1059
1060 assert!(!AnotherSetOfFlags::ANOTHER_FLAG.is_empty());
1061
1062 assert!(EmptyFlags::empty().is_empty());
1063 assert!(EmptyFlags::all().is_empty());
1064 }
1065
1066 #[test]
1067 fn test_is_all() {
1068 assert!(Flags::all().is_all());
1069 assert!(!Flags::A.is_all());
1070 assert!(Flags::ABC.is_all());
1071
1072 let extra = unsafe { Flags::from_bits_unchecked(0b1000) };
1073 assert!(!extra.is_all());
1074 assert!(!(Flags::A | extra).is_all());
1075 assert!((Flags::ABC | extra).is_all());
1076
1077 assert!(AnotherSetOfFlags::ANOTHER_FLAG.is_all());
1078
1079 assert!(EmptyFlags::all().is_all());
1080 assert!(EmptyFlags::empty().is_all());
1081 }
1082
1083 #[test]
1084 fn test_two_empties_do_not_intersect() {
1085 let e1 = Flags::empty();
1086 let e2 = Flags::empty();
1087 assert!(!e1.intersects(e2));
1088
1089 assert!(AnotherSetOfFlags::ANOTHER_FLAG.intersects(AnotherSetOfFlags::ANOTHER_FLAG));
1090 }
1091
1092 #[test]
1093 fn test_empty_does_not_intersect_with_full() {
1094 let e1 = Flags::empty();
1095 let e2 = Flags::ABC;
1096 assert!(!e1.intersects(e2));
1097 }
1098
1099 #[test]
1100 fn test_disjoint_intersects() {
1101 let e1 = Flags::A;
1102 let e2 = Flags::B;
1103 assert!(!e1.intersects(e2));
1104 }
1105
1106 #[test]
1107 fn test_overlapping_intersects() {
1108 let e1 = Flags::A;
1109 let e2 = Flags::A | Flags::B;
1110 assert!(e1.intersects(e2));
1111 }
1112
1113 #[test]
1114 fn test_contains() {
1115 let e1 = Flags::A;
1116 let e2 = Flags::A | Flags::B;
1117 assert!(!e1.contains(e2));
1118 assert!(e2.contains(e1));
1119 assert!(Flags::ABC.contains(e2));
1120
1121 assert!(AnotherSetOfFlags::ANOTHER_FLAG.contains(AnotherSetOfFlags::ANOTHER_FLAG));
1122
1123 assert!(EmptyFlags::empty().contains(EmptyFlags::empty()));
1124 }
1125
1126 #[test]
1127 fn test_insert() {
1128 let mut e1 = Flags::A;
1129 let e2 = Flags::A | Flags::B;
1130 e1.insert(e2);
1131 assert_eq!(e1, e2);
1132
1133 let mut e3 = AnotherSetOfFlags::empty();
1134 e3.insert(AnotherSetOfFlags::ANOTHER_FLAG);
1135 assert_eq!(e3, AnotherSetOfFlags::ANOTHER_FLAG);
1136 }
1137
1138 #[test]
1139 fn test_remove() {
1140 let mut e1 = Flags::A | Flags::B;
1141 let e2 = Flags::A | Flags::C;
1142 e1.remove(e2);
1143 assert_eq!(e1, Flags::B);
1144
1145 let mut e3 = AnotherSetOfFlags::ANOTHER_FLAG;
1146 e3.remove(AnotherSetOfFlags::ANOTHER_FLAG);
1147 assert_eq!(e3, AnotherSetOfFlags::empty());
1148 }
1149
1150 #[test]
1151 fn test_operators() {
1152 let e1 = Flags::A | Flags::C;
1153 let e2 = Flags::B | Flags::C;
1154 assert_eq!((e1 | e2), Flags::ABC); // union
1155 assert_eq!((e1 & e2), Flags::C); // intersection
1156 assert_eq!((e1 - e2), Flags::A); // set difference
1157 assert_eq!(!e2, Flags::A); // set complement
1158 assert_eq!(e1 ^ e2, Flags::A | Flags::B); // toggle
1159 let mut e3 = e1;
1160 e3.toggle(e2);
1161 assert_eq!(e3, Flags::A | Flags::B);
1162
1163 let mut m4 = AnotherSetOfFlags::empty();
1164 m4.toggle(AnotherSetOfFlags::empty());
1165 assert_eq!(m4, AnotherSetOfFlags::empty());
1166 }
1167
1168 #[test]
1169 fn test_operators_unchecked() {
1170 let extra = unsafe { Flags::from_bits_unchecked(0b1000) };
1171 let e1 = Flags::A | Flags::C | extra;
1172 let e2 = Flags::B | Flags::C;
1173 assert_eq!((e1 | e2), (Flags::ABC | extra)); // union
1174 assert_eq!((e1 & e2), Flags::C); // intersection
1175 assert_eq!((e1 - e2), (Flags::A | extra)); // set difference
1176 assert_eq!(!e2, Flags::A); // set complement
1177 assert_eq!(!e1, Flags::B); // set complement
1178 assert_eq!(e1 ^ e2, Flags::A | Flags::B | extra); // toggle
1179 let mut e3 = e1;
1180 e3.toggle(e2);
1181 assert_eq!(e3, Flags::A | Flags::B | extra);
1182 }
1183
1184 #[test]
1185 fn test_set_ops_basic() {
1186 let ab = Flags::A.union(Flags::B);
1187 let ac = Flags::A.union(Flags::C);
1188 let bc = Flags::B.union(Flags::C);
1189 assert_eq!(ab.bits, 0b011);
1190 assert_eq!(bc.bits, 0b110);
1191 assert_eq!(ac.bits, 0b101);
1192
1193 assert_eq!(ab, Flags::B.union(Flags::A));
1194 assert_eq!(ac, Flags::C.union(Flags::A));
1195 assert_eq!(bc, Flags::C.union(Flags::B));
1196
1197 assert_eq!(ac, Flags::A | Flags::C);
1198 assert_eq!(bc, Flags::B | Flags::C);
1199 assert_eq!(ab.union(bc), Flags::ABC);
1200
1201 assert_eq!(ac, Flags::A | Flags::C);
1202 assert_eq!(bc, Flags::B | Flags::C);
1203
1204 assert_eq!(ac.union(bc), ac | bc);
1205 assert_eq!(ac.union(bc), Flags::ABC);
1206 assert_eq!(bc.union(ac), Flags::ABC);
1207
1208 assert_eq!(ac.intersection(bc), ac & bc);
1209 assert_eq!(ac.intersection(bc), Flags::C);
1210 assert_eq!(bc.intersection(ac), Flags::C);
1211
1212 assert_eq!(ac.difference(bc), ac - bc);
1213 assert_eq!(bc.difference(ac), bc - ac);
1214 assert_eq!(ac.difference(bc), Flags::A);
1215 assert_eq!(bc.difference(ac), Flags::B);
1216
1217 assert_eq!(bc.complement(), !bc);
1218 assert_eq!(bc.complement(), Flags::A);
1219 assert_eq!(ac.symmetric_difference(bc), Flags::A.union(Flags::B));
1220 assert_eq!(bc.symmetric_difference(ac), Flags::A.union(Flags::B));
1221 }
1222
1223 #[test]
1224 fn test_set_ops_const() {
1225 // These just test that these compile and don't cause use-site panics
1226 // (would be possible if we had some sort of UB)
1227 const INTERSECT: Flags = Flags::all().intersection(Flags::C);
1228 const UNION: Flags = Flags::A.union(Flags::C);
1229 const DIFFERENCE: Flags = Flags::all().difference(Flags::A);
1230 const COMPLEMENT: Flags = Flags::C.complement();
1231 const SYM_DIFFERENCE: Flags = UNION.symmetric_difference(DIFFERENCE);
1232 assert_eq!(INTERSECT, Flags::C);
1233 assert_eq!(UNION, Flags::A | Flags::C);
1234 assert_eq!(DIFFERENCE, Flags::all() - Flags::A);
1235 assert_eq!(COMPLEMENT, !Flags::C);
1236 assert_eq!(SYM_DIFFERENCE, (Flags::A | Flags::C) ^ (Flags::all() - Flags::A));
1237 }
1238
1239 #[test]
1240 fn test_set_ops_unchecked() {
1241 let extra = unsafe { Flags::from_bits_unchecked(0b1000) };
1242 let e1 = Flags::A.union(Flags::C).union(extra);
1243 let e2 = Flags::B.union(Flags::C);
1244 assert_eq!(e1.bits, 0b1101);
1245 assert_eq!(e1.union(e2), (Flags::ABC | extra));
1246 assert_eq!(e1.intersection(e2), Flags::C);
1247 assert_eq!(e1.difference(e2), Flags::A | extra);
1248 assert_eq!(e2.difference(e1), Flags::B);
1249 assert_eq!(e2.complement(), Flags::A);
1250 assert_eq!(e1.complement(), Flags::B);
1251 assert_eq!(e1.symmetric_difference(e2), Flags::A | Flags::B | extra); // toggle
1252 }
1253
1254 #[test]
1255 fn test_set_ops_exhaustive() {
1256 // Define a flag that contains gaps to help exercise edge-cases,
1257 // especially around "unknown" flags (e.g. ones outside of `all()`
1258 // `from_bits_unchecked`).
1259 // - when lhs and rhs both have different sets of unknown flags.
1260 // - unknown flags at both ends, and in the middle
1261 // - cases with "gaps".
1262 bitflags! {
1263 struct Test: u16 {
1264 // Intentionally no `A`
1265 const B = 0b000000010;
1266 // Intentionally no `C`
1267 const D = 0b000001000;
1268 const E = 0b000010000;
1269 const F = 0b000100000;
1270 const G = 0b001000000;
1271 // Intentionally no `H`
1272 const I = 0b100000000;
1273 }
1274 }
1275 let iter_test_flags =
1276 || (0..=0b111_1111_1111).map(|bits| unsafe { Test::from_bits_unchecked(bits) });
1277
1278 for a in iter_test_flags() {
1279 assert_eq!(
1280 a.complement(),
1281 Test::from_bits_truncate(!a.bits),
1282 "wrong result: !({:?})",
1283 a,
1284 );
1285 assert_eq!(a.complement(), !a, "named != op: !({:?})", a);
1286 for b in iter_test_flags() {
1287 // Check that the named operations produce the expected bitwise
1288 // values.
1289 assert_eq!(
1290 a.union(b).bits,
1291 a.bits | b.bits,
1292 "wrong result: `{:?}` | `{:?}`",
1293 a,
1294 b,
1295 );
1296 assert_eq!(
1297 a.intersection(b).bits,
1298 a.bits & b.bits,
1299 "wrong result: `{:?}` & `{:?}`",
1300 a,
1301 b,
1302 );
1303 assert_eq!(
1304 a.symmetric_difference(b).bits,
1305 a.bits ^ b.bits,
1306 "wrong result: `{:?}` ^ `{:?}`",
1307 a,
1308 b,
1309 );
1310 assert_eq!(
1311 a.difference(b).bits,
1312 a.bits & !b.bits,
1313 "wrong result: `{:?}` - `{:?}`",
1314 a,
1315 b,
1316 );
1317 // Note: Difference is checked as both `a - b` and `b - a`
1318 assert_eq!(
1319 b.difference(a).bits,
1320 b.bits & !a.bits,
1321 "wrong result: `{:?}` - `{:?}`",
1322 b,
1323 a,
1324 );
1325 // Check that the named set operations are equivalent to the
1326 // bitwise equivalents
1327 assert_eq!(a.union(b), a | b, "named != op: `{:?}` | `{:?}`", a, b,);
1328 assert_eq!(
1329 a.intersection(b),
1330 a & b,
1331 "named != op: `{:?}` & `{:?}`",
1332 a,
1333 b,
1334 );
1335 assert_eq!(
1336 a.symmetric_difference(b),
1337 a ^ b,
1338 "named != op: `{:?}` ^ `{:?}`",
1339 a,
1340 b,
1341 );
1342 assert_eq!(a.difference(b), a - b, "named != op: `{:?}` - `{:?}`", a, b,);
1343 // Note: Difference is checked as both `a - b` and `b - a`
1344 assert_eq!(b.difference(a), b - a, "named != op: `{:?}` - `{:?}`", b, a,);
1345 // Verify that the operations which should be symmetric are
1346 // actually symmetric.
1347 assert_eq!(a.union(b), b.union(a), "asymmetry: `{:?}` | `{:?}`", a, b,);
1348 assert_eq!(
1349 a.intersection(b),
1350 b.intersection(a),
1351 "asymmetry: `{:?}` & `{:?}`",
1352 a,
1353 b,
1354 );
1355 assert_eq!(
1356 a.symmetric_difference(b),
1357 b.symmetric_difference(a),
1358 "asymmetry: `{:?}` ^ `{:?}`",
1359 a,
1360 b,
1361 );
1362 }
1363 }
1364 }
1365
1366 #[test]
1367 fn test_set() {
1368 let mut e1 = Flags::A | Flags::C;
1369 e1.set(Flags::B, true);
1370 e1.set(Flags::C, false);
1371
1372 assert_eq!(e1, Flags::A | Flags::B);
1373 }
1374
1375 #[test]
1376 fn test_assignment_operators() {
1377 let mut m1 = Flags::empty();
1378 let e1 = Flags::A | Flags::C;
1379 // union
1380 m1 |= Flags::A;
1381 assert_eq!(m1, Flags::A);
1382 // intersection
1383 m1 &= e1;
1384 assert_eq!(m1, Flags::A);
1385 // set difference
1386 m1 -= m1;
1387 assert_eq!(m1, Flags::empty());
1388 // toggle
1389 m1 ^= e1;
1390 assert_eq!(m1, e1);
1391 }
1392
1393 #[test]
1394 fn test_const_fn() {
1395 const _M1: Flags = Flags::empty();
1396
1397 const M2: Flags = Flags::A;
1398 assert_eq!(M2, Flags::A);
1399
1400 const M3: Flags = Flags::C;
1401 assert_eq!(M3, Flags::C);
1402 }
1403
1404 #[test]
1405 fn test_extend() {
1406 let mut flags;
1407
1408 flags = Flags::empty();
1409 flags.extend([].iter().cloned());
1410 assert_eq!(flags, Flags::empty());
1411
1412 flags = Flags::empty();
1413 flags.extend([Flags::A, Flags::B].iter().cloned());
1414 assert_eq!(flags, Flags::A | Flags::B);
1415
1416 flags = Flags::A;
1417 flags.extend([Flags::A, Flags::B].iter().cloned());
1418 assert_eq!(flags, Flags::A | Flags::B);
1419
1420 flags = Flags::B;
1421 flags.extend([Flags::A, Flags::ABC].iter().cloned());
1422 assert_eq!(flags, Flags::ABC);
1423 }
1424
1425 #[test]
1426 fn test_from_iterator() {
1427 assert_eq!([].iter().cloned().collect::<Flags>(), Flags::empty());
1428 assert_eq!(
1429 [Flags::A, Flags::B].iter().cloned().collect::<Flags>(),
1430 Flags::A | Flags::B
1431 );
1432 assert_eq!(
1433 [Flags::A, Flags::ABC].iter().cloned().collect::<Flags>(),
1434 Flags::ABC
1435 );
1436 }
1437
1438 #[test]
1439 fn test_lt() {
1440 let mut a = Flags::empty();
1441 let mut b = Flags::empty();
1442
1443 assert!(!(a < b) && !(b < a));
1444 b = Flags::B;
1445 assert!(a < b);
1446 a = Flags::C;
1447 assert!(!(a < b) && b < a);
1448 b = Flags::C | Flags::B;
1449 assert!(a < b);
1450 }
1451
1452 #[test]
1453 fn test_ord() {
1454 let mut a = Flags::empty();
1455 let mut b = Flags::empty();
1456
1457 assert!(a <= b && a >= b);
1458 a = Flags::A;
1459 assert!(a > b && a >= b);
1460 assert!(b < a && b <= a);
1461 b = Flags::B;
1462 assert!(b > a && b >= a);
1463 assert!(a < b && a <= b);
1464 }
1465
1466 fn hash<T: Hash>(t: &T) -> u64 {
1467 let mut s = DefaultHasher::new();
1468 t.hash(&mut s);
1469 s.finish()
1470 }
1471
1472 #[test]
1473 fn test_hash() {
1474 let mut x = Flags::empty();
1475 let mut y = Flags::empty();
1476 assert_eq!(hash(&x), hash(&y));
1477 x = Flags::all();
1478 y = Flags::ABC;
1479 assert_eq!(hash(&x), hash(&y));
1480 }
1481
1482 #[test]
1483 fn test_default() {
1484 assert_eq!(Flags::empty(), Flags::default());
1485 }
1486
1487 #[test]
1488 fn test_debug() {
1489 assert_eq!(format!("{:?}", Flags::A | Flags::B), "A | B");
1490 assert_eq!(format!("{:?}", Flags::empty()), "(empty)");
1491 assert_eq!(format!("{:?}", Flags::ABC), "A | B | C | ABC");
1492 let extra = unsafe { Flags::from_bits_unchecked(0xb8) };
1493 assert_eq!(format!("{:?}", extra), "0xb8");
1494 assert_eq!(format!("{:?}", Flags::A | extra), "A | 0xb8");
1495
1496 assert_eq!(
1497 format!("{:?}", Flags::ABC | extra),
1498 "A | B | C | ABC | 0xb8"
1499 );
1500
1501 assert_eq!(format!("{:?}", EmptyFlags::empty()), "(empty)");
1502 }
1503
1504 #[test]
1505 fn test_binary() {
1506 assert_eq!(format!("{:b}", Flags::ABC), "111");
1507 assert_eq!(format!("{:#b}", Flags::ABC), "0b111");
1508 let extra = unsafe { Flags::from_bits_unchecked(0b1010000) };
1509 assert_eq!(format!("{:b}", Flags::ABC | extra), "1010111");
1510 assert_eq!(format!("{:#b}", Flags::ABC | extra), "0b1010111");
1511 }
1512
1513 #[test]
1514 fn test_octal() {
1515 assert_eq!(format!("{:o}", LongFlags::LONG_A), "177777");
1516 assert_eq!(format!("{:#o}", LongFlags::LONG_A), "0o177777");
1517 let extra = unsafe { LongFlags::from_bits_unchecked(0o5000000) };
1518 assert_eq!(format!("{:o}", LongFlags::LONG_A | extra), "5177777");
1519 assert_eq!(format!("{:#o}", LongFlags::LONG_A | extra), "0o5177777");
1520 }
1521
1522 #[test]
1523 fn test_lowerhex() {
1524 assert_eq!(format!("{:x}", LongFlags::LONG_A), "ffff");
1525 assert_eq!(format!("{:#x}", LongFlags::LONG_A), "0xffff");
1526 let extra = unsafe { LongFlags::from_bits_unchecked(0xe00000) };
1527 assert_eq!(format!("{:x}", LongFlags::LONG_A | extra), "e0ffff");
1528 assert_eq!(format!("{:#x}", LongFlags::LONG_A | extra), "0xe0ffff");
1529 }
1530
1531 #[test]
1532 fn test_upperhex() {
1533 assert_eq!(format!("{:X}", LongFlags::LONG_A), "FFFF");
1534 assert_eq!(format!("{:#X}", LongFlags::LONG_A), "0xFFFF");
1535 let extra = unsafe { LongFlags::from_bits_unchecked(0xe00000) };
1536 assert_eq!(format!("{:X}", LongFlags::LONG_A | extra), "E0FFFF");
1537 assert_eq!(format!("{:#X}", LongFlags::LONG_A | extra), "0xE0FFFF");
1538 }
1539
1540 mod submodule {
1541 bitflags! {
1542 pub struct PublicFlags: i8 {
1543 const X = 0;
1544 }
1545
1546 struct PrivateFlags: i8 {
1547 const Y = 0;
1548 }
1549 }
1550
1551 #[test]
1552 fn test_private() {
1553 let _ = PrivateFlags::Y;
1554 }
1555 }
1556
1557 #[test]
1558 fn test_public() {
1559 let _ = submodule::PublicFlags::X;
1560 }
1561
1562 mod t1 {
1563 mod foo {
1564 pub type Bar = i32;
1565 }
1566
1567 bitflags! {
1568 /// baz
1569 struct Flags: foo::Bar {
1570 const A = 0b00000001;
1571 #[cfg(foo)]
1572 const B = 0b00000010;
1573 #[cfg(foo)]
1574 const C = 0b00000010;
1575 }
1576 }
1577 }
1578
1579 #[test]
1580 fn test_in_function() {
1581 bitflags! {
1582 struct Flags: u8 {
1583 const A = 1;
1584 #[cfg(any())] // false
1585 const B = 2;
1586 }
1587 }
1588 assert_eq!(Flags::all(), Flags::A);
1589 assert_eq!(format!("{:?}", Flags::A), "A");
1590 }
1591
1592 #[test]
1593 fn test_deprecated() {
1594 bitflags! {
1595 pub struct TestFlags: u32 {
1596 #[deprecated(note = "Use something else.")]
1597 const ONE = 1;
1598 }
1599 }
1600 }
1601
1602 #[test]
1603 fn test_pub_crate() {
1604 mod module {
1605 bitflags! {
1606 pub (crate) struct Test: u8 {
1607 const FOO = 1;
1608 }
1609 }
1610 }
1611
1612 assert_eq!(module::Test::FOO.bits(), 1);
1613 }
1614
1615 #[test]
1616 fn test_pub_in_module() {
1617 mod module {
1618 mod submodule {
1619 bitflags! {
1620 // `pub (in super)` means only the module `module` will
1621 // be able to access this.
1622 pub (in super) struct Test: u8 {
1623 const FOO = 1;
1624 }
1625 }
1626 }
1627
1628 mod test {
1629 // Note: due to `pub (in super)`,
1630 // this cannot be accessed directly by the testing code.
1631 pub(super) fn value() -> u8 {
1632 super::submodule::Test::FOO.bits()
1633 }
1634 }
1635
1636 pub fn value() -> u8 {
1637 test::value()
1638 }
1639 }
1640
1641 assert_eq!(module::value(), 1)
1642 }
1643
1644 #[test]
1645 fn test_zero_value_flags() {
1646 bitflags! {
1647 struct Flags: u32 {
1648 const NONE = 0b0;
1649 const SOME = 0b1;
1650 }
1651 }
1652
1653 assert!(Flags::empty().contains(Flags::NONE));
1654 assert!(Flags::SOME.contains(Flags::NONE));
1655 assert!(Flags::NONE.is_empty());
1656
1657 assert_eq!(format!("{:?}", Flags::empty()), "NONE");
1658 assert_eq!(format!("{:?}", Flags::SOME), "SOME");
1659 }
1660
1661 #[test]
1662 fn test_empty_bitflags() {
1663 bitflags! {}
1664 }
1665
1666 #[test]
1667 fn test_u128_bitflags() {
1668 bitflags! {
1669 struct Flags128: u128 {
1670 const A = 0x0000_0000_0000_0000_0000_0000_0000_0001;
1671 const B = 0x0000_0000_0000_1000_0000_0000_0000_0000;
1672 const C = 0x8000_0000_0000_0000_0000_0000_0000_0000;
1673 const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
1674 }
1675 }
1676
1677 assert_eq!(Flags128::ABC, Flags128::A | Flags128::B | Flags128::C);
1678 assert_eq!(Flags128::A.bits, 0x0000_0000_0000_0000_0000_0000_0000_0001);
1679 assert_eq!(Flags128::B.bits, 0x0000_0000_0000_1000_0000_0000_0000_0000);
1680 assert_eq!(Flags128::C.bits, 0x8000_0000_0000_0000_0000_0000_0000_0000);
1681 assert_eq!(
1682 Flags128::ABC.bits,
1683 0x8000_0000_0000_1000_0000_0000_0000_0001
1684 );
1685 assert_eq!(format!("{:?}", Flags128::A), "A");
1686 assert_eq!(format!("{:?}", Flags128::B), "B");
1687 assert_eq!(format!("{:?}", Flags128::C), "C");
1688 assert_eq!(format!("{:?}", Flags128::ABC), "A | B | C | ABC");
1689 }
1690
1691 #[test]
1692 fn test_serde_bitflags_serialize() {
1693 let flags = SerdeFlags::A | SerdeFlags::B;
1694
1695 let serialized = serde_json::to_string(&flags).unwrap();
1696
1697 assert_eq!(serialized, r#"{"bits":3}"#);
1698 }
1699
1700 #[test]
1701 fn test_serde_bitflags_deserialize() {
1702 let deserialized: SerdeFlags = serde_json::from_str(r#"{"bits":12}"#).unwrap();
1703
1704 let expected = SerdeFlags::C | SerdeFlags::D;
1705
1706 assert_eq!(deserialized.bits, expected.bits);
1707 }
1708
1709 #[test]
1710 fn test_serde_bitflags_roundtrip() {
1711 let flags = SerdeFlags::A | SerdeFlags::B;
1712
1713 let deserialized: SerdeFlags = serde_json::from_str(&serde_json::to_string(&flags).unwrap()).unwrap();
1714
1715 assert_eq!(deserialized.bits, flags.bits);
1716 }
1717
1718 bitflags! {
1719 #[derive(serde::Serialize, serde::Deserialize)]
1720 struct SerdeFlags: u32 {
1721 const A = 1;
1722 const B = 2;
1723 const C = 4;
1724 const D = 8;
1725 }
1726 }
1727 }