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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use super::{change_lifetime, Iterator};

/// See [`IntoLending::into_lending`]
pub struct FromCore<I>(pub(crate) I);

impl<I> Iterator for FromCore<I>
where
    I: core::iter::Iterator,
{
    type Item<'a> = I::Item
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        self.0.next()
    }
}

/// See [`Iterator::map`]
pub struct Map<I, F> {
    iter: I,
    func: F,
}

impl<I, F> Map<I, F> {
    pub(crate) fn new(iter: I, func: F) -> Map<I, F> {
        Map { iter, func }
    }
}

impl<I, F, O> core::iter::Iterator for Map<I, F>
where
    I: Iterator,
    F: FnMut(I::Item<'_>) -> O,
{
    type Item = O;

    fn next(&mut self) -> Option<Self::Item> {
        Some((self.func)(self.iter.next()?))
    }
}

/// See [`Iterator::touch`]
pub struct Touch<I, F> {
    iter: I,
    func: F,
}

impl<I, F> Touch<I, F> {
    pub(crate) fn new(iter: I, func: F) -> Touch<I, F> {
        Touch { iter, func }
    }
}

impl<I, F> Iterator for Touch<I, F>
where
    I: Iterator,
    F: FnMut(&mut I::Item<'_>),
{
    type Item<'a> = I::Item<'a>
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        let mut out = self.iter.next()?;
        (self.func)(&mut out);
        Some(out)
    }
}

/// See [`Iterator::filter`]
pub struct Filter<I, F> {
    iter: I,
    func: F,
}

impl<I, F> Filter<I, F> {
    pub(crate) fn new(iter: I, func: F) -> Filter<I, F> {
        Filter { iter, func }
    }
}

impl<I, F> Iterator for Filter<I, F>
where
    I: Iterator,
    F: FnMut(&I::Item<'_>) -> bool,
{
    type Item<'a> = I::Item<'a>
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        while let Some(val) = self.iter.next() {
            if (self.func)(&val) {
                // SAFETY: This is the polonius case
                return Some(unsafe { change_lifetime::<Self>(val) });
            }
        }
        None
    }
}

/// See [`Iterator::step_by`]
pub struct StepBy<I> {
    iter: I,
    step: usize,
    first: bool,
}

impl<I> StepBy<I> {
    pub(crate) fn new(iter: I, step: usize) -> StepBy<I> {
        StepBy {
            iter,
            step,
            first: true,
        }
    }
}

impl<I> Iterator for StepBy<I>
where
    I: Iterator,
{
    type Item<'a> = I::Item<'a>
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        if self.first {
            self.first = false;
            self.iter.next()
        } else {
            self.nth(self.step - 1)
        }
    }
}

/// See [`Iterator::chain`]
pub struct Chain<I1, I2> {
    first: Option<I1>,
    second: Option<I2>,
}

impl<I1, I2> Chain<I1, I2> {
    pub(crate) fn new(first: I1, second: I2) -> Chain<I1, I2> {
        Chain {
            first: Some(first),
            second: Some(second),
        }
    }
}

impl<'b, I1, I2> Iterator for Chain<I1, I2>
where
    I1: Iterator + 'b,
    I2: Iterator<Item<'b> = I1::Item<'b>> + 'b,
{
    type Item<'a> = I1::Item<'a>
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        if let Some(iter) = &mut self.first {
            if let Some(val) = iter.next() {
                // SAFETY: This is the polonius case
                return Some(unsafe { change_lifetime::<Self>(val) });
            }
            self.first = None;
        }

        if let Some(iter) = &mut self.second {
            // SAFETY: Due to our lie in the where bounds, we need to convince Rust
            //         that we actually borrow iter for a different length of time than it
            //         thinks
            let iter = unsafe { core::mem::transmute::<&mut I2, &mut I2>(iter) };
            if let Some(val) = iter.next() {
                // SAFETY: This is the polonius case
                return Some(unsafe { change_lifetime::<Self>(val) });
            }
            self.second = None;
        }
        None
    }
}

/// See [`Iterator::zip`]
pub struct Zip<I1, I2> {
    left: I1,
    right: I2,
}

impl<I1, I2> Zip<I1, I2> {
    pub(crate) fn new(left: I1, right: I2) -> Zip<I1, I2> {
        Zip { left, right }
    }
}

