Skip to content

Commit b733df0

Browse files
committed
Allow attributes in std::bitflags::bitflags!
The `std::bitflags::bitflags!` macro did not provide support for adding attributes to the generated structure or flags, due to limitations in the parser for macros. This patch works around the parser limitations by requiring a `flags` keyword in the overall `bitflags!` invocation, and a `static` keyword for each flag: bitflags!( #[deriving(Hash)] #[doc="Three flags"] flags Flags: u32 { #[doc="The first flag"] static FlagA = 0x00000001, static FlagB = 0x00000010, static FlagC = 0x00000100 } )
1 parent 600507d commit b733df0

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/libstd/bitflags.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
//! # Example
1818
//!
1919
//! ~~~rust
20-
//! bitflags!(Flags: u32 {
21-
//! FlagA = 0x00000001,
22-
//! FlagB = 0x00000010,
23-
//! FlagC = 0x00000100,
24-
//! FlagABC = FlagA.bits
25-
//! | FlagB.bits
26-
//! | FlagC.bits
27-
//! })
20+
//! bitflags!(
21+
//! flags Flags: u32 {
22+
//! static FlagA = 0x00000001,
23+
//! static FlagB = 0x00000010,
24+
//! static FlagC = 0x00000100,
25+
//! static FlagABC = FlagA.bits
26+
//! | FlagB.bits
27+
//! | FlagC.bits
28+
//! }
29+
//! )
2830
//!
2931
//! fn main() {
3032
//! let e1 = FlagA | FlagC;
@@ -40,10 +42,12 @@
4042
//! ~~~rust
4143
//! use std::fmt;
4244
//!
43-
//! bitflags!(Flags: u32 {
44-
//! FlagA = 0x00000001,
45-
//! FlagB = 0x00000010
46-
//! })
45+
//! bitflags!(
46+
//! flags Flags: u32 {
47+
//! static FlagA = 0x00000001,
48+
//! static FlagB = 0x00000010
49+
//! }
50+
//! )
4751
//!
4852
//! impl Flags {
4953
//! pub fn clear(&mut self) {
@@ -66,10 +70,16 @@
6670
//! }
6771
//! ~~~
6872
//!
73+
//! # Attributes
74+
//!
75+
//! Attributes can be attached to the generated `struct` by placing them
76+
//! before the `flags` keyword.
77+
//!
6978
//! # Derived traits
7079
//!
71-
//! The `Eq`, `TotalEq`, and `Clone` traits are automatically derived for the
72-
//! `struct` using the `deriving` attribute.
80+
//! The `Eq` and `Clone` traits are automatically derived for the `struct` using
81+
//! the `deriving` attribute. Additional traits can be derived by providing an
82+
//! explicit `deriving` attribute on `flags`.
7383
//!
7484
//! # Operators
7585
//!
@@ -91,17 +101,20 @@
91101
//! - `insert`: inserts the specified flags in-place
92102
//! - `remove`: removes the specified flags in-place
93103
104+
#![macro_escape]
105+
94106
#[macro_export]
95107
macro_rules! bitflags(
96-
($BitFlags:ident: $T:ty {
97-
$($Flag:ident = $value:expr),+
108+
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
109+
$($(#[$Flag_attr:meta])* static $Flag:ident = $value:expr),+
98110
}) => (
99111
#[deriving(Eq, TotalEq, Clone)]
112+
$(#[$attr])*
100113
pub struct $BitFlags {
101114
bits: $T,
102115
}
103116

104-
$(pub static $Flag: $BitFlags = $BitFlags { bits: $value };)+
117+
$($(#[$Flag_attr])* pub static $Flag: $BitFlags = $BitFlags { bits: $value };)+
105118

106119
impl $BitFlags {
107120
/// Returns an empty set of flags.
@@ -170,14 +183,16 @@ macro_rules! bitflags(
170183
mod tests {
171184
use ops::{BitOr, BitAnd, Sub};
172185

173-
bitflags!(Flags: u32 {
174-
FlagA = 0x00000001,
175-
FlagB = 0x00000010,
176-
FlagC = 0x00000100,
177-
FlagABC = FlagA.bits
178-
| FlagB.bits
179-
| FlagC.bits
180-
})
186+
bitflags!(
187+
flags Flags: u32 {
188+
static FlagA = 0x00000001,
189+
static FlagB = 0x00000010,
190+
static FlagC = 0x00000100,
191+
static FlagABC = FlagA.bits
192+
| FlagB.bits
193+
| FlagC.bits
194+
}
195+
)
181196

182197
#[test]
183198
fn test_bits(){

0 commit comments

Comments
 (0)