#[cfg(test)]
mod test;
pub struct ForLoopIterator<Item>
{
current_value: Item,
while_predicate: Box<dyn Fn(&Item) -> bool>,
next_value_function: Box<dyn Fn(&Item) -> Item>
}
impl<Item> ForLoopIterator<Item> {
pub fn new(init: Item, pred: impl Fn(&Item) -> bool + 'static, next: impl Fn(&Item) -> Item + 'static) -> ForLoopIterator<Item>
{
ForLoopIterator {
current_value: init,
while_predicate: Box::new(pred),
next_value_function: Box::new(next)
}
}
}
impl<Item> Iterator for ForLoopIterator<Item> {
type Item = Item;
fn next(&mut self) -> Option<Self::Item> {
use std::mem;
if !(self.while_predicate)(&self.current_value) {
return None;
}
let mut swap_with = (self.next_value_function)(&self.current_value);
mem::swap(&mut self.current_value, &mut swap_with);
Some(swap_with)
}
}