|
26 | 26 | //! |
27 | 27 | //! The `bitflags` crate has long been part of the Rust ecosystem. |
28 | 28 | //! Unfortunately, it doesn't feel like natural Rust. The `bitflags` crate |
29 | | -//! uses a wierd struct format to define flags. Flags themselves are just |
| 29 | +//! uses a weird struct format to define flags. Flags themselves are just |
30 | 30 | //! integers constants, so there is little type-safety involved. But it doesn't |
31 | 31 | //! have any dependencies. It also allows you to define implied flags (otherwise |
32 | 32 | //! known as overlapping flags). |
@@ -310,13 +310,16 @@ pub trait Flags: |
310 | 310 | + BitXorAssign<Self::Type> |
311 | 311 | + Not<Output = Self::Type>; |
312 | 312 |
|
| 313 | + /// The zero value for this type (empty flagset). |
| 314 | + const ZERO: Self::Type; |
| 315 | + |
313 | 316 | /// A slice containing all the possible flag values. |
314 | 317 | const LIST: &'static [Self]; |
315 | 318 |
|
316 | 319 | /// Creates an empty `FlagSet` of this type |
317 | 320 | #[inline] |
318 | 321 | fn none() -> FlagSet<Self> { |
319 | | - FlagSet::default() |
| 322 | + FlagSet::empty() |
320 | 323 | } |
321 | 324 | } |
322 | 325 |
|
@@ -456,7 +459,7 @@ impl<F: Flags> Default for FlagSet<F> { |
456 | 459 | /// ``` |
457 | 460 | #[inline] |
458 | 461 | fn default() -> Self { |
459 | | - FlagSet(F::Type::default()) |
| 462 | + Self::empty() |
460 | 463 | } |
461 | 464 | } |
462 | 465 |
|
@@ -887,6 +890,31 @@ impl<F: Flags> FlagSet<F> { |
887 | 890 | FlagSet(bits) |
888 | 891 | } |
889 | 892 |
|
| 893 | + /// Creates a new, empty FlagSet. |
| 894 | + /// |
| 895 | + /// ``` |
| 896 | + /// use flagset::{FlagSet, flags}; |
| 897 | + /// |
| 898 | + /// flags! { |
| 899 | + /// enum Flag: u8 { |
| 900 | + /// Foo = 0b001, |
| 901 | + /// Bar = 0b010, |
| 902 | + /// Baz = 0b100 |
| 903 | + /// } |
| 904 | + /// } |
| 905 | + /// |
| 906 | + /// let set = FlagSet::<Flag>::empty(); |
| 907 | + /// assert!(set.is_empty()); |
| 908 | + /// assert!(!set.is_full()); |
| 909 | + /// assert!(!set.contains(Flag::Foo)); |
| 910 | + /// assert!(!set.contains(Flag::Bar)); |
| 911 | + /// assert!(!set.contains(Flag::Baz)); |
| 912 | + /// ``` |
| 913 | + #[inline] |
| 914 | + pub const fn empty() -> Self { |
| 915 | + FlagSet(F::ZERO) |
| 916 | + } |
| 917 | + |
890 | 918 | /// Creates a new FlagSet containing all possible flags. |
891 | 919 | /// |
892 | 920 | /// ``` |
@@ -1247,6 +1275,8 @@ macro_rules! flags { |
1247 | 1275 | impl $crate::Flags for $n { |
1248 | 1276 | type Type = $t; |
1249 | 1277 |
|
| 1278 | + const ZERO: Self::Type = 0; |
| 1279 | + |
1250 | 1280 | const LIST: &'static [Self] = &[$($n::$k),*]; |
1251 | 1281 | } |
1252 | 1282 |
|
|
0 commit comments