Skip to content

Commit 1a1645d

Browse files
committed
auto merge of #14009 : jcmoyer/rust/bitflags-complement, r=alexcrichton
I feel that this is a very vital, missing piece of functionality. This adds on to #13072. Only bits used in the definition of the bitflag are considered for the universe set. This is a bit safer than simply inverting all of the bits in the wrapped value. ```rust bitflags!(flags Flags: u32 { FlagA = 0x00000001, FlagB = 0x00000010, FlagC = 0x00000100, FlagABC = FlagA.bits | FlagB.bits | FlagC.bits }) ... // `Not` implements set complement assert!(!(FlagB | FlagC) == FlagA); // `all` and `is_all` are the inverses of `empty` and `is_empty` assert!(Flags::all() - FlagA == !FlagA); assert!(FlagABC.is_all()); ```
2 parents 96bcadc + 1595885 commit 1a1645d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/libstd/bitflags.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
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
//!
@@ -88,14 +89,17 @@
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)]
189211
mod tests {
190-
use ops::{BitOr, BitAnd, Sub};
212+
use ops::{BitOr, BitAnd, Sub, Not};
191213

192214
bitflags!(
193215
flags Flags: u32 {
@@ -221,6 +243,13 @@ mod tests {
221243
assert!(!FlagABC.is_empty());
222244
}
223245

246+
#[test]
247+
fn test_is_all() {
248+
assert!(Flags::all().is_all());
249+
assert!(!FlagA.is_all());
250+
assert!(FlagABC.is_all());
251+
}
252+
224253
#[test]
225254
fn test_two_empties_do_not_intersect() {
226255
let e1 = Flags::empty();
@@ -281,5 +310,6 @@ mod tests {
281310
assert!((e1 | e2) == FlagABC); // union
282311
assert!((e1 & e2) == FlagC); // intersection
283312
assert!((e1 - e2) == FlagA); // set difference
313+
assert!(!e2 == FlagA); // set complement
284314
}
285315
}

src/libstd/io/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ use int;
220220
use iter::Iterator;
221221
use libc;
222222
use mem::transmute;
223-
use ops::{BitOr, BitAnd, Sub};
223+
use ops::{BitOr, BitAnd, Sub, Not};
224224
use option::{Option, Some, None};
225225
use os;
226226
use owned::Box;

0 commit comments

Comments
 (0)