File: swap.md

package info (click to toggle)
rust-coreutils 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 505,620 kB
  • sloc: ansic: 103,594; asm: 28,570; sh: 8,910; python: 5,581; makefile: 472; cpp: 97; javascript: 72
file content (34 lines) | stat: -rw-r--r-- 806 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Bit Swap

This exchanges the bit-values in two locations. It is semantically and
behaviorally equivalent to [`BitRef::swap`][0], except that it works on
bit-pointer structures rather than proxy references. Prefer to use a proxy
reference or [`BitSlice::swap`][1] instead.

## Original

[`ptr::swap`](core::ptr::swap)

## Safety

This has the same safety requirements as [`ptr::read`][2] and [`ptr::write`][3],
as it is required to use them in its implementation.

## Examples

```rust
use bitvec::prelude::*;
use bitvec::ptr as bv_ptr;

let mut data = 2u8;
let x = BitPtr::<_, _, Lsb0>::from_mut(&mut data);
let y = unsafe { x.add(1) };

unsafe { bv_ptr::swap(x, y); }
assert_eq!(data, 1);
```

[0]: crate::ptr::BitRef::swap
[1]: crate::slice::BitSlice::swap
[2]: crate::ptr::read
[3]: crate::ptr::write