macro_rules! bitflags(
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
$($(#[$Flag_attr:meta])* static $Flag:ident = $value:expr),+
}) => (
#[deriving(PartialEq, Eq, Clone)]
$(#[$attr])*
pub struct $BitFlags {
bits: $T,
}
$($(#[$Flag_attr])* pub static $Flag: $BitFlags = $BitFlags { bits: $value };)+
impl $BitFlags {
pub fn empty() -> $BitFlags {
$BitFlags { bits: 0 }
}
pub fn all() -> $BitFlags {
$BitFlags { bits: $($value)|+ }
}
pub fn bits(&self) -> $T {
self.bits
}
pub fn from_bits(bits: $T) -> ::std::option::Option<$BitFlags> {
if (bits & !$BitFlags::all().bits()) != 0 {
::std::option::None
} else {
::std::option::Some($BitFlags { bits: bits })
}
}
pub fn from_bits_truncate(bits: $T) -> $BitFlags {
$BitFlags { bits: bits } & $BitFlags::all()
}
pub fn is_empty(&self) -> bool {
*self == $BitFlags::empty()
}
pub fn is_all(&self) -> bool {
*self == $BitFlags::all()
}
pub fn intersects(&self, other: $BitFlags) -> bool {
!(self & other).is_empty()
}
pub fn contains(&self, other: $BitFlags) -> bool {
(self & other) == other
}
pub fn insert(&mut self, other: $BitFlags) {
self.bits |= other.bits;
}
pub fn remove(&mut self, other: $BitFlags) {
self.bits &= !other.bits;
}
}
impl core::ops::BitOr<$BitFlags, $BitFlags> for $BitFlags {
#[inline]
fn bitor(&self, other: &$BitFlags) -> $BitFlags {
$BitFlags { bits: self.bits | other.bits }
}
}
impl core::ops::BitAnd<$BitFlags, $BitFlags> for $BitFlags {
#[inline]
fn bitand(&self, other: &$BitFlags) -> $BitFlags {
$BitFlags { bits: self.bits & other.bits }
}
}
impl core::ops::BitXor<$BitFlags, $BitFlags> for $BitFlags {
#[inline]
fn bitxor(&self, other: &$BitFlags) -> $BitFlags {
$BitFlags { bits: self.bits ^ other.bits }
}
}
impl core::ops::Sub<$BitFlags, $BitFlags> for $BitFlags {
#[inline]
fn sub(&self, other: &$BitFlags) -> $BitFlags {
$BitFlags { bits: self.bits & !other.bits }
}
}
impl core::ops::Not<$BitFlags> for $BitFlags {
#[inline]
fn not(&self) -> $BitFlags {
$BitFlags { bits: !self.bits } & $BitFlags::all()
}
}
)
)