|
17 | 17 | //! # Example
|
18 | 18 | //!
|
19 | 19 | //! ~~~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 | +//! ) |
28 | 30 | //!
|
29 | 31 | //! fn main() {
|
30 | 32 | //! let e1 = FlagA | FlagC;
|
|
40 | 42 | //! ~~~rust
|
41 | 43 | //! use std::fmt;
|
42 | 44 | //!
|
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 | +//! ) |
47 | 51 | //!
|
48 | 52 | //! impl Flags {
|
49 | 53 | //! pub fn clear(&mut self) {
|
|
66 | 70 | //! }
|
67 | 71 | //! ~~~
|
68 | 72 | //!
|
| 73 | +//! # Attributes |
| 74 | +//! |
| 75 | +//! Attributes can be attached to the generated `struct` by placing them |
| 76 | +//! before the `flags` keyword. |
| 77 | +//! |
69 | 78 | //! # Derived traits
|
70 | 79 | //!
|
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`. |
73 | 83 | //!
|
74 | 84 | //! # Operators
|
75 | 85 | //!
|
|
91 | 101 | //! - `insert`: inserts the specified flags in-place
|
92 | 102 | //! - `remove`: removes the specified flags in-place
|
93 | 103 |
|
| 104 | +#![macro_escape] |
| 105 | + |
94 | 106 | #[macro_export]
|
95 | 107 | 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),+ |
98 | 110 | }) => (
|
99 | 111 | #[deriving(Eq, TotalEq, Clone)]
|
| 112 | + $(#[$attr])* |
100 | 113 | pub struct $BitFlags {
|
101 | 114 | bits: $T,
|
102 | 115 | }
|
103 | 116 |
|
104 |
| - $(pub static $Flag: $BitFlags = $BitFlags { bits: $value };)+ |
| 117 | + $($(#[$Flag_attr])* pub static $Flag: $BitFlags = $BitFlags { bits: $value };)+ |
105 | 118 |
|
106 | 119 | impl $BitFlags {
|
107 | 120 | /// Returns an empty set of flags.
|
@@ -170,14 +183,16 @@ macro_rules! bitflags(
|
170 | 183 | mod tests {
|
171 | 184 | use ops::{BitOr, BitAnd, Sub};
|
172 | 185 |
|
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 | + ) |
181 | 196 |
|
182 | 197 | #[test]
|
183 | 198 | fn test_bits(){
|
|
0 commit comments