about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorAaron Turon <aturon@mozilla.com>2014-05-02 09:41:34 -0700
committerAaron Turon <aturon@mozilla.com>2014-05-05 15:24:36 -0700
commitb733df0fc7942aea205d8fc451d2a4d17d47dfe9 (patch)
treec72580abfe9870fea2a9e5b3e5eb34c93df23599 /src/libstd
parent600507d5380ca7d6d5536fabc2d4aca400c21bb9 (diff)
downloadrust-b733df0fc7942aea205d8fc451d2a4d17d47dfe9.tar.gz
rust-b733df0fc7942aea205d8fc451d2a4d17d47dfe9.zip
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
        }
    )
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/bitflags.rs65
1 files changed, 40 insertions, 25 deletions
diff --git a/src/libstd/bitflags.rs b/src/libstd/bitflags.rs
index bf12dd2d94a..3c06682eaaf 100644
--- a/src/libstd/bitflags.rs
+++ b/src/libstd/bitflags.rs
@@ -17,14 +17,16 @@
 //! # Example
 //!
 //! ~~~rust
-//! bitflags!(Flags: u32 {
-//!     FlagA       = 0x00000001,
-//!     FlagB       = 0x00000010,
-//!     FlagC       = 0x00000100,
-//!     FlagABC     = FlagA.bits
-//!                 | FlagB.bits
-//!                 | FlagC.bits
-//! })
+//! bitflags!(
+//!     flags Flags: u32 {
+//!         static FlagA       = 0x00000001,
+//!         static FlagB       = 0x00000010,
+//!         static FlagC       = 0x00000100,
+//!         static FlagABC     = FlagA.bits
+//!                            | FlagB.bits
+//!                            | FlagC.bits
+//!     }
+//! )
 //!
 //! fn main() {
 //!     let e1 = FlagA | FlagC;
@@ -40,10 +42,12 @@
 //! ~~~rust
 //! use std::fmt;
 //!
-//! bitflags!(Flags: u32 {
-//!     FlagA   = 0x00000001,
-//!     FlagB   = 0x00000010
-//! })
+//! bitflags!(
+//!     flags Flags: u32 {
+//!         static FlagA   = 0x00000001,
+//!         static FlagB   = 0x00000010
+//!     }
+//! )
 //!
 //! impl Flags {
 //!     pub fn clear(&mut self) {
@@ -66,10 +70,16 @@
 //! }
 //! ~~~
 //!
+//! # Attributes
+//!
+//! Attributes can be attached to the generated `struct` by placing them
+//! before the `flags` keyword.
+//!
 //! # Derived traits
 //!
-//! The `Eq`, `TotalEq`, and `Clone` traits are automatically derived for the
-//! `struct` using the `deriving` attribute.
+//! The `Eq` and `Clone` traits are automatically derived for the `struct` using
+//! the `deriving` attribute. Additional traits can be derived by providing an
+//! explicit `deriving` attribute on `flags`.
 //!
 //! # Operators
 //!
@@ -91,17 +101,20 @@
 //! - `insert`: inserts the specified flags in-place
 //! - `remove`: removes the specified flags in-place
 
+#![macro_escape]
+
 #[macro_export]
 macro_rules! bitflags(
-    ($BitFlags:ident: $T:ty {
-        $($Flag:ident = $value:expr),+
+    ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
+        $($(#[$Flag_attr:meta])* static $Flag:ident = $value:expr),+
     }) => (
         #[deriving(Eq, TotalEq, Clone)]
+        $(#[$attr])*
         pub struct $BitFlags {
             bits: $T,
         }
 
-        $(pub static $Flag: $BitFlags = $BitFlags { bits: $value };)+
+        $($(#[$Flag_attr])* pub static $Flag: $BitFlags = $BitFlags { bits: $value };)+
 
         impl $BitFlags {
             /// Returns an empty set of flags.
@@ -170,14 +183,16 @@ macro_rules! bitflags(
 mod tests {
     use ops::{BitOr, BitAnd, Sub};
 
-    bitflags!(Flags: u32 {
-        FlagA       = 0x00000001,
-        FlagB       = 0x00000010,
-        FlagC       = 0x00000100,
-        FlagABC     = FlagA.bits
-                    | FlagB.bits
-                    | FlagC.bits
-    })
+    bitflags!(
+        flags Flags: u32 {
+            static FlagA       = 0x00000001,
+            static FlagB       = 0x00000010,
+            static FlagC       = 0x00000100,
+            static FlagABC     = FlagA.bits
+                               | FlagB.bits
+                               | FlagC.bits
+        }
+    )
 
     #[test]
     fn test_bits(){