Debian Codegraph @

debian/rust-vendor/bitvec/doc/field/BitField.md

raw
1 # C-Style Bit-Field Access
2
3 This trait describes data transfer between a [`BitSlice`] region and an ordinary
4 integer. It is not intended for use by any other types than the data structures
5 in this crate.
6
7 The methods in this trait always operate on the `bitslice.len()` least
8 significant bits of an integer, and ignore any remaining high bits. When
9 loading, any excess high bits not copied out of a bit-slice are cleared to zero.
10
11 ## Usage
12
13 The trait methods all panic if called on a bit-slice that is wider than the
14 integer type being transferred. As such, the first step is generally to subslice
15 a larger data structure into exactly the region used for storage, with
16 `bits[start .. end]`. Then, call the desired method on the narrowed bit-slice.
17
18 ## Target-Specific Behavior
19
20 If you do not care about the details of the memory layout of stored values, you
21 can use the [`.load()`] and [`.store()`] unadorned methods. These each forward
22 to their `_le` variant on little-endian targets, and their `_be` variant on
23 big-endian. These will provide a reasonable default behavior, but do not
24 guarantee a stable memory layout, and their buffers are not suitable for
25 de/serialization.
26
27 If you require a stable memory layout, you will need to choose a `BitSlice`
28 with a fixed `O: BitOrder` type parameter (not `LocalBits`), and use a fixed
29 method suffix (`_le` or `_be`). You should *probably* also use `u8` as your
30 `T: BitStore` parameter, in order to avoid any byte-ordering issues. `bitvec`
31 never interferes with processor concepts of wide-integer layout, and always
32 relies on the target machine’s behavior for this work.
33
34 ## Element- and Bit- Ordering Combinations
35
36 Remember: the `_le` and `_be` method suffixes are completely independent of the
37 `Lsb0` and `Msb0` types! `_le` and `_be` refer to the order in which successive
38 memory elements are considered to gain numerical significance, while `BitOrder`
39 refers only to the order of successive bits in *one* memory element.
40
41 The `BitField` and `BitOrder` traits are ***not*** related.
42
43 When a load or store operation is contained in only one memory element, then the
44 `_le` and `_be` methods have the same behavior: they exchange an integer value
45 with the segment of the element that its governing `BitSlice` considers live.
46 Only when a `BitSlice` covers multiple elements does the distinction come into
47 play.
48
49 The `_le` methods consider numerical significance to start low and increase with
50 increasing memory address, while the `_be` methods consider numerical
51 significance to start high and *decrease* with increasing memory address. This
52 distinction affects the order in which memory elements are used to load or store
53 segments of the exchanged integer value.
54
55 Each trait method has detailed visual diagrams in its documentation.
56 Additionally, each *implementation*’s documentation has diagrams that show what
57 the governed bit-sections of elements are! Be sure to check each, or to run the
58 demonstration with `cargo run --example bitfield`.
59
60 ## Bitfield Value Types
61
62 When interacting with a bit-slice as a C-style bitfield, it can *only* store the
63 signed or unsigned integer types. No other type is permitted, as the
64 implementation relies on the 2’s-complement significance behavior of processor
65 integers. Record types and floating-point numbers do not have this property, and
66 thus have no sensible default protocol for truncation and un/marshalling that
67 `bitvec` can use.
68
69 If you have such a protocol, you may implement it yourself by providing a
70 de/serialization transform between your type and the integers. For instance, a
71 numerically-correct protocol to store floating-point numbers in bitfields might
72 look like this:
73
74 ```rust
75 use bitvec::mem::bits_of;
76 use funty::Floating;
77
78 fn to_storage<F>(num: F, width: usize) -> F::Raw
79 where F: Floating {
80 num.to_bits() >> (bits_of::<F>() - width)
81 }
82
83 fn from_storage<F>(val: F::Raw, width: usize) -> F
84 where F: Floating {
85 F::from_bits(val << (bits_of::<F>() - width))
86 }
87 ```
88
89 This implements truncation in the least-significant bits, where floating-point
90 numbers store disposable bits in the mantissa, rather than in the
91 most-significant bits which contain the sign, exponent, and most significant
92 portion of the mantissa.
93
94 [`BitSlice`]: crate::slice::BitSlice
95 [`.load()`]: Self::load
96 [`.store()`]: Self::store