Macro new_bitflags::new_bitflags
[−]
[src]
macro_rules! new_bitflags { ( $(#[$attr:meta])* pub flags $name:ident : $inner:ty { $( $(#[$flag_attr:meta])* const $flag:ident = $value:expr ; )* } ) => { ... }; ( $(#[$attr:meta])* flags $name:ident : $inner:ty { $( $(#[$flag_attr:meta])* const $flag:ident = $value:expr ; )* } ) => { ... }; ( @_impl $name:ident : $inner:ty { $( $(#[$flag_attr:meta])* const $flag:ident = $value:expr ; )* } ) => { ... }; }
Generates a bitflags type, wrapping a given primitive integer type.
Example
The following macro invocation will create a struct Foo
:
#[macro_use] extern crate new_bitflags; new_bitflags!{ pub flags Foo: u32 { const flag_a = 1 << 0; const flag_b = 1 << 1; const flag_c = 1 << 2; } } impl Foo { pub fn flag_abc() -> Foo { Foo::flag_a() | Foo::flag_b() | Foo::flag_c() } } fn main() { let f1 = Foo::flag_a() | Foo::flag_c(); let f2 = Foo::flag_b() | Foo::flag_c(); assert_eq!((f1 | f2), Foo::flag_abc()); // union assert_eq!((f1 & f2), Foo::flag_c()); // intersection assert_eq!((f1 - f2), Foo::flag_a()); // difference assert_eq!(!f2, Foo::flag_a()); // complement }
The generated struct
can be extended with type and trait impl
s.
impl Foo { pub fn is_flag_a(&self) -> bool { self.contains(Foo::flag_a()) } }
Visibility
The visibility of the generated struct
can be controlled within the
invocation of new_bitflags!
#[macro_use] extern crate new_bitflags; mod example { // `struct Public` will be visible outside this module. new_bitflags!{ pub flags Public: u32 { // ... } } // `struct Private` will not be visible outside this module. new_bitflags!{ flags Private: u32 { // ... } } }
Trait implementations
Generated struct
types will have derived implementations of the following
traits: Copy
, Clone
, Hash
, PartialEq
, Eq
, PartialOrd
, and Ord
.
The traits Extend
and FromIterator
are implemented for sequences of
Self
and &Self
.
The Debug
trait implementation will display the set of named flags contained
in a set.
Operators
The following operators are implemented for generated struct
types:
BitOr
andBitOrAssign
perform unionBitAnd
andBitAndAssign
perform intersectionBitXor
andBitXorAssign
perform toggleSub
andSubAssign
perform set differenceNot
performs set complement
Methods
The following methods are implemented for generated struct
types:
fn from_bits(bits) -> Option<Self>
converts from underlying bits, checking that all bits correspond to defined flags.fn from_bits_truncate(bits) -> Option<Self>
converts from underlying bits, truncating any bits that do not correspond to defined flags.fn bits(&self) -> bits
returns the underlying bitsfn contains(&self, other: Self) -> bool
returns whether the set contains all flags present inother
fn clear(&mut self)
clears all flags on the setfn all() -> Self
returns all defined flagsfn empty() -> Self
returns an empty setfn is_all(&self) -> bool
returns whether the set contains all flagsfn is_empty(&self) -> bool
returns whether the set is emptyfn intersects(&self, other: Self) -> bool
returns whether any flags are common betweenself
andother
.fn insert(&mut self, other: Self)
inserts all flags inother
fn remove(&mut self, other: Self)
removes all flags inother
fn toggle(&mut self, other: Self)
toggles all flags inother
fn set(&mut self, other: Self, value: bool)
sets or removes all flags inother
, depending on booleanvalue
Additionally, for each defined flag, a static method of signature
fn() -> Self
is defined, returning a set containing only the named flag.