use std::iter::Enumerate; use std::iter::Peekable; pub struct TemplateLoop where I: Iterator, { iter: Peekable>, } impl TemplateLoop where I: Iterator, { #[inline] pub fn new(iter: I) -> Self { TemplateLoop { iter: iter.enumerate().peekable(), } } } impl Iterator for TemplateLoop where I: Iterator, { type Item = (::Item, LoopItem); #[inline] fn next(&mut self) -> Option<(::Item, LoopItem)> { self.iter.next().map(|(index, item)| { ( item, LoopItem { index, first: index == 0, last: self.iter.peek().is_none(), }, ) }) } } #[derive(Copy, Clone)] pub struct LoopItem { pub index: usize, pub first: bool, pub last: bool, }