Debian Codegraph @

debian/rust-vendor/bitvec/doc/serdes.md

raw
1 # Support for `serde`
2
3 `bitvec` structures are able to de/serialize their contents using `serde`.
4 Because `bitvec` is itself designed to be a transport buffer and have
5 memory-efficient storage properties, the implementations are somewhat strange
6 and not necessarily suitable for transport across heterogenous hosts.
7
8 `bitvec` always serializes its underlying memory storage as a sequence of raw
9 memory. It also includes the necessary metadata to prevent deserialization into
10 an incorrect type.
11
12 ## Serialization
13
14 All data types serialize through `BitSlice`. While in version 0, `BitArray` had
15 its own serialization logic; this is no longer the case.
16
17 `BitSlice` serializes the bit-width of `T::Mem` and the `any::type_name` of `O`.
18 This may cause deserialization failures in the future if `any::type_name`
19 changes its behavior, but as it is run while compiling `bitvec` itself, clients
20 that rename `bitvec` when bringing it into their namespace should not be
21 affected.
22
23 Note that because `LocalBits` is a reëxport rather than a type in its own right,
24 it always serializes as the real type to which it forwards. This prevents
25 accidental mismatch when transporting between machines with different
26 destinations for this alias.
27
28 The next items serialized are the index of the starting bit within the starting
29 element, and the total number of bits included in the bit-slice. After these,
30 the data buffer is serialized directly.
31
32 Each element in the data buffer is loaded, has any dead bits cleared to `0`, and
33 is then serialized directly into the collector. In particular, no
34 byte-reördering for transport is performed here, so integers wider than a byte
35 must use a de/serialization format that handles this if, for example, byte-order
36 endian transforms are required.
37
38 ## Deserialization
39
40 Serde only permits no-copy slice deserialization on `&'a [u8]` slices, so
41 `bitvec` in turn can only deserialize into `&'a BitSlice<u8, O>` bit-slices. It
42 can deserialize into `BitArray`s of any type, relying on the serialization layer
43 to reverse any byte-order transforms.
44
45 `&BitSlice` will only deserialize if the transport format contains the bytes
46 directly in it. If you do do not have an allocator, you should always transport
47 `BitArray`. If you do have an allocator, and are serializing `BitBox` or
48 `BitVec`, and expect to deserialize into a `BitArray`, then you will need to use
49 `.force_align()` and ensure that you fully occupy the buffer being transported.
50
51 `BitArray` will fail to deserialize if the data stream does not have a head-bit
52 index of `0` and a length that exactly matches its own. This limitation is a
53 consequence of the implementation, and likely will not be relaxed. `BitBox` and
54 `BitVec`, however, are able to deserialize any bit-sequence without issue.
55
56 ## Warnings
57
58 `usize` *does* de/serialize! However, because it does not have a fixed width,
59 `bitvec` always serializes it as the local fixed-width equivalent, and places
60 the word width into the serialization stream. This will prevent roundtripping a
61 `BitArray<[usize; N]>` between hosts with different `usize` widths, even though
62 the types in the source code line up.
63
64 This behavior was not present in version 0, and users were able to write
65 programs that incorrectly handled de/serialization when used on heterogenous
66 systems.
67
68 In addition, remember that `bitvec` serializes its data buffer *directly* as
69 2’s-complement integers. You must ensure that your transport layer can handle
70 them correctly. As an example, JSON is not required to transport 64-bit integers
71 with perfect fidelity. `bitvec` has no way to detect inadequacy in the transport
72 layer, and will not prevent you from using a serialization format that damages
73 or alters the bit-stream you send through it.
74
75 ## Transport Format
76
77 All `bitvec` data structures produce the same basic format: a structure (named
78 `BitSeq` for `BitSlice`, `BitBox`, and `BitVec`, or `BitArr` for `BitArray`)
79 with four fields:
80
81 1. `order` is a string naming the `O: BitOrder` parameter. Because it uses
82 [`any::type_name`][0], its value cannot be *guaranteed* to be stable. You
83 cannot assume that transport buffers are compatible across versions of the
84 compiler used to create applications exchanging them.
85 1. `head` is a `BitIdx` structure containing two fields:
86 1. `width` is a single byte containing `8`, `16`, `32`, or `64`, describing
87 the bit-width of each element in the data buffer. `bitvec` structures will
88 refuse to deserialize if the serialized bit-width does not match their
89 `T::Mem` type.
90 1. `index` is a single byte containing the head-bit that begins the live
91 `BitSlice` region. `BitArray` will refuse to deserialize if this is not
92 zero.
93 1. `bits` is the number of live bits in the region, as a `u64`. `BitArray` fails
94 to deserialize if it does not match [`mem::bits_of::<A>()`][1].
95 1. `data` is the actual data buffer containing the bits being transported. For
96 `BitSeq` serialization, it is a sequence; for `BitArr`, it is a tuple. This
97 may affect the transport representation, and so the two are not guaranteed to
98 be interchangeable over all transports.
99
100 As known examples, JSON does not have a fixed-size array type, so the contents
101 of all `bitvec` structures have identical rendering, while the [`bincode`] crate
102 does distinguish between run-length-encoded slices and non-length-encoded
103 arrays.
104
105 ## Implementation Details
106
107 `bitvec` supports deserializing from both of Serde’s models for aggregate
108 structures: dictionaries and sequences. It always serializes as a dictionary,
109 but if your serialization layer does not want to include field names, you may
110 emit only the values *in the temporal order that they are received* and `bitvec`
111 will correctly deserialize from them.
112
113 `BitSlice` (and `BitBox` and `BitVec`, which forward to it) transports its data
114 buffer using Serde’s *sequence* model. `BitArray` uses Serde’s *tuple* model
115 instead. These models might not be interchangeable in certain transport formats!
116 You should always deserialize into the same container type that produced a
117 serialized stream.
118
119 [0]: core::any::type_name
120 [1]: crate::mem::bits_of
121 [`bincode`]: https://docs.rs/bincode/latest/bincode