Debian Codegraph @

debian/rust-vendor/bitvec/doc/order/BitOrder.md

raw
1 # In-Element Bit Ordering
2
3 This trait manages the translation of semantic bit indices into electrical
4 positions within storage elements of a memory region.
5
6 ## Usage
7
8 `bitvec` APIs operate on semantic index counters that exist in an abstract
9 memory space independently of the real memory that underlies them. In order to
10 affect real memory, `bitvec` must translate these indices into real values. The
11 [`at`] function maps abstract index values into their corresponding real
12 positions that can then be used to access memory.
13
14 You will likely never call any of the trait functions yourself. They are used by
15 `bitvec` internals to operate on memory regions; all you need to do is provide
16 an implementation of this trait as a type parameter to `bitvec` data structures.
17
18 ## Safety
19
20 `BitOrder` is unsafe to implement because its translation of index to position
21 cannot be forcibly checked by `bitvec` itself, and an improper implementation
22 will lead to memory unsafety errors and unexpected collisions. The trait has
23 strict requirements for each function. If these are not upheld, then the
24 implementation is considered undefined at the library level and its use may
25 produce incorrect or undefined behavior during compilation.
26
27 You are responsible for running [`verify_for_type`] or [`verify`] in your test
28 suite if you implement `BitOrder`.
29
30 ## Implementation Rules
31
32 Values of this type are never constructed or passed to `bitvec` functions. Your
33 implementation does not need to be zero-sized, but it will never have access to
34 an instance to view its state. It *may* refer to other global state, but per the
35 rules of `at`, that state may not change while any `bitvec` data structures are
36 alive.
37
38 The only function you *need* to provide is `at`. Its requirements are listed in
39 its trait documentation.
40
41 You *may* also choose to provide implementations of `select` and `mask`. These
42 have a default implementation that is correct, but may be unoptimized for your
43 implementation. As such, you may replace them with a better version, but your
44 implementation of these functions must be exactly equal to the default
45 implementation for all possible inputs.
46
47 This requirement is checked by the `verify_for_type` function.
48
49 ## Verification
50
51 The `verify_for_type` function verifies that a `BitOrder` implementation is
52 correct for a single `BitStore` implementor, and the `verify` function runs
53 `verify_for_type` on all unsigned integers that implement `BitStore` on a
54 target. If you run these functions in your test suite, they will provide
55 detailed information if your implementation is incorrect.
56
57 ## Examples
58
59 Implementations are not required to remain contiguous over a register, and may
60 have any mapping they wish as long as it is total and bijective. This example
61 swizzles the high and low halves of each byte.
62
63 ```rust
64 use bitvec::{
65 order::BitOrder,
66 index::{BitIdx, BitPos},
67 mem::BitRegister,
68 };
69
70 pub struct HiLo;
71
72 unsafe impl BitOrder for HiLo {
73 fn at<R>(index: BitIdx<R>) -> BitPos<R>
74 where R: BitRegister {
75 unsafe { BitPos::new_unchecked(index.into_inner() ^ 4) }
76 }
77 }
78
79 #[test]
80 #[cfg(test)]
81 fn prove_hilo() {
82 bitvec::order::verify::<HiLo>();
83 }
84 ```
85
86 Once a `BitOrder` implementation passes the test suite, it can be freely used as
87 a type parameter in `bitvec` data structures. The translation takes place
88 automatically, and you never need to look at this trait again.
89
90 [`at`]: Self::at
91 [`verify`]: crate::order::verify
92 [`verify_for_type`]: crate::order::verify_for_type