| 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 overlap. |
| 7 |
|
| 8 |
If the two regions are known to *never* overlap, then [`copy_nonoverlapping`][0] |
| 9 |
can be used instead. |
| 10 |
|
| 11 |
## Original |
| 12 |
|
| 13 |
[`ptr::copy`](core::ptr::copy) |
| 14 |
|
| 15 |
## Overlap Definition |
| 16 |
|
| 17 |
`bitvec` defines region overlap only when the bit-pointers used to access them |
| 18 |
have the same `O: BitOrder` type parameter. When this parameter differs, the |
| 19 |
regions are always assumed to not overlap in real memory, because `bitvec` does |
| 20 |
not define the effects of different orderings mapping to the same locations. |
| 21 |
|
| 22 |
## Safety |
| 23 |
|
| 24 |
In addition to the bit-ordering constraints, this inherits the restrictions of |
| 25 |
the original `ptr::copy`: |
| 26 |
|
| 27 |
- `src` must be valid to read the next `count` bits out of memory. |
| 28 |
- `dst` must be valid to write into the next `count` bits. |
| 29 |
- Both `src` and `dst` must satisfy [`BitPtr`]’s non-null, well-aligned, |
| 30 |
requirements. |
| 31 |
|
| 32 |
## Behavior |
| 33 |
|
| 34 |
This reads and writes each bit individually. It is incapable of optimizing its |
| 35 |
behavior to perform batched memory accesses that have better awareness of the |
| 36 |
underlying memory. |
| 37 |
|
| 38 |
The [`BitSlice::copy_from_bitslice`][1] method *is* able to perform this |
| 39 |
optimization. You should always prefer to use `BitSlice` if you are sensitive to |
| 40 |
performance. |
| 41 |
|
| 42 |
## Examples |
| 43 |
|
| 44 |
This example performs a simple copy across independent regions. You can see that |
| 45 |
it follows the ordering parameter for the source and destination regions as it |
| 46 |
walks each bit individually. |
| 47 |
|
| 48 |
```rust |
| 49 |
use bitvec::prelude::*; |
| 50 |
use bitvec::ptr as bv_ptr; |
| 51 |
|
| 52 |
let start = 0b1011u8; |
| 53 |
let mut end = 0u16; |
| 54 |
|
| 55 |
let src = BitPtr::<_, _, Lsb0>::from_ref(&start); |
| 56 |
let dst = BitPtr::<_, _, Msb0>::from_mut(&mut end); |
| 57 |
|
| 58 |
unsafe { |
| 59 |
bv_ptr::copy(src, dst, 4); |
| 60 |
} |
| 61 |
assert_eq!(end, 0b1101_0000_0000_0000); |
| 62 |
``` |
| 63 |
|
| 64 |
This can detect overlapping regions. Note again that overlap only exists when |
| 65 |
the ordering parameter is the same! Using bit-pointers that overlap in real |
| 66 |
memory with different ordering is not defined, and `bitvec` does not specify any |
| 67 |
result. |
| 68 |
|
| 69 |
```rust |
| 70 |
use bitvec::prelude::*; |
| 71 |
use bitvec::ptr as bv_ptr; |
| 72 |
|
| 73 |
let mut x = 0b1111_0010u8; |
| 74 |
let src = BitPtr::<_, _, Lsb0>::from_mut(&mut x); |
| 75 |
let dst = unsafe { src.add(2) }; |
| 76 |
|
| 77 |
unsafe { |
| 78 |
bv_ptr::copy(src.to_const(), dst, 4); |
| 79 |
} |
| 80 |
|
| 81 |
assert_eq!(x, 0b1100_1010); |
| 82 |
// bottom nibble ^^ ^^ moved here |
| 83 |
``` |
| 84 |
|
| 85 |
[`BitPtr`]: crate::ptr::BitPtr |
| 86 |
[0]: crate::ptr::copy_nonoverlapping |
| 87 |
[1]: crate::slice::BitSlice::copy_from_bitslice |