| 1 |
# Single-Bit Pointer |
| 2 |
|
| 3 |
This structure defines a pointer to exactly one bit in a memory element. It is a |
| 4 |
structure, rather than an encoding of a `*Bit` raw pointer, because it contains |
| 5 |
more information than can be packed into such a pointer. Furthermore, it can |
| 6 |
uphold the same requirements and guarantees that the rest of the crate demands, |
| 7 |
whereƤs a raw pointer cannot. |
| 8 |
|
| 9 |
## Original |
| 10 |
|
| 11 |
[`*bool`](https://doc.rust-lang.org/std/primitive.pointer.html) and |
| 12 |
[`NonNull<bool>`](core::ptr::NonNull) |
| 13 |
|
| 14 |
## API Differences |
| 15 |
|
| 16 |
Since raw pointers are not sufficient in space or guarantees, and are limited by |
| 17 |
not being marked `#[fundamental]`, this is an ordinary `struct`. Because it |
| 18 |
cannot use the `*const`/`*mut` distinction that raw pointers and references can, |
| 19 |
this encodes mutability in a type parameter instead. |
| 20 |
|
| 21 |
In order to be consistent with the rest of the crate, particularly the |
| 22 |
`*BitSlice` encoding, this enforces that all `T` element addresses are |
| 23 |
well-aligned to `T` and non-null. While this type is used in the API as an |
| 24 |
analogue of raw pointers, it is restricted in value to only contain the values |
| 25 |
of valid *references* to memory, not arbitrary pointers. |
| 26 |
|
| 27 |
## ABI Differences |
| 28 |
|
| 29 |
This is aligned to `1`, rather than the processor word, in order to enable some |
| 30 |
crate-internal space optimizations. |
| 31 |
|
| 32 |
## Type Parameters |
| 33 |
|
| 34 |
- `M`: Marks whether the pointer has mutability permissions to the referent |
| 35 |
memory. Only `Mut` pointers can be used to create `&mut` references. |
| 36 |
- `T`: A memory type used to select both the register width and the bus behavior |
| 37 |
when performing memory accesses. |
| 38 |
- `O`: The ordering of bits within a memory element. |
| 39 |
|
| 40 |
## Usage |
| 41 |
|
| 42 |
This structure is used as the `bitvec` equivalent to `*bool`. It is used in all |
| 43 |
raw-pointer APIs and provides behavior to emulate raw pointers. It cannot be |
| 44 |
directly dereferenced, as it is not a pointer; it can only be transformed back |
| 45 |
into higher referential types, or used in functions that accept it. |
| 46 |
|
| 47 |
These pointers can never be null or misaligned. |
| 48 |
|
| 49 |
## Safety |
| 50 |
|
| 51 |
Rust and LLVM **do not** have a concept of bit-level initialization yet. |
| 52 |
Furthermore, the underlying foundational code that this type uses to manipulate |
| 53 |
individual bits in memory relies on construction of **shared references** to |
| 54 |
memory, which means that unlike standard pointers, the `T` element to which |
| 55 |
`BitPtr` values point must always be **already initialized** in your program |
| 56 |
context. |
| 57 |
|
| 58 |
`bitvec` is not able to detect or enforce this requirement, and is currently not |
| 59 |
able to avoid it. See [`BitAccess`] for more information. |
| 60 |
|
| 61 |
[`BitAccess`]: crate::access::BitAccess |