Debian Codegraph @

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

raw
1 # Memory Region Description
2
3 This module bridges the abstract [`BitSlice`] region to real memory by
4 segmenting any bit-slice along its maybe-aliased and known-unaliased boundaries.
5 This segmentation applies to both bit-slice and ordinary-element views of
6 memory, and can be used to selectively remove alias restrictions or to enable
7 access to the underlying memory with ordinary types.
8
9 The four enums in this module all intentionally have the same variants by name
10 and shape, in order to maintain textual consistency.
11
12 ## Memory Layout Model
13
14 Any bit-slice resident in memory has one of two major kinds, which the enums in
15 this module refer to as `Enclave` and `Region`
16
17 ### Enclave
18
19 An `Enclave` layout occurs when a bit-slice is contained entirely within a
20 single memory element, and does not include either the initial or final semantic
21 index in its span.
22
23 ```text
24 [ 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 ]
25 [ ^^^^^^^^^^^^^^^^^^^^^ ]
26 ```
27
28 In an 8-bit element, a bit-slice is considered to be an `Enclave` if it is
29 contained entirely in the marked interior bits, and touches *neither* bit 7 nor
30 bit 0. Wider elements may touch interior byte boundaries, and only restrict bits
31 0 and `width - 1`.
32
33 ### Region
34
35 A `Region` layout occurs when a bit-slice consists of:
36
37 - zero or one half-spanned head element (excludes bit 0, includes `width - 1`)
38 - zero or more fully-spanned elements body (includes both 0 and `width - 1`)
39 - zero or one half-spanned tail element (includes bit 0, excludes `width - 1`)
40
41 Each of these three sections is optionally present independently of the other
42 two. That is, in the following three bytes, all of the following bit-slices have
43 the `Region` layout:
44
45 ```text
46 [ 7 6 5 4 3 2 1 0 ] [ 7 6 5 4 3 2 1 0 ] [ 7 6 5 4 3 2 1 0 ]
47 [ ]
48
49 [ h h h h ]
50 [ b b b b b b b b ]
51 [ t t t t ]
52
53 [ h h h h t t t t ]
54
55 [ h h h h b b b b b b b b ]
56 [ b b b b b b b b t t t t ]
57 [ h h h h b b b b b b b b t t t t ]
58 ```
59
60 1. The empty bit-slice is a region with all of its segments blank.
61 1. A bit-slice with one element that touches `width - 1` but not 0 has a head,
62 but no body or tail.
63 1. A bit-slice that touches both `0` and `width - 1` of any number of elements
64 has a body, but no head or tail.
65 1. A bit-slice with one element that touches 0 but not `width - 1` has a tail,
66 but no head or body.
67 1. A bit-slice with two elements, that touches neither 0 of the first nor
68 `width - 1` of the second (but by definition `width - 1` of the first and 0
69 of the second; bit-slices are contiguous) has a head and tail, but no body.
70
71 The final three rows show how the individual segments can be composed to
72 describe all possible bit-slices.
73
74 ## Aliasing Awareness
75
76 The contiguity property of `BitSlice` combines with the `&`/`&mut` exclusion
77 rules of the Rust language to provide additional information about the state of
78 the program that allows a given bit-slice to exist.
79
80 Specifically, any well-formed Rust program knows that *if* a bit-slice is able
81 to produce a `Region.body` segment, *then* that body is not aliased by `bitvec`,
82 and can safely transition to the `T::Unalias` state. Alias-permitting types like
83 `Cell` and the atomics will never change their types (because `bitvec` cannot
84 know that there are no views to a region other than what it has been given), but
85 a tainted `BitSlice<O, u8::Alias>` bit-slice can revert its interior body back
86 to `u8` and no longer require the alias tainting.
87
88 The head and tail segments do not retain their history, and cannot tell whether
89 they have been created by splitting or by shrinking, so they do not change their
90 types at all.
91
92 ## Raw Memory Access
93
94 The [`BitDomain`] enum only splits a bit-slice along these boundaries, and
95 allows a bit-slice view to safely shed aliasing protection added to it by
96 [`.split_at_mut()`].
97
98 The [`Domain`] enum completely sheds its bit-precision views, and reverts to
99 ordinary element accesses. The body segment is an ordinary Rust slice with no
100 additional information or restriction; it can be freely used without regard for
101 any of `bitvec`’s constraints.
102
103 In order to preserve the rules that any given bit-slice can never be used to
104 affect bits outside of its own view of memory, the underlying memory of the head
105 and tail segments is only made accessible through a [`PartialElement`] reference
106 guard. This guard is an opaque proxy to the memory location, and holds both a
107 reference and the bit-mask required to prevent reading from or writing to the
108 bits outside the scope of the originating bit-slice.
109
110 ## Generics
111
112 This module, and the contents of [`ptr`], make extensive use of a trait-level
113 mutability and reference tracking system in order to reduce code duplication and
114 provide a more powerful development environment than would be achieved with
115 macros.
116
117 As such, the trait bounds on types in this module are more intense than the
118 standard `<T, O>` fare in the crate’s main data structures. However, they are
119 only ever instantiated with shared or exclusive references, and all of the
120 bounds are a much more verbose way of saying “a reference, that is maybe-mut and
121 maybe-slice, of `T`”.
122
123 User code does not need to be aware of any of this: the `BitSlice` APIs that
124 call into this module always result in structures where the complex bounds are
125 reduced to ordinary slice references.
126
127 [`BitDomain`]: BitDomain
128 [`BitSlice`]: crate::slice::BitSlice
129 [`Domain`]: Domain
130 [`PartialElement`]: PartialElement
131 [`ptr`]: crate::ptr
132 [`.split_at_mut()`]: crate::slice::BitSlice::split_at_mut