Skip to content

Commit 24f03af

Browse files
committed
make_bitflags: Allow omitting { } for singular flags
1 parent 754a8de commit 24f03af

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/lib.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ pub struct BitFlags<T, N = <T as _internal::RawBitFlags>::Numeric> {
558558
/// assert_eq!(x, Test::A | Test::C);
559559
///
560560
/// // Also works in const contexts:
561-
/// const X: BitFlags<Test> = make_bitflags!(Test::{A | C});
561+
/// const X: BitFlags<Test> = make_bitflags!(Test::A);
562562
/// ```
563563
#[macro_export]
564564
macro_rules! make_bitflags {
@@ -576,7 +576,17 @@ macro_rules! make_bitflags {
576576
unsafe { $crate::BitFlags::<$enum>::from_bits_unchecked_c(
577577
n, $crate::BitFlags::CONST_TOKEN) }
578578
}
579-
}
579+
};
580+
( $enum:ident :: $variant:ident ) => {
581+
{
582+
let flag: $enum = $enum::$variant;
583+
let n = flag as <$enum as $crate::_internal::RawBitFlags>::Numeric;
584+
// SAFETY: The value has been created from the numeric value of
585+
// the underlying enum, so only valid bits are set.
586+
unsafe { $crate::BitFlags::<$enum>::from_bits_unchecked_c(
587+
n, $crate::BitFlags::CONST_TOKEN) }
588+
}
589+
};
580590
}
581591

582592
/// The default value returned is one with all flags unset, i. e. [`empty`][Self::empty],

0 commit comments

Comments
 (0)