| 1 |
# Fixed-Size, Heap-Allocated, Bit Slice |
| 2 |
|
| 3 |
`BitBox` is a heap-allocated [`BitSlice`] region. It is a distinct type because |
| 4 |
the implementation of bit-slice pointers means that `Box<BitSlice>` cannot |
| 5 |
exist. It can be created by cloning a bit-slice into the heap, or by freezing |
| 6 |
the allocation of a [`BitVec`] |
| 7 |
|
| 8 |
## Original |
| 9 |
|
| 10 |
[`Box<[T]>`](alloc::boxed::Box) |
| 11 |
|
| 12 |
## API Differences |
| 13 |
|
| 14 |
As with `BitSlice`, this takes a pair of [`BitOrder`] and [`BitStore`] type |
| 15 |
parameters to govern the buffer’s memory representation. Because `BitSlice` is |
| 16 |
unsized, `BitBox` has almost none of the `Box` API, and is difficult to use |
| 17 |
directly. |
| 18 |
|
| 19 |
## Behavior |
| 20 |
|
| 21 |
`BitBox`, like `&BitSlice`, is an opaque pointer to a bit-addressed slice |
| 22 |
region. Unlike `&BitSlice`, it uses the allocator to guarantee that it is the |
| 23 |
sole accessor to the referent buffer, and is able to use that uniqueness |
| 24 |
guarantee to specialize some `BitSlice` behavior to be faster or more efficient. |
| 25 |
|
| 26 |
## Safety |
| 27 |
|
| 28 |
`BitBox` is, essentially, a `NonNull<BitSlice<T, O>>` pointer. The internal |
| 29 |
value is opaque and cannot be inspected or modified by user code. |
| 30 |
|
| 31 |
If you attempt to do so, your program becomes inconsistent. You will likely |
| 32 |
break the allocator’s internal state and cause a crash. No guarantees of crash |
| 33 |
*or* recovery are provided. Do not inspect or modify the `BitBox` handle value. |
| 34 |
|
| 35 |
## Construction |
| 36 |
|
| 37 |
The simplest way to construct a `BitBox` is by using the [`bitbox!`] macro. You |
| 38 |
can also explicitly clone a `BitSlice` with [`BitBox::from_bitslice`], or freeze |
| 39 |
a `BitVec` with [`BitVec::into_boxed_bitslice`]. |
| 40 |
|
| 41 |
## Examples |
| 42 |
|
| 43 |
```rust |
| 44 |
use bitvec::prelude::*; |
| 45 |
|
| 46 |
let a = BitBox::from_bitslice(bits![1, 0, 1, 1, 0]); |
| 47 |
let b = bitbox![0, 1, 0, 0, 1]; |
| 48 |
|
| 49 |
let b_raw: *mut BitSlice = BitBox::into_raw(b); |
| 50 |
let b_reformed = unsafe { BitBox::from_raw(b_raw) }; |
| 51 |
``` |
| 52 |
|
| 53 |
[`BitBox::from_bitslice`]: self::BitBox::from_bitslice |
| 54 |
[`BitOrder`]: crate::order::BitOrder |
| 55 |
[`BitSlice`]: crate::slice::BitSlice |
| 56 |
[`BitStore`]: crate::store::BitStore |
| 57 |
[`BitVec`]: crate::vec::BitVec |
| 58 |
[`BitVec::into_boxed_bitslice`]: crate::vec::BitVec::into_boxed_bitslice |
| 59 |
[`bitbox!`]: macro@crate::bitbox |