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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#![doc = include_str!("../../doc/boxed/iter.md")]

use core::{
	fmt::{
		self,
		Debug,
		Formatter,
	},
	iter::FusedIterator,
	ops::Range,
};

use super::BitBox;
use crate::{
	order::{
		BitOrder,
		Lsb0,
	},
	slice::BitSlice,
	store::BitStore,
};

/// [Original](alloc::vec::IntoIter)
impl<T, O> IntoIterator for BitBox<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	type IntoIter = IntoIter<T, O>;
	type Item = bool;

	#[inline]
	fn into_iter(self) -> Self::IntoIter {
		IntoIter::new(self)
	}
}

/** An iterator over a `BitBox`.

## Original

[`vec::IntoIter`](alloc::vec::IntoIter)
**/
pub struct IntoIter<T = usize, O = Lsb0>
where
	T: BitStore,
	O: BitOrder,
{
	/// The original `BitBox`, kept so it can correctly drop.
	_buf: BitBox<T, O>,
	/// A range of indices yet to be iterated.
	//  TODO(myrrlyn): Race this against `BitPtrRange<Mut, T, O>`.
	iter: Range<usize>,
}

impl<T, O> IntoIter<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	/// Wraps a bit-array in an iterator view. This is irreversible.
	#[inline]
	fn new(this: BitBox<T, O>) -> Self {
		let iter = 0 .. this.len();
		Self { _buf: this, iter }
	}

	/// Views the remaining unyielded bits as a bit-slice.
	///
	/// ## Original
	///
	/// [`IntoIter::as_slice`](alloc::vec::IntoIter::as_slice)
	#[inline]
	pub fn as_bitslice(&self) -> &BitSlice<T, O> {
		//  While the memory is never actually deïnitialized, this is still a
		//  good habit to do.
		unsafe {
			self._buf
				.as_bitptr()
				.add(self.iter.start)
				.span_unchecked(self.iter.len())
				.into_bitslice_ref()
		}
	}

	#[inline]
	#[doc(hidden)]
	#[cfg(not(tarpaulin_include))]
	#[deprecated = "use `.as_bitslice()` instead"]
	#[allow(missing_docs, clippy::missing_docs_in_private_items)]
	pub fn as_slice(&self) -> &BitSlice<T, O> {
		self.as_bitslice()
	}

	/// Views the remaining unyielded bits as a mutable bit-slice.
	///
	/// ## Original
	///
	/// [`IntoIter::as_mut_slice`](alloc::vec::IntoIter::as_mut_slice)
	#[inline]
	pub fn as_mut_bitslice(&mut self) -> &mut BitSlice<T, O> {
		unsafe {
			self._buf
				.as_mut_bitptr()
				.add(self.iter.start)
				.span_unchecked(self.iter.len())
				.into_bitslice_mut()
		}
	}

	#[inline]
	#[doc(hidden)]
	#[cfg(not(tarpaulin_include))]
	#[deprecated = "use `.as_mut_bitslice()` instead"]
	#[allow(missing_docs, clippy::missing_docs_in_private_items)]
	pub fn as_mut_slice(&mut self) -> &mut BitSlice<T, O> {
		self.as_mut_bitslice()
	}
}

/// [Original](https://doc.rust-lang.org/alloc/vec/struct.IntoIter.html#impl-AsRef%3C%5BT%5D%3E)
#[cfg(not(tarpaulin_include))]
impl<T, O> AsRef<BitSlice<T, O>> for IntoIter<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn as_ref(&self) -> &BitSlice<T, O> {
		self.as_bitslice()
	}
}

#[cfg(not(tarpaulin_include))]
impl<T, O> Clone for IntoIter<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn clone(&self) -> Self {
		Self {
			_buf: self._buf.clone(),
			iter: self.iter.clone(),
		}
	}
}

/// [Original](https://doc.rust-lang.org/alloc/vec/struct.IntoIter.html#impl-Debug)
#[cfg(not(tarpaulin_include))]
impl<T, O> Debug for IntoIter<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
		fmt.debug_tuple("IntoIter")
			.field(&self.as_bitslice())
			.finish()
	}
}

impl<T, O> Iterator for IntoIter<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	type Item = bool;

	easy_iter!();

	#[inline]
	fn next(&mut self) -> Option<Self::Item> {
		self.iter
			.next()
			.map(|idx| unsafe { self._buf.as_bitptr().add(idx).read() })
	}

	#[inline]
	fn nth(&mut self, n: usize) -> Option<Self::Item> {
		self.iter
			.nth(n)
			.map(|idx| unsafe { self._buf.as_bitptr().add(idx).read() })
	}
}

impl<T, O> DoubleEndedIterator for IntoIter<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn next_back(&mut self) -> Option<Self::Item> {
		self.iter
			.next_back()
			.map(|idx| unsafe { self._buf.as_bitptr().add(idx).read() })
	}

	#[inline]
	fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
		self.iter
			.nth_back(n)
			.map(|idx| unsafe { self._buf.as_bitptr().add(idx).read() })
	}
}

impl<T, O> ExactSizeIterator for IntoIter<T, O>
where
	T: BitStore,
	O: BitOrder,
{
	#[inline]
	fn len(&self) -> usize {
		self.iter.len()
	}
}

impl<T, O> FusedIterator for IntoIter<T, O>
where
	T: BitStore,
	O: BitOrder,
{
}

/// [Original](https://doc.rust-lang.org/alloc/vec/struct.IntoIter.html#impl-Send)
// #[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl<T, O> Send for IntoIter<T, O>
where
	T: BitStore + Sync,
	O: BitOrder,
{
}

/// [Original](https://doc.rust-lang.org/alloc/vec/struct.IntoIter.html#impl-Sync)
unsafe impl<T, O> Sync for IntoIter<T, O>
where
	T: BitStore + Sync,
	O: BitOrder,
{
}