]> git.proxmox.com Git - rustc.git/blob - src/vendor/bitflags-0.5.0/src/lib.rs
New upstream version 1.19.0+dfsg3
[rustc.git] / src / vendor / bitflags-0.5.0 / 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.
12
13 // Compile the crate with no_std if possible. The "no_std" feature can be
14 // removed once no_std becomes available in 1.6.0 stable. In the meantime no_std
15 // must be disabled by default to allow the crate to work on older rust versions.
16 #![cfg_attr(all(feature = "no_std", not(test)), no_std)]
17
18 #![cfg_attr(feature = "assignment_operators", feature(augmented_assignments))]
19 #![cfg_attr(all(feature = "assignment_operators", test), feature(op_assign_traits))]
20
21 #[cfg(all(feature = "no_std", not(test)))]
22 #[macro_use]
23 extern crate core as std;
24
25 // Re-export libstd/libcore using an alias so that the macros can work in no_std
26 // crates while remaining compatible with normal crates.
27 #[doc(hidden)]
28 pub use std as __core;
29
30 /// The `bitflags!` macro generates a `struct` that holds a set of C-style
31 /// bitmask flags. It is useful for creating typesafe wrappers for C APIs.
32 ///
33 /// The flags should only be defined for integer types, otherwise unexpected
34 /// type errors may occur at compile time.
35 ///
36 /// # Example
37 ///
38 /// ```{.rust}
39 /// #![cfg_attr(feature = "assignment_operators", feature(augmented_assignments, op_assign_traits))]
40 /// #[macro_use]
41 /// extern crate bitflags;
42 ///
43 /// bitflags! {
44 /// flags Flags: u32 {
45 /// const FLAG_A = 0b00000001,
46 /// const FLAG_B = 0b00000010,
47 /// const FLAG_C = 0b00000100,
48 /// const FLAG_ABC = FLAG_A.bits
49 /// | FLAG_B.bits
50 /// | FLAG_C.bits,
51 /// }
52 /// }
53 ///
54 /// fn main() {
55 /// let e1 = FLAG_A | FLAG_C;
56 /// let e2 = FLAG_B | FLAG_C;
57 /// assert!((e1 | e2) == FLAG_ABC); // union
58 /// assert!((e1 & e2) == FLAG_C); // intersection
59 /// assert!((e1 - e2) == FLAG_A); // set difference
60 /// assert!(!e2 == FLAG_A); // set complement
61 /// }
62 /// ```
63 ///
64 /// The generated `struct`s can also be extended with type and trait
65 /// implementations:
66 ///
67 /// ```{.rust}
68 /// #![cfg_attr(feature = "assignment_operators", feature(augmented_assignments, op_assign_traits))]
69 /// #[macro_use]
70 /// extern crate bitflags;
71 ///
72 /// use std::fmt;
73 ///
74 /// bitflags! {
75 /// flags Flags: u32 {
76 /// const FLAG_A = 0b00000001,
77 /// const FLAG_B = 0b00000010,
78 /// }
79 /// }
80 ///
81 /// impl Flags {
82 /// pub fn clear(&mut self) {
83 /// self.bits = 0; // The `bits` field can be accessed from within the
84 /// // same module where the `bitflags!` macro was invoked.
85 /// }
86 /// }
87 ///
88 /// impl fmt::Display for Flags {
89 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90 /// write!(f, "hi!")
91 /// }
92 /// }
93 ///
94 /// fn main() {
95 /// let mut flags = FLAG_A | FLAG_B;
96 /// flags.clear();
97 /// assert!(flags.is_empty());
98 /// assert_eq!(format!("{}", flags), "hi!");
99 /// assert_eq!(format!("{:?}", FLAG_A | FLAG_B), "FLAG_A | FLAG_B");
100 /// assert_eq!(format!("{:?}", FLAG_B), "FLAG_B");
101 /// }
102 /// ```
103 ///
104 /// # Visibility
105 ///
106 /// The generated struct and its associated flag constants are not exported
107 /// out of the current module by default. A definition can be exported out of
108 /// the current module by adding `pub` before `flags`:
109 ///
110 /// ```{.rust},ignore
111 /// #[macro_use]
112 /// extern crate bitflags;
113 ///
114 /// mod example {
115 /// bitflags! {
116 /// pub flags Flags1: u32 {
117 /// const FLAG_A = 0b00000001,
118 /// }
119 /// }
120 /// bitflags! {
121 /// flags Flags2: u32 {
122 /// const FLAG_B = 0b00000010,
123 /// }
124 /// }
125 /// }
126 ///
127 /// fn main() {
128 /// let flag1 = example::FLAG_A;
129 /// let flag2 = example::FLAG_B; // error: const `FLAG_B` is private
130 /// }
131 /// ```
132 ///
133 /// # Attributes
134 ///
135 /// Attributes can be attached to the generated `struct` by placing them
136 /// before the `flags` keyword.
137 ///
138 /// # Trait implementations
139 ///
140 /// The `Copy`, `Clone`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash`
141 /// traits automatically derived for the `struct` using the `derive` attribute.
142 /// Additional traits can be derived by providing an explicit `derive`
143 /// attribute on `flags`.
144 ///
145 /// The `FromIterator` trait is implemented for the `struct`, too, calculating
146 /// the union of the instances of the `struct` iterated over.
147 ///
148 /// The `Debug` trait is also implemented by displaying the bits value of the
149 /// internal struct.
150 ///
151 /// ## Operators
152 ///
153 /// The following operator traits are implemented for the generated `struct`:
154 ///
155 /// - `BitOr` and `BitOrAssign`: union
156 /// - `BitAnd` and `BitAndAssign`: intersection
157 /// - `BitXor` and `BitXorAssign`: toggle
158 /// - `Sub` and `SubAssign`: set difference
159 /// - `Not`: set complement
160 ///
161 /// As long as the assignment operators are unstable rust feature they are only
162 /// available with the crate feature `assignment_ops` enabled.
163 ///
164 /// # Methods
165 ///
166 /// The following methods are defined for the generated `struct`:
167 ///
168 /// - `empty`: an empty set of flags
169 /// - `all`: the set of all flags
170 /// - `bits`: the raw value of the flags currently stored
171 /// - `from_bits`: convert from underlying bit representation, unless that
172 /// representation contains bits that do not correspond to a flag
173 /// - `from_bits_truncate`: convert from underlying bit representation, dropping
174 /// any bits that do not correspond to flags
175 /// - `is_empty`: `true` if no flags are currently stored
176 /// - `is_all`: `true` if all flags are currently set
177 /// - `intersects`: `true` if there are flags common to both `self` and `other`
178 /// - `contains`: `true` all of the flags in `other` are contained within `self`
179 /// - `insert`: inserts the specified flags in-place
180 /// - `remove`: removes the specified flags in-place
181 /// - `toggle`: the specified flags will be inserted if not present, and removed
182 /// if they are.
183 #[macro_export]
184 macro_rules! bitflags {
185 ($(#[$attr:meta])* pub flags $BitFlags:ident: $T:ty {
186 $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+
187 }) => {
188 #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
189 $(#[$attr])*
190 pub struct $BitFlags {
191 bits: $T,
192 }
193
194 $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+
195
196 bitflags! {
197 @_impl flags $BitFlags: $T {
198 $($(#[$Flag_attr])* const $Flag = $value),+
199 }
200 }
201 };
202 ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
203 $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+
204 }) => {
205 #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
206 $(#[$attr])*
207 struct $BitFlags {
208 bits: $T,
209 }
210
211 $($(#[$Flag_attr])* const $Flag: $BitFlags = $BitFlags { bits: $value };)+
212
213 bitflags! {
214 @_impl flags $BitFlags: $T {
215 $($(#[$Flag_attr])* const $Flag = $value),+
216 }
217 }
218 };
219 (@_impl flags $BitFlags:ident: $T:ty {
220 $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+
221 }) => {
222 impl $crate::__core::fmt::Debug for $BitFlags {
223 fn fmt(&self, f: &mut $crate::__core::fmt::Formatter) -> $crate::__core::fmt::Result {
224 // This convoluted approach is to handle #[cfg]-based flag
225 // omission correctly. Some of the $Flag variants may not be
226 // defined in this module so we create an inner module which
227 // defines *all* flags to the value of 0. We then create a
228 // second inner module that defines all of the flags with #[cfg]
229 // to their real values. Afterwards the glob will import
230 // variants from the second inner module, shadowing all
231 // defined variants, leaving only the undefined ones with the
232 // bit value of 0.
233 #[allow(dead_code)]
234 #[allow(unused_assignments)]
235 mod dummy {
236 // We can't use the real $BitFlags struct because it may be
237 // private, which prevents us from using it to define
238 // public constants.
239 pub struct $BitFlags {
240 bits: $T,
241 }
242 mod real_flags {
243 use super::$BitFlags;
244 $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+
245 }
246 // Now we define the "undefined" versions of the flags.
247 // This way, all the names exist, even if some are #[cfg]ed
248 // out.
249 $(const $Flag: $BitFlags = $BitFlags { bits: 0 };)+
250
251 #[inline]
252 pub fn fmt(self_: $T,
253 f: &mut $crate::__core::fmt::Formatter)
254 -> $crate::__core::fmt::Result {
255 // Now we import the real values for the flags.
256 // Only ones that are #[cfg]ed out will be 0.
257 use self::real_flags::*;
258
259 let mut first = true;
260 $(
261 // $Flag.bits == 0 means that $Flag doesn't exist
262 if $Flag.bits != 0 && self_ & $Flag.bits == $Flag.bits {
263 if !first {
264 try!(f.write_str(" | "));
265 }
266 first = false;
267 try!(f.write_str(stringify!($Flag)));
268 }
269 )+
270 Ok(())
271 }
272 }
273 dummy::fmt(self.bits, f)
274 }
275 }
276
277 #[allow(dead_code)]
278 impl $BitFlags {
279 /// Returns an empty set of flags.
280 #[inline]
281 pub fn empty() -> $BitFlags {
282 $BitFlags { bits: 0 }
283 }
284
285 /// Returns the set containing all flags.
286 #[inline]
287 pub fn all() -> $BitFlags {
288 // See above `dummy` module for why this approach is taken.
289 #[allow(dead_code)]
290 mod dummy {
291 pub struct $BitFlags {
292 bits: $T,
293 }
294 mod real_flags {
295 use super::$BitFlags;
296 $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+
297 }
298 $(const $Flag: $BitFlags = $BitFlags { bits: 0 };)+
299
300 #[inline]
301 pub fn all() -> $T {
302 use self::real_flags::*;
303 $($Flag.bits)|+
304 }
305 }
306 $BitFlags { bits: dummy::all() }
307 }
308
309 /// Returns the raw value of the flags currently stored.
310 #[inline]
311 pub fn bits(&self) -> $T {
312 self.bits
313 }
314
315 /// Convert from underlying bit representation, unless that
316 /// representation contains bits that do not correspond to a flag.
317 #[inline]
318 pub fn from_bits(bits: $T) -> $crate::__core::option::Option<$BitFlags> {
319 if (bits & !$BitFlags::all().bits()) != 0 {
320 $crate::__core::option::Option::None
321 } else {
322 $crate::__core::option::Option::Some($BitFlags { bits: bits })
323 }
324 }
325
326 /// Convert from underlying bit representation, dropping any bits
327 /// that do not correspond to flags.
328 #[inline]
329 pub fn from_bits_truncate(bits: $T) -> $BitFlags {
330 $BitFlags { bits: bits } & $BitFlags::all()
331 }
332
333 /// Returns `true` if no flags are currently stored.
334 #[inline]
335 pub fn is_empty(&self) -> bool {
336 *self == $BitFlags::empty()
337 }
338
339 /// Returns `true` if all flags are currently set.
340 #[inline]
341 pub fn is_all(&self) -> bool {
342 *self == $BitFlags::all()
343 }
344
345 /// Returns `true` if there are flags common to both `self` and `other`.
346 #[inline]
347 pub fn intersects(&self, other: $BitFlags) -> bool {
348 !(*self & other).is_empty()
349 }
350
351 /// Returns `true` all of the flags in `other` are contained within `self`.
352 #[inline]
353 pub fn contains(&self, other: $BitFlags) -> bool {
354 (*self & other) == other
355 }
356
357 /// Inserts the specified flags in-place.
358 #[inline]
359 pub fn insert(&mut self, other: $BitFlags) {
360 self.bits |= other.bits;
361 }
362
363 /// Removes the specified flags in-place.
364 #[inline]
365 pub fn remove(&mut self, other: $BitFlags) {
366 self.bits &= !other.bits;
367 }
368
369 /// Toggles the specified flags in-place.
370 #[inline]
371 pub fn toggle(&mut self, other: $BitFlags) {
372 self.bits ^= other.bits;
373 }
374 }
375
376 impl $crate::__core::ops::BitOr for $BitFlags {
377 type Output = $BitFlags;
378
379 /// Returns the union of the two sets of flags.
380 #[inline]
381 fn bitor(self, other: $BitFlags) -> $BitFlags {
382 $BitFlags { bits: self.bits | other.bits }
383 }
384 }
385
386 #[cfg(feature="assignment_operators")]
387 impl $crate::__core::ops::BitOrAssign for $BitFlags {
388
389 /// Adds the set of flags.
390 #[inline]
391 fn bitor_assign(&mut self, other: $BitFlags) {
392 self.bits |= other.bits;
393 }
394 }
395
396 impl $crate::__core::ops::BitXor for $BitFlags {
397 type Output = $BitFlags;
398
399 /// Returns the left flags, but with all the right flags toggled.
400 #[inline]
401 fn bitxor(self, other: $BitFlags) -> $BitFlags {
402 $BitFlags { bits: self.bits ^ other.bits }
403 }
404 }
405
406 #[cfg(feature="assignment_operators")]
407 impl $crate::__core::ops::BitXorAssign for $BitFlags {
408
409 /// Toggles the set of flags.
410 #[inline]
411 fn bitxor_assign(&mut self, other: $BitFlags) {
412 self.bits ^= other.bits;
413 }
414 }
415
416 impl $crate::__core::ops::BitAnd for $BitFlags {
417 type Output = $BitFlags;
418
419 /// Returns the intersection between the two sets of flags.
420 #[inline]
421 fn bitand(self, other: $BitFlags) -> $BitFlags {
422 $BitFlags { bits: self.bits & other.bits }
423 }
424 }
425
426 #[cfg(feature="assignment_operators")]
427 impl $crate::__core::ops::BitAndAssign for $BitFlags {
428
429 /// Disables all flags disabled in the set.
430 #[inline]
431 fn bitand_assign(&mut self, other: $BitFlags) {
432 self.bits &= other.bits;
433 }
434 }
435
436 impl $crate::__core::ops::Sub for $BitFlags {
437 type Output = $BitFlags;
438
439 /// Returns the set difference of the two sets of flags.
440 #[inline]
441 fn sub(self, other: $BitFlags) -> $BitFlags {
442 $BitFlags { bits: self.bits & !other.bits }
443 }
444 }
445
446 #[cfg(feature="assignment_operators")]
447 impl $crate::__core::ops::SubAssign for $BitFlags {
448
449 /// Disables all flags enabled in the set.
450 #[inline]
451 fn sub_assign(&mut self, other: $BitFlags) {
452 self.bits &= !other.bits;
453 }
454 }
455
456 impl $crate::__core::ops::Not for $BitFlags {
457 type Output = $BitFlags;
458
459 /// Returns the complement of this set of flags.
460 #[inline]
461 fn not(self) -> $BitFlags {
462 $BitFlags { bits: !self.bits } & $BitFlags::all()
463 }
464 }
465
466 impl $crate::__core::iter::FromIterator<$BitFlags> for $BitFlags {
467 fn from_iter<T: $crate::__core::iter::IntoIterator<Item=$BitFlags>>(iterator: T) -> $BitFlags {
468 let mut result = Self::empty();
469 for item in iterator {
470 result.insert(item)
471 }
472 result
473 }
474 }
475 };
476 ($(#[$attr:meta])* pub flags $BitFlags:ident: $T:ty {
477 $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+,
478 }) => {
479 bitflags! {
480 $(#[$attr])*
481 pub flags $BitFlags: $T {
482 $($(#[$Flag_attr])* const $Flag = $value),+
483 }
484 }
485 };
486 ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
487 $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+,
488 }) => {
489 bitflags! {
490 $(#[$attr])*
491 flags $BitFlags: $T {
492 $($(#[$Flag_attr])* const $Flag = $value),+
493 }
494 }
495 };
496 }
497
498 #[cfg(test)]
499 #[allow(non_upper_case_globals, dead_code)]
500 mod tests {
501 use std::hash::{SipHasher, Hash, Hasher};
502
503 bitflags! {
504 #[doc = "> The first principle is that you must not fool yourself — and"]
505 #[doc = "> you are the easiest person to fool."]
506 #[doc = "> "]
507 #[doc = "> - Richard Feynman"]
508 flags Flags: u32 {
509 const FlagA = 0b00000001,
510 #[doc = "<pcwalton> macros are way better at generating code than trans is"]
511 const FlagB = 0b00000010,
512 const FlagC = 0b00000100,
513 #[doc = "* cmr bed"]
514 #[doc = "* strcat table"]
515 #[doc = "<strcat> wait what?"]
516 const FlagABC = FlagA.bits
517 | FlagB.bits
518 | FlagC.bits,
519 }
520 }
521
522 bitflags! {
523 flags _CfgFlags: u32 {
524 #[cfg(windows)]
525 const _CfgA = 0b01,
526 #[cfg(unix)]
527 const _CfgB = 0b01,
528 #[cfg(windows)]
529 const _CfgC = _CfgA.bits | 0b10,
530 }
531 }
532
533 bitflags! {
534 flags AnotherSetOfFlags: i8 {
535 const AnotherFlag = -1_i8,
536 }
537 }
538
539 #[test]
540 fn test_bits(){
541 assert_eq!(Flags::empty().bits(), 0b00000000);
542 assert_eq!(FlagA.bits(), 0b00000001);
543 assert_eq!(FlagABC.bits(), 0b00000111);
544
545 assert_eq!(AnotherSetOfFlags::empty().bits(), 0b00);
546 assert_eq!(AnotherFlag.bits(), !0_i8);
547 }
548
549 #[test]
550 fn test_from_bits() {
551 assert!(Flags::from_bits(0) == Some(Flags::empty()));
552 assert!(Flags::from_bits(0b1) == Some(FlagA));
553 assert!(Flags::from_bits(0b10) == Some(FlagB));
554 assert!(Flags::from_bits(0b11) == Some(FlagA | FlagB));
555 assert!(Flags::from_bits(0b1000) == None);
556
557 assert!(AnotherSetOfFlags::from_bits(!0_i8) == Some(AnotherFlag));
558 }
559
560 #[test]
561 fn test_from_bits_truncate() {
562 assert!(Flags::from_bits_truncate(0) == Flags::empty());
563 assert!(Flags::from_bits_truncate(0b1) == FlagA);
564 assert!(Flags::from_bits_truncate(0b10) == FlagB);
565 assert!(Flags::from_bits_truncate(0b11) == (FlagA | FlagB));
566 assert!(Flags::from_bits_truncate(0b1000) == Flags::empty());
567 assert!(Flags::from_bits_truncate(0b1001) == FlagA);
568
569 assert!(AnotherSetOfFlags::from_bits_truncate(0_i8) == AnotherSetOfFlags::empty());
570 }
571
572 #[test]
573 fn test_is_empty(){
574 assert!(Flags::empty().is_empty());
575 assert!(!FlagA.is_empty());
576 assert!(!FlagABC.is_empty());
577
578 assert!(!AnotherFlag.is_empty());
579 }
580
581 #[test]
582 fn test_is_all() {
583 assert!(Flags::all().is_all());
584 assert!(!FlagA.is_all());
585 assert!(FlagABC.is_all());
586
587 assert!(AnotherFlag.is_all());
588 }
589
590 #[test]
591 fn test_two_empties_do_not_intersect() {
592 let e1 = Flags::empty();
593 let e2 = Flags::empty();
594 assert!(!e1.intersects(e2));
595
596 assert!(AnotherFlag.intersects(AnotherFlag));
597 }
598
599 #[test]
600 fn test_empty_does_not_intersect_with_full() {
601 let e1 = Flags::empty();
602 let e2 = FlagABC;
603 assert!(!e1.intersects(e2));
604 }
605
606 #[test]
607 fn test_disjoint_intersects() {
608 let e1 = FlagA;
609 let e2 = FlagB;
610 assert!(!e1.intersects(e2));
611 }
612
613 #[test]
614 fn test_overlapping_intersects() {
615 let e1 = FlagA;
616 let e2 = FlagA | FlagB;
617 assert!(e1.intersects(e2));
618 }
619
620 #[test]
621 fn test_contains() {
622 let e1 = FlagA;
623 let e2 = FlagA | FlagB;
624 assert!(!e1.contains(e2));
625 assert!(e2.contains(e1));
626 assert!(FlagABC.contains(e2));
627
628 assert!(AnotherFlag.contains(AnotherFlag));
629 }
630
631 #[test]
632 fn test_insert(){
633 let mut e1 = FlagA;
634 let e2 = FlagA | FlagB;
635 e1.insert(e2);
636 assert!(e1 == e2);
637
638 let mut e3 = AnotherSetOfFlags::empty();
639 e3.insert(AnotherFlag);
640 assert!(e3 == AnotherFlag);
641 }
642
643 #[test]
644 fn test_remove(){
645 let mut e1 = FlagA | FlagB;
646 let e2 = FlagA | FlagC;
647 e1.remove(e2);
648 assert!(e1 == FlagB);
649
650 let mut e3 = AnotherFlag;
651 e3.remove(AnotherFlag);
652 assert!(e3 == AnotherSetOfFlags::empty());
653 }
654
655 #[test]
656 fn test_operators() {
657 let e1 = FlagA | FlagC;
658 let e2 = FlagB | FlagC;
659 assert!((e1 | e2) == FlagABC); // union
660 assert!((e1 & e2) == FlagC); // intersection
661 assert!((e1 - e2) == FlagA); // set difference
662 assert!(!e2 == FlagA); // set complement
663 assert!(e1 ^ e2 == FlagA | FlagB); // toggle
664 let mut e3 = e1;
665 e3.toggle(e2);
666 assert!(e3 == FlagA | FlagB);
667
668 let mut m4 = AnotherSetOfFlags::empty();
669 m4.toggle(AnotherSetOfFlags::empty());
670 assert!(m4 == AnotherSetOfFlags::empty());
671 }
672
673 #[cfg(feature="assignment_operators")]
674 #[test]
675 fn test_assignment_operators() {
676 let mut m1 = Flags::empty();
677 let e1 = FlagA | FlagC;
678 // union
679 m1 |= FlagA;
680 assert!(m1 == FlagA);
681 // intersection
682 m1 &= e1;
683 assert!(m1 == FlagA);
684 // set difference
685 m1 -= m1;
686 assert!(m1 == Flags::empty());
687 // toggle
688 m1 ^= e1;
689 assert!(m1 == e1);
690 }
691
692 #[test]
693 fn test_from_iterator() {
694 assert_eq!([].iter().cloned().collect::<Flags>(), Flags::empty());
695 assert_eq!([FlagA, FlagB].iter().cloned().collect::<Flags>(), FlagA | FlagB);
696 assert_eq!([FlagA, FlagABC].iter().cloned().collect::<Flags>(), FlagABC);
697 }
698
699 #[test]
700 fn test_lt() {
701 let mut a = Flags::empty();
702 let mut b = Flags::empty();
703
704 assert!(!(a < b) && !(b < a));
705 b = FlagB;
706 assert!(a < b);
707 a = FlagC;
708 assert!(!(a < b) && b < a);
709 b = FlagC | FlagB;
710 assert!(a < b);
711 }
712
713 #[test]
714 fn test_ord() {
715 let mut a = Flags::empty();
716 let mut b = Flags::empty();
717
718 assert!(a <= b && a >= b);
719 a = FlagA;
720 assert!(a > b && a >= b);
721 assert!(b < a && b <= a);
722 b = FlagB;
723 assert!(b > a && b >= a);
724 assert!(a < b && a <= b);
725 }
726
727 fn hash<T: Hash>(t: &T) -> u64 {
728 let mut s = SipHasher::new_with_keys(0, 0);
729 t.hash(&mut s);
730 s.finish()
731 }
732
733 #[test]
734 fn test_hash() {
735 let mut x = Flags::empty();
736 let mut y = Flags::empty();
737 assert!(hash(&x) == hash(&y));
738 x = Flags::all();
739 y = FlagABC;
740 assert!(hash(&x) == hash(&y));
741 }
742
743 #[test]
744 fn test_debug() {
745 assert_eq!(format!("{:?}", FlagA | FlagB), "FlagA | FlagB");
746 assert_eq!(format!("{:?}", FlagABC), "FlagA | FlagB | FlagC | FlagABC");
747 }
748
749 mod submodule {
750 bitflags! {
751 pub flags PublicFlags: i8 {
752 const FlagX = 0,
753 }
754 }
755 bitflags! {
756 flags PrivateFlags: i8 {
757 const FlagY = 0,
758 }
759 }
760
761 #[test]
762 fn test_private() {
763 let _ = FlagY;
764 }
765 }
766
767 #[test]
768 fn test_public() {
769 let _ = submodule::FlagX;
770 }
771 }