impl<I1, I2> Iterator for Zip<I1, I2>
where
    I1: Iterator,
    I2: Iterator,
{
    type Item<'a> = (I1::Item<'a>, I2::Item<'a>)
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        let left = self.left.next()?;
        let right = self.right.next()?;
        Some((left, right))
    }
}

/// See [`Iterator::enumerate`]
pub struct Enumerate<I> {
    iter: I,
    pos: usize,
}

impl<I> Enumerate<I> {
    pub(crate) fn new(iter: I) -> Enumerate<I> {
        Enumerate { iter, pos: 0 }
    }
}

impl<I> Iterator for Enumerate<I>
where
    I: Iterator,
{
    type Item<'a> = (usize, I::Item<'a>)
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        let out = (self.pos, self.iter.next()?);
        self.pos += 1;
        Some(out)
    }
}

/// See [`Iterator::skip_while`]
pub struct SkipWhile<I, F> {
    iter: I,
    func: Option<F>,
}

impl<I, F> SkipWhile<I, F> {
    pub(crate) fn new(iter: I, func: F) -> SkipWhile<I, F> {
        SkipWhile {
            iter,
            func: Some(func),
        }
    }
}

impl<I, F> Iterator for SkipWhile<I, F>
where
    I: Iterator,
    F: FnMut(&I::Item<'_>) -> bool,
{
    type Item<'a> = I::Item<'a>
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        match self.func.take() {
            Some(mut f) => {
                while let Some(val) = self.iter.next() {
                    if !f(&val) {
                        // SAFETY: This is the polonius case
                        return Some(unsafe { change_lifetime::<Self>(val) });
                    }
                }
                None
            }
            None => self.iter.next(),
        }
    }
}

/// See [`Iterator::take_while`]
pub struct TakeWhile<I, F> {
    iter: I,
    func: Option<F>,
}

impl<I, F> TakeWhile<I, F> {
    pub(crate) fn new(iter: I, func: F) -> TakeWhile<I, F> {
        TakeWhile {
            iter,
            func: Some(func),
        }
    }
}

impl<I, F> Iterator for TakeWhile<I, F>
where
    I: Iterator,
    F: FnMut(&I::Item<'_>) -> bool,
{
    type Item<'a> = I::Item<'a>
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        match &mut self.func {
            Some(f) => {
                let next = self.iter.next()?;
                if !f(&next) {
                    self.func = None;
                    None
                } else {
                    Some(next)
                }
            }
            None => None,
        }
    }
}

/// See [`Iterator::skip`]
pub struct Skip<I> {
    iter: I,
    skip: usize,
}

impl<I> Skip<I> {
    pub(crate) fn new(iter: I, skip: usize) -> Skip<I> {
        Skip { iter, skip }
    }
}

impl<I> Iterator for Skip<I>
where
    I: Iterator,
{
    type Item<'a> = I::Item<'a>
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        while self.skip > 0 {
            self.skip -= 1;
            match self.iter.next() {
                Some(_) => self.skip -= 1,
                None => {
                    self.skip = 0;
                    return None;
                }
            }
        }
        self.iter.next()
    }
}

/// See [`Iterator::take`]
pub struct Take<I> {
    iter: I,
    take: usize,
}

impl<I> Take<I> {
    pub(crate) fn new(iter: I, take: usize) -> Take<I> {
        Take { iter, take }
    }
}

impl<I> Iterator for Take<I>
where
    I: Iterator,
{
    type Item<'a> = I::Item<'a>
    where
        Self: 'a;

    fn next(&mut self) -> Option<Self::Item<'_>> {
        if self.take > 0 {
            self.take -= 1;
            self.iter.next()
        } else {
            None
        }
    }
}

/// See [`Iterator::scan`]
pub struct Scan<I, T, F> {
    iter: I,
    state: T,
    func: F,
}

impl<I, T, F> Scan<I, T, F> {
    pub(crate) fn new(iter: I, state: T, func: F) -> Scan<I, T, F> {
        Scan { iter, state, func }
    }
}

impl<I, T, F, O> core::iter::Iterator for Scan<I, T, F>
where
    I: Iterator,
    F: FnMut(&mut T, I::Item<'_>) -> Option<O>,
{
    type Item = O;

    fn next(&mut self) -> Option<Self::Item> {
        let a = self.iter.next()?;
        (self.func)(&mut self.state, a)
    }
}