Skip to main content

BitFlagsSpec

Trait BitFlagsSpec 

Source
pub trait BitFlagsSpec<T>
where T: 'static,
{ const VALID_BITS_MASK: T; const FLAG_NAMES: &[&[&str]]; const COMBINED_NAMES: &[(T, &str)]; const DEFAULT_NAME_MATCHER: NameMatcher; }
Expand description

Associates a bit-storage type T with the set of bits that are valid for a particular BitFlags tag.

Implement this trait for the tag type U in BitFlags<T, U>. VALID_BITS_MASK defines which bits are valid for the flag set.

Bits not set in VALID_BITS_MASK are invalid:

Unlike the bitflags crate, valid bits are not declared by a macro. They are part of this trait implementation for the tag type.

§Examples

use bitflags_lite::{BitFlags, BitFlagsSpec, NameMatcher};

struct PermissionsTag;

impl BitFlagsSpec<u32> for PermissionsTag {
    const VALID_BITS_MASK: u32 = 0b111;
    const FLAG_NAMES: &[&[&str]] = &[&["read"], &["write"], &["exec", "execute"]];
    const COMBINED_NAMES: &[(u32, &str)] = &[(0, "none"), (0b111, "rwx")];
    const DEFAULT_NAME_MATCHER: NameMatcher = NameMatcher::Exact;
}

type Permissions = BitFlags<u32, PermissionsTag>;

const READ: Permissions = Permissions::try_from_bit(0).unwrap();
const WRITE: Permissions = Permissions::try_from_bit(1).unwrap();
const EXEC: Permissions = Permissions::try_from_bit(2).unwrap();

assert_eq!(Permissions::try_from_bits(0b1), Some(READ));
assert_eq!(Permissions::try_from_bits(0b11), Some(READ | WRITE));
assert_eq!(Permissions::try_from_bits(0b111), Some(READ | WRITE | EXEC));
assert_eq!(Permissions::try_from_bits(0b1111), None);

Required Associated Constants§

Source

const VALID_BITS_MASK: T

A bit pattern whose set bits are the valid bits for this flag set.

Bits not set in this value are invalid for the associated BitFlags type. They are rejected by BitFlags::try_from_bits and cleared by BitFlags::from_bits_truncate, BitFlags::truncate, BitFlags::complement, and Not.

Source

const FLAG_NAMES: &[&[&str]]

A slice indexed by bit position, where each element contains the names associated with that bit.

The first name in each inner slice is used by BitFlag’s and BitFlags’s Display implementations. All names are accepted when parsing. An empty inner slice means the bit has no name.

The index i corresponds to the bit mask 1 << i.

Source

const COMBINED_NAMES: &[(T, &str)]

Names for combinations of flags, consulted when parsing.

Each entry pairs a bit pattern with the single name that should parse to it. When parsing, the input string is matched against all entries; the first match wins.

The bit pattern may include bits that are not named in FLAG_NAMES, or that are not declared as individual flags at all. This const does not validate against VALID_BITS_MASK. Parsing preserves whatever bits the entry contains.

This const is not consulted by Display. Formatting a value with multiple bits set always writes the individual flag names from FLAG_NAMES, never a combined name.

Source

const DEFAULT_NAME_MATCHER: NameMatcher

Controls whether the default FromStr and Deserialize implementations for BitFlags parse named flags case-sensitively.

If NameMatcher::Exact, named flags are matched exactly. If NameMatcher::AsciiCaseInsensitive, named flags are matched using ASCII case-insensitive comparison.

This value does not affect methods that accept an explicit NameMatcher, such as BitFlags::try_from_str.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§