pub struct FlagSet<F: Flags>(_);
Implementations
sourceimpl<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 unsafe fn new_unchecked(bits: F::Type) -> Self
pub 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.
let set = 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
sourceimpl<F: Flags, R: Into<FlagSet<F>>> BitAnd<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitAnd<R> for FlagSet<F>
sourcefn 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);
type Output = Self
type Output = Self
The resulting type after applying the &
operator.
sourceimpl<F: Flags, R: Into<FlagSet<F>>> BitAndAssign<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitAndAssign<R> for FlagSet<F>
sourcefn 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);
sourceimpl<F: Flags, R: Into<FlagSet<F>>> BitOr<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitOr<R> for FlagSet<F>
sourcefn 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());
type Output = Self
type Output = Self
The resulting type after applying the |
operator.
sourceimpl<F: Flags, R: Into<FlagSet<F>>> BitOrAssign<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitOrAssign<R> for FlagSet<F>
sourcefn 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);
sourceimpl<F: Flags, R: Into<FlagSet<F>>> BitXor<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitXor<R> for FlagSet<F>
sourcefn 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);
type Output = Self
type Output = Self
The resulting type after applying the ^
operator.
sourceimpl<F: Flags, R: Into<FlagSet<F>>> BitXorAssign<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> BitXorAssign<R> for FlagSet<F>
sourcefn 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);
sourceimpl<F: Flags> Default for FlagSet<F>
impl<F: Flags> Default for FlagSet<F>
sourcefn 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));
sourceimpl<'de, F: Flags> Deserialize<'de> for FlagSet<F> where
F::Type: Deserialize<'de>,
impl<'de, F: Flags> Deserialize<'de> for FlagSet<F> where
F::Type: Deserialize<'de>,
sourcefn 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>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<F: Flags> From<Option<FlagSet<F>>> for FlagSet<F>
impl<F: Flags> From<Option<FlagSet<F>>> for FlagSet<F>
sourcefn 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);
sourceimpl<F: Flags> IntoIterator for FlagSet<F>
impl<F: Flags> IntoIterator for FlagSet<F>
sourcefn 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);
type Item = F
type Item = F
The type of the elements being iterated over.
type IntoIter = Iter<F>
type IntoIter = Iter<F>
Which kind of iterator are we turning this into?
sourceimpl<F: Flags> Not for FlagSet<F>
impl<F: Flags> Not for FlagSet<F>
sourcefn 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));
type Output = Self
type Output = Self
The resulting type after applying the !
operator.
sourceimpl<F: Flags, R: Into<FlagSet<F>>> Rem<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> Rem<R> for FlagSet<F>
sourcefn 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);
type Output = Self
type Output = Self
The resulting type after applying the %
operator.
sourceimpl<F: Flags, R: Into<FlagSet<F>>> RemAssign<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> RemAssign<R> for FlagSet<F>
sourcefn 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);
sourceimpl<F: Flags, R: Into<FlagSet<F>>> Sub<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> Sub<R> for FlagSet<F>
sourcefn 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);
type Output = Self
type Output = Self
The resulting type after applying the -
operator.
sourceimpl<F: Flags, R: Into<FlagSet<F>>> SubAssign<R> for FlagSet<F>
impl<F: Flags, R: Into<FlagSet<F>>> SubAssign<R> for FlagSet<F>
sourcefn 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,
impl<F: Flags> StructuralEq for FlagSet<F>
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more