use core::fmt;
use crate::{Covar, Lend, Lender, Lending, higher_order::FnMutHKAOpt};
#[inline]
pub fn from_fn<St, F>(state: St, f: Covar<F>) -> FromFn<St, F>
where
F: for<'all> FnMutHKAOpt<'all, &'all mut St>,
{
FromFn { state, f }
}
#[derive(Clone)]
#[must_use = "lenders are lazy and do nothing unless consumed"]
pub struct FromFn<St, F> {
state: St,
f: Covar<F>,
}
impl<St: fmt::Debug, F> fmt::Debug for FromFn<St, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FromFn")
.field("state", &self.state)
.finish_non_exhaustive()
}
}
impl<'lend, St, F> Lending<'lend> for FromFn<St, F>
where
F: for<'all> FnMutHKAOpt<'all, &'all mut St>,
{
type Lend = <F as FnMutHKAOpt<'lend, &'lend mut St>>::B;
}
impl<St, F> Lender for FromFn<St, F>
where
F: for<'all> FnMutHKAOpt<'all, &'all mut St>,
{
crate::unsafe_assume_covariance!();
#[inline]
fn next(&mut self) -> Option<Lend<'_, Self>> {
(self.f.as_inner_mut())(&mut self.state)
}
}