enumflags2

Trait BitFlag

source
pub trait BitFlag:
    Copy
    + Clone
    + 'static
    + RawBitFlags {
    // Provided methods
    fn empty() -> BitFlags<Self> { ... }
    fn all() -> BitFlags<Self> { ... }
    fn from_bits(
        bits: Self::Numeric,
    ) -> Result<BitFlags<Self>, FromBitsError<Self>> { ... }
    fn from_bits_truncate(bits: Self::Numeric) -> BitFlags<Self> { ... }
    unsafe fn from_bits_unchecked(bits: Self::Numeric) -> BitFlags<Self> { ... }
}
Expand description

A trait automatically implemented by #[bitflags] to make the enum a valid type parameter for BitFlags<T>.

Provided Methods§

source

fn empty() -> BitFlags<Self>

Create a BitFlags with no flags set (in other words, with a value of 0).

This is a convenience reexport of BitFlags::empty. It can be called with MyFlag::empty(), thus bypassing the need for type hints in some situations.

#[bitflags]
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq)]
enum MyFlag {
    One = 1 << 0,
    Two = 1 << 1,
    Three = 1 << 2,
}

use enumflags2::BitFlag;

let empty = MyFlag::empty();
assert!(empty.is_empty());
assert_eq!(empty.contains(MyFlag::One), false);
assert_eq!(empty.contains(MyFlag::Two), false);
assert_eq!(empty.contains(MyFlag::Three), false);
source

fn all() -> BitFlags<Self>

Create a BitFlags with all flags set.

This is a convenience reexport of BitFlags::all. It can be called with MyFlag::all(), thus bypassing the need for type hints in some situations.

#[bitflags]
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq)]
enum MyFlag {
    One = 1 << 0,
    Two = 1 << 1,
    Three = 1 << 2,
}

use enumflags2::BitFlag;

let all = MyFlag::all();
assert!(all.is_all());
assert_eq!(all.contains(MyFlag::One), true);
assert_eq!(all.contains(MyFlag::Two), true);
assert_eq!(all.contains(MyFlag::Three), true);
source

fn from_bits(bits: Self::Numeric) -> Result<BitFlags<Self>, FromBitsError<Self>>

Create a BitFlags if the raw value provided does not contain any illegal flags.

This is a convenience reexport of BitFlags::from_bits. It can be called with MyFlag::from_bits(bits), thus bypassing the need for type hints in some situations.

#[bitflags]
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum MyFlag {
    One = 1 << 0,
    Two = 1 << 1,
    Three = 1 << 2,
}

use enumflags2::BitFlag;

let flags = MyFlag::from_bits(0b11).unwrap();
assert_eq!(flags.contains(MyFlag::One), true);
assert_eq!(flags.contains(MyFlag::Two), true);
assert_eq!(flags.contains(MyFlag::Three), false);
let invalid = MyFlag::from_bits(1 << 3);
assert!(invalid.is_err());
source

fn from_bits_truncate(bits: Self::Numeric) -> BitFlags<Self>

Create a BitFlags from an underlying bitwise value. If any invalid bits are set, ignore them.

This is a convenience reexport of BitFlags::from_bits_truncate. It can be called with MyFlag::from_bits_truncate(bits), thus bypassing the need for type hints in some situations.

#[bitflags]
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq)]
enum MyFlag {
    One = 1 << 0,
    Two = 1 << 1,
    Three = 1 << 2,
}

use enumflags2::BitFlag;

let flags = MyFlag::from_bits_truncate(0b1_1011);
assert_eq!(flags.contains(MyFlag::One), true);
assert_eq!(flags.contains(MyFlag::Two), true);
assert_eq!(flags.contains(MyFlag::Three), false);
source

unsafe fn from_bits_unchecked(bits: Self::Numeric) -> BitFlags<Self>

Create a BitFlags unsafely, without checking if the bits form a valid bit pattern for the type.

Consider using from_bits or from_bits_truncate instead.

§Safety

All bits set in val must correspond to a value of the enum.

§Example

This is a convenience reexport of BitFlags::from_bits_unchecked. It can be called with MyFlag::from_bits_unchecked(bits), thus bypassing the need for type hints in some situations.

#[bitflags]
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq)]
enum MyFlag {
    One = 1 << 0,
    Two = 1 << 1,
    Three = 1 << 2,
}

use enumflags2::BitFlag;

let flags = unsafe {
    MyFlag::from_bits_unchecked(0b011)
};

assert_eq!(flags.contains(MyFlag::One), true);
assert_eq!(flags.contains(MyFlag::Two), true);
assert_eq!(flags.contains(MyFlag::Three), false);

Object Safety§

This trait is not object safe.

Implementors§