Skip to content

Commit 8c72192

Browse files
committed
feat(#42): add a FlagSet::empty method
This is consistent with `bitflags` and mirrors the `FlagSet::full` method. Closes #42 Signed-off-by: Jalil David Salamé Messina <[email protected]>
1 parent d4335ab commit 8c72192

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/lib.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//!
2727
//! The `bitflags` crate has long been part of the Rust ecosystem.
2828
//! 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
3030
//! integers constants, so there is little type-safety involved. But it doesn't
3131
//! have any dependencies. It also allows you to define implied flags (otherwise
3232
//! known as overlapping flags).
@@ -310,13 +310,16 @@ pub trait Flags:
310310
+ BitXorAssign<Self::Type>
311311
+ Not<Output = Self::Type>;
312312

313+
/// The zero value for this type (empty flagset).
314+
const ZERO: Self::Type;
315+
313316
/// A slice containing all the possible flag values.
314317
const LIST: &'static [Self];
315318

316319
/// Creates an empty `FlagSet` of this type
317320
#[inline]
318321
fn none() -> FlagSet<Self> {
319-
FlagSet::default()
322+
FlagSet::empty()
320323
}
321324
}
322325

@@ -456,7 +459,7 @@ impl<F: Flags> Default for FlagSet<F> {
456459
/// ```
457460
#[inline]
458461
fn default() -> Self {
459-
FlagSet(F::Type::default())
462+
Self::empty()
460463
}
461464
}
462465

@@ -887,6 +890,31 @@ impl<F: Flags> FlagSet<F> {
887890
FlagSet(bits)
888891
}
889892

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+
890918
/// Creates a new FlagSet containing all possible flags.
891919
///
892920
/// ```
@@ -1247,6 +1275,8 @@ macro_rules! flags {
12471275
impl $crate::Flags for $n {
12481276
type Type = $t;
12491277

1278+
const ZERO: Self::Type = 0;
1279+
12501280
const LIST: &'static [Self] = &[$($n::$k),*];
12511281
}
12521282

0 commit comments

Comments
 (0)