Coverage Report

Created: 2025-06-22 07:07

/rust/registry/src/index.crates.io-6f17d22bba15001f/bitvec-1.0.1/src/ptr.rs
Line
Count
Source (jump to first uncovered line)
1
#![doc = include_str!("../doc/ptr.md")]
2
3
use core::hash::{
4
  Hash,
5
  Hasher,
6
};
7
8
use wyz::bidi::BidiIterator;
9
10
use crate::{
11
  devel as dvl,
12
  order::BitOrder,
13
  slice::BitSlice,
14
  store::BitStore,
15
};
16
17
mod addr;
18
mod proxy;
19
mod range;
20
mod single;
21
mod span;
22
mod tests;
23
24
pub use wyz::comu::{
25
  Const,
26
  Mut,
27
  Mutability,
28
};
29
30
pub(crate) use self::{
31
  addr::AddressExt,
32
  span::BitSpan,
33
};
34
pub use self::{
35
  addr::{
36
    check_alignment,
37
    MisalignError,
38
  },
39
  proxy::BitRef,
40
  range::BitPtrRange,
41
  single::{
42
    BitPtr,
43
    BitPtrError,
44
  },
45
  span::BitSpanError,
46
};
47
48
#[inline]
49
#[doc = include_str!("../doc/ptr/copy.md")]
50
0
pub unsafe fn copy<T1, T2, O1, O2>(
51
0
  src: BitPtr<Const, T1, O1>,
52
0
  dst: BitPtr<Mut, T2, O2>,
53
0
  count: usize,
54
0
) where
55
0
  O1: BitOrder,
56
0
  O2: BitOrder,
57
0
  T1: BitStore,
58
0
  T2: BitStore,
59
0
{
60
0
  //  Overlap is only defined if the orderings are identical.
61
0
  if dvl::match_order::<O1, O2>() {
62
0
    let (addr, head) = dst.raw_parts();
63
0
    let dst = BitPtr::<Mut, T2, O1>::new_unchecked(addr, head);
64
0
    let src_pair = src.range(count);
65
0
66
0
    let rev = src_pair.contains(&dst);
67
0
    for (from, to) in src_pair.zip(dst.range(count)).bidi(rev) {
68
0
      to.write(from.read());
69
0
    }
70
  }
71
0
  else {
72
0
    copy_nonoverlapping(src, dst, count);
73
0
  }
74
0
}
75
76
#[inline]
77
#[doc = include_str!("../doc/ptr/copy_nonoverlapping.md")]
78
0
pub unsafe fn copy_nonoverlapping<T1, T2, O1, O2>(
79
0
  src: BitPtr<Const, T1, O1>,
80
0
  dst: BitPtr<Mut, T2, O2>,
81
0
  count: usize,
82
0
) where
83
0
  O1: BitOrder,
84
0
  O2: BitOrder,
85
0
  T1: BitStore,
86
0
  T2: BitStore,
87
0
{
88
0
  for (from, to) in src.range(count).zip(dst.range(count)) {
89
0
    to.write(from.read());
90
0
  }
91
0
}
92
93
#[inline]
94
#[doc = include_str!("../doc/ptr/drop_in_place.md")]
95
#[deprecated = "this has no effect, and should not be called"]
96
0
pub unsafe fn drop_in_place<T, O>(_: BitPtr<Mut, T, O>)
97
0
where
98
0
  T: BitStore,
99
0
  O: BitOrder,
100
0
{
101
0
}
102
103
#[doc = include_str!("../doc/ptr/eq.md")]
104
#[inline]
105
0
pub fn eq<T1, T2, O>(
106
0
  this: BitPtr<Const, T1, O>,
107
0
  that: BitPtr<Const, T2, O>,
108
0
) -> bool
109
0
where
110
0
  T1: BitStore,
111
0
  T2: BitStore,
112
0
  O: BitOrder,
113
0
{
114
0
  this == that
115
0
}
116
117
#[inline]
118
#[cfg(not(tarpaulin_include))]
119
#[doc = include_str!("../doc/ptr/hash.md")]
120
0
pub fn hash<T, O, S>(ptr: BitPtr<Const, T, O>, into: &mut S)
121
0
where
122
0
  T: BitStore,
123
0
  O: BitOrder,
124
0
  S: Hasher,
