Implement set complement and universe for bitflags

This commit is contained in:
J.C. Moyer 2014-05-07 01:54:44 -04:00
parent cb115ac2d4
commit 1595885501
2 changed files with 32 additions and 2 deletions

View File

@ -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
//! }
//! ~~~
//!
@ -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
@ -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
@ -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()
@ -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 {
@ -214,6 +236,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();
@ -274,5 +303,6 @@ mod tests {
assert!((e1 | e2) == FlagABC); // union
assert!((e1 & e2) == FlagC); // intersection
assert!((e1 - e2) == FlagA); // set difference
assert!(!e2 == FlagA); // set complement
}
}

View File

@ -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;