#![feature(no_std)]
#![no_std]
#[macro_export]
macro_rules! bitflags {
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
$($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+
}) => {
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
$(#[$attr])*
pub struct $BitFlags {
bits: $T,
}
$($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+
impl ::core::fmt::Debug for $BitFlags {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
#[allow(dead_code)]
mod dummy {
$(const $Flag: super::$BitFlags = super::$BitFlags { bits: 0 };)+
#[inline]
pub fn fmt(self_: &super::$BitFlags,
f: &mut ::core::fmt::Formatter)
-> ::core::fmt::Result {
use super::*;
let mut _first = true;
$(
if $Flag.bits != 0 && self_.contains($Flag) {
if !_first {
try!(f.write_str(" | "));
}
_first = false;
try!(f.write_str(stringify!($Flag)));
}
)+
Ok(())
}
}
dummy::fmt(self, f)
}
}
#[allow(dead_code)]
impl $BitFlags {
#[inline]
pub fn empty() -> $BitFlags {
$BitFlags { bits: 0 }
}
#[inline]
pub fn all() -> $BitFlags {
#[allow(dead_code)]
mod dummy {
$(const $Flag: super::$BitFlags = super::$BitFlags { bits: 0 };)+
#[inline]
pub fn all() -> super::$BitFlags {
use super::*;
$BitFlags { bits: $($Flag.bits)|+ }
}
}
dummy::all()
}
#[inline]
pub fn bits(&self) -> $T {
self.bits
}
#[inline]
pub fn from_bits(bits: $T) -> ::core::option::Option<$BitFlags> {
if (bits & !$BitFlags::all().bits()) != 0 {
::core::option::Option::None
} else {
::core::option::Option::Some($BitFlags { bits: bits })
}
}
#[inline]
pub fn from_bits_truncate(bits: $T) -> $BitFlags {
$BitFlags { bits: bits } & $BitFlags::all()
}
#[inline]
pub fn is_empty(&self) -> bool {
*self == $BitFlags::empty()
}
#[inline]
pub fn is_all(&self) -> bool {
*self == $BitFlags::all()
}
#[inline]
pub fn intersects(&self, other: $BitFlags) -> bool {
!(*self & other).is_empty()
}
#[inline]
pub fn contains(&self, other: $BitFlags) -> bool {
(*self & other) == other
}
#[inline]
pub fn insert(&mut self, other: $BitFlags) {
self.bits |= other.bits;
}
#[inline]
pub fn remove(&mut self, other: $BitFlags) {
self.bits &= !other.bits;
}
#[inline]
pub fn toggle(&mut self, other: $BitFlags) {
self.bits ^= other.bits;
}
}
impl ::core::ops::BitOr for $BitFlags {
type Output = $BitFlags;
#[inline]
fn bitor(self, other: $BitFlags) -> $BitFlags {
$BitFlags { bits: self.bits | other.bits }
}
}
impl ::core::ops::BitXor for $BitFlags {
type Output = $BitFlags;
#[inline]
fn bitxor(self, other: $BitFlags) -> $BitFlags {
$BitFlags { bits: self.bits ^ other.bits }
}
}
impl ::core::ops::BitAnd for $BitFlags {
type Output = $BitFlags;
#[inline]
fn bitand(self, other: $BitFlags) -> $BitFlags {
$BitFlags { bits: self.bits & other.bits }
}
}
impl ::core::ops::Sub for $BitFlags {
type Output = $BitFlags;
#[inline]
fn sub(self, other: $BitFlags) -> $BitFlags {
$BitFlags { bits: self.bits & !other.bits }
}
}
impl ::core::ops::Not for $BitFlags {
type Output = $BitFlags;
#[inline]
fn not(self) -> $BitFlags {
$BitFlags { bits: !self.bits } & $BitFlags::all()
}
}
impl ::core::iter::FromIterator<$BitFlags> for $BitFlags {
fn from_iter<T: ::core::iter::IntoIterator<Item=$BitFlags>>(iterator: T) -> $BitFlags {
let mut result = Self::empty();
for item in iterator {
result.insert(item)
}
result
}
}
};
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
$($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+,
}) => {
bitflags! {
$(#[$attr])*
flags $BitFlags: $T {
$($(#[$Flag_attr])* const $Flag = $value),+
}
}
};
}