1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use {Bits, BitsMut};
use range_compat::*;
pub trait BitSliceable<Range>: Bits {
type Slice: Bits<Block = Self::Block>;
fn bit_slice(self, range: Range) -> Self::Slice;
}
pub trait BitSliceableMut<Range>: BitSliceable<Range> {
fn bit_slice_mut(self, range: Range) -> Self::Slice where Self: Sized {
self.bit_slice(range)
}
}
impl<Range, T> BitSliceableMut<Range> for T
where T: BitSliceable<Range>,
T::Slice: BitsMut { }
impl<'a> BitSliceable<RangeFull> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, _: RangeFull) -> &'a [bool] {
self
}
}
impl<'a> BitSliceable<RangeFull> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, _: RangeFull) -> &'a mut [bool] {
self
}
}
impl<'a> BitSliceable<Range<u64>> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, range: Range<u64>) -> &'a [bool] {
&self[range.start as usize .. range.end as usize]
}
}
impl<'a> BitSliceable<Range<u64>> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, range: Range<u64>) -> &'a mut [bool] {
&mut self[range.start as usize .. range.end as usize]
}
}
#[cfg(inclusive_range)]
impl<'a> BitSliceable<RangeInclusive<u64>> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, range: RangeInclusive<u64>) -> &'a [bool] {
let (start, end) = get_inclusive_bounds(range)
.expect("<&[bool]>::bit_slice: bad inclusive range");
&self[start as usize .. end as usize + 1]
}
}
#[cfg(inclusive_range)]
impl<'a> BitSliceable<RangeInclusive<u64>> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, range: RangeInclusive<u64>) -> &'a mut [bool] {
let (start, end) = get_inclusive_bounds(range)
.expect("<&mut [bool]>::bit_slice: bad inclusive range");
&mut self[start as usize .. end as usize + 1]
}
}
impl<'a> BitSliceable<RangeFrom<u64>> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, range: RangeFrom<u64>) -> &'a [bool] {
&self[range.start as usize ..]
}
}
impl<'a> BitSliceable<RangeFrom<u64>> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, range: RangeFrom<u64>) -> &'a mut [bool] {
&mut self[range.start as usize ..]
}
}
impl<'a> BitSliceable<RangeTo<u64>> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, range: RangeTo<u64>) -> &'a [bool] {
&self[.. range.end as usize]
}
}
impl<'a> BitSliceable<RangeTo<u64>> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, range: RangeTo<u64>) -> &'a mut [bool] {
&mut self[.. range.end as usize]
}
}
#[cfg(inclusive_range)]
impl<'a> BitSliceable<RangeToInclusive<u64>> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, range: RangeToInclusive<u64>) -> &'a [bool] {
&self[RangeToInclusive { end: range.end as usize }]
}
}
#[cfg(inclusive_range)]
impl<'a> BitSliceable<RangeToInclusive<u64>> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, range: RangeToInclusive<u64>) -> &'a mut [bool] {
&mut self[RangeToInclusive { end: range.end as usize }]
}
}