| 1 |
# Bit-Array De/Serialization |
| 2 |
|
| 3 |
The Serde model distinguishes between *sequences*, which have a dynamic length |
| 4 |
which must always be transported with the data, and *tuples*, which have a fixed |
| 5 |
length known at compile-time that does not require transport. |
| 6 |
|
| 7 |
Serde handles arrays using its tuple model, not its sequence model, which means |
| 8 |
that `BitArray` cannot use the `BitSlice` Serde implementations (which must use |
| 9 |
the sequence model in order to handle `&[u8]` and `Vec<T>` de/serialization). |
| 10 |
Instead, `BitArray` has a standalone implementation using the tuple model so |
| 11 |
that its wrapped array can be transported (nearly) as if it were unwrapped. |
| 12 |
|
| 13 |
For consistency, `BitArray` has the same wire format that `BitSlice` does; the |
| 14 |
only distinction is that the data buffer is a tuple rather than a sequence. |
| 15 |
|
| 16 |
Additionally, Serde’s support for old versions of Rust means that it only |
| 17 |
implements its traits on arrays `[T; 0 ..= 32]`. Since `bitvec` has a much |
| 18 |
higher MSRV that includes support for the const-generic `[T; N]` family, it |
| 19 |
reïmplements Serde’s behavior on a custom `Array<T, N>` type in order to ensure |
| 20 |
that all possible `BitArray` storage types are transportable. Note, however, |
| 21 |
that *because* each `[T; N]` combination is a new implementation, de/serializing |
| 22 |
`BitArray`s directly is a great way to pessimize codegen. |
| 23 |
|
| 24 |
While it would be nice if `rustc` or LLVM could collapse the implementations and |
| 25 |
restore `N` as a run-time argument rather than a compile-time constant, neither |
| 26 |
`bitvec` nor Serde attempt to promise this in any way. Use at your discretion. |