Skip to content

Commit

Permalink
auto merge of #14009 : jcmoyer/rust/bitflags-complement, r=alexcrichton
Browse files Browse the repository at this point in the history
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());
```
  • Loading branch information
bors committed May 14, 2014
2 parents 96bcadc + 1595885 commit 1a1645d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/libstd/bitflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
//! assert!((e1 | e2) == FlagABC); // union
//! assert!((e1 & e2) == FlagC); // intersection
//! assert!((e1 - e2) == FlagA); // set difference
//! assert!(!e2 == FlagA); // set complement
//! }
//! ~~~
//!
Expand Down Expand Up @@ -88,14 +89,17 @@
//! - `BitOr`: union
//! - `BitAnd`: intersection
//! - `Sub`: set difference
//! - `Not`: set complement
//!
//! # Methods
//!
//! The following methods are defined for the generated `struct`:
//!
//! - `empty`: an empty set of flags
//! - `all`: the set of all flags
//! - `bits`: the raw value of the flags currently stored
//! - `is_empty`: `true` if no flags are currently stored
//! - `is_all`: `true` if all flags are currently set
//! - `intersects`: `true` if there are flags common to both `self` and `other`
//! - `contains`: `true` all of the flags in `other` are contained within `self`
//! - `insert`: inserts the specified flags in-place
Expand All @@ -122,6 +126,11 @@ macro_rules! bitflags(
$BitFlags { bits: 0 }
}

/// Returns the set containing all flags.
pub fn all() -> $BitFlags {
$BitFlags { bits: $($value)|+ }
}

/// Returns the raw value of the flags currently stored.
pub fn bits(&self) -> $T {
self.bits
Expand All @@ -138,6 +147,11 @@ macro_rules! bitflags(
*self == $BitFlags::empty()
}

/// Returns `true` if all flags are currently set.
pub fn is_all(&self) -> bool {
*self == $BitFlags::all()
}

/// Returns `true` if there are flags common to both `self` and `other`.
pub fn intersects(&self, other: $BitFlags) -> bool {
!(self & other).is_empty()
Expand Down Expand Up @@ -182,12 +196,20 @@ macro_rules! bitflags(
$BitFlags { bits: self.bits & !other.bits }
}
}

impl Not<$BitFlags> for $BitFlags {
/// Returns the complement of this set of flags.
#[inline]
fn not(&self) -> $BitFlags {
$BitFlags { bits: !self.bits } & $BitFlags::all()
}
}
)
)

#[cfg(test)]
mod tests {
use ops::{BitOr, BitAnd, Sub};
use ops::{BitOr, BitAnd, Sub, Not};

bitflags!(
flags Flags: u32 {
Expand Down Expand Up @@ -221,6 +243,13 @@ mod tests {
assert!(!FlagABC.is_empty());
}

#[test]
fn test_is_all() {
assert!(Flags::all().is_all());
assert!(!FlagA.is_all());
assert!(FlagABC.is_all());
}

#[test]
fn test_two_empties_do_not_intersect() {
let e1 = Flags::empty();
Expand Down Expand Up @@ -281,5 +310,6 @@ mod tests {
assert!((e1 | e2) == FlagABC); // union
assert!((e1 & e2) == FlagC); // intersection
assert!((e1 - e2) == FlagA); // set difference
assert!(!e2 == FlagA); // set complement
}
}
2 changes: 1 addition & 1 deletion src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ use int;
use iter::Iterator;
use libc;
use mem::transmute;
use ops::{BitOr, BitAnd, Sub};
use ops::{BitOr, BitAnd, Sub, Not};
use option::{Option, Some, None};
use os;
use owned::Box;
Expand Down

0 comments on commit 1a1645d

Please sign in to comment.