| 1 |
# Bit-Slice Region Constructor |
| 2 |
|
| 3 |
This macro provides a bit-initializer syntax for [`BitSlice`] reference values. |
| 4 |
It takes a superset of the [`vec!`] arguments, and is capable of producing |
| 5 |
bit-slices in `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 two modifier prefixes, zero or two type parameters, and the bit |
| 14 |
expressions described above. |
| 15 |
|
| 16 |
The modifier prefixes are separated from the remaining arguments by clearspace. |
| 17 |
|
| 18 |
- `static`: If the first argument is the keyword `static`, then this produces a |
| 19 |
`&'static BitSlice` reference bound into a (hidden, unnameable) |
| 20 |
`static BitArray` item. If not, then it produces a stack temporary that the |
| 21 |
Rust compiler automatically extends to have the lifetime of the returned |
| 22 |
reference. Note that non-`static` invocations rely on the compiler’s escape |
| 23 |
analysis, and you should typically not try to move them up the call stack. |
| 24 |
- `mut`: If the first argument is the keyword `mut`, then this produces a `&mut` |
| 25 |
writable `BitSlice`. |
| 26 |
- `static mut`: These can be combined to create a `&'static mut BitSlice`. It is |
| 27 |
always safe to use this reference, because the `static mut BitArray` it |
| 28 |
creates is concealed and unreachable by any other codepath, and so the |
| 29 |
produced reference is always the sole handle that can reach it. |
| 30 |
|
| 31 |
The next possible arguments are a pair of `BitOrder`/`BitStore` type parameters. |
| 32 |
|
| 33 |
- `$order ,`: When this is one of the three literal tokens `LocalBits`, `Lsb0`, |
| 34 |
or `Msb0`, then the macro is able to compute the encoded bit-array contents at |
| 35 |
compile time, including in `const` contexts. When it is anything else, the |
| 36 |
encoding must take place at runtime. The name or path chosen must be in scope |
| 37 |
at the macro invocation site. |
| 38 |
|
| 39 |
When not provided, this defaults to `Lsb0`. |
| 40 |
- `$store ;`: This must be one of `uTYPE`, `Cell<uTYPE>`, `AtomicUTYPE`, or |
| 41 |
`RadiumUTYPE` where `TYPE` is one of `8`, `16`, `32`, `64`, or `size`. The |
| 42 |
macro recognizes this token textually, and does not have access to the type |
| 43 |
system resolver, so it will not accept aliases or qualified paths. |
| 44 |
|
| 45 |
When not provided, this defaults to `usize`. |
| 46 |
|
| 47 |
The `static`/`mut` modifiers may be individually present or absent independently |
| 48 |
of the type-parameter pair. The pair must be either both absent or both present |
| 49 |
together. |
| 50 |
|
| 51 |
> Previous versions of `bitvec` supported $order`-only arguments. This has been |
| 52 |
> removed for clarity of use and ease of implementation. |
| 53 |
|
| 54 |
## Safety |
| 55 |
|
| 56 |
Rust considers all `static mut` bindings to be `unsafe` to use. While `bits!` |
| 57 |
can prevent *some* of this unsafety by preventing direct access to the created |
| 58 |
`static mut` buffer, there are still ways to create multiple names referring to |
| 59 |
the same underlying buffer. |
| 60 |
|
| 61 |
```rust,ignore |
| 62 |
use bitvec::prelude::*; |
| 63 |
|
| 64 |
fn unsound() -> &'static mut BitSlice<usize, Lsb0> { |
| 65 |
unsafe { bits![static mut 0; 64] } |
| 66 |
} |
| 67 |
|
| 68 |
let a = unsound(); |
| 69 |
let b = unsound(); |
| 70 |
``` |
| 71 |
|
| 72 |
The two names `a` and `b` can be used to produce aliasing `&mut [usize]` |
| 73 |
references. |
| 74 |
|
| 75 |
**You must not invoke `bits![static mut …]` in a context where it can be used** |
| 76 |
**to create multiple escaping names**. This, and only this, argument combination |
| 77 |
of the macro produces a value that requires a call-site `unsafe` block to use. |
| 78 |
|
| 79 |
If you do not use this behavior to create multiple names over the same |
| 80 |
underlying buffer, then the macro’s expansion is safe to use, as `bitvec`’s |
| 81 |
existing alias-protection behavior suffices. |
| 82 |
|
| 83 |
## Examples |
| 84 |
|
| 85 |
```rust |
| 86 |
use bitvec::prelude::*; |
| 87 |
use core::cell::Cell; |
| 88 |
use radium::types::*; |
| 89 |
|
| 90 |
let a: &BitSlice = bits![0, 1, 0, 0, 1]; |
| 91 |
|
| 92 |
let b: &BitSlice = bits![1; 5]; |
| 93 |
assert_eq!(b.len(), 5); |
| 94 |
|
| 95 |
let c = bits![u16, Lsb0; 0, 1, 0, 0, 1]; |
| 96 |
let d = bits![static Cell<u16>, Msb0; 1; 10]; |
| 97 |
let e = unsafe { bits![static mut u32, LocalBits; 0; 15] }; |
| 98 |
let f = bits![RadiumU32, Msb0; 1; 20]; |
| 99 |
``` |
| 100 |
|
| 101 |
[`BitSlice`]: crate::slice::BitSlice |
| 102 |
[`vec!`]: macro@alloc::vec |