Macro bitvec::bitarr [−][src]
Constructs a new BitArray from a bit-pattern description.
This macro takes a superset of the vec! argument syntax: it may be invoked
with either a sequence of bit expressions, or a single bit expression and a
repetition counter. Additionally, you may provide the names of a BitOrder
and a BitStore implementor as the BitArray’s type arguments.
Argument Rules
Bit expressions must be integer literals. Ambiguity restrictions in the macro
syntax forbid the use of identifiers to existing variables, even const values.
These are converted to bool through the expression $val != 0. Any non-zero
enteger becomes true, and 0 becomes false.
You may use any name or path to a BitOrder implementation. However, the
identifier tokens Lsb0, Msb0, and LocalBits are matched directly and
specialized to have compile-time constructions, whereäs any other name or path
will not be known to the macro, and will execute at runtime.
The BitStore argument must be the name of an unsigned integer
fundamental, an atomic, or a Cell<> wrapper of that unsigned integer. These
are matched by token, not by type, and no other identifier is accepted. Using
any other token will cause the macro to fail.
Type Name Construction
In addition to the value construction, this macro can also construct the name of
a BitArray type that contains a requested number of bits. This is useful
for typing a binding before constructing a value for it.
The argument syntax for this is a for $BITS, optionally followed by , $TYPE
or , $ORDER, $TYPE. $BITS may be any constant-evaluable usize expression.
$ORDER and TYPE may be any valid names or paths for the appropriate trait
implementations.
Examples
use bitvec::prelude::*; use core::cell::Cell; radium::if_atomic! { if atomic(32) { use core::sync::atomic::AtomicU32; } } let a: BitArray = bitarr![0, 1, 0, 1, 2]; assert_eq!(a.count_ones(), 3); let b: BitArray = bitarr![2; 5]; assert!(b.all()); assert!(b.len() >= 5); let c = bitarr![Lsb0, Cell<u16>; 0, 1, 0, 0, 1]; let d = bitarr![Msb0, AtomicU32; 0, 0, 1, 0, 1]; let e: bitarr!(for 20, in LocalBits, u8) = bitarr![LocalBits, u8; 0; 20];