Coverage Report

Created: 2025-07-18 06:16

/rust/registry/src/index.crates.io-6f17d22bba15001f/bitvec-1.0.1/src/array/iter.rs
Line
Count
Source (jump to first uncovered line)
1
#![doc = include_str!("../../doc/array/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 tap::Pipe;
14
use wyz::comu::Const;
15
16
use super::BitArray;
17
use crate::{
18
  mem,
19
  order::BitOrder,
20
  ptr::BitPtr,
21
  slice::BitSlice,
22
  view::BitViewSized,
23
};
24
25
/// [Original](https://doc.rust-lang.org/std/primitive.array.html#impl-IntoIterator)
26
impl<A, O> IntoIterator for BitArray<A, O>
27
where
28
  A: BitViewSized,
29
  O: BitOrder,
30
{
31
  type IntoIter = IntoIter<A, O>;
32
  type Item = <IntoIter<A, O> as Iterator>::Item;
33
34
  #[inline]
35
0
  fn into_iter(self) -> Self::IntoIter {
36
0
    IntoIter::new(self)
37
0
  }
38
}
39
40
/// [Original](https://doc.rust-lang.org/std/primitive.array.html#impl-IntoIterator-1)
41
#[cfg(not(tarpaulin_include))]
42
impl<'a, A, O> IntoIterator for &'a BitArray<A, O>
43
where
44
  O: BitOrder,
45
  A: 'a + BitViewSized,
46
{
47
  type IntoIter = <&'a BitSlice<A::Store, O> as IntoIterator>::IntoIter;
48
  type Item = <&'a BitSlice<A::Store, O> as IntoIterator>::Item;
49
50
  #[inline]
51
0
  fn into_iter(self) -> Self::IntoIter {
52
0
    self.as_bitslice().into_iter()
53
0
  }
54
}
55
56
/// [Original](https://doc.rust-lang.org/std/primitive.array.html#impl-IntoIterator-2)
57
#[cfg(not(tarpaulin_include))]
58
impl<'a, A, O> IntoIterator for &'a mut BitArray<A, O>
59
where
60
  O: BitOrder,
61
  A: 'a + BitViewSized,
62
{
63
  type IntoIter = <&'a mut BitSlice<A::Store, O> as IntoIterator>::IntoIter;
64
  type Item = <&'a mut BitSlice<A::Store, O> as IntoIterator>::Item;
65
66
  #[inline]
67
0
  fn into_iter(self) -> Self::IntoIter {
68
0
    self.as_mut_bitslice().into_iter()
69
0
  }
70
}
71
72
#[derive(Clone)]
73
#[doc = include_str!("../../doc/array/IntoIter.md")]
74
pub struct IntoIter<A, O>
75
where
76
  A: BitViewSized,
77
  O: BitOrder,
78
{
79
  /// The bit-array being iterated.
80
  array: BitArray<A, O>,
81
  /// The indices in `.array` that have not yet been yielded.
82
  ///
83
  /// This range is always a strict subset of `0 .. self.array.len()`.
84
  alive: Range<usize>,
85
}
86
87
impl<A, O> IntoIter<A, O>
88
where
89
  A: BitViewSized,
90
  O: BitOrder,
