#![allow(deprecated)]
use std::fmt;
use std::mem;
#[deprecated(
note = "Use [std::iter::from_fn](https://doc.rust-lang.org/std/iter/fn.from_fn.html) instead",
since = "0.13.0"
)]
pub fn unfold<A, St, F>(initial_state: St, f: F) -> Unfold<St, F>
where
F: FnMut(&mut St) -> Option<A>,
{
Unfold {
f,
state: initial_state,
}
}
impl<St, F> fmt::Debug for Unfold<St, F>
where
St: fmt::Debug,
{
debug_fmt_fields!(Unfold, state);
}
#[derive(Clone)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[deprecated(
note = "Use [std::iter::FromFn](https://doc.rust-lang.org/std/iter/struct.FromFn.html) instead",
since = "0.13.0"
)]
pub struct Unfold<St, F> {
f: F,
pub state: St,
}
impl<A, St, F> Iterator for Unfold<St, F>
where
F: FnMut(&mut St) -> Option<A>,
{
type Item = A;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
(self.f)(&mut self.state)
}
}
#[derive(Clone)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Iterate<St, F> {
state: St,
f: F,
}
impl<St, F> fmt::Debug for Iterate<St, F>
where
St: fmt::Debug,
{
debug_fmt_fields!(Iterate, state);
}
impl<St, F> Iterator for Iterate<St, F>
where
F: FnMut(&St) -> St,
{
type Item = St;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let next_state = (self.f)(&self.state);
Some(mem::replace(&mut self.state, next_state))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(usize::MAX, None)
}
}
pub fn iterate<St, F>(initial_value: St, f: F) -> Iterate<St, F>
where
F: FnMut(&St) -> St,
{
Iterate {
state: initial_value,
f,
}
}