/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bitvec-1.0.1/src/boxed/iter.rs
Line | Count | Source |
1 | | #![doc = include_str!("../../doc/boxed/iter.md")] |
2 | | |
3 | | use core::{ |
4 | | fmt::{ |
5 | | self, |
6 | | Debug, |
7 | | Formatter, |
8 | | }, |
9 | | iter::FusedIterator, |
10 | | ops::Range, |
11 | | }; |
12 | | |
13 | | use super::BitBox; |
14 | | use crate::{ |
15 | | order::{ |
16 | | BitOrder, |
17 | | Lsb0, |
18 | | }, |
19 | | slice::BitSlice, |
20 | | store::BitStore, |
21 | | }; |
22 | | |
23 | | /// [Original](alloc::vec::IntoIter) |
24 | | impl<T, O> IntoIterator for BitBox<T, O> |
25 | | where |
26 | | T: BitStore, |
27 | | O: BitOrder, |
28 | | { |
29 | | type IntoIter = IntoIter<T, O>; |
30 | | type Item = bool; |
31 | | |
32 | | #[inline] |
33 | 0 | fn into_iter(self) -> Self::IntoIter { |
34 | 0 | IntoIter::new(self) |
35 | 0 | } |
36 | | } |
37 | | |
38 | | /** An iterator over a `BitBox`. |
39 | | |
40 | | ## Original |
41 | | |
42 | | [`vec::IntoIter`](alloc::vec::IntoIter) |
43 | | **/ |
44 | | pub struct IntoIter<T = usize, O = Lsb0> |
45 | | where |
46 | | T: BitStore, |
47 | | O: BitOrder, |
48 | | { |
49 | | /// The original `BitBox`, kept so it can correctly drop. |
50 | | _buf: BitBox<T, O>, |
51 | | /// A range of indices yet to be iterated. |
52 | | // TODO(myrrlyn): Race this against `BitPtrRange<Mut, T, O>`. |
53 | | iter: Range<usize>, |
54 | | } |
55 | | |
56 | | impl<T, O> IntoIter<T, O> |
57 | | where |
58 | | T: BitStore, |
59 | | O: BitOrder, |
60 | | { |
61 | | /// Wraps a bit-array in an iterator view. This is irreversible. |
62 | | #[inline] |
63 | 0 | fn new(this: BitBox<T, O>) -> Self { |
64 | 0 | let iter = 0 .. this.len(); |
65 | 0 | Self { _buf: this, iter } |
66 | 0 | } |
67 | | |
68 | | /// Views the remaining unyielded bits as a bit-slice. |
69 | | /// |
70 | | /// ## Original |
71 | | /// |
72 | | /// [`IntoIter::as_slice`](alloc::vec::IntoIter::as_slice) |
73 | | #[inline] |
74 | 0 | pub fn as_bitslice(&self) -> &BitSlice<T, O> { |
75 | | // While the memory is never actually deïnitialized, this is still a |
76 | | // good habit to do. |
77 | | unsafe { |
78 | 0 | self._buf |
79 | 0 | .as_bitptr() |
80 | 0 | .add(self.iter.start) |
81 | 0 | .span_unchecked(self.iter.len()) |
82 | 0 | .into_bitslice_ref() |
83 | | } |
84 | 0 | } |
85 | | |
86 | | #[inline] |
87 | | #[doc(hidden)] |
88 | | #[cfg(not(tarpaulin_include))] |
89 | | #[deprecated = "use `.as_bitslice()` instead"] |
90 | | #[allow(missing_docs, clippy::missing_docs_in_private_items)] |
91 | 0 | pub fn as_slice(&self) -> &BitSlice<T, O> { |
92 | 0 | self.as_bitslice() |
93 | 0 | } |
94 | | |
95 | | /// Views the remaining unyielded bits as a mutable bit-slice. |
96 | | /// |
97 | | /// ## Original |
98 | | /// |
99 | | /// [`IntoIter::as_mut_slice`](alloc::vec::IntoIter::as_mut_slice) |
100 | | #[inline] |
101 | 0 | pub fn as_mut_bitslice(&mut self) -> &mut BitSlice<T, O> { |
102 | 0 | unsafe { |
103 | 0 | self._buf |
104 | 0 | .as_mut_bitptr() |
105 | 0 | .add(self.iter.start) |
106 | 0 | .span_unchecked(self.iter.len()) |
107 | 0 | .into_bitslice_mut() |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | | #[inline] |
112 | | #[doc(hidden)] |
113 | | #[cfg(not(tarpaulin_include))] |
114 | | #[deprecated = "use `.as_mut_bitslice()` instead"] |
115 | | #[allow(missing_docs, clippy::missing_docs_in_private_items)] |
116 | 0 | pub fn as_mut_slice(&mut self) -> &mut BitSlice<T, O> { |
117 | 0 | self.as_mut_bitslice() |
118 | 0 | } |
119 | | } |
120 | | |
121 | | /// [Original](https://doc.rust-lang.org/alloc/vec/struct.IntoIter.html#impl-AsRef%3C%5BT%5D%3E) |
122 | | #[cfg(not(tarpaulin_include))] |
123 | | impl<T, O> AsRef<BitSlice<T, O>> for IntoIter<T, O> |
124 | | where |
125 | | T: BitStore, |
126 | | O: BitOrder, |
127 | | { |
128 | | #[inline] |
129 | 0 | fn as_ref(&self) -> &BitSlice<T, O> { |
130 | 0 | self.as_bitslice() |
131 | 0 | } |
132 | | } |
133 | | |
134 | | #[cfg(not(tarpaulin_include))] |
135 | | impl<T, O> Clone for IntoIter<T, O> |
136 | | where |
137 | | T: BitStore, |
138 | | O: BitOrder, |
139 | | { |
140 | | #[inline] |
141 | 0 | fn clone(&self) -> Self { |
142 | 0 | Self { |
143 | 0 | _buf: self._buf.clone(), |
144 | 0 | iter: self.iter.clone(), |
145 | 0 | } |
146 | 0 | } |
147 | | } |
148 | | |
149 | | /// [Original](https://doc.rust-lang.org/alloc/vec/struct.IntoIter.html#impl-Debug) |
150 | | #[cfg(not(tarpaulin_include))] |
151 | | impl<T, O> Debug for IntoIter<T, O> |
152 | | where |
153 | | T: BitStore, |
154 | | O: BitOrder, |
155 | | { |
156 | | #[inline] |
157 | 0 | fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { |
158 | 0 | fmt.debug_tuple("IntoIter") |
159 | 0 | .field(&self.as_bitslice()) |
160 | 0 | .finish() |
161 | 0 | } |
162 | | } |
163 | | |
164 | | impl<T, O> Iterator for IntoIter<T, O> |
165 | | where |
166 | | T: BitStore, |
167 | | O: BitOrder, |
168 | | { |
169 | | type Item = bool; |
170 | | |
171 | | easy_iter!(); |
172 | | |
173 | | #[inline] |
174 | 0 | fn next(&mut self) -> Option<Self::Item> { |
175 | 0 | self.iter |
176 | 0 | .next() |
177 | 0 | .map(|idx| unsafe { self._buf.as_bitptr().add(idx).read() }) |
178 | 0 | } |
179 | | |
180 | | #[inline] |
181 | 0 | fn nth(&mut self, n: usize) -> Option<Self::Item> { |
182 | 0 | self.iter |
183 | 0 | .nth(n) |
184 | 0 | .map(|idx| unsafe { self._buf.as_bitptr().add(idx).read() }) |
185 | 0 | } |
186 | | } |
187 | | |
188 | | impl<T, O> DoubleEndedIterator for IntoIter<T, O> |
189 | | where |
190 | | T: BitStore, |
191 | | O: BitOrder, |
192 | | { |
193 | | #[inline] |
194 | 0 | fn next_back(&mut self) -> Option<Self::Item> { |
195 | 0 | self.iter |
196 | 0 | .next_back() |
197 | 0 | .map(|idx| unsafe { self._buf.as_bitptr().add(idx).read() }) |
198 | 0 | } |
199 | | |
200 | | #[inline] |
201 | 0 | fn nth_back(&mut self, n: usize) -> Option<Self::Item> { |
202 | 0 | self.iter |
203 | 0 | .nth_back(n) |
204 | 0 | .map(|idx| unsafe { self._buf.as_bitptr().add(idx).read() }) |
205 | 0 | } |
206 | | } |
207 | | |
208 | | impl<T, O> ExactSizeIterator for IntoIter<T, O> |
209 | | where |
210 | | T: BitStore, |
211 | | O: BitOrder, |
212 | | { |
213 | | #[inline] |
214 | 0 | fn len(&self) -> usize { |
215 | 0 | self.iter.len() |
216 | 0 | } |
217 | | } |
218 | | |
219 | | impl<T, O> FusedIterator for IntoIter<T, O> |
220 | | where |
221 | | T: BitStore, |
222 | | O: BitOrder, |
223 | | { |
224 | | } |
225 | | |
226 | | /// [Original](https://doc.rust-lang.org/alloc/vec/struct.IntoIter.html#impl-Send) |
227 | | // #[allow(clippy::non_send_fields_in_send_ty)] |
228 | | unsafe impl<T, O> Send for IntoIter<T, O> |
229 | | where |
230 | | T: BitStore + Sync, |
231 | | O: BitOrder, |
232 | | { |
233 | | } |
234 | | |
235 | | /// [Original](https://doc.rust-lang.org/alloc/vec/struct.IntoIter.html#impl-Sync) |
236 | | unsafe impl<T, O> Sync for IntoIter<T, O> |
237 | | where |
238 | | T: BitStore + Sync, |
239 | | O: BitOrder, |
240 | | { |
241 | | } |