91
{
92
  /// Converts a bit-array into its iterator.
93
  ///
94
  /// The [`.into_iter()`] method on bit-arrays forwards to this. While
95
  /// `BitArray` does deref to `&/mut BitSlice`, which also has
96
  /// `.into_iter()`, this behavior has always been present alongside
97
  /// `BitArray` and there is no legacy forwarding to preserve.
98
  ///
99
  /// ## Original
100
  ///
101
  /// [`IntoIter::new`](core::array::IntoIter::new)s
102
  #[inline]
103
0
  pub fn new(array: BitArray<A, O>) -> Self {
104
0
    Self {
105
0
      array,
106
0
      alive: 0 .. mem::bits_of::<A>(),
107
0
    }
108
0
  }
109
110
  /// Views the remaining unyielded bits in the iterator.
111
  ///
112
  /// ## Original
113
  ///
114
  /// [`IntoIter::as_slice`](core::array::IntoIter::as_slice)
115
  #[inline]
116
0
  pub fn as_bitslice(&self) -> &BitSlice<A::Store, O> {
117
0
    unsafe { self.array.as_bitslice().get_unchecked(self.alive.clone()) }
118
0
  }
119
120
  #[inline]
121
  #[cfg(not(tarpaulin_include))]
122
  #[deprecated = "use `.as_bitslice()` instead"]
123
  #[allow(missing_docs, clippy::missing_docs_in_private_items)]
124
0
  pub fn as_slice(&self) -> &BitSlice<A::Store, O> {
125
0
    self.as_bitslice()
126
0
  }
127
128
  /// Mutably views the remaining unyielded bits in the iterator.
129
  ///
130
  /// ## Original
131
  ///
132
  /// [`IntoIter::as_mut_slice`](core::array::IntoIter::as_mut_slice)
133
  #[inline]
134
0
  pub fn as_mut_bitslice(&mut self) -> &mut BitSlice<A::Store, O> {
135
0
    unsafe {
136
0
      self.array
137
0
        .as_mut_bitslice()
138
0
        .get_unchecked_mut(self.alive.clone())
139
0
    }
140
0
  }
141
142
  #[inline]
143
  #[cfg(not(tarpaulin_include))]
144
  #[deprecated = "use `.as_bitslice_mut()` instead"]
145
  #[allow(missing_docs, clippy::missing_docs_in_private_items)]
146
0
  pub fn as_mut_slice(&mut self) -> &mut BitSlice<A::Store, O> {
147
0
    self.as_mut_bitslice()
148
0
  }
149
150
  /// Gets a bit from the bit-array.
151
  #[inline]
152
0
  fn get(&self, index: usize) -> bool {
153
0
    unsafe {
154
0
      self.array
155
0
        .as_raw_slice()
156
0
        .pipe(BitPtr::<Const, A::Store, O>::from_slice)
157
0
        .add(index)
158
0
        .read()
159
0
    }
160
0
  }
161
}
162
163
#[cfg(not(tarpaulin_include))]
164
impl<A, O> Debug for IntoIter<A, O>
165
where
166
  A: BitViewSized,
167
  O: BitOrder,
168
{
169
  #[inline]
170
0
  fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
171
0
    fmt.debug_tuple("IntoIter")
172
0
      .field(&self.as_bitslice())
173
0
      .finish()
174
0
  }
175
}
176
177
impl<A, O> Iterator for IntoIter<A, O>
178
where
179
  A: BitViewSized,
180
  O: BitOrder,
181
{
182
  type Item = bool;
183
184
  easy_iter!();
185
186
  #[inline]
187
0
  fn next(&mut self) -> Option<Self::Item> {
188
0
    self.alive.next().map(|idx| self.get(idx))
189
0
  }
190
191
  #[inline]
192
0
  fn nth(&mut self, n: usize) -> Option<Self::Item> {
193
0
    self.alive.nth(n).map(|idx| self.get(idx))
194
0
  }
195
}
196
197
impl<A, O> DoubleEndedIterator for IntoIter<A, O>
198
where
199
  A: BitViewSized,
200
  O: BitOrder,
201
{
202
  #[inline]
203
0
  fn next_back(&mut self) -> Option<Self::Item> {
204
0
    self.alive.next_back().map(|idx| self.get(idx))
205
0
  }
206
207
  #[inline]
208
0
  fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
209
0
    self.alive.nth_back(n).map(|idx| self.get(idx))
210
0
  }
211
}
212
213
impl<A, O> ExactSizeIterator for IntoIter<A, O>
214
where
215
  A: BitViewSized,
216
  O: BitOrder,
217
{
218
  #[inline]
219
0
  fn len(&self) -> usize {
220
0
    self.alive.len()
221
0
  }
222
}
223
224
impl<A, O> FusedIterator for IntoIter<A, O>
225
where
226
  A: BitViewSized,
227
  O: BitOrder,
228
{
229
}