use std::marker::PhantomData; use super::*; pub struct ControlFlowClass(ControlFlow<(), C>); impl WeakFunctor for ControlFlowClass { type F<'a, A: 'a> = ControlFlow where Self: 'a; } impl Functor for ControlFlowClass { fn fmap<'a, A: 'a, B: 'a>(f: impl 'a + FnOnce(A) -> B, fa: Self::F<'a, A>) -> Self::F<'a, B> where Self: 'a, { match fa { ControlFlow::Continue(c) => ControlFlow::Continue(c), ControlFlow::Break(a) => ControlFlow::Break(f(a)), } } } impl Pure for ControlFlowClass { fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> where Self: 'a, { ControlFlow::Break(a) } } /// Next [Iterative] state, wrapped. pub type IterativeWrapped<'a, G> = <>::T as WeakFunctor>::F< 'a, ControlFlow<>::B, (>::A, G)>, >; /// Value passed to [`Monad::iibind`]. pub trait Iterative<'a>: 'a + Sized { /// [`ControlFlow::Continue`] type A: 'a; /// [`ControlFlow::Break`] type B: 'a; /// Corresponding [`WeakFunctor`]. type T: 'a + ?Sized + WeakFunctor; /// Get next state. fn iterate(self, a: Self::A) -> IterativeWrapped<'a, Self>; } pub struct BindableMut(F, PhantomData, PhantomData, PhantomData); impl< 'a, T: 'a + ?Sized + Functor, A: 'a, B: 'a, F: 'a + FnMut(A) -> T::F<'a, ControlFlow>, > BindableMut { pub fn new(f: F) -> Self { BindableMut(f, PhantomData, PhantomData, PhantomData) } } impl< 'a, T: 'a + ?Sized + Functor, A: 'a, B: 'a, F: 'a + FnMut(A) -> T::F<'a, ControlFlow>, > Iterative<'a> for BindableMut { type A = A; type B = B; type T = T; fn iterate(mut self, a: Self::A) -> IterativeWrapped<'a, Self> { let fa = self.0(a); T::fmap( move |state| match state { ControlFlow::Continue(next_a) => ControlFlow::Continue((next_a, self)), ControlFlow::Break(b) => ControlFlow::Break(b), }, fa, ) } }