altbitflags/lib.rs
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
//! This crate contains macros that make creating bitfields more intuitive.
//!
//! It is an alternative way of what 'Rust Compiler Team Bitflags' do.
//! It is not similar to C bitflags but it may be a bit easier to read and use
//! in some cases. Furthermore, this crate does not conflict with
//! 'Rust Compiler Team Bitflags' and you can use both as it must be okay.
//!
//! # Example
//! ```rust
//! # #[macro_use]
//! # extern crate altbitflags;
//! #
//! // Create some structure which will act like a bitfield structure.
//! struct Something(i64);
//!
//! impl Something {
//!
//! // Create ReadWrite flag called 'present' (or shorthand 'p').
//! // To set a value use 'set_p' or 'set_present'.
//! // Flag position is bit number 0.
//! flag_rw!(p, set_p, present, set_present, 0);
//!
//! // Create ReadOnly flag 'extended' (and 'e') at position 1.
//! flag_ro!(e, extended, 1);
//!
//! // Shorter version of a flag_rw! macro.
//! flag_rw!(a, set_a, 2);
//!
//! // Shorter version of a flag_ro! macro.
//! flag_ro!(b, 3);
//! }
//!
//! fn main() {
//! let mut something = Something(0);
//!
//! // This means the same thing:
//! if (something.p()) { /* ... */ }
//! if (something.present()) { /* ... */ }
//!
//! // As does this:
//! something.set_present(true);
//! something.set_p(true);
//! }
//! ```
/// Create read-only flag.
///
/// It accepts a name argument, optional full name argument and a number,
/// which indicates the bit number with required flag.
///
/// Later, the given flag can be accessed either by short or full name
/// in a form of a normal function.
///
/// It will work only if it's put in a structure implementation
/// where integer number with bit fields can be accessed with 'self.0'.
///
/// ReadOnly flags accepts 'self.0' to be of any integer type.
///
/// # Example
///
/// ```
/// # #![allow(unused_parens)]
/// #[macro_use]
/// extern crate altbitflags;
///
/// struct Something(i64);
///
/// impl Something {
/// flag_ro!(present, 0);
/// flag_ro!(tx, 1);
/// flag_ro!(e, extended, 2);
///
/// // It is okay to define different functions for a single bit:
/// flag_ro!(some, 3);
/// flag_ro!(some_bit, 3);
/// }
///
/// fn main() {
/// let mut something = Something(0);
///
/// if (something.present()) { /* ... */ }
/// if (something.tx()) { /* ... */ }
///
/// // These do the same thing:
/// if (something.e()) { /* ... */ }
/// if (something.extended()) { /* ... */ }
/// }
/// ```
#[macro_export]
macro_rules! flag_ro {
($name:ident, $pos:expr) => (
#[inline="always"]
pub fn $name(&self) -> bool { self.0 & (1 << $pos) != 0 }
);
($name:ident, $full_name:ident, $pos:expr) => (
flag_ro!($name, $pos);
flag_ro!($full_name, $pos);
);
}
/// Create read-write flag.
///
/// It accepts an name argument, optional full name argument and a number,
/// which indicates the bit number with required flag.
///
/// Later, the given flag can be accessed either by short or full name
/// like with a normal function.
///
/// It will work only if it's put in a structure implementation
/// where integer number with fields can be accessed with 'self.0'.
///
/// Currently, 'self.0' must be of type 'i64'.
///
/// # Example
///
/// ```
/// # #![allow(unused_parens)]
/// #[macro_use]
/// extern crate altbitflags;
///
/// struct Something(i64);
///
/// impl Something {
/// flag_rw!(present, set_present, 0);
/// flag_rw!(e, set_e, extended, set_extended, 1);
///
/// // It is okay to define different functions for a single bit:
/// flag_rw!(a, set_a, 2);
/// flag_rw!(b, set_b, 2);
/// }
///
/// fn main() {
/// let mut something = Something(0);
///
/// if (something.present()) { /* ... */ }
/// if (something.e()) { /* ... */ }
///
/// // These do the same thing:
/// something.set_e(true);
/// something.set_extended(true);
/// }
/// ```
#[macro_export]
macro_rules! flag_rw {
($name:ident, $set_name:ident, $pos:expr) => (
flag_ro!($name, $pos);
#[inline="always"]
pub fn $set_name(&mut self, b: bool) {
let x = b as i64;
self.0 ^= (-x ^ self.0) & (1 << $pos);
}
);
($name:ident, $set_name:ident,
$full_name:ident, $full_set_name:ident,
$pos:expr) => (
flag_rw!($name, $set_name, $pos);
flag_rw!($full_name, $full_set_name, $pos);
);
}
#[cfg(test)]
mod test;