34
34
//! assert!((e1 | e2) == FlagABC); // union
35
35
//! assert!((e1 & e2) == FlagC); // intersection
36
36
//! assert!((e1 - e2) == FlagA); // set difference
37
+ //! assert!(!e2 == FlagA); // set complement
37
38
//! }
38
39
//! ~~~
39
40
//!
88
89
//! - `BitOr`: union
89
90
//! - `BitAnd`: intersection
90
91
//! - `Sub`: set difference
92
+ //! - `Not`: set complement
91
93
//!
92
94
//! # Methods
93
95
//!
94
96
//! The following methods are defined for the generated `struct`:
95
97
//!
96
98
//! - `empty`: an empty set of flags
99
+ //! - `all`: the set of all flags
97
100
//! - `bits`: the raw value of the flags currently stored
98
101
//! - `is_empty`: `true` if no flags are currently stored
102
+ //! - `is_all`: `true` if all flags are currently set
99
103
//! - `intersects`: `true` if there are flags common to both `self` and `other`
100
104
//! - `contains`: `true` all of the flags in `other` are contained within `self`
101
105
//! - `insert`: inserts the specified flags in-place
@@ -122,6 +126,11 @@ macro_rules! bitflags(
122
126
$BitFlags { bits: 0 }
123
127
}
124
128
129
+ /// Returns the set containing all flags.
130
+ pub fn all( ) -> $BitFlags {
131
+ $BitFlags { bits: $( $value) |+ }
132
+ }
133
+
125
134
/// Returns the raw value of the flags currently stored.
126
135
pub fn bits( & self ) -> $T {
127
136
self . bits
@@ -138,6 +147,11 @@ macro_rules! bitflags(
138
147
* self == $BitFlags:: empty( )
139
148
}
140
149
150
+ /// Returns `true` if all flags are currently set.
151
+ pub fn is_all( & self ) -> bool {
152
+ * self == $BitFlags:: all( )
153
+ }
154
+
141
155
/// Returns `true` if there are flags common to both `self` and `other`.
142
156
pub fn intersects( & self , other: $BitFlags) -> bool {
143
157
!( self & other) . is_empty( )
@@ -182,12 +196,20 @@ macro_rules! bitflags(
182
196
$BitFlags { bits: self . bits & !other. bits }
183
197
}
184
198
}
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
+ }
185
207
)
186
208
)
187
209
188
210
#[ cfg( test) ]
189
211
mod tests {
190
- use ops:: { BitOr , BitAnd , Sub } ;
212
+ use ops:: { BitOr , BitAnd , Sub , Not } ;
191
213
192
214
bitflags ! (
193
215
flags Flags : u32 {
@@ -214,6 +236,13 @@ mod tests {
214
236
assert ! ( !FlagABC . is_empty( ) ) ;
215
237
}
216
238
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
+
217
246
#[ test]
218
247
fn test_two_empties_do_not_intersect ( ) {
219
248
let e1 = Flags :: empty ( ) ;
@@ -274,5 +303,6 @@ mod tests {
274
303
assert ! ( ( e1 | e2) == FlagABC ) ; // union
275
304
assert ! ( ( e1 & e2) == FlagC ) ; // intersection
276
305
assert ! ( ( e1 - e2) == FlagA ) ; // set difference
306
+ assert ! ( !e2 == FlagA ) ; // set complement
277
307
}
278
308
}
0 commit comments