Add (unsafe) coercion from bits to std::bitflags

The intent of `std::bitflags` is to allow building type-safe wrappers
around C-style flags APIs. But in addition to construction these flags
from the Rust side, we need a way to convert them from the C
side. This patch adds a `from_bits` function, which is unsafe since
the bits in question may not represent a valid combination of flags.
This commit is contained in:
Aaron Turon 2014-05-02 10:41:07 -07:00
parent b733df0fc7
commit c00d8fd9a0
1 changed files with 6 additions and 0 deletions

View File

@ -127,6 +127,12 @@ macro_rules! bitflags(
self.bits
}
/// Convert from underlying bit representation. Unsafe because the
/// bits are not guaranteed to represent valid flags.
pub unsafe fn from_bits(bits: $T) -> $BitFlags {
$BitFlags { bits: bits }
}
/// Returns `true` if no flags are currently stored.
pub fn is_empty(&self) -> bool {
*self == $BitFlags::empty()