1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
// 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 Flags: u32 { //! static FlagA = 0x00000001, //! static FlagB = 0x00000010, //! static FlagC = 0x00000100, //! static FlagAB = FlagA.bits //! | FlagB.bits, //! static 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) == FlagAB); // symmetric difference //! assert!((e1 - e2) == FlagA); // set difference //! assert!(!e2 == FlagA); // set complement //! } //! ~~~ //! //! The generated `struct`s can also be extended with type and trait implementations: //! //! ~~~rust //! use std::fmt; //! //! bitflags!( //! flags Flags: u32 { //! static FlagA = 0x00000001, //! static 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, "hi!") //! } //! } //! //! fn main() { //! let mut flags = FlagA | FlagB; //! flags.clear(); //! assert!(flags.is_empty()); //! assert_eq!(format!("{}", flags).as_slice(), "hi!"); //! } //! ~~~ //! //! # Attributes //! //! Attributes can be attached to the generated `struct` by placing them //! before the `flags` keyword. //! //! # Derived traits //! //! The `PartialEq` 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 //! //! The following operator traits are implemented for the generated `struct`: //! //! - `BitOr`: union //! - `BitAnd`: intersection //! - `BitXor`: symmetric difference //! - `Sub`: set difference //! - `Not`: set complement //! //! # Methods //! //! The following methods are defined for the generated `struct`: //! //! - `empty`: an empty set of flags //! - `all`: the set of all flags //! - `bits`: the raw value of the flags currently stored //! - `is_empty`: `true` if no flags are currently stored //! - `is_all`: `true` if all flags are currently set //! - `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 #![experimental] #![macro_escape] #[macro_export] macro_rules! bitflags( ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty { $($(#[$Flag_attr:meta])* static $Flag:ident = $value:expr),+ }) => ( #[deriving(PartialEq, Eq, Clone)] $(#[$attr])* pub struct $BitFlags { bits: $T, } $($(#[$Flag_attr])* pub static $Flag: $BitFlags = $BitFlags { bits: $value };)+ impl $BitFlags { /// Returns an empty set of flags. pub fn empty() -> $BitFlags { $BitFlags { bits: 0 } } /// Returns the set containing all flags. pub fn all() -> $BitFlags { $BitFlags { bits: $($value)|+ } } /// Returns the raw value of the flags currently stored. pub fn bits(&self) -> $T { self.bits } /// Convert from underlying bit representation, unless that /// representation contains bits that do not correspond to a flag. pub fn from_bits(bits: $T) -> ::std::option::Option<$BitFlags> { if (bits & !$BitFlags::all().bits()) != 0 { ::std::option::None } else { ::std::option::Some($BitFlags { bits: bits }) } } /// Convert from underlying bit representation, dropping any bits /// that do not correspond to flags. pub fn from_bits_truncate(bits: $T) -> $BitFlags { $BitFlags { bits: bits } & $BitFlags::all() } /// Returns `true` if no flags are currently stored. pub fn is_empty(&self) -> bool { *self == $BitFlags::empty() } /// Returns `true` if all flags are currently set. pub fn is_all(&self) -> bool { *self == $BitFlags::all() } /// 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 core::ops::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 core::ops::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 core::ops::BitXor<$BitFlags, $BitFlags> for $BitFlags { /// Returns the symmetric difference between the two sets of flags. #[inline] fn bitxor(&self, other: &$BitFlags) -> $BitFlags { $BitFlags { bits: self.bits ^ other.bits } } } impl core::ops::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 } } } impl core::ops::Not<$BitFlags> for $BitFlags { /// Returns the complement of this set of flags. #[inline] fn not(&self) -> $BitFlags { $BitFlags { bits: !self.bits } & $BitFlags::all() } } ) )