bitflag_attr

Trait Flags

Source
pub trait Flags:
    Sized
    + Copy
    + 'static {
    type Bits: BitsPrimitive;

    const KNOWN_FLAGS: &'static [(&'static str, Self)];
    const EXTRA_VALID_BITS: Self::Bits;
Show 27 methods // Required methods fn bits(&self) -> Self::Bits; fn from_bits_retain(bits: Self::Bits) -> Self; // Provided methods fn from_bits(bits: Self::Bits) -> Option<Self> { ... } fn from_bits_truncate(bits: Self::Bits) -> Self { ... } fn from_flag_name(name: &str) -> Option<Self> { ... } fn from_name(name: &str) -> Option<Self> { ... } fn empty() -> Self { ... } fn is_empty(&self) -> bool { ... } fn all_bits() -> Self { ... } fn is_all_bits(&self) -> bool { ... } fn all() -> Self { ... } fn is_all(&self) -> bool { ... } fn contains_unknown_bits(&self) -> bool { ... } fn truncated(&self) -> Self { ... } fn intersects(&self, other: Self) -> bool where Self: Sized { ... } fn contains(&self, other: Self) -> bool where Self: Sized { ... } fn truncate(&mut self) where Self: Sized { ... } fn intersection(self, other: Self) -> Self { ... } fn union(self, other: Self) -> Self { ... } fn difference(self, other: Self) -> Self { ... } fn symmetric_difference(self, other: Self) -> Self { ... } fn complement(self) -> Self { ... } fn set(&mut self, other: Self) where Self: Sized { ... } fn unset(&mut self, other: Self) where Self: Sized { ... } fn toggle(&mut self, other: Self) where Self: Sized { ... } fn iter(&self) -> Iter<Self> { ... } fn iter_names(&self) -> IterNames<Self> { ... }
}
Expand description

A set of defined flags using a bits type as storage.

§Implementing Flags

This trait is implemented by the bitflag macro:

use bitflag_attr::bitflag;

#[bitflag(u8)]
#[derive(Clone, Copy)]
enum MyFlags {
  A = 1,
  B = 1 << 1,
}

It can also be implemented manually:

use bitflag_attr::{Flags};

#[derive(Clone, Copy)]
struct MyFlags(u8);

impl Flags for MyFlags {
    const KNOWN_FLAGS: &'static [(&'static str, Self)] = &[
        ("A", MyFlags(1)),
        ("B", MyFlags(1 << 1)),
    ];

    const EXTRA_VALID_BITS: Self::Bits = 1 | (1 << 1);

    type Bits = u8;

    fn from_bits_retain(bits: Self::Bits) -> Self {
        MyFlags(bits)
    }

    fn bits(&self) -> Self::Bits {
        self.0
    }
}

§Using Flags

The Flags trait can be used generically to work with any flags types. In this example, we can count the number of defined named flags:

fn defined_flags<F: Flags>() -> usize {
    F::KNOWN_FLAGS.iter().count()
}

#[bitflag(u8)]
#[non_exhaustive]
#[derive(Clone, Copy)]
enum MyFlags {
    A = 1,
    B = 1 << 1,
    C = 1 << 2,
}

assert_eq!(3, defined_flags::<MyFlags>());

Required Associated Constants§

Source

const KNOWN_FLAGS: &'static [(&'static str, Self)]

The set of named defined flags.

Source

const EXTRA_VALID_BITS: Self::Bits

Extra possible bits values for the flags.

Useful for externally defined flags

Required Associated Types§

Source

type Bits: BitsPrimitive

The underlying bits type.

Required Methods§

Source

fn bits(&self) -> Self::Bits

Return the underlying bits of this bitflag.

The returned value is exactly the bits set in this flags value.

Source

fn from_bits_retain(bits: Self::Bits) -> Self

Convert from bits value exactly.

Provided Methods§

Source

fn from_bits(bits: Self::Bits) -> Option<Self>

Converts from a bits value. Returning None is any unknown bits are set.

Source

fn from_bits_truncate(bits: Self::Bits) -> Self

Convert from bits value, unsetting any unknown bits.

Source

fn from_flag_name(name: &str) -> Option<Self>

Convert from a flag name.

Source

fn from_name(name: &str) -> Option<Self>

Get a flags value with the bits of a flag with the given name set.

This method will return None if name is empty or doesn’t correspond to any named flag.

Source

fn empty() -> Self

Construct a flag value with all bits unset.

Source

fn is_empty(&self) -> bool

Returns true if the flag value has all bits unset.

Source

fn all_bits() -> Self

Returns a flag value that contains all value.

This will include bits that do not have any flags/meaning. Use all if you want only the specified flags set.

Source

fn is_all_bits(&self) -> bool

Returns true if the bitflag contains all value bits set.

This will check for all bits. Use is_all if you want to check for all specified flags.

Source

fn all() -> Self

Construct a flag value with all known flags set.

This will only set the flags specified as associated constant.

Source

fn is_all(&self) -> bool

Whether all known bits in this flags value are set.

Source

fn contains_unknown_bits(&self) -> bool

Returns true if there are any unknown bits set in the flag value.

Source

fn truncated(&self) -> Self

Returns a bit flag that only has bits corresponding to the specified flags as associated constant.

Source

fn intersects(&self, other: Self) -> bool
where Self: Sized,

Returns true if this flag value intersects with any value in other.

This is equivalent to (self & other) != Self::empty()

Source

fn contains(&self, other: Self) -> bool
where Self: Sized,

Returns true if this flag value contains all values of other.

This is equivalent to (self & other) == other

Source

fn truncate(&mut self)
where Self: Sized,

Remove any unknown bits from the flags.

Source

fn intersection(self, other: Self) -> Self

Returns the intersection from this value with other.

Source

fn union(self, other: Self) -> Self

Returns the union from this value with other.

Source

fn difference(self, other: Self) -> Self

Returns the difference from this value with other.

In other words, returns the intersection of this value with the negation of other.

This method is not equivalent to self & !other when other has unknown bits set. difference won’t truncate other, but the ! operator will.

Source

fn symmetric_difference(self, other: Self) -> Self

TReturns the symmetric difference from this value with other..

Source

fn complement(self) -> Self

Returns the complement of the value.

This is very similar to the not operation, but truncates non used bits.

Source

fn set(&mut self, other: Self)
where Self: Sized,

Set the flags in other in the value.

Source

fn unset(&mut self, other: Self)
where Self: Sized,

/// Unset the flags bits in other in the value.

This method is not equivalent to self & !other when other has unknown bits set. remove won’t truncate other, but the ! operator will.

Source

fn toggle(&mut self, other: Self)
where Self: Sized,

Toggle the flags in other in the value.

Source

fn iter(&self) -> Iter<Self>

Yield a set of contained flags values.

Each yielded flags value will correspond to a defined named flag. Any unknown bits will be yielded together as a final flags value.

Source

fn iter_names(&self) -> IterNames<Self>

Yield a set of contained named flags values.

This method is like Flags::iter, except only yields bits in contained named flags. Any unknown bits, or bits not corresponding to a contained flag will not be yielded.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Flags for ExampleFlags

Source§

const KNOWN_FLAGS: &'static [(&'static str, ExampleFlags)]

Source§

const EXTRA_VALID_BITS: u32 = 4_294_967_295u32

Source§

type Bits = u32