[][src]Struct flagset::FlagSet

pub struct FlagSet<F: Flags>(_);

Methods

impl<F: Flags> FlagSet<F>[src]

pub fn new(bits: F::Type) -> Result<Self, ()>[src]

Creates a new set from bits; returning Err(()) 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(())); // Invalid
assert_eq!(FlagSet::<Flag>::new(0b10101), Err(())); // Unknown

pub fn new_truncated(bits: F::Type) -> Self[src]

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.

pub unsafe fn new_unchecked(bits: F::Type) -> Self[src]

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);

pub fn full() -> Self[src]

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));

pub fn bits(self) -> F::Type[src]

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);

pub fn is_empty(self) -> bool[src]

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());

pub fn is_full(self) -> bool[src]

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());

pub fn is_disjoint(self, rhs: impl Into<FlagSet<F>>) -> bool[src]

Returns true if the two FlagSets 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));

pub fn contains(self, rhs: impl Into<FlagSet<F>>) -> bool[src]

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));

pub fn clear(&mut self)[src]

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());

pub fn drain(&mut self) -> Iter<F>[src]

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);

pub fn retain(&mut self, func: impl Fn(F) -> bool)[src]

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

impl<F: Copy + Flags> Copy for FlagSet<F> where
    F::Type: Copy
[src]

impl<F: Flags> Debug for FlagSet<F>[src]

impl<F: Flags, R: Into<FlagSet<F>>> Sub<R> for FlagSet<F>[src]

type Output = Self

The resulting type after applying the - operator.

fn sub(self, rhs: R) -> Self[src]

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);

impl<F: Flags, R: Copy + Into<FlagSet<F>>> PartialEq<R> for FlagSet<F>[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<F: Eq + Flags> Eq for FlagSet<F> where
    F::Type: Eq
[src]

impl<F: Flags> From<Option<FlagSet<F>>> for FlagSet<F>[src]

fn from(value: Option<FlagSet<F>>) -> FlagSet<F>[src]

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);

impl<F: Flags, R: Into<FlagSet<F>>> Rem<R> for FlagSet<F>[src]

type Output = Self

The resulting type after applying the % operator.

fn rem(self, rhs: R) -> Self[src]

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);

impl<F: Flags, R: Into<FlagSet<F>>> SubAssign<R> for FlagSet<F>[src]

fn sub_assign(&mut self, rhs: R)[src]

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: Flags, R: Into<FlagSet<F>>> RemAssign<R> for FlagSet<F>[src]

fn rem_assign(&mut self, rhs: R)[src]

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);

impl<F: Flags> Not for FlagSet<F>[src]

type Output = Self

The resulting type after applying the ! operator.

fn not(self) -> Self[src]

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));

impl<F: Flags, R: Into<FlagSet<F>>> BitAnd<R> for FlagSet<F>[src]

type Output = Self

The resulting type after applying the & operator.

fn bitand(self, rhs: R) -> Self[src]

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);

impl<F: Flags, R: Into<FlagSet<F>>> BitOr<R> for FlagSet<F>[src]

type Output = Self

The resulting type after applying the | operator.

fn bitor(self, rhs: R) -> Self[src]

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());

impl<F: Flags, R: Into<FlagSet<F>>> BitXor<R> for FlagSet<F>[src]

type Output = Self

The resulting type after applying the ^ operator.

fn bitxor(self, rhs: R) -> Self[src]

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);

impl<F: Flags, R: Into<FlagSet<F>>> BitAndAssign<R> for FlagSet<F>[src]

fn bitand_assign(&mut self, rhs: R)[src]

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);

impl<F: Flags, R: Into<FlagSet<F>>> BitOrAssign<R> for FlagSet<F>[src]

fn bitor_assign(&mut self, rhs: R)[src]

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);

impl<F: Flags, R: Into<FlagSet<F>>> BitXorAssign<R> for FlagSet<F>[src]

fn bitxor_assign(&mut self, rhs: R)[src]

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);

impl<F: Flags> IntoIterator for FlagSet<F>[src]

type Item = F

The type of the elements being iterated over.

type IntoIter = Iter<F>

Which kind of iterator are we turning this into?

fn into_iter(self) -> Self::IntoIter[src]

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);

impl<F: Clone + Flags> Clone for FlagSet<F> where
    F::Type: Clone
[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<F: Flags> Default for FlagSet<F>[src]

fn default() -> Self[src]

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));

Auto Trait Implementations

impl<F> Send for FlagSet<F> where
    <F as Flags>::Type: Send

impl<F> Sync for FlagSet<F> where
    <F as Flags>::Type: Sync

Blanket Implementations

impl<T> From for T[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]