Skip to content

Commit

Permalink
Merge pull request #371 from KodrAus/feat/unnamed_flags
Browse files Browse the repository at this point in the history
Support unnamed flags
  • Loading branch information
KodrAus committed Jul 14, 2023
2 parents 75a85b6 + e825f6b commit 27f0e43
Show file tree
Hide file tree
Showing 27 changed files with 402 additions and 137 deletions.
16 changes: 8 additions & 8 deletions src/example_generated.rs
Expand Up @@ -21,16 +21,16 @@ __impl_internal_bitflags! {
// Field `A`.
///
/// This flag has the value `0b00000001`.
A = 0b00000001;
const A = 0b00000001;
/// Field `B`.
///
/// This flag has the value `0b00000010`.
B = 0b00000010;
const B = 0b00000010;
/// Field `C`.
///
/// This flag has the value `0b00000100`.
C = 0b00000100;
ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
const C = 0b00000100;
const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
}
}

Expand All @@ -51,15 +51,15 @@ __impl_public_bitflags_consts! {
/// Field `A`.
///
/// This flag has the value `0b00000001`.
A = 0b00000001;
const A = 0b00000001;
/// Field `B`.
///
/// This flag has the value `0b00000010`.
B = 0b00000010;
const B = 0b00000010;
/// Field `C`.
///
/// This flag has the value `0b00000100`.
C = 0b00000100;
ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
const C = 0b00000100;
const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
}
}
52 changes: 26 additions & 26 deletions src/external.rs
Expand Up @@ -21,8 +21,8 @@ macro_rules! __impl_external_bitflags_my_library {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
$(#[$inner:ident $($args:tt)*])*
const $Flag:tt;
)*
}
) => {
Expand All @@ -37,8 +37,8 @@ macro_rules! __impl_external_bitflags_my_library {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
$(#[$inner:ident $($args:tt)*])*
const $Flag:tt;
)*
}
) => {};
Expand All @@ -57,8 +57,8 @@ Now, we add our macro call to the `__impl_external_bitflags` macro body:
__impl_external_bitflags_my_library! {
$InternalBitFlags: $T, $PublicBitFlags {
$(
$(#[$attr $($args)*])*
$Flag;
$(#[$inner $($args)*])*
const $Flag;
)*
}
}
Expand All @@ -83,8 +83,8 @@ macro_rules! __impl_external_bitflags {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
$(#[$inner:ident $($args:tt)*])*
const $Flag:tt;
)*
}
) => {
Expand All @@ -95,26 +95,26 @@ macro_rules! __impl_external_bitflags {
__impl_external_bitflags_serde! {
$InternalBitFlags: $T, $PublicBitFlags {
$(
$(#[$attr $($args)*])*
$Flag;
$(#[$inner $($args)*])*
const $Flag;
)*
}
}

__impl_external_bitflags_arbitrary! {
$InternalBitFlags: $T, $PublicBitFlags {
$(
$(#[$attr $($args)*])*
$Flag;
$(#[$inner $($args)*])*
const $Flag;
)*
}
}

__impl_external_bitflags_bytemuck! {
$InternalBitFlags: $T, $PublicBitFlags {
$(
$(#[$attr $($args)*])*
$Flag;
$(#[$inner $($args)*])*
const $Flag;
)*
}
}
Expand All @@ -132,8 +132,8 @@ macro_rules! __impl_external_bitflags_serde {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
$(#[$inner:ident $($args:tt)*])*
const $Flag:tt;
)*
}
) => {
Expand Down Expand Up @@ -168,8 +168,8 @@ macro_rules! __impl_external_bitflags_serde {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
$(#[$inner:ident $($args:tt)*])*
const $Flag:tt;
)*
}
) => {};
Expand All @@ -189,8 +189,8 @@ macro_rules! __impl_external_bitflags_arbitrary {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
$(#[$inner:ident $($args:tt)*])*
const $Flag:tt;
)*
}
) => {
Expand All @@ -211,8 +211,8 @@ macro_rules! __impl_external_bitflags_arbitrary {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
$(#[$inner:ident $($args:tt)*])*
const $Flag:tt;
)*
}
) => {};
Expand All @@ -226,8 +226,8 @@ macro_rules! __impl_external_bitflags_bytemuck {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
$(#[$inner:ident $($args:tt)*])*
const $Flag:tt;
)*
}
) => {
Expand All @@ -254,8 +254,8 @@ macro_rules! __impl_external_bitflags_bytemuck {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
$(#[$inner:ident $($args:tt)*])*
const $Flag:tt;
)*
}
) => {};
Expand Down
8 changes: 4 additions & 4 deletions src/internal.rs
Expand Up @@ -31,8 +31,8 @@ macro_rules! __impl_internal_bitflags {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident = $value:expr;
$(#[$inner:ident $($args:tt)*])*
const $Flag:tt = $value:expr;
)*
}
) => {
Expand Down Expand Up @@ -100,8 +100,8 @@ macro_rules! __impl_internal_bitflags {
__impl_public_bitflags! {
$InternalBitFlags: $T, $PublicBitFlags {
$(
$(#[$attr $($args)*])*
$Flag;
$(#[$inner $($args)*])*
const $Flag = $value;
)*
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/iter.rs
Expand Up @@ -109,6 +109,11 @@ impl<B: Flags> Iterator for IterNames<B> {

self.idx += 1;

// Skip unnamed flags
if flag.name().is_empty() {
continue;
}

let bits = flag.value().bits();

// If the flag is set in the original source _and_ it has bits that haven't
Expand Down
49 changes: 36 additions & 13 deletions src/lib.rs
Expand Up @@ -352,10 +352,9 @@
//!
//! ## Multi-bit Flags
//!
//! It is allowed to define a flag with multiple bits set, however such
//! flags are _not_ treated as a set where any of those bits is a valid
//! flag. Instead, each flag is treated as a unit when converting from
//! bits with [`from_bits`] or [`from_bits_truncate`].
//! It is allowed to define a flag with multiple bits set. When using multi-bit flags
//! with [`from_bits`] or [`from_bits_truncate`], if only a subset of the bits in that flag
//! are set, the result will still be non-empty:
//!
//! ```
//! use bitflags::bitflags;
Expand All @@ -369,8 +368,8 @@
//!
//! fn main() {
//! // This bit pattern does not set all the bits in `F3`, so it is rejected.
//! assert!(Flags::from_bits(0b00000001).is_none());
//! assert!(Flags::from_bits_truncate(0b00000001).is_empty());
//! assert!(Flags::from_bits(0b00000001).is_some());
//! assert!(!Flags::from_bits_truncate(0b00000001).is_empty());
//! }
//! ```
//!
Expand Down Expand Up @@ -560,7 +559,7 @@ macro_rules! bitflags {
$vis:vis struct $BitFlags:ident: $T:ty {
$(
$(#[$inner:ident $($args:tt)*])*
const $Flag:ident = $value:expr;
const $Flag:tt = $value:expr;
)*
}

Expand All @@ -578,7 +577,7 @@ macro_rules! bitflags {
$BitFlags: $T {
$(
$(#[$inner $($args)*])*
$Flag = $value;
const $Flag = $value;
)*
}
}
Expand All @@ -604,7 +603,7 @@ macro_rules! bitflags {
InternalBitFlags: $T, $BitFlags {
$(
$(#[$inner $($args)*])*
$Flag = $value;
const $Flag = $value;
)*
}
}
Expand All @@ -614,7 +613,7 @@ macro_rules! bitflags {
InternalBitFlags: $T, $BitFlags {
$(
$(#[$inner $($args)*])*
$Flag;
const $Flag;
)*
}
}
Expand All @@ -640,7 +639,7 @@ macro_rules! bitflags {
impl $BitFlags:ident: $T:ty {
$(
$(#[$inner:ident $($args:tt)*])*
const $Flag:ident = $value:expr;
const $Flag:tt = $value:expr;
)*
}

Expand All @@ -650,7 +649,7 @@ macro_rules! bitflags {
$BitFlags: $T {
$(
$(#[$inner $($args)*])*
$Flag = $value;
const $Flag = $value;
)*
}
}
Expand All @@ -670,7 +669,7 @@ macro_rules! bitflags {
$BitFlags: $T, $BitFlags {
$(
$(#[$inner $($args)*])*
$Flag;
const $Flag = $value;
)*
}
}
Expand Down Expand Up @@ -1012,6 +1011,30 @@ macro_rules! __bitflags_expr_safe_attrs {
}
}

/// Implement a flag, which may be a wildcard `_`.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __bitflags_flag {
(
{
name: _,
named: { $($named:tt)* },
unnamed: { $($unnamed:tt)* },
}
) => {
$($unnamed)*
};
(
{
name: $Flag:ident,
named: { $($named:tt)* },
unnamed: { $($unnamed:tt)* },
}
) => {
$($named)*
};
}

#[macro_use]
mod public;
#[macro_use]
Expand Down

0 comments on commit 27f0e43

Please sign in to comment.