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
#![no_std]
use core::fmt::{self, Debug, Formatter};
use core::iter::{FusedIterator, Iterator, Map, Peekable};
use core::mem::replace;
pub struct IdentifyFirstLast<I: Iterator>(
Map<IdentifyFirst<IdentifyLast<I>>, fn((bool, (bool, I::Item))) -> (bool, bool, I::Item)>
);
impl<I: Iterator + Debug> Debug for IdentifyFirstLast<I> where I::Item: Debug {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { self.0.fmt(f) }
}
impl<I: Iterator + Clone> Clone for IdentifyFirstLast<I> where I::Item: Clone {
fn clone(&self) -> Self { IdentifyFirstLast(self.0.clone()) }
}
impl<I: Iterator> Iterator for IdentifyFirstLast<I> {
type Item = (bool, bool, I::Item);
fn next(&mut self) -> Option<Self::Item> { self.0.next() }
fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
}
impl<I: ExactSizeIterator> ExactSizeIterator for IdentifyFirstLast<I> {
fn len(&self) -> usize { self.0.len() }
}
impl<I: FusedIterator> FusedIterator for IdentifyFirstLast<I> { }
pub struct IdentifyLast<I: Iterator> {
iter: Peekable<I>,
}
impl<I: Iterator + Debug> Debug for IdentifyLast<I> where I::Item: Debug {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { self.iter.fmt(f) }
}
impl<I: Iterator + Clone> Clone for IdentifyLast<I> where I::Item: Clone {
fn clone(&self) -> Self { IdentifyLast { iter: self.iter.clone() } }
}
impl<I: Iterator> Iterator for IdentifyLast<I> {
type Item = (bool, I::Item);
fn next(&mut self) -> Option<Self::Item> {
if let Some(item) = self.iter.next() {
let is_last = self.iter.peek().is_none();
Some((is_last, item))
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
}
impl<I: ExactSizeIterator> ExactSizeIterator for IdentifyLast<I> {
fn len(&self) -> usize { self.iter.len() }
}
impl<I: FusedIterator> FusedIterator for IdentifyLast<I> { }
#[derive(Debug, Clone)]
pub struct IdentifyFirst<I: Iterator> {
is_first: bool,
iter: I,
}
impl<I: Iterator> Iterator for IdentifyFirst<I> {
type Item = (bool, I::Item);
fn next(&mut self) -> Option<Self::Item> {
if let Some(item) = self.iter.next() {
let is_first = replace(&mut self.is_first, false);
Some((is_first, item))
} else {
self.is_first = true;
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
}
impl<I: ExactSizeIterator> ExactSizeIterator for IdentifyFirst<I> {
fn len(&self) -> usize { self.iter.len() }
}
impl<I: FusedIterator> FusedIterator for IdentifyFirst<I> { }
pub trait IteratorIdentifyFirstLastExt: Iterator + Sized {
fn identify_first(self) -> IdentifyFirst<Self>;
fn identify_last(self) -> IdentifyLast<Self>;
fn identify_first_last(self) -> IdentifyFirstLast<Self>;
}
impl<I: Iterator + Sized> IteratorIdentifyFirstLastExt for I {
fn identify_first(self) -> IdentifyFirst<Self> { IdentifyFirst { is_first: true, iter: self } }
fn identify_last(self) -> IdentifyLast<Self> { IdentifyLast { iter: self.peekable() } }
fn identify_first_last(self) -> IdentifyFirstLast<Self> {
IdentifyFirstLast(self.identify_last().identify_first().map(
|(is_first, (is_last, item))| (is_first, is_last, item)
))
}
}
#[cfg(test)]
mod tests {
use arrayvec::ArrayVec;
use crate::IteratorIdentifyFirstLastExt;
#[test]
fn identify_first() {
assert_eq!(
[1, 2, 3, 4].into_iter().identify_first().collect::<ArrayVec<_, 4>>().as_slice(),
&[(true, 1), (false, 2), (false, 3), (false, 4)]
);
}
#[test]
fn identify_last() {
assert_eq!(
[1, 2, 3, 4].into_iter().identify_last().collect::<ArrayVec<_, 4>>().as_slice(),
&[(false, 1), (false, 2), (false, 3), (true, 4)]
);
}
#[test]
fn identify_first_last() {
assert_eq!(
[1, 2, 3, 4].into_iter().identify_first_last().collect::<ArrayVec<_, 4>>().as_slice(),
&[(true, false, 1), (false, false, 2), (false, false, 3), (false, true, 4)]
);
}
}