radix_clis/utils/iter.rs
1use std::iter;
2
3/// A trait for identifying the last element.
4pub trait IdentifyLast: Iterator + Sized {
5 fn identify_last(self) -> Iter<Self>;
6}
7
8impl<I: Iterator> IdentifyLast for I {
9 fn identify_last(self) -> Iter<Self> {
10 Iter(self.peekable())
11 }
12}
13
14/// An iterator that supports `IdentifyLast`.
15pub struct Iter<I: Iterator>(iter::Peekable<I>);
16
17impl<I: Iterator> Iterator for Iter<I> {
18 type Item = (bool, I::Item);
19
20 fn next(&mut self) -> Option<Self::Item> {
21 self.0.next().map(|e| (self.0.peek().is_none(), e))
22 }
23}