125
0
{
126
0
  ptr.hash(into);
127
0
}
128
129
#[inline]
130
#[cfg(not(tarpaulin_include))]
131
#[doc = include_str!("../doc/ptr/null.md")]
132
0
pub fn null<T, O>() -> BitPtr<Const, T, O>
133
0
where
134
0
  T: BitStore,
135
0
  O: BitOrder,
136
0
{
137
0
  BitPtr::DANGLING
138
0
}
139
140
#[inline]
141
#[cfg(not(tarpaulin_include))]
142
#[doc = include_str!("../doc/ptr/null_mut.md")]
143
0
pub fn null_mut<T, O>() -> BitPtr<Mut, T, O>
144
0
where
145
0
  T: BitStore,
146
0
  O: BitOrder,
147
0
{
148
0
  BitPtr::DANGLING
149
0
}
150
151
#[inline]
152
#[cfg(not(tarpaulin_include))]
153
#[doc = include_str!("../doc/ptr/read.md")]
154
0
pub unsafe fn read<T, O>(src: BitPtr<Const, T, O>) -> bool
155
0
where
156
0
  T: BitStore,
157
0
  O: BitOrder,
158
0
{
159
0
  src.read()
160
0
}
161
162
#[inline]
163
#[allow(deprecated)]
164
#[cfg(not(tarpaulin_include))]
165
#[doc = include_str!("../doc/ptr/read_unaligned.md")]
166
#[deprecated = "`BitPtr` does not have unaligned addresses"]
167
0
pub unsafe fn read_unaligned<T, O>(src: BitPtr<Const, T, O>) -> bool
168
0
where
169
0
  T: BitStore,
170
0
  O: BitOrder,
171
0
{
172
0
  src.read_unaligned()
173
0
}
174
175
#[inline]
176
#[cfg(not(tarpaulin_include))]
177
#[doc = include_str!("../doc/ptr/read_volatile.md")]
178
0
pub unsafe fn read_volatile<T, O>(src: BitPtr<Const, T, O>) -> bool
179
0
where
180
0
  T: BitStore,
181
0
  O: BitOrder,
182
0
{
183
0
  src.read_volatile()
184
0
}
185
186
#[inline]
187
#[cfg(not(tarpaulin_include))]
188
#[doc = include_str!("../doc/ptr/replace.md")]
189
0
pub unsafe fn replace<T, O>(dst: BitPtr<Mut, T, O>, src: bool) -> bool
190
0
where
191
0
  T: BitStore,
192
0
  O: BitOrder,
193
0
{
194
0
  dst.replace(src)
195
0
}
196
197
#[inline]
198
#[cfg(not(tarpaulin_include))]
199
#[doc = include_str!("../doc/ptr/slice_from_raw_parts.md")]
200
0
pub fn slice_from_raw_parts<T, O>(
201
0
  ptr: BitPtr<Const, T, O>,
202
0
  len: usize,
203
0
) -> *const BitSlice<T, O>
204
0
where
205
0
  T: BitStore,
206
0
  O: BitOrder,
207
0
{
208
0
  bitslice_from_raw_parts(ptr, len)
209
0
}
210
211
#[inline]
212
#[cfg(not(tarpaulin_include))]
213
#[doc = include_str!("../doc/ptr/slice_from_raw_parts_mut.md")]
214
0
pub fn slice_from_raw_parts_mut<T, O>(
215
0
  ptr: BitPtr<Mut, T, O>,
216
0
  len: usize,
217
0
) -> *mut BitSlice<T, O>
218
0
where
219
0
  T: BitStore,
220
0
  O: BitOrder,
221
0
{
222
0
  bitslice_from_raw_parts_mut(ptr, len)
223
0
}
224
225
#[inline]
226
#[doc = include_str!("../doc/ptr/swap.md")]
227
0
pub unsafe fn swap<T1, T2, O1, O2>(
228
0
  one: BitPtr<Mut, T1, O1>,
229
0
  two: BitPtr<Mut, T2, O2>,
230
0
) where
231
0
  T1: BitStore,
232
0
  T2: BitStore,
233
0
  O1: BitOrder,
234
0
  O2: BitOrder,
