| 1 |
# Bit-Array Value Constructor |
| 2 |
|
| 3 |
This macro provides a bit-initializer syntax for [`BitArray`] values. It takes a |
| 4 |
superset of the [`vec!`] arguments, and is capable of producing bit-arrays in |
| 5 |
`const` contexts (for known type parameters). |
| 6 |
|
| 7 |
Like `vec!`, it can accept a sequence of comma-separated bit values, or a |
| 8 |
semicolon-separated pair of a bit value and a repetition counter. Bit values may |
| 9 |
be any integer or name of a `const` integer, but *should* only be `0` or `1`. |
| 10 |
|
| 11 |
## Argument Syntax |
| 12 |
|
| 13 |
It accepts zero, one, or three prefix arguments: |
| 14 |
|
| 15 |
- `const`: If the first argument to the macro is the keyword `const`, separated |
| 16 |
from remaining arguments by a space, then the macro expands to a |
| 17 |
`const`-expression that can be used in any appropriate context (initializing |
| 18 |
a `static`, a `const`, or passed to a `const fn`). This only works when the |
| 19 |
bit-ordering argument is either implicit, or one of the three tokens that |
| 20 |
`bitvec` can recognize. |
| 21 |
- `$order ,`: When this is one of the three literal tokens `LocalBits`, `Lsb0`, |
| 22 |
or `Msb0`, then the macro is able to compute the encoded bit-array contents at |
| 23 |
compile time, including in `const` contexts. When it is anything else, the |
| 24 |
encoding must take place at runtime. The name or path chosen must be in scope |
| 25 |
at the macro invocation site. |
| 26 |
|
| 27 |
When not provided, this defaults to `Lsb0`. |
| 28 |
- `$store ;`: This must be one of `uTYPE`, `Cell<uTYPE>`, `AtomicUTYPE`, or |
| 29 |
`RadiumUTYPE` where `TYPE` is one of `8`, `16`, `32`, `64`, or `size`. The |
| 30 |
macro recognizes this token textually, and does not have access to the type |
| 31 |
system resolver, so it will not accept aliases or qualified paths. |
| 32 |
|
| 33 |
When not provided, this defaults to `usize`. |
| 34 |
|
| 35 |
The `const` argument can be present or absent independently of the |
| 36 |
type-parameter pair. The pair must be either both absent or both present |
| 37 |
together. |
| 38 |
|
| 39 |
> Previous versions of `bitvec` supported `$order`-only arguments. This has been |
| 40 |
> removed for clarity of use and ease of implementation. |
| 41 |
|
| 42 |
## Examples |
| 43 |
|
| 44 |
```rust |
| 45 |
use bitvec::prelude::*; |
| 46 |
use core::{cell::Cell, mem}; |
| 47 |
use radium::types::*; |
| 48 |
|
| 49 |
let a: BitArray = bitarr![0, 1, 0, 0, 1]; |
| 50 |
|
| 51 |
let b: BitArray = bitarr![1; 5]; |
| 52 |
assert_eq!(b.len(), mem::size_of::<usize>() * 8); |
| 53 |
|
| 54 |
let c = bitarr![u16, Lsb0; 0, 1, 0, 0, 1]; |
| 55 |
let d = bitarr![Cell<u16>, Msb0; 1; 10]; |
| 56 |
const E: BitArray<[u32; 1], LocalBits> = bitarr![u32, LocalBits; 1; 15]; |
| 57 |
let f = bitarr![RadiumU32, Msb0; 1; 20]; |
| 58 |
``` |
| 59 |
|
| 60 |
[`BitArray`]: crate::array::BitArray |
| 61 |
[`vec!`]: macro@alloc::vec |