#[repr(C)]pub struct FlagSet<F: Flags>(/* private fields */);
Implementations§
source§impl<F: Flags> FlagSet<F>
impl<F: Flags> FlagSet<F>
sourcepub fn new(bits: F::Type) -> Result<Self, InvalidBits>
pub fn new(bits: F::Type) -> Result<Self, InvalidBits>
Creates a new set from bits; returning Err(InvalidBits)
on invalid/unknown bits.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u16 {
Foo = 0b0001,
Bar = 0b0010,
Baz = 0b0100,
Qux = 0b1010, // Implies Bar
}
}
assert_eq!(FlagSet::<Flag>::new(0b00101), Ok(Flag::Foo | Flag::Baz));
assert_eq!(FlagSet::<Flag>::new(0b01101), Err(flagset::InvalidBits)); // Invalid
assert_eq!(FlagSet::<Flag>::new(0b10101), Err(flagset::InvalidBits)); // Unknown
sourcepub fn new_truncated(bits: F::Type) -> Self
pub fn new_truncated(bits: F::Type) -> Self
Creates a new set from bits; truncating invalid/unknown bits.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u16 {
Foo = 0b0001,
Bar = 0b0010,
Baz = 0b0100,
Qux = 0b1010, // Implies Bar
}
}
let set = FlagSet::new_truncated(0b11101); // Has invalid and unknown.
assert_eq!(set, Flag::Foo | Flag::Baz);
assert_eq!(set.bits(), 0b00101); // Has neither.
sourcepub const unsafe fn new_unchecked(bits: F::Type) -> Self
pub const unsafe fn new_unchecked(bits: F::Type) -> Self
Creates a new set from bits; use of invalid/unknown bits is undefined.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u16 {
Foo = 0b0001,
Bar = 0b0010,
Baz = 0b0100,
Qux = 0b1010, // Implies Bar
}
}
// Unknown and invalid bits are retained. Behavior is undefined.
const set: FlagSet<Flag> = unsafe { FlagSet::<Flag>::new_unchecked(0b11101) };
assert_eq!(set.bits(), 0b11101);
§Safety
This constructor doesn’t check that the bits are valid. If you pass undefined flags, undefined behavior may result.
sourcepub fn full() -> Self
pub fn full() -> Self
Creates a new FlagSet containing all possible flags.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let set = FlagSet::full();
assert!(!set.is_empty());
assert!(set.is_full());
assert!(set.contains(Flag::Foo));
assert!(set.contains(Flag::Bar));
assert!(set.contains(Flag::Baz));
sourcepub fn bits(self) -> F::Type
pub fn bits(self) -> F::Type
Returns the raw bits of the set.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u16 {
Foo = 0b0001,
Bar = 0b0010,
Baz = 0b0100,
}
}
let set = Flag::Foo | Flag::Baz;
assert_eq!(set.bits(), 0b0101u16);
sourcepub fn is_empty(self) -> bool
pub fn is_empty(self) -> bool
Returns true if the FlagSet contains no flags.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let mut set = Flag::Foo | Flag::Bar;
assert!(!set.is_empty());
set &= Flag::Baz;
assert!(set.is_empty());
sourcepub fn is_full(self) -> bool
pub fn is_full(self) -> bool
Returns true if the FlagSet contains all possible flags.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let mut set = Flag::Foo | Flag::Bar;
assert!(!set.is_full());
set |= Flag::Baz;
assert!(set.is_full());
sourcepub fn is_disjoint(self, rhs: impl Into<FlagSet<F>>) -> bool
pub fn is_disjoint(self, rhs: impl Into<FlagSet<F>>) -> bool
Returns true if the two FlagSet
s do not share any flags.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let set = Flag::Foo | Flag::Bar;
assert!(!set.is_disjoint(Flag::Foo));
assert!(!set.is_disjoint(Flag::Foo | Flag::Baz));
assert!(set.is_disjoint(Flag::Baz));
sourcepub fn contains(self, rhs: impl Into<FlagSet<F>>) -> bool
pub fn contains(self, rhs: impl Into<FlagSet<F>>) -> bool
Returns true if this FlagSet is a superset of the specified flags.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let set = Flag::Foo | Flag::Bar;
assert!(set.contains(Flag::Foo));
assert!(set.contains(Flag::Foo | Flag::Bar));
assert!(!set.contains(Flag::Foo | Flag::Bar | Flag::Baz));
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all flags from the FlagSet.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let mut set = Flag::Foo | Flag::Bar;
assert!(!set.is_empty());
set.clear();
assert!(set.is_empty());
sourcepub fn drain(&mut self) -> Iter<F>
pub fn drain(&mut self) -> Iter<F>
Clears the current set and returns an iterator of all removed flags.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let mut set = Flag::Foo | Flag::Bar;
let mut iter = set.drain();
assert!(set.is_empty());
assert_eq!(iter.next(), Some(Flag::Foo));
assert_eq!(iter.next(), Some(Flag::Bar));
assert_eq!(iter.next(), None);
sourcepub fn retain(&mut self, func: impl Fn(F) -> bool)
pub fn retain(&mut self, func: impl Fn(F) -> bool)
Retain only the flags flags specified by the predicate.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let mut set0 = Flag::Foo | Flag::Bar;
set0.retain(|f| f != Flag::Foo);
assert_eq!(set0, Flag::Bar);
Trait Implementations§
source§impl<F: Flags, R: Into<FlagSet<F>>> BitAnd<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitAnd<R> for FlagSet<F>
source§fn bitand(self, rhs: R) -> Self
fn bitand(self, rhs: R) -> Self
Calculates the intersection of the current set and the specified flags.
use flagset::{FlagSet, flags};
flags! {
#[derive(PartialOrd, Ord)]
pub enum Flag: u8 {
Foo = 0b001,
Bar = 0b010,
Baz = 0b100
}
}
let set0 = Flag::Foo | Flag::Bar;
let set1 = Flag::Baz | Flag::Bar;
assert_eq!(set0 & set1, Flag::Bar);
assert_eq!(set0 & Flag::Foo, Flag::Foo);
assert_eq!(set1 & Flag::Baz, Flag::Baz);
source§impl<F: Flags, R: Into<FlagSet<F>>> BitAndAssign<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitAndAssign<R> for FlagSet<F>
source§fn bitand_assign(&mut self, rhs: R)
fn bitand_assign(&mut self, rhs: R)
Assigns the intersection of the current set and the specified flags.
use flagset::{FlagSet, flags};
flags! {
enum Flag: u64 {
Foo = 0b001,
Bar = 0b010,
Baz = 0b100
}
}
let mut set0 = Flag::Foo | Flag::Bar;
let mut set1 = Flag::Baz | Flag::Bar;
set0 &= set1;
assert_eq!(set0, Flag::Bar);
set1 &= Flag::Baz;
assert_eq!(set0, Flag::Bar);
source§impl<F: Flags, R: Into<FlagSet<F>>> BitOr<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitOr<R> for FlagSet<F>
source§fn bitor(self, rhs: R) -> Self
fn bitor(self, rhs: R) -> Self
Calculates the union of the current set with the specified flags.
use flagset::{FlagSet, flags};
flags! {
#[derive(PartialOrd, Ord)]
pub enum Flag: u8 {
Foo = 0b001,
Bar = 0b010,
Baz = 0b100
}
}
let set0 = Flag::Foo | Flag::Bar;
let set1 = Flag::Baz | Flag::Bar;
assert_eq!(set0 | set1, FlagSet::full());
source§impl<F: Flags, R: Into<FlagSet<F>>> BitOrAssign<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitOrAssign<R> for FlagSet<F>
source§fn bitor_assign(&mut self, rhs: R)
fn bitor_assign(&mut self, rhs: R)
Assigns the union of the current set with the specified flags.
use flagset::{FlagSet, flags};
flags! {
enum Flag: u64 {
Foo = 0b001,
Bar = 0b010,
Baz = 0b100
}
}
let mut set0 = Flag::Foo | Flag::Bar;
let mut set1 = Flag::Bar | Flag::Baz;
set0 |= set1;
assert_eq!(set0, FlagSet::full());
set1 |= Flag::Baz;
assert_eq!(set1, Flag::Bar | Flag::Baz);
source§impl<F: Flags, R: Into<FlagSet<F>>> BitXor<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitXor<R> for FlagSet<F>
source§fn bitxor(self, rhs: R) -> Self
fn bitxor(self, rhs: R) -> Self
Calculates the current set with the specified flags toggled.
This is commonly known as toggling the presence
use flagset::{FlagSet, flags};
flags! {
enum Flag: u32 {
Foo = 0b001,
Bar = 0b010,
Baz = 0b100
}
}
let set0 = Flag::Foo | Flag::Bar;
let set1 = Flag::Baz | Flag::Bar;
assert_eq!(set0 ^ set1, Flag::Foo | Flag::Baz);
assert_eq!(set0 ^ Flag::Foo, Flag::Bar);
source§impl<F: Flags, R: Into<FlagSet<F>>> BitXorAssign<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitXorAssign<R> for FlagSet<F>
source§fn bitxor_assign(&mut self, rhs: R)
fn bitxor_assign(&mut self, rhs: R)
Assigns the current set with the specified flags toggled.
use flagset::{FlagSet, flags};
flags! {
enum Flag: u16 {
Foo = 0b001,
Bar = 0b010,
Baz = 0b100
}
}
let mut set0 = Flag::Foo | Flag::Bar;
let mut set1 = Flag::Baz | Flag::Bar;
set0 ^= set1;
assert_eq!(set0, Flag::Foo | Flag::Baz);
set1 ^= Flag::Baz;
assert_eq!(set1, Flag::Bar);
source§impl<F: Flags> Default for FlagSet<F>
impl<F: Flags> Default for FlagSet<F>
source§fn default() -> Self
fn default() -> Self
Creates a new, empty FlagSet.
use flagset::{FlagSet, flags};
flags! {
enum Flag: u8 {
Foo = 0b001,
Bar = 0b010,
Baz = 0b100
}
}
let set = FlagSet::<Flag>::default();
assert!(set.is_empty());
assert!(!set.is_full());
assert!(!set.contains(Flag::Foo));
assert!(!set.contains(Flag::Bar));
assert!(!set.contains(Flag::Baz));
source§impl<'de, F: Flags> Deserialize<'de> for FlagSet<F>where
F::Type: Deserialize<'de>,
Available on crate feature serde
only.
impl<'de, F: Flags> Deserialize<'de> for FlagSet<F>where
F::Type: Deserialize<'de>,
serde
only.source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl<F: Flags, R: Into<FlagSet<F>>> Extend<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> Extend<R> for FlagSet<F>
source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = R>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = R>,
Add values by iterating over some collection.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let flag_vec = vec![Flag::Bar, Flag::Baz];
let mut some_extended_flags = FlagSet::from(Flag::Foo);
some_extended_flags.extend(flag_vec);
assert_eq!(some_extended_flags, Flag::Foo | Flag::Bar | Flag::Baz);
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<F: Flags> From<Option<FlagSet<F>>> for FlagSet<F>
impl<F: Flags> From<Option<FlagSet<F>>> for FlagSet<F>
source§fn from(value: Option<FlagSet<F>>) -> FlagSet<F>
fn from(value: Option<FlagSet<F>>) -> FlagSet<F>
Converts from Option<FlagSet<F>>
to FlagSet<F>
.
Most notably, this allows for the use of None
in many places to
substitute for manually creating an empty FlagSet<F>
. See below.
use flagset::{FlagSet, flags};
flags! {
enum Flag: u8 {
Foo = 0b001,
Bar = 0b010,
Baz = 0b100
}
}
fn convert(v: impl Into<FlagSet<Flag>>) -> u8 {
v.into().bits()
}
assert_eq!(convert(Flag::Foo | Flag::Bar), 0b011);
assert_eq!(convert(Flag::Foo), 0b001);
assert_eq!(convert(None), 0b000);
source§impl<F: Flags> IntoIterator for FlagSet<F>
impl<F: Flags> IntoIterator for FlagSet<F>
source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Iterate over the flags in the set.
NOTE: The order in which the flags are iterated is undefined.
use flagset::{FlagSet, flags};
flags! {
enum Flag: u8 {
Foo = 0b001,
Bar = 0b010,
Baz = 0b100
}
}
let set = Flag::Foo | Flag::Bar;
let mut iter = set.into_iter();
assert_eq!(iter.next(), Some(Flag::Foo));
assert_eq!(iter.next(), Some(Flag::Bar));
assert_eq!(iter.next(), None);
source§impl<F: Flags> Not for FlagSet<F>
impl<F: Flags> Not for FlagSet<F>
source§fn not(self) -> Self
fn not(self) -> Self
Calculates the complement of the current set.
In common parlance, this returns the set of all possible flags that are not in the current set.
use flagset::{FlagSet, flags};
flags! {
#[derive(PartialOrd, Ord)]
enum Flag: u8 {
Foo = 1 << 0,
Bar = 1 << 1,
Baz = 1 << 2
}
}
let set = !FlagSet::from(Flag::Foo);
assert!(!set.is_empty());
assert!(!set.is_full());
assert!(!set.contains(Flag::Foo));
assert!(set.contains(Flag::Bar));
assert!(set.contains(Flag::Baz));
source§impl<F: Flags, R: Into<FlagSet<F>>> Rem<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> Rem<R> for FlagSet<F>
source§fn rem(self, rhs: R) -> Self
fn rem(self, rhs: R) -> Self
Calculates the symmetric difference between two sets.
The symmetric difference between two sets is the set of all flags that appear in one set or the other, but not both.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let set0 = Flag::Foo | Flag::Bar;
let set1 = Flag::Baz | Flag::Bar;
assert_eq!(set0 % set1, Flag::Foo | Flag::Baz);
source§impl<F: Flags, R: Into<FlagSet<F>>> RemAssign<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> RemAssign<R> for FlagSet<F>
source§fn rem_assign(&mut self, rhs: R)
fn rem_assign(&mut self, rhs: R)
Assigns the symmetric difference between two sets.
The symmetric difference between two sets is the set of all flags that appear in one set or the other, but not both.
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let mut set0 = Flag::Foo | Flag::Bar;
let set1 = Flag::Baz | Flag::Bar;
set0 %= set1;
assert_eq!(set0, Flag::Foo | Flag::Baz);
source§impl<F: Flags> Serialize for FlagSet<F>where
F::Type: Serialize,
Available on crate feature serde
only.
impl<F: Flags> Serialize for FlagSet<F>where
F::Type: Serialize,
serde
only.source§impl<F: Flags, R: Into<FlagSet<F>>> Sub<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> Sub<R> for FlagSet<F>
source§fn sub(self, rhs: R) -> Self
fn sub(self, rhs: R) -> Self
Calculates set difference (the current set without the specified flags).
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let set0 = Flag::Foo | Flag::Bar;
let set1 = Flag::Baz | Flag::Bar;
assert_eq!(set0 - set1, Flag::Foo);
source§impl<F: Flags, R: Into<FlagSet<F>>> SubAssign<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> SubAssign<R> for FlagSet<F>
source§fn sub_assign(&mut self, rhs: R)
fn sub_assign(&mut self, rhs: R)
Assigns set difference (the current set without the specified flags).
use flagset::{FlagSet, flags};
flags! {
pub enum Flag: u8 {
Foo = 1,
Bar = 2,
Baz = 4
}
}
let mut set0 = Flag::Foo | Flag::Bar;
set0 -= Flag::Baz | Flag::Bar;
assert_eq!(set0, Flag::Foo);
impl<F: Copy + Flags> Copy for FlagSet<F>where
F::Type: Copy,
impl<F: Eq + Flags> Eq for FlagSet<F>where
F::Type: Eq,
Auto Trait Implementations§
impl<F> Freeze for FlagSet<F>where
<F as Flags>::Type: Freeze,
impl<F> RefUnwindSafe for FlagSet<F>where
<F as Flags>::Type: RefUnwindSafe,
impl<F> Send for FlagSet<F>where
<F as Flags>::Type: Send,
impl<F> Sync for FlagSet<F>where
<F as Flags>::Type: Sync,
impl<F> Unpin for FlagSet<F>where
<F as Flags>::Type: Unpin,
impl<F> UnwindSafe for FlagSet<F>where
<F as Flags>::Type: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)