Crate bitflags + + [−] + + [src]
+A typesafe bitmask flag generator.
+Macros
+bitflags! | +
+ The |
+
From 64106c4d3d4ddba8c7bc2af75376e6d3d3d75601 Mon Sep 17 00:00:00 2001 From: Date: Mon, 29 Jun 2015 20:16:15 +0000 Subject: Update documentation --- bitflags/index.html | 113 +++++++++++++ bitflags/macro.bitflags!.html | 380 ++++++++++++++++++++++++++++++++++++++++++ bitflags/sidebar-items.js | 1 + 3 files changed, 494 insertions(+) create mode 100644 bitflags/index.html create mode 100644 bitflags/macro.bitflags!.html create mode 100644 bitflags/sidebar-items.js (limited to 'bitflags') diff --git a/bitflags/index.html b/bitflags/index.html new file mode 100644 index 0000000..041b5ef --- /dev/null +++ b/bitflags/index.html @@ -0,0 +1,113 @@ + + +
+ + + + + + +A typesafe bitmask flag generator.
+bitflags! | +
+ The |
+
+ Prefix searches with a type followed by a colon (e.g.
+ fn:
) to restrict the search to a given type.
+
+ Accepted types are: fn
, mod
,
+ struct
, enum
,
+ trait
, typedef
(or
+ tdef
).
+
+ Search functions by type signature (e.g.
+ vec -> usize
)
+
+macro_rules! bitflags { + ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty { + $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+ + }) => { + #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)] + $(#[$attr])* + pub struct $BitFlags { + bits: $T, + } + + $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+ + + impl ::std::fmt::Debug for $BitFlags { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + let out = format!("{} {{ bits: {:#b} }}", + stringify!($BitFlags), + self.bits); + f.write_str(&out[..]) + } + } + + impl $BitFlags { + /// Returns an empty set of flags. + #[inline] + pub fn empty() -> $BitFlags { + $BitFlags { bits: 0 } + } + + /// Returns the set containing all flags. + #[inline] + pub fn all() -> $BitFlags { + $BitFlags { bits: $($value)|+ } + } + + /// Returns the raw value of the flags currently stored. + #[inline] + pub fn bits(&self) -> $T { + self.bits + } + + /// Convert from underlying bit representation, unless that + /// representation contains bits that do not correspond to a flag. + #[inline] + pub fn from_bits(bits: $T) -> ::std::option::Option<$BitFlags> { + if (bits & !$BitFlags::all().bits()) != 0 { + ::std::option::Option::None + } else { + ::std::option::Option::Some($BitFlags { bits: bits }) + } + } + + /// Convert from underlying bit representation, dropping any bits + /// that do not correspond to flags. + #[inline] + pub fn from_bits_truncate(bits: $T) -> $BitFlags { + $BitFlags { bits: bits } & $BitFlags::all() + } + + /// Returns `true` if no flags are currently stored. + #[inline] + pub fn is_empty(&self) -> bool { + *self == $BitFlags::empty() + } + + /// Returns `true` if all flags are currently set. + #[inline] + pub fn is_all(&self) -> bool { + *self == $BitFlags::all() + } + + /// Returns `true` if there are flags common to both `self` and `other`. + #[inline] + pub fn intersects(&self, other: $BitFlags) -> bool { + !(*self & other).is_empty() + } + + /// Returns `true` all of the flags in `other` are contained within `self`. + #[inline] + pub fn contains(&self, other: $BitFlags) -> bool { + (*self & other) == other + } + + /// Inserts the specified flags in-place. + #[inline] + pub fn insert(&mut self, other: $BitFlags) { + self.bits |= other.bits; + } + + /// Removes the specified flags in-place. + #[inline] + pub fn remove(&mut self, other: $BitFlags) { + self.bits &= !other.bits; + } + + /// Toggles the specified flags in-place. + #[inline] + pub fn toggle(&mut self, other: $BitFlags) { + self.bits ^= other.bits; + } + } + + impl ::std::ops::BitOr for $BitFlags { + type Output = $BitFlags; + + /// Returns the union of the two sets of flags. + #[inline] + fn bitor(self, other: $BitFlags) -> $BitFlags { + $BitFlags { bits: self.bits | other.bits } + } + } + + impl ::std::ops::BitXor for $BitFlags { + type Output = $BitFlags; + + /// Returns the left flags, but with all the right flags toggled. + #[inline] + fn bitxor(self, other: $BitFlags) -> $BitFlags { + $BitFlags { bits: self.bits ^ other.bits } + } + } + + impl ::std::ops::BitAnd for $BitFlags { + type Output = $BitFlags; + + /// Returns the intersection between the two sets of flags. + #[inline] + fn bitand(self, other: $BitFlags) -> $BitFlags { + $BitFlags { bits: self.bits & other.bits } + } + } + + impl ::std::ops::Sub for $BitFlags { + type Output = $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 ::std::ops::Not for $BitFlags { + type Output = $BitFlags; + + /// Returns the complement of this set of flags. + #[inline] + fn not(self) -> $BitFlags { + $BitFlags { bits: !self.bits } & $BitFlags::all() + } + } + }; + ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty { + $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+, + }) => { + bitflags! { + $(#[$attr])* + flags $BitFlags: $T { + $($(#[$Flag_attr])* const $Flag = $value),+ + } + } + }; +} ++
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.
+ ++#[macro_use] +extern crate bitflags; + +bitflags! { + flags Flags: u32 { + const FLAG_A = 0b00000001, + const FLAG_B = 0b00000010, + const FLAG_C = 0b00000100, + const FLAG_ABC = FLAG_A.bits + | FLAG_B.bits + | FLAG_C.bits, + } +} + +fn main() { + let e1 = FLAG_A | FLAG_C; + let e2 = FLAG_B | FLAG_C; + assert!((e1 | e2) == FLAG_ABC); // union + assert!((e1 & e2) == FLAG_C); // intersection + assert!((e1 - e2) == FLAG_A); // set difference + assert!(!e2 == FLAG_A); // set complement +} ++ +
The generated struct
s can also be extended with type and trait
+implementations:
+#[macro_use] +extern crate bitflags; + +use std::fmt; + +bitflags! { + flags Flags: u32 { + const FLAG_A = 0b00000001, + const FLAG_B = 0b00000010, + } +} + +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::Display for Flags { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "hi!") + } +} + +fn main() { + let mut flags = FLAG_A | FLAG_B; + flags.clear(); + assert!(flags.is_empty()); + assert_eq!(format!("{}", flags), "hi!"); + assert_eq!(format!("{:?}", FLAG_A | FLAG_B), "Flags { bits: 0b11 }"); + assert_eq!(format!("{:?}", FLAG_B), "Flags { bits: 0b10 }"); +} ++ +
Attributes can be attached to the generated struct
by placing them
+before the flags
keyword.
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
. The Debug
trait is
+also implemented by displaying the bits value of the internal struct.
The following operator traits are implemented for the generated struct
:
BitOr
: unionBitAnd
: intersectionBitXor
: toggleSub
: set differenceNot
: set complementThe following methods are defined for the generated struct
:
empty
: an empty set of flagsall
: the set of all flagsbits
: the raw value of the flags currently storedfrom_bits
: convert from underlying bit representation, unless that
+ representation contains bits that do not correspond to a flagfrom_bits_truncate
: convert from underlying bit representation, dropping
+ any bits that do not correspond to flagsis_empty
: true
if no flags are currently storedis_all
: true
if all flags are currently setintersects
: 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-placeremove
: removes the specified flags in-placetoggle
: the specified flags will be inserted if not present, and removed
+ if they are.
+ Prefix searches with a type followed by a colon (e.g.
+ fn:
) to restrict the search to a given type.
+
+ Accepted types are: fn
, mod
,
+ struct
, enum
,
+ trait
, typedef
(or
+ tdef
).
+
+ Search functions by type signature (e.g.
+ vec -> usize
)
+