Lines Matching full:flags
11 //! A typesafe bitmask flag generator useful for sets of C-style bitmask flags.
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
24 //! struct Flags: u32 {
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
42 //! See [`example_generated::Flags`](./example_generated/struct.Flags.html) for documentation of co…
54 //! struct Flags: u32 {
60 //! impl Flags {
67 //! impl fmt::Display for Flags {
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");
126 //! struct Flags: u32 {
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
169 //! any bits that do not correspond to defined flags
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
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
184 //! - `union`: returns a new set of flags, containing any flags present in
186 //! - `difference`: returns a new set of flags, containing all flags present in
187 //! `self` without any of the flags present in `other` (the
189 //! - `symmetric_difference`: returns a new set of flags, containing all flags
192 //! - `complement`: returns a new set of flags, containing all flags which are
208 //! struct Flags: u32 {
216 //! let derived_default: Flags = Default::default();
227 //! struct Flags: u32 {
235 //! impl Default for Flags {
236 //! fn default() -> Flags {
237 //! Flags::A | Flags::C
242 //! let implemented_default: Flags = Default::default();
243 //! assert_eq!(implemented_default, (Flags::A | Flags::C));
247 //! # Zero Flags
249 //! Flags with a value equal to zero will have some strange behavior that one should be aware of.
255 //! struct Flags: u32 {
262 //! let empty = Flags::empty();
263 //! let none = Flags::NONE;
264 //! let some = Flags::SOME;
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));
271 //! // Zero flags will be ignored when testing for emptiness
294 /// struct Flags: u32 {
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
321 /// struct Flags: u32 {
327 /// impl Flags {
334 /// impl fmt::Display for Flags {
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");
451 // Conditionally override the check for just those flags that
524 /// Returns an empty set of flags.
530 /// Returns the set containing all flags.
543 /// Returns the raw value of the flags currently stored.
561 /// that do not correspond to flags.
583 /// Returns `true` if no flags are currently stored.
589 /// Returns `true` if all flags are currently set.
595 /// Returns `true` if there are flags common to both `self` and `other`.
601 /// Returns `true` if all of the flags in `other` are contained within `self`.
607 /// Inserts the specified flags in-place.
613 /// Removes the specified flags in-place.
619 /// Toggles the specified flags in-place.
625 /// Inserts or removes the specified flags depending on the passed value.
635 /// Returns the intersection between the flags in `self` and
638 /// Specifically, the returned set contains only the flags which are
642 /// [`ops::BitAnd`]), as in `flags & other`.
651 /// Returns the union of between the flags in `self` and `other`.
653 /// Specifically, the returned set contains all flags which are
659 /// [`ops::BitOr`]), as in `flags | other`.
668 /// Returns the difference between the flags in `self` and `other`.
670 /// Specifically, the returned set contains all flags present in
674 /// `flags & !other` (and this syntax is also supported).
677 /// [`ops::Sub`]), as in `flags - other`.
686 /// Returns the [symmetric difference][sym-diff] between the flags
689 /// Specifically, the returned set contains the flags present which
691 /// both. Equivalently, it contains the flags present in *exactly
695 /// [`ops::BitXor`]), as in `flags ^ other`.
705 /// Returns the complement of this set of flags.
707 /// Specifically, the returned set contains all the flags which are
714 /// [`ops::Not`]), as in `!flags`.
729 /// Returns the union of the two sets of flags.
737 /// Adds the set of flags.
747 /// Returns the left flags, but with all the right flags toggled.
755 /// Toggles the set of flags.
765 /// Returns the intersection between the two sets of flags.
773 /// Disables all flags disabled in the set.
783 /// Returns the set difference of the two sets of flags.
791 /// Disables all flags enabled in the set.
801 /// Returns the complement of this set of flags.
948 struct Flags: u32 {
984 assert_eq!(Flags::empty().bits(), 0b00000000); in test_bits()
985 assert_eq!(Flags::A.bits(), 0b00000001); in test_bits()
986 assert_eq!(Flags::ABC.bits(), 0b00000111); in test_bits()
996 assert_eq!(Flags::from_bits(0), Some(Flags::empty())); in test_from_bits()
997 assert_eq!(Flags::from_bits(0b1), Some(Flags::A)); in test_from_bits()
998 assert_eq!(Flags::from_bits(0b10), Some(Flags::B)); in test_from_bits()
999 assert_eq!(Flags::from_bits(0b11), Some(Flags::A | Flags::B)); in test_from_bits()
1000 assert_eq!(Flags::from_bits(0b1000), None); in test_from_bits()
1013 assert_eq!(Flags::from_bits_truncate(0), Flags::empty()); in test_from_bits_truncate()
1014 assert_eq!(Flags::from_bits_truncate(0b1), Flags::A); in test_from_bits_truncate()
1015 assert_eq!(Flags::from_bits_truncate(0b10), Flags::B); in test_from_bits_truncate()
1016 assert_eq!(Flags::from_bits_truncate(0b11), (Flags::A | Flags::B)); in test_from_bits_truncate()
1017 assert_eq!(Flags::from_bits_truncate(0b1000), Flags::empty()); in test_from_bits_truncate()
1018 assert_eq!(Flags::from_bits_truncate(0b1001), Flags::A); in test_from_bits_truncate()
1031 let extra = unsafe { Flags::from_bits_unchecked(0b1000) }; in test_from_bits_unchecked()
1032 assert_eq!(unsafe { Flags::from_bits_unchecked(0) }, Flags::empty()); in test_from_bits_unchecked()
1033 assert_eq!(unsafe { Flags::from_bits_unchecked(0b1) }, Flags::A); in test_from_bits_unchecked()
1034 assert_eq!(unsafe { Flags::from_bits_unchecked(0b10) }, Flags::B); in test_from_bits_unchecked()
1037 unsafe { Flags::from_bits_unchecked(0b11) }, in test_from_bits_unchecked()
1038 (Flags::A | Flags::B) in test_from_bits_unchecked()
1041 unsafe { Flags::from_bits_unchecked(0b1000) }, in test_from_bits_unchecked()
1042 (extra | Flags::empty()) in test_from_bits_unchecked()
1045 unsafe { Flags::from_bits_unchecked(0b1001) }, in test_from_bits_unchecked()
1046 (extra | Flags::A) in test_from_bits_unchecked()
1058 assert!(Flags::empty().is_empty()); in test_is_empty()
1059 assert!(!Flags::A.is_empty()); in test_is_empty()
1060 assert!(!Flags::ABC.is_empty()); in test_is_empty()
1070 assert!(Flags::all().is_all()); in test_is_all()
1071 assert!(!Flags::A.is_all()); in test_is_all()
1072 assert!(Flags::ABC.is_all()); in test_is_all()
1074 let extra = unsafe { Flags::from_bits_unchecked(0b1000) }; in test_is_all()
1076 assert!(!(Flags::A | extra).is_all()); in test_is_all()
1077 assert!((Flags::ABC | extra).is_all()); in test_is_all()
1087 let e1 = Flags::empty(); in test_two_empties_do_not_intersect()
1088 let e2 = Flags::empty(); in test_two_empties_do_not_intersect()
1096 let e1 = Flags::empty(); in test_empty_does_not_intersect_with_full()
1097 let e2 = Flags::ABC; in test_empty_does_not_intersect_with_full()
1103 let e1 = Flags::A; in test_disjoint_intersects()
1104 let e2 = Flags::B; in test_disjoint_intersects()
1110 let e1 = Flags::A; in test_overlapping_intersects()
1111 let e2 = Flags::A | Flags::B; in test_overlapping_intersects()
1117 let e1 = Flags::A; in test_contains()
1118 let e2 = Flags::A | Flags::B; in test_contains()
1121 assert!(Flags::ABC.contains(e2)); in test_contains()
1130 let mut e1 = Flags::A; in test_insert()
1131 let e2 = Flags::A | Flags::B; in test_insert()
1142 let mut e1 = Flags::A | Flags::B; in test_remove()
1143 let e2 = Flags::A | Flags::C; in test_remove()
1145 assert_eq!(e1, Flags::B); in test_remove()
1154 let e1 = Flags::A | Flags::C; in test_operators()
1155 let e2 = Flags::B | Flags::C; in test_operators()
1156 assert_eq!((e1 | e2), Flags::ABC); // union in test_operators()
1157 assert_eq!((e1 & e2), Flags::C); // intersection in test_operators()
1158 assert_eq!((e1 - e2), Flags::A); // set difference in test_operators()
1159 assert_eq!(!e2, Flags::A); // set complement in test_operators()
1160 assert_eq!(e1 ^ e2, Flags::A | Flags::B); // toggle in test_operators()
1163 assert_eq!(e3, Flags::A | Flags::B); in test_operators()
1172 let extra = unsafe { Flags::from_bits_unchecked(0b1000) }; in test_operators_unchecked()
1173 let e1 = Flags::A | Flags::C | extra; in test_operators_unchecked()
1174 let e2 = Flags::B | Flags::C; in test_operators_unchecked()
1175 assert_eq!((e1 | e2), (Flags::ABC | extra)); // union in test_operators_unchecked()
1176 assert_eq!((e1 & e2), Flags::C); // intersection in test_operators_unchecked()
1177 assert_eq!((e1 - e2), (Flags::A | extra)); // set difference in test_operators_unchecked()
1178 assert_eq!(!e2, Flags::A); // set complement in test_operators_unchecked()
1179 assert_eq!(!e1, Flags::B); // set complement in test_operators_unchecked()
1180 assert_eq!(e1 ^ e2, Flags::A | Flags::B | extra); // toggle in test_operators_unchecked()
1183 assert_eq!(e3, Flags::A | Flags::B | extra); in test_operators_unchecked()
1188 let ab = Flags::A.union(Flags::B); in test_set_ops_basic()
1189 let ac = Flags::A.union(Flags::C); in test_set_ops_basic()
1190 let bc = Flags::B.union(Flags::C); in test_set_ops_basic()
1195 assert_eq!(ab, Flags::B.union(Flags::A)); in test_set_ops_basic()
1196 assert_eq!(ac, Flags::C.union(Flags::A)); in test_set_ops_basic()
1197 assert_eq!(bc, Flags::C.union(Flags::B)); in test_set_ops_basic()
1199 assert_eq!(ac, Flags::A | Flags::C); in test_set_ops_basic()
1200 assert_eq!(bc, Flags::B | Flags::C); in test_set_ops_basic()
1201 assert_eq!(ab.union(bc), Flags::ABC); in test_set_ops_basic()
1203 assert_eq!(ac, Flags::A | Flags::C); in test_set_ops_basic()
1204 assert_eq!(bc, Flags::B | Flags::C); in test_set_ops_basic()
1207 assert_eq!(ac.union(bc), Flags::ABC); in test_set_ops_basic()
1208 assert_eq!(bc.union(ac), Flags::ABC); in test_set_ops_basic()
1211 assert_eq!(ac.intersection(bc), Flags::C); in test_set_ops_basic()
1212 assert_eq!(bc.intersection(ac), Flags::C); in test_set_ops_basic()
1216 assert_eq!(ac.difference(bc), Flags::A); in test_set_ops_basic()
1217 assert_eq!(bc.difference(ac), Flags::B); in test_set_ops_basic()
1220 assert_eq!(bc.complement(), Flags::A); in test_set_ops_basic()
1221 assert_eq!(ac.symmetric_difference(bc), Flags::A.union(Flags::B)); in test_set_ops_basic()
1222 assert_eq!(bc.symmetric_difference(ac), Flags::A.union(Flags::B)); in test_set_ops_basic()
1229 const INTERSECT: Flags = Flags::all().intersection(Flags::C); in test_set_ops_const()
1230 const UNION: Flags = Flags::A.union(Flags::C); in test_set_ops_const()
1231 const DIFFERENCE: Flags = Flags::all().difference(Flags::A); in test_set_ops_const()
1232 const COMPLEMENT: Flags = Flags::C.complement(); in test_set_ops_const()
1233 const SYM_DIFFERENCE: Flags = UNION.symmetric_difference(DIFFERENCE); in test_set_ops_const()
1234 assert_eq!(INTERSECT, Flags::C); in test_set_ops_const()
1235 assert_eq!(UNION, Flags::A | Flags::C); in test_set_ops_const()
1236 assert_eq!(DIFFERENCE, Flags::all() - Flags::A); in test_set_ops_const()
1237 assert_eq!(COMPLEMENT, !Flags::C); in test_set_ops_const()
1238 assert_eq!(SYM_DIFFERENCE, (Flags::A | Flags::C) ^ (Flags::all() - Flags::A)); in test_set_ops_const()
1243 let extra = unsafe { Flags::from_bits_unchecked(0b1000) }; in test_set_ops_unchecked()
1244 let e1 = Flags::A.union(Flags::C).union(extra); in test_set_ops_unchecked()
1245 let e2 = Flags::B.union(Flags::C); in test_set_ops_unchecked()
1247 assert_eq!(e1.union(e2), (Flags::ABC | extra)); in test_set_ops_unchecked()
1248 assert_eq!(e1.intersection(e2), Flags::C); in test_set_ops_unchecked()
1249 assert_eq!(e1.difference(e2), Flags::A | extra); in test_set_ops_unchecked()
1250 assert_eq!(e2.difference(e1), Flags::B); in test_set_ops_unchecked()
1251 assert_eq!(e2.complement(), Flags::A); in test_set_ops_unchecked()
1252 assert_eq!(e1.complement(), Flags::B); in test_set_ops_unchecked()
1253 assert_eq!(e1.symmetric_difference(e2), Flags::A | Flags::B | extra); // toggle in test_set_ops_unchecked()
1259 // especially around "unknown" flags (e.g. ones outside of `all()` in test_set_ops_exhaustive()
1261 // - when lhs and rhs both have different sets of unknown flags. in test_set_ops_exhaustive()
1262 // - unknown flags at both ends, and in the middle in test_set_ops_exhaustive()
1370 let mut e1 = Flags::A | Flags::C; in test_set()
1371 e1.set(Flags::B, true); in test_set()
1372 e1.set(Flags::C, false); in test_set()
1374 assert_eq!(e1, Flags::A | Flags::B); in test_set()
1379 let mut m1 = Flags::empty(); in test_assignment_operators()
1380 let e1 = Flags::A | Flags::C; in test_assignment_operators()
1382 m1 |= Flags::A; in test_assignment_operators()
1383 assert_eq!(m1, Flags::A); in test_assignment_operators()
1386 assert_eq!(m1, Flags::A); in test_assignment_operators()
1389 assert_eq!(m1, Flags::empty()); in test_assignment_operators()
1397 const _M1: Flags = Flags::empty(); in test_const_fn()
1399 const M2: Flags = Flags::A; in test_const_fn()
1400 assert_eq!(M2, Flags::A); in test_const_fn()
1402 const M3: Flags = Flags::C; in test_const_fn()
1403 assert_eq!(M3, Flags::C); in test_const_fn()
1408 let mut flags; in test_extend() localVariable
1410 flags = Flags::empty(); in test_extend()
1411 flags.extend([].iter().cloned()); in test_extend()
1412 assert_eq!(flags, Flags::empty()); in test_extend()
1414 flags = Flags::empty(); in test_extend()
1415 flags.extend([Flags::A, Flags::B].iter().cloned()); in test_extend()
1416 assert_eq!(flags, Flags::A | Flags::B); in test_extend()
1418 flags = Flags::A; in test_extend()
1419 flags.extend([Flags::A, Flags::B].iter().cloned()); in test_extend()
1420 assert_eq!(flags, Flags::A | Flags::B); in test_extend()
1422 flags = Flags::B; in test_extend()
1423 flags.extend([Flags::A, Flags::ABC].iter().cloned()); in test_extend()
1424 assert_eq!(flags, Flags::ABC); in test_extend()
1429 assert_eq!([].iter().cloned().collect::<Flags>(), Flags::empty()); in test_from_iterator()
1431 [Flags::A, Flags::B].iter().cloned().collect::<Flags>(), in test_from_iterator()
1432 Flags::A | Flags::B in test_from_iterator()
1435 [Flags::A, Flags::ABC].iter().cloned().collect::<Flags>(), in test_from_iterator()
1436 Flags::ABC in test_from_iterator()
1442 let mut a = Flags::empty(); in test_lt()
1443 let mut b = Flags::empty(); in test_lt()
1446 b = Flags::B; in test_lt()
1448 a = Flags::C; in test_lt()
1450 b = Flags::C | Flags::B; in test_lt()
1456 let mut a = Flags::empty(); in test_ord()
1457 let mut b = Flags::empty(); in test_ord()
1460 a = Flags::A; in test_ord()
1463 b = Flags::B; in test_ord()
1476 let mut x = Flags::empty(); in test_hash()
1477 let mut y = Flags::empty(); in test_hash()
1479 x = Flags::all(); in test_hash()
1480 y = Flags::ABC; in test_hash()
1486 assert_eq!(Flags::empty(), Flags::default()); in test_default()
1491 assert_eq!(format!("{:?}", Flags::A | Flags::B), "A | B"); in test_debug()
1492 assert_eq!(format!("{:?}", Flags::empty()), "(empty)"); in test_debug()
1493 assert_eq!(format!("{:?}", Flags::ABC), "A | B | C | ABC"); in test_debug()
1494 let extra = unsafe { Flags::from_bits_unchecked(0xb8) }; in test_debug()
1496 assert_eq!(format!("{:?}", Flags::A | extra), "A | 0xb8"); in test_debug()
1499 format!("{:?}", Flags::ABC | extra), in test_debug()
1508 assert_eq!(format!("{:b}", Flags::ABC), "111"); in test_binary()
1509 assert_eq!(format!("{:#b}", Flags::ABC), "0b111"); in test_binary()
1510 let extra = unsafe { Flags::from_bits_unchecked(0b1010000) }; in test_binary()
1511 assert_eq!(format!("{:b}", Flags::ABC | extra), "1010111"); in test_binary()
1512 assert_eq!(format!("{:#b}", Flags::ABC | extra), "0b1010111"); in test_binary()
1571 struct Flags: foo::Bar {
1584 struct Flags: u8 { in test_in_function()
1590 assert_eq!(Flags::all(), Flags::A); in test_in_function()
1591 assert_eq!(format!("{:?}", Flags::A), "A"); in test_in_function()
1649 struct Flags: u32 { in test_zero_value_flags()
1655 assert!(Flags::empty().contains(Flags::NONE)); in test_zero_value_flags()
1656 assert!(Flags::SOME.contains(Flags::NONE)); in test_zero_value_flags()
1657 assert!(Flags::NONE.is_empty()); in test_zero_value_flags()
1659 assert_eq!(format!("{:?}", Flags::empty()), "NONE"); in test_zero_value_flags()
1660 assert_eq!(format!("{:?}", Flags::SOME), "SOME"); in test_zero_value_flags()
1695 let flags = SerdeFlags::A | SerdeFlags::B; in test_serde_bitflags_serialize() localVariable
1697 let serialized = serde_json::to_string(&flags).unwrap(); in test_serde_bitflags_serialize()
1713 let flags = SerdeFlags::A | SerdeFlags::B; in test_serde_bitflags_roundtrip() localVariable
1715 …let deserialized: SerdeFlags = serde_json::from_str(&serde_json::to_string(&flags).unwrap()).unwra… in test_serde_bitflags_roundtrip()
1717 assert_eq!(deserialized.bits, flags.bits); in test_serde_bitflags_roundtrip()