Skip to content

Commit c00d8fd

Browse files
committed
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.
1 parent b733df0 commit c00d8fd

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/libstd/bitflags.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ macro_rules! bitflags(
127127
self.bits
128128
}
129129

130+
/// Convert from underlying bit representation. Unsafe because the
131+
/// bits are not guaranteed to represent valid flags.
132+
pub unsafe fn from_bits(bits: $T) -> $BitFlags {
133+
$BitFlags { bits: bits }
134+
}
135+
130136
/// Returns `true` if no flags are currently stored.
131137
pub fn is_empty(&self) -> bool {
132138
*self == $BitFlags::empty()

0 commit comments

Comments
 (0)