Trait Flags
pub trait Flags: Sized + 'static
A set of defined flags using a bits type as storage.
Implementing Flags
This trait is implemented by the bitflags macro:
use bitflags;
bitflags!
It can also be implemented manually:
use ;
;
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:
# use ;
bitflags!
assert_eq!;
Associated Types
type Bits: Bits;The underlying bits type.
Associated Constants
const FLAGS: &'static [Flag<Self>];The set of defined flags.
Required Methods
fn bits(&self) -> Self::BitsGet the underlying bits value.
The returned value is exactly the bits set in this flags value.
fn from_bits_retain(bits: Self::Bits) -> SelfConvert from a bits value exactly.
Provided Methods
fn empty() -> SelfGet a flags value with all bits unset.
fn all() -> SelfGet a flags value with all known bits set.
fn all_named() -> SelfGet a flags value with all bits from named flags set.
This method is equivalent to
Flags::allunlessFlags::FLAGScontains unnamed flags.fn known_bits(&self) -> Self::BitsGet the known bits from a flags value.
fn unknown_bits(&self) -> Self::BitsGet the unknown bits from a flags value.
fn contains_unknown_bits(&self) -> boolThis method will return
trueif any unknown bits are set.fn from_bits(bits: Self::Bits) -> Option<Self>Convert from a bits value.
This method will return
Noneif any unknown bits are set.fn from_bits_truncate(bits: Self::Bits) -> SelfConvert from a bits value, unsetting any unknown bits.
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
Noneifnameis empty or doesn't correspond to any named flag.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.
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.fn iter_defined_names() -> IterDefinedNames<Self>Yield a set of all named flags defined by
Self::FLAGS.fn iter_equal_names(&self) -> IterEqualNames<Self>Get an iterator over all defined names for this flags value.
This iterator will yield all defined names for the flags value, including any convenience flags.
fn is_empty(&self) -> boolWhether all bits in this flags value are unset.
fn is_all(&self) -> boolWhether all known bits in this flags value are set.
fn intersects(&self, other: Self) -> bool where Self: Sized,Whether any set bits in
otherare also set inself.fn contains(&self, other: Self) -> bool where Self: Sized,Whether all set bits in
otherare also set inself.fn truncate(&mut self) where Self: Sized,Remove any unknown bits from the flags.
fn insert(&mut self, other: Self) where Self: Sized,The bitwise or (
|) of the bits inselfandother.fn remove(&mut self, other: Self) where Self: Sized,The intersection of
selfwith the complement ofother(&!).This method is not equivalent to
self & !otherwhenotherhas unknown bits set.removewon't truncateother, but the!operator will.fn toggle(&mut self, other: Self) where Self: Sized,The bitwise exclusive-or (
^) of the bits inselfandother.fn set(&mut self, other: Self, value: bool) where Self: Sized,Call
Flags::insertwhenvalueistrueorFlags::removewhenvalueisfalse.fn clear(&mut self) where Self: Sized,Unsets all bits in the flags.
fn intersection(self, other: Self) -> SelfThe bitwise and (
&) of the bits inselfandother.fn union(self, other: Self) -> SelfThe bitwise or (
|) of the bits inselfandother.fn difference(self, other: Self) -> SelfThe intersection of
selfwith the complement ofother(&!).This method is not equivalent to
self & !otherwhenotherhas unknown bits set.differencewon't truncateother, but the!operator will.fn symmetric_difference(self, other: Self) -> SelfThe bitwise exclusive-or (
^) of the bits inselfandother.fn complement(self) -> SelfThe bitwise negation (
!) of the bits inself, truncating the result.