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