Struct flagset::FlagSet[][src]

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

Implementations

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

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.

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.

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

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

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

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

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

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

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

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

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

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

The resulting type after applying the & operator.

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

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

The resulting type after applying the | operator.

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

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

The resulting type after applying the ^ operator.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

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

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

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

The resulting type after applying the ! operator.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

The resulting type after applying the % operator.

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

Serialize this value into the given Serde serializer. Read more

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

The resulting type after applying the - operator.

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.