| 1 |
# Single-Bit Unaligned Write |
| 2 |
|
| 3 |
This writes a bit into `dst` directly. It uses compiler intrinsics to tolerate |
| 4 |
an unaligned `T` address. However, because `BitPtr` has a type invariant that |
| 5 |
addresses are always well-aligned (and non-null), this has no benefit or |
| 6 |
purpose. |
| 7 |
|
| 8 |
## Original |
| 9 |
|
| 10 |
[`ptr::write_unaligned](core::ptr::write_unaligned) |
| 11 |
|
| 12 |
## Safety |
| 13 |
|
| 14 |
- `dst` must be valid to write |
| 15 |
- `dst` must be properly aligned. This is an invariant of the `BitPtr` type as |
| 16 |
well as of the memory access. |
| 17 |
|
| 18 |
Additionally, `dst` must point to an initialized value of `T`. Integers cannot |
| 19 |
be initialized one bit at a time. |
| 20 |
|
| 21 |
## Behavior |
| 22 |
|
| 23 |
This is required to perform a read/modify/write cycle on the memory location. |
| 24 |
LLVM *may or may not* emit a bit-write instruction on targets that have them in |
| 25 |
the ISA, but this is not specified in any way. |
| 26 |
|
| 27 |
## Examples |
| 28 |
|
| 29 |
```rust |
| 30 |
use bitvec::prelude::*; |
| 31 |
use bitvec::ptr as bv_ptr; |
| 32 |
|
| 33 |
let mut data = 0u8; |
| 34 |
let ptr = BitPtr::<_, _, Lsb0>::from_mut(&mut data); |
| 35 |
unsafe { bv_ptr::write_unaligned(ptr.add(2), true); } |
| 36 |
assert_eq!(data, 4); |
| 37 |
``` |