Debian Codegraph @

debian/rust-vendor/bitvec/doc/array/BitArray.md

raw
1 # Bit-Precision Array Immediate
2
3 This type is a wrapper over the [array fundamental][0] `[T; N]` that views its
4 contents as a [`BitSlice`] region. As an array, it can be held directly by value
5 and does not require an indirection such as the `&BitSlice` reference.
6
7 ## Original
8
9 [`[T; N]`](https://doc.rust-lang.org/std/primitive.array.html)
10
11 ## Usage
12
13 `BitArray` is a Rust analogue of the C++ [`std::bitset<N>`] container. However,
14 restrictions in the Rust type system do not allow specifying exact bit lengths
15 in the array type. Instead, it must specify a storage array that can contain all
16 the bits you want.
17
18 Because `BitArray` is a plain-old-data object, its fields are public and it has
19 no restrictions on its interior value. You can freely access the interior
20 storage and move data in or out of the `BitArray` type with no cost.
21
22 As a convenience, the [`BitArr!`] type-constructor macro can produce correct
23 type definitions from an exact bit count and your memory-layout type parameters.
24 Values of that type can then be built from the [`bitarr!`] *value*-constructor
25 macro:
26
27 ```rust
28 use bitvec::prelude::*;
29
30 type Example = BitArr!(for 43, in u32, Msb0);
31 let example: Example = bitarr!(u32, Msb0; 1; 33);
32
33 struct HasBitfield {
34 inner: Example,
35 }
36
37 let ex2 = HasBitfield {
38 inner: BitArray::new([1, 2]),
39 };
40 ```
41
42 Note that the actual type of the `Example` alias is `BitArray<[u32; 2], Msb0>`,
43 as that is `ceil(32, 43)`, so the `bitarr!` macro can accept any number of bits
44 in `33 .. 65` and will produce a value of the correct type.
45
46 ## Type Parameters
47
48 `BitArray` differs from the other data structures in the crate in that it does
49 not take a `T: BitStore` parameter, but rather takes `A: BitViewSized`. That
50 trait is implemented by all `T: BitStore` scalars and all `[T; N]` arrays of
51 them, and provides the logic to translate the aggregate storage into the memory
52 sequence that the crate expects.
53
54 As with all `BitSlice` regions, the `O: BitOrder` parameter specifies the
55 ordering of bits within a single `A::Store` element.
56
57 ## Future API Changes
58
59 Exact bit lengths cannot be encoded into the `BitArray` type until the
60 const-generics system in the compiler can allow type-level computation on type
61 integers. When this stabilizes, `bitvec` will issue a major upgrade that
62 replaces the `BitArray<A, O>` definition with `BitArray<T, O, const N: usize>`
63 and match the C++ `std::bitset<N>` definition.
64
65 ## Large Bit-Arrays
66
67 As with ordinary arrays, large arrays can be expensive to move by value, and
68 should generally be preferred to have static locations such as actual `static`
69 bindings, a long lifetime in a low stack frame, or a heap allocation. While you
70 certainly can `Box<[BitArray<A, O>]>` directly, you may instead prefer the
71 [`BitBox`] or [`BitVec`] heap-allocated regions. These offer the same storage
72 behavior and are better optimized than `Box<BitArray>` for working with the
73 contained `BitSlice` region.
74
75 ## Examples
76
77 ```rust
78 use bitvec::prelude::*;
79
80 const WELL_KNOWN: BitArr!(for 16, in u8, Lsb0) = BitArray::<[u8; 2], Lsb0> {
81 data: *b"bv",
82 ..BitArray::ZERO
83 };
84
85 struct HasBitfields {
86 inner: BitArr!(for 50, in u8, Lsb0),
87 }
88
89 impl HasBitfields {
90 fn new() -> Self {
91 Self {
92 inner: bitarr!(u8, Lsb0; 0; 50),
93 }
94 }
95
96 fn some_field(&self) -> &BitSlice<u8, Lsb0> {
97 &self.inner[2 .. 52]
98 }
99 }
100 ```
101
102 [0]: https://doc.rust-lang.org/std/primitive.array.html
103 [`BitArr!`]: macro@crate::BitArr
104 [`BitBox`]: crate::boxed::BitBox
105 [`BitSlice`]: crate::slice::BitSlice
106 [`BitVec`]: crate::vec::BitVec
107 [`bitarr!`]: macro@crate::bitarr
108 [`std::bitset<N>`]: https://en.cppreference.com/w/cpp/utility/bitset