| 1 |
# Bit-wise `memcpy` |
| 2 |
|
| 3 |
This copies bits from a region beginning at `src` into a region beginning at |
| 4 |
`dst`, each extending upwards in the address space for `count` bits. |
| 5 |
|
| 6 |
The two regions *may not* overlap. |
| 7 |
|
| 8 |
## Original |
| 9 |
|
| 10 |
[`ptr::copy_nonoverlapping`](core::ptr::copy_nonoverlapping) |
| 11 |
|
| 12 |
## Overlap Definition |
| 13 |
|
| 14 |
The two regions may be in the same provenance as long as they have no common |
| 15 |
bits. `bitvec` only defines the possibility of overlap when the `O1` and `O2` |
| 16 |
bit-ordering parameters are the same; if they are different, then it considers |
| 17 |
the regions to not overlap, and does not attempt to detect real-memory |
| 18 |
collisions. |
| 19 |
|
| 20 |
## Safety |
| 21 |
|
| 22 |
In addition to the bit-ordering constraints, this inherits the restrictions of |
| 23 |
the original `ptr::copy_nonoverlapping`: |
| 24 |
|
| 25 |
- `src` must be valid to read the next `count` bits out of memory. |
| 26 |
- `dst` must be valid to write into the next `count` bits. |
| 27 |
- Both `src` and `dst` must satisfy [`BitPtr`]’s non-null, well-aligned, |
| 28 |
requirements. |
| 29 |
|
| 30 |
## Behavior |
| 31 |
|
| 32 |
This reads and writes each bit individually. It is incapable of optimizing its |
| 33 |
behavior to perform batched memory accesses that have better awareness of the |
| 34 |
underlying memory. |
| 35 |
|
| 36 |
The [`BitSlice::copy_from_bitslice`][1] method *is* able to perform this |
| 37 |
optimization, and tolerates overlap. You should always prefer to use `BitSlice` |
| 38 |
if you are sensitive to performance. |
| 39 |
|
| 40 |
## Examples |
| 41 |
|
| 42 |
```rust |
| 43 |
use bitvec::prelude::*; |
| 44 |
use bitvec::ptr as bv_ptr; |
| 45 |
|
| 46 |
let start = 0b1011u8; |
| 47 |
let mut end = 0u16; |
| 48 |
|
| 49 |
let src = BitPtr::<_, _, Lsb0>::from_ref(&start); |
| 50 |
let dst = BitPtr::<_, _, Msb0>::from_mut(&mut end); |
| 51 |
|
| 52 |
unsafe { |
| 53 |
bv_ptr::copy_nonoverlapping(src, dst, 4); |
| 54 |
} |
| 55 |
assert_eq!(end, 0b1101_0000_0000_0000); |
| 56 |
``` |
| 57 |
|
| 58 |
[1]: crate::slice::BitSlice::copy_from_bitslice |
| 59 |
[`BitPtr`]: crate::ptr::BitPtr |