Macro bitflag_array

Source
macro_rules! bitflag_array {
    {
        $(#[$inner:ident $($args:tt)*])*
        pub struct $flags:ident : $size:expr;
        pub struct $flag:ident;

        $(
            $(#[$var_inner:ident $($var_args:tt)*])*
            const $var:ident = $octet:expr, $mask:expr;
        )+
    } => { ... };
}
Expand description

Implements an arbitrary-length bitfield. The implementation and interface is similar to and derived from bitflags.

Instead of implementing the bitfield over an integral type, this implements it on an array of bytes. Each flag is defined using the byte and the mask within that byte. It does not support masks across bytes.

ยงExample

See [ChannelClassification] and [event::command::CommandFlags] for examples in this crate.

Basic usage is similar to bitflags:

bitflag_array! {
    #[derive(Clone)]
    pub struct Flags : 3; // Bit field over a [u8; 3]
    pub struct Flag;      // Name the internal struct

    const ALPHA = 0, 0x01; // First byte, first bit
    const BETA = 0, 0x02;
    // ...
    const THETA = 1, 0x01; // Second byte, first bit
    // ...
    const OMEGA = 2, 0x80; // Third byte, last bit
}

A subset of the bitflags interface is implemented, including bitwise OR operations:

let mut letters = Flags::ALPHA | Flags::BETA;
letters |= Flags::THETA;
assert_eq!(letters.bits(), [0x03, 0x01, 0x00]);
assert_eq!(letters.is_empty(), false);
assert_eq!(letters.is_set(Flags::OMEGA), false);
assert!(letters.contains(Flags::BETA | Flags::THETA));