1use super::{change_lifetime, Iterator};
2
3pub struct FromCore<I>(pub(crate) I);
5
6impl<I> Iterator for FromCore<I>
7where
8 I: core::iter::Iterator,
9{
10 type Item<'a> = I::Item
11 where
12 Self: 'a;
13
14 fn next(&mut self) -> Option<Self::Item<'_>> {
15 self.0.next()
16 }
17}
18
19pub struct Map<I, F> {
21 iter: I,
22 func: F,
23}
24
25impl<I, F> Map<I, F> {
26 pub(crate) fn new(iter: I, func: F) -> Map<I, F> {
27 Map { iter, func }
28 }
29}
30
31impl<I, F, O> core::iter::Iterator for Map<I, F>
32where
33 I: Iterator,
34 F: FnMut(I::Item<'_>) -> O,
35{
36 type Item = O;
37
38 fn next(&mut self) -> Option<Self::Item> {
39 Some((self.func)(self.iter.next()?))
40 }
41}
42
43pub struct Touch<I, F> {
45 iter: I,
46 func: F,
47}
48
49impl<I, F> Touch<I, F> {
50 pub(crate) fn new(iter: I, func: F) -> Touch<I, F> {
51 Touch { iter, func }
52 }
53}
54
55impl<I, F> Iterator for Touch<I, F>
56where
57 I: Iterator,
58 F: FnMut(&mut I::Item<'_>),
59{
60 type Item<'a> = I::Item<'a>
61 where
62 Self: 'a;
63
64 fn next(&mut self) -> Option<Self::Item<'_>> {
65 let mut out = self.iter.next()?;
66 (self.func)(&mut out);
67 Some(out)
68 }
69}
70
71pub struct Filter<I, F> {
73 iter: I,
74 func: F,
75}
76
77impl<I, F> Filter<I, F> {
78 pub(crate) fn new(iter: I, func: F) -> Filter<I, F> {
79 Filter { iter, func }
80 }
81}
82
83impl<I, F> Iterator for Filter<I, F>
84where
85 I: Iterator,
86 F: FnMut(&I::Item<'_>) -> bool,
87{
88 type Item<'a> = I::Item<'a>
89 where
90 Self: 'a;
91
92 fn next(&mut self) -> Option<Self::Item<'_>> {
93 while let Some(val) = self.iter.next() {
94 if (self.func)(&val) {
95 return Some(unsafe { change_lifetime::<Self>(val) });
97 }
98 }
99 None
100 }
101}
102
103pub struct StepBy<I> {
105 iter: I,
106 step: usize,
107 first: bool,
108}
109
110impl<I> StepBy<I> {
111 pub(crate) fn new(iter: I, step: usize) -> StepBy<I> {
112 StepBy {
113 iter,
114 step,
115 first: true,
116 }
117 }
118}
119
120impl<I> Iterator for StepBy<I>
121where
122 I: Iterator,
123{
124 type Item<'a> = I::Item<'a>
125 where
126 Self: 'a;
127
128 fn next(&mut self) -> Option<Self::Item<'_>> {
129 if self.first {
130 self.first = false;
131 self.iter.next()
132 } else {
133 self.nth(self.step - 1)
134 }
135 }
136}
137
138pub struct Chain<I1, I2> {
140 first: Option<I1>,
141 second: Option<I2>,
142}
143
144impl<I1, I2> Chain<I1, I2> {
145 pub(crate) fn new(first: I1, second: I2) -> Chain<I1, I2> {
146 Chain {
147 first: Some(first),
148 second: Some(second),
149 }
150 }
151}
152
153impl<'b, I1, I2> Iterator for Chain<I1, I2>
154where
155 I1: Iterator + 'b,
156 I2: Iterator<Item<'b> = I1::Item<'b>> + 'b,
157{
158 type Item<'a> = I1::Item<'a>
159 where
160 Self: 'a;
161
162 fn next(&mut self) -> Option<Self::Item<'_>> {
163 if let Some(iter) = &mut self.first {
164 if let Some(val) = iter.next() {
165 return Some(unsafe { change_lifetime::<Self>(val) });
167 }
168 self.first = None;
169 }
170
171 if let Some(iter) = &mut self.second {
172 let iter = unsafe { core::mem::transmute::<&mut I2, &mut I2>(iter) };
176 if let Some(val) = iter.next() {
177 return Some(unsafe { change_lifetime::<Self>(val) });
179 }
180 self.second = None;
181 }
182 None
183 }
184}
185
186pub struct Zip<I1, I2> {
188 left: I1,
189 right: I2,
190}
191
192impl<I1, I2> Zip<I1, I2> {
193 pub(crate) fn new(left: I1, right: I2) -> Zip<I1, I2> {
194 Zip { left, right }
195 }
196}
197
198impl<I1, I2> Iterator for Zip<I1, I2>
199where
200 I1: Iterator,
201 I2: Iterator,
202{
203 type Item<'a> = (I1::Item<'a>, I2::Item<'a>)
204 where
205 Self: 'a;
206
207 fn next(&mut self) -> Option<Self::Item<'_>> {
208 let left = self.left.next()?;
209 let right = self.right.next()?;
210 Some((left, right))
211 }
212}
213
214pub struct Enumerate<I> {
216 iter: I,
217 pos: usize,
218}
219
220impl<I> Enumerate<I> {
221 pub(crate) fn new(iter: I) -> Enumerate<I> {
222 Enumerate { iter, pos: 0 }
223 }
224}
225
226impl<I> Iterator for Enumerate<I>
227where
228 I: Iterator,
229{
230 type Item<'a> = (usize, I::Item<'a>)
231 where
232 Self: 'a;
233
234 fn next(&mut self) -> Option<Self::Item<'_>> {
235 let out = (self.pos, self.iter.next()?);
236 self.pos += 1;
237 Some(out)
238 }
239}
240
241pub struct SkipWhile<I, F> {
243 iter: I,
244 func: Option<F>,
245}
246
247impl<I, F> SkipWhile<I, F> {
248 pub(crate) fn new(iter: I, func: F) -> SkipWhile<I, F> {
249 SkipWhile {
250 iter,
251 func: Some(func),
252 }
253 }
254}
255
256impl<I, F> Iterator for SkipWhile<I, F>
257where
258 I: Iterator,
259 F: FnMut(&I::Item<'_>) -> bool,
260{
261 type Item<'a> = I::Item<'a>
262 where
263 Self: 'a;
264
265 fn next(&mut self) -> Option<Self::Item<'_>> {
266 match self.func.take() {
267 Some(mut f) => {
268 while let Some(val) = self.iter.next() {
269 if !f(&val) {
270 return Some(unsafe { change_lifetime::<Self>(val) });
272 }
273 }
274 None
275 }
276 None => self.iter.next(),
277 }
278 }
279}
280
281pub struct TakeWhile<I, F> {
283 iter: I,
284 func: Option<F>,
285}
286
287impl<I, F> TakeWhile<I, F> {
288 pub(crate) fn new(iter: I, func: F) -> TakeWhile<I, F> {
289 TakeWhile {
290 iter,
291 func: Some(func),
292 }
293 }
294}
295
296impl<I, F> Iterator for TakeWhile<I, F>
297where
298 I: Iterator,
299 F: FnMut(&I::Item<'_>) -> bool,
300{
301 type Item<'a> = I::Item<'a>
302 where
303 Self: 'a;
304
305 fn next(&mut self) -> Option<Self::Item<'_>> {
306 match &mut self.func {
307 Some(f) => {
308 let next = self.iter.next()?;
309 if !f(&next) {
310 self.func = None;
311 None
312 } else {
313 Some(next)
314 }
315 }
316 None => None,
317 }
318 }
319}
320
321pub struct Skip<I> {
323 iter: I,
324 skip: usize,
325}
326
327impl<I> Skip<I> {
328 pub(crate) fn new(iter: I, skip: usize) -> Skip<I> {
329 Skip { iter, skip }
330 }
331}
332
333impl<I> Iterator for Skip<I>
334where
335 I: Iterator,
336{
337 type Item<'a> = I::Item<'a>
338 where
339 Self: 'a;
340
341 fn next(&mut self) -> Option<Self::Item<'_>> {
342 while self.skip > 0 {
343 self.skip -= 1;
344 match self.iter.next() {
345 Some(_) => self.skip -= 1,
346 None => {
347 self.skip = 0;
348 return None;
349 }
350 }
351 }
352 self.iter.next()
353 }
354}
355
356pub struct Take<I> {
358 iter: I,
359 take: usize,
360}
361
362impl<I> Take<I> {
363 pub(crate) fn new(iter: I, take: usize) -> Take<I> {
364 Take { iter, take }
365 }
366}
367
368impl<I> Iterator for Take<I>
369where
370 I: Iterator,
371{
372 type Item<'a> = I::Item<'a>
373 where
374 Self: 'a;
375
376 fn next(&mut self) -> Option<Self::Item<'_>> {
377 if self.take > 0 {
378 self.take -= 1;
379 self.iter.next()
380 } else {
381 None
382 }
383 }
384}
385
386pub struct Scan<I, T, F> {
388 iter: I,
389 state: T,
390 func: F,
391}
392
393impl<I, T, F> Scan<I, T, F> {
394 pub(crate) fn new(iter: I, state: T, func: F) -> Scan<I, T, F> {
395 Scan { iter, state, func }
396 }
397}
398
399impl<I, T, F, O> core::iter::Iterator for Scan<I, T, F>
400where
401 I: Iterator,
402 F: FnMut(&mut T, I::Item<'_>) -> Option<O>,
403{
404 type Item = O;
405
406 fn next(&mut self) -> Option<Self::Item> {
407 let a = self.iter.next()?;
408 (self.func)(&mut self.state, a)
409 }
410}