use core::fmt;
pub fn cfor<T, CondF, StepF>(init: T, cond: CondF, step: StepF) -> CFor<T, CondF, StepF>
where
T: Clone,
CondF: FnMut(&T) -> bool,
StepF: FnMut(&mut T),
{
CFor {
current: init,
cond,
step,
}
}
#[derive(Clone)]
pub struct CFor<T, CondF = fn(&T) -> bool, StepF = fn(&mut T)> {
pub current: T,
cond: CondF,
step: StepF,
}
impl<T: fmt::Debug, CondF, StepF> fmt::Debug for CFor<T, CondF, StepF> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CFor")
.field("current", &self.current)
.finish_non_exhaustive()
}
}
impl<T, CondF, StepF> Iterator for CFor<T, CondF, StepF>
where
T: Clone,
CondF: FnMut(&T) -> bool,
StepF: FnMut(&mut T),
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
let Self {
current,
cond,
step,
} = self;
match cond(current) {
true => {
let yieldme = current.clone();
step(current);
Some(yieldme)
}
false => None,
}
}
}