| 1 |
# Bit-Precision Dynamic Array |
| 2 |
|
| 3 |
This is an analogue to `Vec<bool>` that stores its data using a compaction |
| 4 |
scheme to ensure that each `bool` takes exactly one bit of memory. It is similar |
| 5 |
to the C++ type [`std::vector<bool>`], but uses `bitvec`’s type parameter system |
| 6 |
to provide more detailed control over the in-memory representation. |
| 7 |
|
| 8 |
This is *always* a heap allocation. If you know your sizes at compile-time, you |
| 9 |
may prefer to use [`BitArray`] instead, which is able to store its data as an |
| 10 |
immediate value rather than through an indirection. |
| 11 |
|
| 12 |
## Documentation Practices |
| 13 |
|
| 14 |
`BitVec` exactly replicates the API of the standard-library `Vec` type, |
| 15 |
including inherent methods, trait implementations, and relationships with the |
| 16 |
[`BitSlice`] slice analogue. |
| 17 |
|
| 18 |
Items that are either direct ports, or renamed variants, of standard-library |
| 19 |
APIs will have a `## Original` section that links to their standard-library |
| 20 |
documentation. Items that map to standard-library APIs but have a different API |
| 21 |
signature will also have an `## API Differences` section that describes what |
| 22 |
the difference is, why it exists, and how to transform your code to fit it. For |
| 23 |
example: |
| 24 |
|
| 25 |
## Original |
| 26 |
|
| 27 |
[`Vec<T>`](alloc::vec::Vec) |
| 28 |
|
| 29 |
## API Differences |
| 30 |
|
| 31 |
As with all `bitvec` data structures, this takes two type parameters `<T, O>` |
| 32 |
that govern the bit-vector’s storage representation in the underlying memory, |
| 33 |
and does *not* take a type parameter to govern what data type it stores (always |
| 34 |
`bool`) |
| 35 |
|
| 36 |
## Suggested Uses |
| 37 |
|
| 38 |
`BitVec` is able to act as a compacted `usize => bool` dictionary, and is useful |
| 39 |
for holding large collections of truthiness. For instance, you might replace a |
| 40 |
`Vec<Option<T>>` with a `(BitVec, Vec<MaybeUninit<T>>`) to cut down on the |
| 41 |
resident size of the discriminant. |
| 42 |
|
| 43 |
Through the [`BitField`] trait, `BitVec` is also able to act as a transport |
| 44 |
buffer for data that can be marshalled as integers. Serializing data to a |
| 45 |
narrower compacted form, or deserializing data *from* that form, can be easily |
| 46 |
accomplished by viewing subsets of a bit-vector and storing integers into, or |
| 47 |
loading integers out of, that subset. As an example, transporting four ten-bit |
| 48 |
integers can be done in five bytes instead of eight like so: |
| 49 |
|
| 50 |
```rust |
| 51 |
use bitvec::prelude::*; |
| 52 |
|
| 53 |
let mut bv = bitvec![u8, Msb0; 0; 40]; |
| 54 |
bv[0 .. 10].store::<u16>(0x3A8); |
| 55 |
bv[10 .. 20].store::<u16>(0x2F9); |
| 56 |
bv[20 .. 30].store::<u16>(0x154); |
| 57 |
bv[30 .. 40].store::<u16>(0x06D); |
| 58 |
``` |
| 59 |
|
| 60 |
If you wish to use bit-field memory representations as `struct` fields rather |
| 61 |
than a transport buffer, consider `BitArray` instead: that type keeps its data |
| 62 |
as an immediate, and is more likely to act like a C struct with bitfields. |
| 63 |
|
| 64 |
## Examples |
| 65 |
|
| 66 |
`BitVec` has exactly the same API as `Vec<bool>`, and even extends it with some |
| 67 |
of `Vec<T>`’s behaviors. As a brief tour: |
| 68 |
|
| 69 |
### Push and Pop |
| 70 |
|
| 71 |
```rust |
| 72 |
use bitvec::prelude::*; |
| 73 |
|
| 74 |
let mut bv: BitVec = BitVec::new(); |
| 75 |
bv.push(false); |
| 76 |
bv.push(true); |
| 77 |
|
| 78 |
assert_eq!(bv.len(), 2); |
| 79 |
assert_eq!(bv[0], false); |
| 80 |
|
| 81 |
assert_eq!(bv.pop(), Some(true)); |
| 82 |
assert_eq!(bv.len(), 1); |
| 83 |
``` |
| 84 |
|
| 85 |
### Writing Into a Bit-Vector |
| 86 |
|
| 87 |
The only `Vec<bool>` API that `BitVec` does *not* implement is `IndexMut`, |
| 88 |
because that is not yet possible. Instead, [`.get_mut()`] can produce a proxy |
| 89 |
reference, or [`.set()`] can take an index and a value to write. |
| 90 |
|
| 91 |
```rust |
| 92 |
use bitvec::prelude::*; |
| 93 |
|
| 94 |
let mut bv: BitVec = BitVec::new(); |
| 95 |
bv.push(false); |
| 96 |
|
| 97 |
*bv.get_mut(0).unwrap() = true; |
| 98 |
assert!(bv[0]); |
| 99 |
bv.set(0, false); |
| 100 |
assert!(!bv[0]); |
| 101 |
``` |
| 102 |
|
| 103 |
### Macro Construction |
| 104 |
|
| 105 |
Like `Vec`, `BitVec` also has a macro constructor: [`bitvec!`] takes a sequence |
| 106 |
of bit expressions and encodes them at compile-time into a suitable buffer. At |
| 107 |
run-time, this buffer is copied into the heap as a `BitVec` with no extra cost |
| 108 |
beyond the allocation. |
| 109 |
|
| 110 |
```rust |
| 111 |
use bitvec::prelude::*; |
| 112 |
|
| 113 |
let bv = bitvec![0; 10]; |
| 114 |
let bv = bitvec![0, 1, 0, 0, 1]; |
| 115 |
let bv = bitvec![u16, Msb0; 1; 20]; |
| 116 |
``` |
| 117 |
|
| 118 |
### Borrowing as `BitSlice` |
| 119 |
|
| 120 |
`BitVec` lends its buffer as a `BitSlice`, so you can freely give permission to |
| 121 |
view or modify the contained data without affecting the allocation: |
| 122 |
|
| 123 |
```rust |
| 124 |
use bitvec::prelude::*; |
| 125 |
|
| 126 |
fn read_bitslice(bits: &BitSlice) { |
| 127 |
// … |
| 128 |
} |
| 129 |
|
| 130 |
let bv = bitvec![0; 30]; |
| 131 |
read_bitslice(&bv); |
| 132 |
let bs: &BitSlice = &bv; |
| 133 |
``` |
| 134 |
|
| 135 |
## Other Notes |
| 136 |
|
| 137 |
The default type parameters are `<usize, Lsb0>`. This is the most performant |
| 138 |
pair when operating on memory, but likely does not match your needs if you are |
| 139 |
using `BitVec` to represent a transport buffer. See [the user guide][book] for |
| 140 |
more details on how the type parameters govern memory representation. |
| 141 |
|
| 142 |
Applications, or single-purpose libraries, built atop `bitvec` will likely want |
| 143 |
to create a `type` alias with specific type parameters for their usage. `bitvec` |
| 144 |
is fully generic over the ordering/storage types, but this generality is rarely |
| 145 |
useful for client crates to propagate. `<usize, Lsb0>` is fastest; `<u8, Msb0>` |
| 146 |
matches what most debugger views of memory will print, and the rest are |
| 147 |
documented in the guide. |
| 148 |
|
| 149 |
## Safety |
| 150 |
|
| 151 |
Unlike the other data structures in this crate, `BitVec` is uniquely able to |
| 152 |
hold uninitialized memory and produce pointers into it. As described in the |
| 153 |
[`BitAccess`] documentation, this crate is categorically unable to operate on |
| 154 |
uninitialized memory in any way. In particular, you may not allocate a buffer |
| 155 |
using [`::with_capacity()`], then use [`.as_mut_bitptr()`] to create a pointer |
| 156 |
used to write into the uninitialized buffer. |
| 157 |
|
| 158 |
You must always initialize the buffer contents of a `BitVec` before attempting |
| 159 |
to view its contents. You can accomplish this through safe APIs such as |
| 160 |
`.push()`, `.extend()`, or `.reserve()`. These are all guaranteed to safely |
| 161 |
initialize the memory elements underlying the `BitVec` buffer without incurring |
| 162 |
undefined behavior in their operation. |
| 163 |
|
| 164 |
[book]: https://bitvecto-rs.github.io/bitvec/type-parameters.html |
| 165 |
[`BitAccess`]: crate::access::BitAccess |
| 166 |
[`BitArray`]: crate::array::BitArray |
| 167 |
[`BitField`]: crate::field::BitField |
| 168 |
[`BitSlice`]: crate::slice::BitSlice |
| 169 |
[`bitvec!`]: macro@crate::bitvec |
| 170 |
[`std::vector<bool>`]: https://en.cppreference.com/w/cpp/container/vector_bool |
| 171 |
[`.as_mut_bitptr()`]: crate::slice::BitSlice::as_mut_bitptr |
| 172 |
[`.get_mut()`]: crate::slice::BitSlice::get_mut |
| 173 |
[`.set()`]: crate::slice::BitSlice::set |
| 174 |
[`::with_capacity()`]: Self::with_capacity |