-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a bitflags! macro #13072
Merged
Merged
Add a bitflags! macro #13072
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8b58981
Add a bitflags! macro
brendanzab a3d9980
Document how generated bitflags can be extended with type and trait i…
brendanzab 43320e5
Document derived traits for bitset! macro
brendanzab 464e375
Move bitflags module to libstd
brendanzab 63ee7bb
Update for language changes
brendanzab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! The `bitflags!` macro generates a `struct` that holds a set of C-style | ||
//! bitmask flags. It is useful for creating typesafe wrappers for C APIs. | ||
//! | ||
//! The flags should only be defined for integer types, otherwise unexpected | ||
//! type errors may occur at compile time. | ||
//! | ||
//! # Example | ||
//! | ||
//! ~~~rust | ||
//! bitflags!(Flags: u32 { | ||
//! FlagA = 0x00000001, | ||
//! FlagB = 0x00000010, | ||
//! FlagC = 0x00000100, | ||
//! FlagABC = FlagA.bits | ||
//! | FlagB.bits | ||
//! | FlagC.bits | ||
//! }) | ||
//! | ||
//! fn main() { | ||
//! let e1 = FlagA | FlagC; | ||
//! let e2 = FlagB | FlagC; | ||
//! assert!((e1 | e2) == FlagABC); // union | ||
//! assert!((e1 & e2) == FlagC); // intersection | ||
//! assert!((e1 - e2) == FlagA); // set difference | ||
//! } | ||
//! ~~~ | ||
//! | ||
//! The generated `struct`s can also be extended with type and trait implementations: | ||
//! | ||
//! ~~~rust | ||
//! use std::fmt; | ||
//! | ||
//! bitflags!(Flags: u32 { | ||
//! FlagA = 0x00000001, | ||
//! FlagB = 0x00000010 | ||
//! }) | ||
//! | ||
//! impl Flags { | ||
//! pub fn clear(&mut self) { | ||
//! self.bits = 0; // The `bits` field can be accessed from within the | ||
//! // same module where the `bitflags!` macro was invoked. | ||
//! } | ||
//! } | ||
//! | ||
//! impl fmt::Show for Flags { | ||
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
//! write!(f.buf, "hi!") | ||
//! } | ||
//! } | ||
//! | ||
//! fn main() { | ||
//! let mut flags = FlagA | FlagB; | ||
//! flags.clear(); | ||
//! assert!(flags.is_empty()); | ||
//! assert_eq!(format!("{}", flags), ~"hi!"); | ||
//! } | ||
//! ~~~ | ||
//! | ||
//! # Derived traits | ||
//! | ||
//! The `Eq`, `TotalEq`, and `Clone` traits are automatically derived for the | ||
//! `struct` using the `deriving` attribute. | ||
//! | ||
//! # Operators | ||
//! | ||
//! The following operator traits are implemented for the generated `struct`: | ||
//! | ||
//! - `BitOr`: union | ||
//! - `BitAnd`: intersection | ||
//! - `Sub`: set difference | ||
//! | ||
//! # Methods | ||
//! | ||
//! The following methods are defined for the generated `struct`: | ||
//! | ||
//! - `empty`: an empty set of flags | ||
//! - `bits`: the raw value of the flags currently stored | ||
//! - `is_empty`: `true` if no flags are currently stored | ||
//! - `intersects`: `true` if there are flags common to both `self` and `other` | ||
//! - `contains`: `true` all of the flags in `other` are contained within `self` | ||
//! - `insert`: inserts the specified flags in-place | ||
//! - `remove`: removes the specified flags in-place | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that we can put documentation on macros themselves, I wonder if this would be best suited on the macro itself. I suppose if you search for |
||
|
||
#[macro_export] | ||
macro_rules! bitflags( | ||
($BitFlags:ident: $T:ty { | ||
$($Flag:ident = $value:expr),+ | ||
}) => ( | ||
#[deriving(Eq, TotalEq, Clone)] | ||
pub struct $BitFlags { | ||
bits: $T, | ||
} | ||
|
||
$(pub static $Flag: $BitFlags = $BitFlags { bits: $value };)+ | ||
|
||
impl $BitFlags { | ||
/// Returns an empty set of flags. | ||
pub fn empty() -> $BitFlags { | ||
$BitFlags { bits: 0 } | ||
} | ||
|
||
/// Returns the raw value of the flags currently stored. | ||
pub fn bits(&self) -> $T { | ||
self.bits | ||
} | ||
|
||
/// Returns `true` if no flags are currently stored. | ||
pub fn is_empty(&self) -> bool { | ||
*self == $BitFlags::empty() | ||
} | ||
|
||
/// Returns `true` if there are flags common to both `self` and `other`. | ||
pub fn intersects(&self, other: $BitFlags) -> bool { | ||
!(self & other).is_empty() | ||
} | ||
|
||
/// Returns `true` all of the flags in `other` are contained within `self`. | ||
pub fn contains(&self, other: $BitFlags) -> bool { | ||
(self & other) == other | ||
} | ||
|
||
/// Inserts the specified flags in-place. | ||
pub fn insert(&mut self, other: $BitFlags) { | ||
self.bits |= other.bits; | ||
} | ||
|
||
/// Removes the specified flags in-place. | ||
pub fn remove(&mut self, other: $BitFlags) { | ||
self.bits &= !other.bits; | ||
} | ||
} | ||
|
||
impl BitOr<$BitFlags, $BitFlags> for $BitFlags { | ||
/// Returns the union of the two sets of flags. | ||
#[inline] | ||
fn bitor(&self, other: &$BitFlags) -> $BitFlags { | ||
$BitFlags { bits: self.bits | other.bits } | ||
} | ||
} | ||
|
||
impl BitAnd<$BitFlags, $BitFlags> for $BitFlags { | ||
/// Returns the intersection between the two sets of flags. | ||
#[inline] | ||
fn bitand(&self, other: &$BitFlags) -> $BitFlags { | ||
$BitFlags { bits: self.bits & other.bits } | ||
} | ||
} | ||
|
||
impl Sub<$BitFlags, $BitFlags> for $BitFlags { | ||
/// Returns the set difference of the two sets of flags. | ||
#[inline] | ||
fn sub(&self, other: &$BitFlags) -> $BitFlags { | ||
$BitFlags { bits: self.bits & !other.bits } | ||
} | ||
} | ||
) | ||
) | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use ops::{BitOr, BitAnd, Sub}; | ||
|
||
bitflags!(Flags: u32 { | ||
FlagA = 0x00000001, | ||
FlagB = 0x00000010, | ||
FlagC = 0x00000100, | ||
FlagABC = FlagA.bits | ||
| FlagB.bits | ||
| FlagC.bits | ||
}) | ||
|
||
#[test] | ||
fn test_bits(){ | ||
assert_eq!(Flags::empty().bits(), 0x00000000); | ||
assert_eq!(FlagA.bits(), 0x00000001); | ||
assert_eq!(FlagABC.bits(), 0x00000111); | ||
} | ||
|
||
#[test] | ||
fn test_is_empty(){ | ||
assert!(Flags::empty().is_empty()); | ||
assert!(!FlagA.is_empty()); | ||
assert!(!FlagABC.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn test_two_empties_do_not_intersect() { | ||
let e1 = Flags::empty(); | ||
let e2 = Flags::empty(); | ||
assert!(!e1.intersects(e2)); | ||
} | ||
|
||
#[test] | ||
fn test_empty_does_not_intersect_with_full() { | ||
let e1 = Flags::empty(); | ||
let e2 = FlagABC; | ||
assert!(!e1.intersects(e2)); | ||
} | ||
|
||
#[test] | ||
fn test_disjoint_intersects() { | ||
let e1 = FlagA; | ||
let e2 = FlagB; | ||
assert!(!e1.intersects(e2)); | ||
} | ||
|
||
#[test] | ||
fn test_overlapping_intersects() { | ||
let e1 = FlagA; | ||
let e2 = FlagA | FlagB; | ||
assert!(e1.intersects(e2)); | ||
} | ||
|
||
#[test] | ||
fn test_contains() { | ||
let e1 = FlagA; | ||
let e2 = FlagA | FlagB; | ||
assert!(!e1.contains(e2)); | ||
assert!(e2.contains(e1)); | ||
assert!(FlagABC.contains(e2)); | ||
} | ||
|
||
#[test] | ||
fn test_insert(){ | ||
let mut e1 = FlagA; | ||
let e2 = FlagA | FlagB; | ||
e1.insert(e2); | ||
assert!(e1 == e2); | ||
} | ||
|
||
#[test] | ||
fn test_remove(){ | ||
let mut e1 = FlagA | FlagB; | ||
let e2 = FlagA | FlagC; | ||
e1.remove(e2); | ||
assert!(e1 == FlagB); | ||
} | ||
|
||
#[test] | ||
fn test_operators() { | ||
let e1 = FlagA | FlagC; | ||
let e2 = FlagB | FlagC; | ||
assert!((e1 | e2) == FlagABC); // union | ||
assert!((e1 & e2) == FlagC); // intersection | ||
assert!((e1 - e2) == FlagA); // set difference | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose we don't want to make the macro too complicated, but perhaps a variant could be added like:
So you can add your custom attributes/deriving modes to the struct. We can wait and see how it turns out though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not with the current macro system, because it causes local ambiguity error.