1#![doc = include_str!("../../doc/slice/specialization.md")]
23use funty::Integral;
45use super::BitSlice;
6use crate::{
7 devel as dvl,
8 mem,
9 order::BitOrder,
10 store::BitStore,
11};
1213mod lsb0;
14mod msb0;
1516/// Processor width, used for chunking.
17const WORD_BITS: usize = mem::bits_of::<usize>();
1819/// Tests whether the masked portion of an integer has a `0` bit in it.
20fn has_zero<T>(val: T, mask: T) -> bool
21where T: Integral {
22 val | !mask != !T::ZERO
23}
2425/// Tests whether the masked portion of an integer has a `1` bit in it.
26fn has_one<T>(val: T, mask: T) -> bool
27where T: Integral {
28 val & mask != T::ZERO
29}
3031impl<T, O> BitSlice<T, O>
32where
33T: 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]
42fn as_accessor(&mut self) -> &BitSlice<T::Access, O> {
43unsafe { &*(self as *const Self as *const BitSlice<T::Access, O>) }
44 }
4546/// 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.
53pub(crate) fn coerce<T2, O2>(&self) -> Option<&BitSlice<T2, O2>>
54where
55T2: BitStore,
56 O2: BitOrder,
57 {
58if dvl::match_types::<T, O, T2, O2>() {
59Some(unsafe { &*(self as *const Self as *const BitSlice<T2, O2>) })
60 }
61else {
62None
63}
64 }
6566/// See [`.coerce()`].
67 ///
68 /// [`.coerce()`]: Self::coerce
69pub(crate) fn coerce_mut<T2, O2>(&mut self) -> Option<&mut BitSlice<T2, O2>>
70where
71T2: BitStore,
72 O2: BitOrder,
73 {
74if dvl::match_types::<T, O, T2, O2>() {
75Some(unsafe { &mut *(self as *mut Self as *mut BitSlice<T2, O2>) })
76 }
77else {
78None
79}
80 }
81}