| 1 |
# Many-Bit Swap |
| 2 |
|
| 3 |
Exchanges the contents of two regions, which cannot overlap. |
| 4 |
|
| 5 |
## Original |
| 6 |
|
| 7 |
[`ptr::swap_nonoverlapping`](core::ptr::swap_nonoverlapping) |
| 8 |
|
| 9 |
## Safety |
| 10 |
|
| 11 |
Both `one` and `two` must be: |
| 12 |
|
| 13 |
- correct `BitPtr` instances (well-aligned, non-null) |
| 14 |
- valid to read and write for the next `count` bits |
| 15 |
|
| 16 |
Additionally, the ranges `one .. one + count` and `two .. two + count` must be |
| 17 |
entirely disjoint. They can be adjacent, but no bit can be in both. |
| 18 |
|
| 19 |
## Examples |
| 20 |
|
| 21 |
```rust |
| 22 |
use bitvec::prelude::*; |
| 23 |
use bitvec::ptr as bv_ptr; |
| 24 |
|
| 25 |
let mut x = [0u8; 2]; |
| 26 |
let mut y = !0u16; |
| 27 |
let x_ptr = BitPtr::<_, _, Msb0>::from_slice_mut(&mut x); |
| 28 |
let y_ptr = BitPtr::<_, _, Lsb0>::from_mut(&mut y); |
| 29 |
|
| 30 |
unsafe { |
| 31 |
bv_ptr::swap_nonoverlapping(x_ptr, y_ptr, 12); |
| 32 |
} |
| 33 |
assert_eq!(x, [!0, 0xF0]); |
| 34 |
assert_eq!(y, 0xF0_00); |
| 35 |
``` |