3434//! assert!((e1 | e2) == FlagABC); // union
3535//! assert!((e1 & e2) == FlagC); // intersection
3636//! assert!((e1 - e2) == FlagA); // set difference
37+ //! assert!(!e2 == FlagA); // set complement
3738//! }
3839//! ~~~
3940//!
8889//! - `BitOr`: union
8990//! - `BitAnd`: intersection
9091//! - `Sub`: set difference
92+ //! - `Not`: set complement
9193//!
9294//! # Methods
9395//!
9496//! The following methods are defined for the generated `struct`:
9597//!
9698//! - `empty`: an empty set of flags
99+ //! - `all`: the set of all flags
97100//! - `bits`: the raw value of the flags currently stored
98101//! - `is_empty`: `true` if no flags are currently stored
102+ //! - `is_all`: `true` if all flags are currently set
99103//! - `intersects`: `true` if there are flags common to both `self` and `other`
100104//! - `contains`: `true` all of the flags in `other` are contained within `self`
101105//! - `insert`: inserts the specified flags in-place
@@ -122,6 +126,11 @@ macro_rules! bitflags(
122126 $BitFlags { bits: 0 }
123127 }
124128
129+ /// Returns the set containing all flags.
130+ pub fn all( ) -> $BitFlags {
131+ $BitFlags { bits: $( $value) |+ }
132+ }
133+
125134 /// Returns the raw value of the flags currently stored.
126135 pub fn bits( & self ) -> $T {
127136 self . bits
@@ -138,6 +147,11 @@ macro_rules! bitflags(
138147 * self == $BitFlags:: empty( )
139148 }
140149
150+ /// Returns `true` if all flags are currently set.
151+ pub fn is_all( & self ) -> bool {
152+ * self == $BitFlags:: all( )
153+ }
154+
141155 /// Returns `true` if there are flags common to both `self` and `other`.
142156 pub fn intersects( & self , other: $BitFlags) -> bool {
143157 !( self & other) . is_empty( )
@@ -182,12 +196,20 @@ macro_rules! bitflags(
182196 $BitFlags { bits: self . bits & !other. bits }
183197 }
184198 }
199+
200+ impl Not <$BitFlags> for $BitFlags {
201+ /// Returns the complement of this set of flags.
202+ #[ inline]
203+ fn not( & self ) -> $BitFlags {
204+ $BitFlags { bits: !self . bits } & $BitFlags:: all( )
205+ }
206+ }
185207 )
186208)
187209
188210#[ cfg( test) ]
189211mod tests {
190- use ops:: { BitOr , BitAnd , Sub } ;
212+ use ops:: { BitOr , BitAnd , Sub , Not } ;
191213
192214 bitflags ! (
193215 flags Flags : u32 {
@@ -214,6 +236,13 @@ mod tests {
214236 assert ! ( !FlagABC . is_empty( ) ) ;
215237 }
216238
239+ #[ test]
240+ fn test_is_all ( ) {
241+ assert ! ( Flags :: all( ) . is_all( ) ) ;
242+ assert ! ( !FlagA . is_all( ) ) ;
243+ assert ! ( FlagABC . is_all( ) ) ;
244+ }
245+
217246 #[ test]
218247 fn test_two_empties_do_not_intersect ( ) {
219248 let e1 = Flags :: empty ( ) ;
@@ -274,5 +303,6 @@ mod tests {
274303 assert ! ( ( e1 | e2) == FlagABC ) ; // union
275304 assert ! ( ( e1 & e2) == FlagC ) ; // intersection
276305 assert ! ( ( e1 - e2) == FlagA ) ; // set difference
306+ assert ! ( !e2 == FlagA ) ; // set complement
277307 }
278308}
0 commit comments