235
0
{
236
0
  one.write(two.replace(one.read()));
237
0
}
238
239
#[inline]
240
#[doc = include_str!("../doc/ptr/swap_nonoverlapping.md")]
241
0
pub unsafe fn swap_nonoverlapping<T1, T2, O1, O2>(
242
0
  mut one: BitPtr<Mut, T1, O1>,
243
0
  mut two: BitPtr<Mut, T2, O2>,
244
0
  count: usize,
245
0
) where
246
0
  O1: BitOrder,
247
0
  O2: BitOrder,
248
0
  T1: BitStore,
249
0
  T2: BitStore,
250
0
{
251
0
  //  Note: compare codegen with `one.range(count).zip(two.range(count))`.
252
0
  for _ in 0 .. count {
253
0
    swap(one, two);
254
0
    one = one.add(1);
255
0
    two = two.add(1);
256
0
  }
257
0
}
258
259
#[inline]
260
#[cfg(not(tarpaulin_include))]
261
#[doc = include_str!("../doc/ptr/write.md")]
262
0
pub unsafe fn write<T, O>(dst: BitPtr<Mut, T, O>, value: bool)
263
0
where
264
0
  T: BitStore,
265
0
  O: BitOrder,
266
0
{
267
0
  dst.write(value);
268
0
}
269
270
#[inline]
271
#[cfg(not(tarpaulin_include))]
272
#[deprecated = "use `write_bits()` instead"]
273
#[doc = include_str!("../doc/ptr/write_bytes.md")]
274
0
pub unsafe fn write_bytes<T, O>(
275
0
  dst: BitPtr<Mut, T, O>,
276
0
  value: bool,
277
0
  count: usize,
278
0
) where
279
0
  T: BitStore,
280
0
  O: BitOrder,
281
0
{
282
0
  write_bits(dst, value, count)
283
0
}
284
285
#[inline]
286
#[allow(deprecated)]
287
#[cfg(not(tarpaulin_include))]
288
#[doc = include_str!("../doc/ptr/write_unaligned.md")]
289
#[deprecated = "`BitPtr` does not have unaligned addresses"]
290
0
pub unsafe fn write_unaligned<T, O>(dst: BitPtr<Mut, T, O>, value: bool)
291
0
where
292
0
  T: BitStore,
293
0
  O: BitOrder,
294
0
{
295
0
  dst.write_unaligned(value);
296
0
}
297
298
#[inline]
299
#[cfg(not(tarpaulin_include))]
300
#[doc = include_str!("../doc/ptr/write_volatile.md")]
301
0
pub unsafe fn write_volatile<T, O>(dst: BitPtr<Mut, T, O>, value: bool)
302
0
where
303
0
  T: BitStore,
304
0
  O: BitOrder,
305
0
{
306
0
  dst.write_volatile(value);
307
0
}
308
309
//  Renamed variants.
310
311
#[inline]
312
#[cfg(not(tarpaulin_include))]
313
#[doc = include_str!("../doc/ptr/bitslice_from_raw_parts.md")]
314
0
pub fn bitslice_from_raw_parts<T, O>(
315
0
  ptr: BitPtr<Const, T, O>,
316
0
  len: usize,
317
0
) -> *const BitSlice<T, O>
318
0
where
319
0
  T: BitStore,
320
0
  O: BitOrder,
321
0
{
322
0
  ptr.span(len).unwrap().into_bitslice_ptr()
323
0
}
324
325
#[inline]
326
#[cfg(not(tarpaulin_include))]
327
#[doc = include_str!("../doc/ptr/bitslice_from_raw_parts_mut.md")]
328
0
pub fn bitslice_from_raw_parts_mut<T, O>(
329
0
  ptr: BitPtr<Mut, T, O>,
330
0
  len: usize,
331
0
) -> *mut BitSlice<T, O>
332
0
where
333
0
  T: BitStore,
334
0
  O: BitOrder,
335
0
{
336
0
  ptr.span(len).unwrap().into_bitslice_ptr_mut()
337
0
}
338
339
#[inline]
340
#[doc = include_str!("../doc/ptr/write_bits.md")]
341
0
pub unsafe fn write_bits<T, O>(dst: BitPtr<Mut, T, O>, value: bool, count: usize)
342
0
where
343
0
  T: BitStore,
344
0
  O: BitOrder,
345
0
{
346
0
  for bit in dst.range(count) {
347
0
    bit.write(value);
348
0
  }
349
0
}