pub struct RepeatCall<F> {
f: F,
}
impl<F> RepeatCall<F>
{
pub fn new<A>(func: F) -> Self where
F: FnMut() -> A,
{
RepeatCall { f: func }
}
}
impl<A, F> Iterator for RepeatCall<F> where
F: FnMut() -> A,
{
type Item = A;
#[inline]
fn next(&mut self) -> Option<A>
{
Some((self.f)())
}
fn size_hint(&self) -> (usize, Option<usize>)
{
(usize::max_value(), None)
}
}
impl<A, F> DoubleEndedIterator for RepeatCall<F> where
F: FnMut() -> A,
{
#[inline]
fn next_back(&mut self) -> Option<A> { self.next() }
}
#[derive(Clone)]
pub struct Unfold<St, F> {
f: F,
pub state: St,
}
impl<A, St, F> Unfold<St, F>
where F: FnMut(&mut St) -> Option<A>
{
#[inline]
pub fn new(initial_state: St, f: F) -> Unfold<St, F> {
Unfold {
f: f,
state: initial_state
}
}
}
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<A> {
(self.f)(&mut self.state)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}