Debian Codegraph @

debian/rust-vendor/bitvec/doc/slice/iter/NoAlias.md

raw
1 # Anti-Aliasing Iterator Adapter
2
3 This structure is an adapter over a corresponding `&mut BitSlice` iterator. It
4 removes the `::Alias` taint marker, allowing mutations through each yielded bit
5 reference to skip any costs associated with aliasing.
6
7 ## Safety
8
9 The default `&mut BitSlice` iterators attach an `::Alias` taint for a reason:
10 the iterator protocol does not mandate that yielded items have a narrower
11 lifespan than the iterator that produced them! As such, it is completely
12 possible to pull multiple yielded items out into the same scope, where they have
13 overlapping lifetimes.
14
15 The `BitStore` principles require that whenever two write-capable handles to the
16 same memory region have overlapping lifetimes, they *must* be `::Alias` tainted.
17 This adapter removes the `::Alias` taint, but is not able to enforce strictly
18 non-overlapping lifetimes of yielded items.
19
20 As such, this adapter is **unsafe to construct**, and you **must** only use it
21 in a `for`-loop where each yielded item does not escape the loop body.
22
23 In order to help enforce this limitation, this adapter structure is *not* `Send`
24 or `Sync`. It must be consumed in the scope where it was created.
25
26 ## Usage
27
28 If you are using a loop that satisfies the safety requirement, you can use the
29 `.remove_alias()` method on your mutable iterator and configure it to yield
30 handles that do not impose additional alias-protection costs when accessing the
31 underlying memory.
32
33 Note that this adapter does not go to `T::Unalias`: it only takes an iterator
34 that yields `T::Alias` and unwinds it to `T`. If the source bit-slice was
35 *already* alias-tainted, the original protection is not removed. You are
36 responsible for doing so by using [`.bit_domain_mut()`].
37
38 ## Examples
39
40 This example shows using `.chunks_mut()` without incurring alias protection.
41
42 This documentation is replicated on all `NoAlias` types; the examples will work
43 for all of them, but are not specialized in the text.
44
45 ```rust
46 use bitvec::prelude::*;
47 use bitvec::slice::{ChunksMut, ChunksMutNoAlias};
48 type Alias8 = <u8 as BitStore>::Alias;
49
50 let mut data: BitArr!(for 40, in u8, Msb0) = bitarr![u8, Msb0; 0; 40];
51
52 let mut chunks: ChunksMut<u8, Msb0> = data.chunks_mut(5);
53 let _chunk: &mut BitSlice<Alias8, Msb0> = chunks.next().unwrap();
54
55 let mut chunks: ChunksMutNoAlias<u8, Msb0> = unsafe { chunks.remove_alias() };
56 let _chunk: &mut BitSlice<u8, Msb0> = chunks.next().unwrap();
57 ```
58
59 This example shows how use of [`.split_at_mut()`] forces the `.remove_alias()` to
60 still retain a layer of alias protection.
61
62 ```rust
63 use bitvec::prelude::*;
64 use bitvec::slice::{ChunksMut, ChunksMutNoAlias};
65 type Alias8 = <u8 as BitStore>::Alias;
66 type Alias8Alias = <Alias8 as BitStore>::Alias;
67
68 let mut data: BitArr!(for 40, in u8, Msb0) = bitarr!(u8, Msb0; 0; 40);
69 let (_head, rest): (_, &mut BitSlice<Alias8, Msb0>) = data.split_at_mut(5);
70
71 let mut chunks: ChunksMut<Alias8, Msb0> = rest.chunks_mut(5);
72 let _chunk: &mut BitSlice<Alias8, Msb0> = chunks.next().unwrap();
73
74 let mut chunks: ChunksMutNoAlias<Alias8, Msb0> = unsafe { chunks.remove_alias() };
75 let _chunk: &mut BitSlice<Alias8, Msb0> = chunks.next().unwrap();
76 ```
77
78 And this example shows how to use `.bit_domain_mut()` in order to undo the
79 effects of `.split_at_mut()`, so that `.remove_alias()` can complete its work.
80
81 ```rust
82 use bitvec::prelude::*;
83 use bitvec::slice::{ChunksMut, ChunksMutNoAlias};
84 type Alias8 = <u8 as BitStore>::Alias;
85
86 let mut data: BitArr!(for 40, in u8, Msb0) = bitarr!(u8, Msb0; 0; 40);
87 let (_head, rest): (_, &mut BitSlice<Alias8, Msb0>) = data.split_at_mut(5);
88
89 let (head, body, tail): (
90 &mut BitSlice<Alias8, Msb0>,
91 &mut BitSlice<u8, Msb0>,
92 &mut BitSlice<Alias8, Msb0>,
93 ) = rest.bit_domain_mut().region().unwrap();
94
95 let mut chunks: ChunksMut<u8, Msb0> = body.chunks_mut(5);
96 let _chunk: &mut BitSlice<Alias8, Msb0> = chunks.next().unwrap();
97
98 let mut chunks: ChunksMutNoAlias<u8, Msb0> = unsafe { chunks.remove_alias() };
99 let _chunk: &mut BitSlice<u8, Msb0> = chunks.next().unwrap();
100 ```
101
102 [`.bit_domain_mut()`]: crate::slice::BitSlice::bit_domain_mut
103 [`.split_at_mut()`]: crate::slice::BitSlice::split_at_mut