1
Fork 0

auto merge of #18103 : pcwalton/rust/bitflags-inline, r=thestinger

Servo really wants this.

r? @nick29581
This commit is contained in:
bors 2014-10-18 10:17:14 +00:00
commit c7c342d10c

View file

@ -127,22 +127,26 @@ macro_rules! bitflags {
impl $BitFlags { impl $BitFlags {
/// Returns an empty set of flags. /// Returns an empty set of flags.
#[inline]
pub fn empty() -> $BitFlags { pub fn empty() -> $BitFlags {
$BitFlags { bits: 0 } $BitFlags { bits: 0 }
} }
/// Returns the set containing all flags. /// Returns the set containing all flags.
#[inline]
pub fn all() -> $BitFlags { pub fn all() -> $BitFlags {
$BitFlags { bits: $($value)|+ } $BitFlags { bits: $($value)|+ }
} }
/// Returns the raw value of the flags currently stored. /// Returns the raw value of the flags currently stored.
#[inline]
pub fn bits(&self) -> $T { pub fn bits(&self) -> $T {
self.bits self.bits
} }
/// Convert from underlying bit representation, unless that /// Convert from underlying bit representation, unless that
/// representation contains bits that do not correspond to a flag. /// representation contains bits that do not correspond to a flag.
#[inline]
pub fn from_bits(bits: $T) -> ::std::option::Option<$BitFlags> { pub fn from_bits(bits: $T) -> ::std::option::Option<$BitFlags> {
if (bits & !$BitFlags::all().bits()) != 0 { if (bits & !$BitFlags::all().bits()) != 0 {
::std::option::None ::std::option::None
@ -153,21 +157,25 @@ macro_rules! bitflags {
/// Convert from underlying bit representation, dropping any bits /// Convert from underlying bit representation, dropping any bits
/// that do not correspond to flags. /// that do not correspond to flags.
#[inline]
pub fn from_bits_truncate(bits: $T) -> $BitFlags { pub fn from_bits_truncate(bits: $T) -> $BitFlags {
$BitFlags { bits: bits } & $BitFlags::all() $BitFlags { bits: bits } & $BitFlags::all()
} }
/// Returns `true` if no flags are currently stored. /// Returns `true` if no flags are currently stored.
#[inline]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
*self == $BitFlags::empty() *self == $BitFlags::empty()
} }
/// Returns `true` if all flags are currently set. /// Returns `true` if all flags are currently set.
#[inline]
pub fn is_all(&self) -> bool { pub fn is_all(&self) -> bool {
*self == $BitFlags::all() *self == $BitFlags::all()
} }
/// Returns `true` if there are flags common to both `self` and `other`. /// Returns `true` if there are flags common to both `self` and `other`.
#[inline]
pub fn intersects(&self, other: $BitFlags) -> bool { pub fn intersects(&self, other: $BitFlags) -> bool {
!(self & other).is_empty() !(self & other).is_empty()
} }
@ -179,16 +187,19 @@ macro_rules! bitflags {
} }
/// Inserts the specified flags in-place. /// Inserts the specified flags in-place.
#[inline]
pub fn insert(&mut self, other: $BitFlags) { pub fn insert(&mut self, other: $BitFlags) {
self.bits |= other.bits; self.bits |= other.bits;
} }
/// Removes the specified flags in-place. /// Removes the specified flags in-place.
#[inline]
pub fn remove(&mut self, other: $BitFlags) { pub fn remove(&mut self, other: $BitFlags) {
self.bits &= !other.bits; self.bits &= !other.bits;
} }
/// Toggles the specified flags in-place. /// Toggles the specified flags in-place.
#[inline]
pub fn toggle(&mut self, other: $BitFlags) { pub fn toggle(&mut self, other: $BitFlags) {
self.bits ^= other.bits; self.bits ^= other.bits;
} }