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
|
use std::iter::Peekable;
pub struct Enumerate<I>
where
I: Iterator,
{
iter: Peekable<I>,
count: usize,
}
impl<I> Iterator for Enumerate<I>
where
I: Iterator,
{
type Item = (usize, bool, <I as Iterator>::Item);
#[inline]
fn next(&mut self) -> Option<(usize, bool, <I as Iterator>::Item)> {
self.iter.next().map(|a| {
let last = self.iter.peek().is_none();
let ret = (self.count, last, a);
// Possible undefined overflow.
self.count += 1;
ret
})
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
#[inline]
fn count(self) -> usize {
self.iter.count()
}
}
#[inline]
pub fn enumerate<I>(iter: I) -> Enumerate<I>
where
I: Iterator,
{
Enumerate {
iter: iter.peekable(),
count: 0,
}
}
|