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§
Sourceconst KNOWN_FLAGS: &'static [(&'static str, Self)]
const KNOWN_FLAGS: &'static [(&'static str, Self)]
The set of named defined flags.
Sourceconst EXTRA_VALID_BITS: Self::Bits
const EXTRA_VALID_BITS: Self::Bits
Extra possible bits values for the flags.
Useful for externally defined flags
Required Associated Types§
Sourcetype Bits: BitsPrimitive
type Bits: BitsPrimitive
The underlying bits type.
Required Methods§
Sourcefn bits(&self) -> Self::Bits
fn bits(&self) -> Self::Bits
Return the underlying bits of this bitflag.
The returned value is exactly the bits set in this flags value.
Sourcefn from_bits_retain(bits: Self::Bits) -> Self
fn from_bits_retain(bits: Self::Bits) -> Self
Convert from bits
value exactly.
Provided Methods§
Sourcefn from_bits(bits: Self::Bits) -> Option<Self>
fn from_bits(bits: Self::Bits) -> Option<Self>
Converts from a bits
value. Returning None
is any unknown bits are set.
Sourcefn from_bits_truncate(bits: Self::Bits) -> Self
fn from_bits_truncate(bits: Self::Bits) -> Self
Convert from bits
value, unsetting any unknown bits.
Sourcefn from_flag_name(name: &str) -> Option<Self>
fn from_flag_name(name: &str) -> Option<Self>
Convert from a flag name
.
Sourcefn from_name(name: &str) -> Option<Self>
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.
Sourcefn all_bits() -> Self
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.
Sourcefn is_all_bits(&self) -> bool
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.
Sourcefn all() -> Self
fn all() -> Self
Construct a flag value with all known flags set.
This will only set the flags specified as associated constant.
Sourcefn contains_unknown_bits(&self) -> bool
fn contains_unknown_bits(&self) -> bool
Returns true
if there are any unknown bits set in the flag value.
Sourcefn truncated(&self) -> Self
fn truncated(&self) -> Self
Returns a bit flag that only has bits corresponding to the specified flags as associated constant.
Sourcefn intersects(&self, other: Self) -> boolwhere
Self: Sized,
fn intersects(&self, other: Self) -> boolwhere
Self: Sized,
Returns true
if this flag value intersects with any value in other
.
This is equivalent to (self & other) != Self::empty()
Sourcefn contains(&self, other: Self) -> boolwhere
Self: Sized,
fn contains(&self, other: Self) -> boolwhere
Self: Sized,
Returns true
if this flag value contains all values of other
.
This is equivalent to (self & other) == other
Sourcefn intersection(self, other: Self) -> Self
fn intersection(self, other: Self) -> Self
Returns the intersection from this value with other
.
Sourcefn difference(self, other: Self) -> Self
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.
Sourcefn symmetric_difference(self, other: Self) -> Self
fn symmetric_difference(self, other: Self) -> Self
TReturns the symmetric difference from this value with other
..
Sourcefn complement(self) -> Self
fn complement(self) -> Self
Returns the complement of the value.
This is very similar to the not
operation, but truncates non used bits.
Sourcefn unset(&mut self, other: Self)where
Self: Sized,
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.
Sourcefn iter(&self) -> Iter<Self> ⓘ
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.
Sourcefn iter_names(&self) -> IterNames<Self> ⓘ
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.