Debian Codegraph @

debian/rust-vendor/bitvec/src/slice/specialization.rs

raw
1 #![doc = include_str!("../../doc/slice/specialization.md")]
2
3 use funty::Integral;
4
5 use super::BitSlice;
6 use crate::{
7 devel as dvl,
8 mem,
9 order::BitOrder,
10 store::BitStore,
11 };
12
13 mod lsb0;
14 mod msb0;
15
16 /// Processor width, used for chunking.
17 const WORD_BITS: usize = mem::bits_of::<usize>();
18
19 /// Tests whether the masked portion of an integer has a `0` bit in it.
20 fn has_zero<T>(val: T, mask: T) -> bool
21 where T: Integral {
22 val | !mask != !T::ZERO
23 }
24
25 /// Tests whether the masked portion of an integer has a `1` bit in it.
26 fn has_one<T>(val: T, mask: T) -> bool
27 where T: Integral {
28 val & mask != T::ZERO
29 }
30
31 impl<T, O> BitSlice<T, O>
32 where
33 T: BitStore,
34 O: BitOrder,
35 {
36 /// Forces the storage type parameter to be its accessor type.
37 ///
38 /// Functions must use this when working with maybe-overlapping regions
39 /// within a single bit-slice, as the accessor is always tolerant of
40 /// aliasing.
41 #[inline]
42 fn as_accessor(&mut self) -> &BitSlice<T::Access, O> {
43 unsafe { &*(self as *const Self as *const BitSlice<T::Access, O>) }
44 }
45
46 /// Attempts to change a bit-slice reference to caller-supplied type
47 /// parameters.
48 ///
49 /// If `<T, O>` is identical to `<T2, O2>`, this returns `Some` with the
50 /// bit-slice reference unchanged in value but changed in type. If the types
51 /// differ, it returns `None`. This is useful for creating statically-known
52 /// bit-slice types within generic contexts.
53 pub(crate) fn coerce<T2, O2>(&self) -> Option<&BitSlice<T2, O2>>
54 where
55 T2: BitStore,
56 O2: BitOrder,
57 {
58 if dvl::match_types::<T, O, T2, O2>() {
59 Some(unsafe { &*(self as *const Self as *const BitSlice<T2, O2>) })
60 }
61 else {
62 None
63 }
64 }
65
66 /// See [`.coerce()`].
67 ///
68 /// [`.coerce()`]: Self::coerce
69 pub(crate) fn coerce_mut<T2, O2>(&mut self) -> Option<&mut BitSlice<T2, O2>>
70 where
71 T2: BitStore,
72 O2: BitOrder,
73 {
74 if dvl::match_types::<T, O, T2, O2>() {
75 Some(unsafe { &mut *(self as *mut Self as *mut BitSlice<T2, O2>) })
76 }
77 else {
78 None
79 }
80 }
81 }