| 1 |
# Single-Bit Volatile Write |
| 2 |
|
| 3 |
This writes a bit into `dst` directly, using a volatile I/O intrinsic to |
| 4 |
prevent compiler reördering or removal. |
| 5 |
|
| 6 |
You should not use `bitvec` to perform any volatile I/O operations. You should |
| 7 |
instead do volatile I/O work on integer values directly, or use a crate like |
| 8 |
[`voladdress`][0] to perform I/O transactions, and use `bitvec` only on stack |
| 9 |
locals that have no additional memory semantics. |
| 10 |
|
| 11 |
## Original |
| 12 |
|
| 13 |
[`ptr::write_volatile](core::ptr::write_volatile) |
| 14 |
|
| 15 |
## Safety |
| 16 |
|
| 17 |
Because this performs a dereference of memory, it inherits the original |
| 18 |
`ptr::write_volatile`’s requirements: |
| 19 |
|
| 20 |
- `dst` must be valid to write |
| 21 |
- `dst` must be properly aligned. This is an invariant of the `BitPtr` type as |
| 22 |
well as of the memory access. |
| 23 |
|
| 24 |
Additionally, `dst` must point to an initialized value of `T`. Integers cannot |
| 25 |
be initialized one bit at a time. |
| 26 |
|
| 27 |
## Behavior |
| 28 |
|
| 29 |
This is required to perform a read/modify/write cycle on the memory location. |
| 30 |
LLVM *may or may not* emit a bit-write instruction on targets that have them in |
| 31 |
the ISA, but this is not specified in any way. |
| 32 |
|
| 33 |
## Examples |
| 34 |
|
| 35 |
```rust |
| 36 |
use bitvec::prelude::*; |
| 37 |
use bitvec::ptr as bv_ptr; |
| 38 |
|
| 39 |
let mut data = 0u8; |
| 40 |
let ptr = BitPtr::<_, _, Lsb0>::from_mut(&mut data); |
| 41 |
unsafe { bv_ptr::write_volatile(ptr.add(2), true); } |
| 42 |
assert_eq!(data, 4); |
| 43 |
``` |
| 44 |
|
| 45 |
[0]: https://docs.rs/voladdress/latest/voladdress |