nostd_bv/traits/
bit_sliceable.rs1use crate::range_compat::*;
2use crate::{Bits, BitsMut};
3
4pub trait BitSliceable<Range>: Bits {
13 type Slice: Bits<Block = Self::Block>;
15
16 fn bit_slice(self, range: Range) -> Self::Slice;
33}
34
35pub trait BitSliceableMut<Range>: BitSliceable<Range> {
42 fn bit_slice_mut(self, range: Range) -> Self::Slice
49 where
50 Self: Sized,
51 {
52 self.bit_slice(range)
53 }
54}
55
56impl<Range, T> BitSliceableMut<Range> for T
57where
58 T: BitSliceable<Range>,
59 T::Slice: BitsMut,
60{
61}
62
63impl<'a> BitSliceable<RangeFull> for &'a [bool] {
64 type Slice = &'a [bool];
65
66 fn bit_slice(self, _: RangeFull) -> &'a [bool] {
67 self
68 }
69}
70
71impl<'a> BitSliceable<RangeFull> for &'a mut [bool] {
72 type Slice = &'a mut [bool];
73
74 fn bit_slice(self, _: RangeFull) -> &'a mut [bool] {
75 self
76 }
77}
78
79impl<'a> BitSliceable<Range<u64>> for &'a [bool] {
80 type Slice = &'a [bool];
81
82 fn bit_slice(self, range: Range<u64>) -> &'a [bool] {
83 &self[range.start as usize..range.end as usize]
84 }
85}
86
87impl<'a> BitSliceable<Range<u64>> for &'a mut [bool] {
88 type Slice = &'a mut [bool];
89
90 fn bit_slice(self, range: Range<u64>) -> &'a mut [bool] {
91 &mut self[range.start as usize..range.end as usize]
92 }
93}
94
95impl<'a> BitSliceable<RangeInclusive<u64>> for &'a [bool] {
96 type Slice = &'a [bool];
97
98 fn bit_slice(self, range: RangeInclusive<u64>) -> &'a [bool] {
99 let (start, end) =
100 get_inclusive_bounds(range).expect("<&[bool]>::bit_slice: bad inclusive range");
101 &self[start as usize..end as usize + 1]
105 }
106}
107
108impl<'a> BitSliceable<RangeInclusive<u64>> for &'a mut [bool] {
109 type Slice = &'a mut [bool];
110
111 fn bit_slice(self, range: RangeInclusive<u64>) -> &'a mut [bool] {
112 let (start, end) =
113 get_inclusive_bounds(range).expect("<&mut [bool]>::bit_slice: bad inclusive range");
114 &mut self[start as usize..end as usize + 1]
115 }
116}
117
118impl<'a> BitSliceable<RangeFrom<u64>> for &'a [bool] {
119 type Slice = &'a [bool];
120
121 fn bit_slice(self, range: RangeFrom<u64>) -> &'a [bool] {
122 &self[range.start as usize..]
123 }
124}
125
126impl<'a> BitSliceable<RangeFrom<u64>> for &'a mut [bool] {
127 type Slice = &'a mut [bool];
128
129 fn bit_slice(self, range: RangeFrom<u64>) -> &'a mut [bool] {
130 &mut self[range.start as usize..]
131 }
132}
133
134impl<'a> BitSliceable<RangeTo<u64>> for &'a [bool] {
135 type Slice = &'a [bool];
136
137 fn bit_slice(self, range: RangeTo<u64>) -> &'a [bool] {
138 &self[..range.end as usize]
139 }
140}
141
142impl<'a> BitSliceable<RangeTo<u64>> for &'a mut [bool] {
143 type Slice = &'a mut [bool];
144
145 fn bit_slice(self, range: RangeTo<u64>) -> &'a mut [bool] {
146 &mut self[..range.end as usize]
147 }
148}
149
150impl<'a> BitSliceable<RangeToInclusive<u64>> for &'a [bool] {
151 type Slice = &'a [bool];
152
153 fn bit_slice(self, range: RangeToInclusive<u64>) -> &'a [bool] {
154 &self[RangeToInclusive {
155 end: range.end as usize,
156 }]
157 }
158}
159
160impl<'a> BitSliceable<RangeToInclusive<u64>> for &'a mut [bool] {
161 type Slice = &'a mut [bool];
162
163 fn bit_slice(self, range: RangeToInclusive<u64>) -> &'a mut [bool] {
164 &mut self[RangeToInclusive {
165 end: range.end as usize,
166 }]
167 }
168}