Diffstat (limited to 'src/effect.rs')
| -rw-r--r-- | src/effect.rs | 202 |
1 files changed, 100 insertions, 102 deletions
diff --git a/src/effect.rs b/src/effect.rs index 50dab50..092705e 100644 --- a/src/effect.rs +++ b/src/effect.rs @@ -1,4 +1,4 @@ -// pub mod r#async; +pub mod r#async; pub mod blocking; use core::{future::Future, ops::ControlFlow}; @@ -217,7 +217,8 @@ pub use tri; pub trait EffectExt: Effect { fn repeat_map<'ctx, 'wrap, I, U, F>(input: I, f: F) -> ErasedEffective<'wrap, U, Self> where - F: for<'temp> FnMut(&'temp mut I) -> ErasedEffective<'temp, ControlFlow<U>, Self, &'ctx ()>, + F: 'wrap + + for<'temp> FnMut(&'temp mut I) -> ErasedEffective<'temp, ControlFlow<U>, Self, &'ctx ()>, I: Ss + 'wrap, U: Ss, F: Ss, @@ -229,7 +230,7 @@ pub trait EffectExt: Effect { #[inline(always)] fn as_ctx<'ctx, 'wrap, I, U, F>(input: I, f: F) -> ErasedEffective<'wrap, (I, U), Self> where - F: for<'temp> FnOnce(&'temp mut I) -> ErasedEffective<'temp, U, Self, &'ctx ()>, + F: 'wrap + for<'temp> FnOnce(&'temp mut I) -> ErasedEffective<'temp, U, Self, &'ctx ()>, I: Ss + 'wrap, U: Ss, F: Ss, @@ -240,7 +241,7 @@ pub trait EffectExt: Effect { fn as_ctx_map<'ctx, 'wrap, I, U, F>(input: I, f: F) -> ErasedEffective<'wrap, U, Self> where - F: for<'temp> FnOnce(&'temp mut I) -> ErasedEffective<'temp, U, Self, &'ctx ()>, + F: 'wrap + for<'temp> FnOnce(&'temp mut I) -> ErasedEffective<'temp, U, Self, &'ctx ()>, I: Ss + 'wrap, U: Ss, F: Ss, @@ -271,7 +272,7 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { fn map<'wrap, U, F>(self, f: F) -> ErasedEffective<'wrap, U, Self::Effect> where - F: FnOnce(Self::Output) -> U, + F: 'wrap + FnOnce(Self::Output) -> U, U: Ss, F: Ss, 'lt: 'wrap, @@ -288,7 +289,7 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { fn then<'wrap, U, F>(self, f: F) -> ErasedEffective<'wrap, U, Self::Effect> where - F: FnOnce(Self::Output) -> ErasedEffective<'wrap, U, Self::Effect>, + F: 'wrap + FnOnce(Self::Output) -> ErasedEffective<'wrap, U, Self::Effect>, U: Ss, F: Ss, 'lt: 'wrap, @@ -306,7 +307,7 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { fn or_else<'wrap, U, F>(self, f: F) -> ErasedEffective<'wrap, Option<U>, Self::Effect> where Self: Effective<'lt, Output = Option<U>>, - F: FnOnce() -> ErasedEffective<'wrap, Option<U>, Self::Effect>, + F: 'wrap + FnOnce() -> ErasedEffective<'wrap, Option<U>, Self::Effect>, U: Ss, F: Ss, 'lt: 'wrap, @@ -336,9 +337,10 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { f: F, ) -> ErasedEffective<'wrap, (Self::Output, U), Self::Effect> where - F: for<'temp> FnOnce( - &'temp mut Self::Output, - ) -> ErasedEffective<'temp, U, Self::Effect, &'ctx ()>, + F: 'wrap + + for<'temp> FnOnce( + &'temp mut Self::Output, + ) -> ErasedEffective<'temp, U, Self::Effect, &'ctx ()>, U: Ss, F: Ss, 'ctx: 'lt, @@ -358,9 +360,10 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { #[allow(clippy::wrong_self_convention)] fn as_ctx_map<'ctx, 'wrap, U, F>(self, f: F) -> ErasedEffective<'wrap, U, Self::Effect> where - F: for<'temp> FnOnce( - &'temp mut Self::Output, - ) -> ErasedEffective<'temp, U, Self::Effect, &'ctx ()>, + F: 'wrap + + for<'temp> FnOnce( + &'temp mut Self::Output, + ) -> ErasedEffective<'temp, U, Self::Effect, &'ctx ()>, 'ctx: 'lt, U: Ss, F: Ss, @@ -383,9 +386,11 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { ) -> ErasedEffective<'wrap, (Ctx, Option<U>), Self::Effect> where Self: Effective<'lt, Output = (Ctx, Option<U>)>, - F: for<'temp> FnOnce( - &'temp mut Ctx, - ) -> ErasedEffective<'temp, Option<U>, Self::Effect, &'ctx ()>, + F: 'wrap + + for<'temp> FnOnce( + &'temp mut Ctx, + ) + -> ErasedEffective<'temp, Option<U>, Self::Effect, &'ctx ()>, 'ctx: 'lt, Ctx: Ss, U: Ss, @@ -417,9 +422,11 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { ) -> ErasedEffective<'wrap, Option<U>, Self::Effect> where Self: Effective<'lt, Output = (Ctx, Option<U>)>, - F: for<'temp> FnOnce( - &'temp mut Ctx, - ) -> ErasedEffective<'temp, Option<U>, Self::Effect, &'ctx ()>, + F: 'wrap + + for<'temp> FnOnce( + &'temp mut Ctx, + ) + -> ErasedEffective<'temp, Option<U>, Self::Effect, &'ctx ()>, 'ctx: 'lt, Ctx: Ss, U: Ss, @@ -449,10 +456,11 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { mut f: F, ) -> ErasedEffective<'wrap, (Self::Output, U), Self::Effect> where - F: for<'temp> FnMut( - &'temp mut Self::Output, - ) - -> ErasedEffective<'temp, ControlFlow<U>, Self::Effect, &'ctx ()>, + F: 'wrap + + for<'temp> FnMut( + &'temp mut Self::Output, + ) + -> ErasedEffective<'temp, ControlFlow<U>, Self::Effect, &'ctx ()>, 'ctx: 'lt, U: Ss, F: Ss, @@ -462,7 +470,7 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { |ctx| (ctx, ControlFlow::Continue(())), |_, _| <Self::Effect as Effect>::ready(()).cast(), |_, _| ControlFlow::Continue(()), - |ctx, _| f(ctx).cast(), + move |ctx, _| f(ctx).cast(), |_, _, v| v, |ctx, _, v| (ctx, v), ) @@ -470,10 +478,11 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { fn repeat_map<'ctx, 'wrap, U, F>(self, mut f: F) -> ErasedEffective<'wrap, U, Self::Effect> where - F: for<'temp> FnMut( - &'temp mut Self::Output, - ) - -> ErasedEffective<'temp, ControlFlow<U>, Self::Effect, &'ctx ()>, + F: 'wrap + + for<'temp> FnMut( + &'temp mut Self::Output, + ) + -> ErasedEffective<'temp, ControlFlow<U>, Self::Effect, &'ctx ()>, 'ctx: 'lt, U: Ss, F: Ss, @@ -483,7 +492,7 @@ pub trait EffectiveExt<'lt>: Effective<'lt> { |ctx| (ctx, ControlFlow::Continue(())), |_, _| <Self::Effect as Effect>::ready(()).cast(), |_, _| ControlFlow::Continue(()), - |ctx, _| f(ctx).cast(), + move |ctx, _| f(ctx).cast(), |_, _, v| v, |_, _, v| v, ) @@ -537,26 +546,34 @@ pub trait Effective<'lt>: Sized + Ss + 'lt { post: Post, ) -> ErasedEffective<'wrap, Return, Self::Effect> where - Pre: Ss + FnOnce(Self::Output) -> (Ctx, ControlFlow<Done, Owned>), + Pre: Ss + 'wrap + FnOnce(Self::Output) -> (Ctx, ControlFlow<Done, Owned>), First: Ss + + 'wrap + for<'temp> FnOnce( &'temp mut Ctx, Owned, ) -> ErasedEffective<'temp, FirstOutput, Self::Effect, &'wrap ()>, - FirstPost: Ss + for<'temp> FnOnce(&'temp mut Ctx, FirstOutput) -> ControlFlow<Done, Extra>, + FirstPost: + Ss + 'wrap + for<'temp> FnOnce(&'temp mut Ctx, FirstOutput) -> ControlFlow<Done, Extra>, Repeat: Ss + + 'wrap + for<'temp> FnMut( &'temp mut Ctx, &'temp mut Extra, ) -> ErasedEffective<'temp, RepeatOutput, Self::Effect, &'wrap ()>, RepeatPost: Ss + + 'wrap + for<'temp> FnMut(&'temp mut Ctx, &'temp mut Extra, RepeatOutput) -> ControlFlow<Done>, - Post: Ss + FnOnce(Ctx, Option<Extra>, Done) -> Return, + Post: Ss + 'wrap + FnOnce(Ctx, Option<Extra>, Done) -> Return, Return: Ss, RepeatOutput: Ss, FirstOutput: Ss, + Owned: Ss, + Done: Ss, + Ctx: Ss, + Extra: Ss, 'lt: 'wrap; } @@ -579,31 +596,16 @@ where pub trait Join { type Effect: Effect; - type Two<'a, T0: Ss + 'a, T1: Ss + 'a>: Effective< - 'a, - Output = (T0::Output, T1::Output), - Effect = Self::Effect, - > - where - T0: Effective<'a, Effect = Self::Effect>, - T1: Effective<'a, Effect = Self::Effect>; - - type Three<'a, T0: Ss + 'a, T1: Ss + 'a, T2: Ss + 'a>: Effective< - 'a, - Output = (T0::Output, T1::Output, T2::Output), - Effect = Self::Effect, - > - where - T0: Effective<'a, Effect = Self::Effect>, - T1: Effective<'a, Effect = Self::Effect>, - T2: Effective<'a, Effect = Self::Effect>; - - fn two<'a, T0, T1>(effectives: (T0, T1)) -> Self::Two<'a, T0, T1> + fn two<'a, T0, T1>( + effectives: (T0, T1), + ) -> ErasedEffective<'a, (T0::Output, T1::Output), Self::Effect> where T0: Ss + 'a + Effective<'a, Effect = Self::Effect>, T1: Ss + 'a + Effective<'a, Effect = Self::Effect>; - fn three<'a, T0, T1, T2>(effectives: (T0, T1, T2)) -> Self::Three<'a, T0, T1, T2> + fn three<'a, T0, T1, T2>( + effectives: (T0, T1, T2), + ) -> ErasedEffective<'a, (T0::Output, T1::Output, T2::Output), Self::Effect> where T0: Ss + 'a + Effective<'a, Effect = Self::Effect>, T1: Ss + 'a + Effective<'a, Effect = Self::Effect>, @@ -613,33 +615,18 @@ pub trait Join { pub trait TryJoin { type Effect: Effect; - type Two<'a, T0: Ss + 'a, T1: Ss + 'a>: Effective< - 'a, - Output = Result<(T0::Ok, T1::Ok), T0::Err>, - Effect = Self::Effect, - > - where - T0: TryEffective<'a, Effect = Self::Effect>, - T1: TryEffective<'a, Err = T0::Err, Effect = Self::Effect>; - - type Three<'a, T0: Ss + 'a, T1: Ss + 'a, T2: Ss + 'a>: Effective< - 'a, - Output = Result<(T0::Ok, T1::Ok, T2::Ok), T0::Err>, - Effect = Self::Effect, - > - where - T0: TryEffective<'a, Effect = Self::Effect>, - T1: TryEffective<'a, Err = T0::Err, Effect = Self::Effect>, - T2: TryEffective<'a, Err = T0::Err, Effect = Self::Effect>; - - fn two<'a, T0, T1, F0, F1>(cb: (F0, F1)) -> Self::Two<'a, T0, T1> + fn two<'a, T0, T1, F0, F1>( + cb: (F0, F1), + ) -> ErasedEffective<'a, Result<(T0::Ok, T1::Ok), T0::Err>, Self::Effect> where T0: Ss + 'a + TryEffective<'a, Effect = Self::Effect>, T1: Ss + 'a + TryEffective<'a, Err = T0::Err, Effect = Self::Effect>, F0: Ss + 'a + FnOnce() -> T0, F1: Ss + 'a + FnOnce() -> T1; - fn three<'a, T0, T1, T2, F0, F1, F2>(cb: (F0, F1, F2)) -> Self::Three<'a, T0, T1, T2> + fn three<'a, T0, T1, T2, F0, F1, F2>( + cb: (F0, F1, F2), + ) -> ErasedEffective<'a, Result<(T0::Ok, T1::Ok, T2::Ok), T0::Err>, Self::Effect> where T0: Ss + 'a + TryEffective<'a, Effect = Self::Effect>, T1: Ss + 'a + TryEffective<'a, Err = T0::Err, Effect = Self::Effect>, @@ -649,20 +636,20 @@ pub trait TryJoin { F2: Ss + 'a + FnOnce() -> T2; } -pub fn join<'lt, E: Effect, T: Joinable<'lt, E>>(x: T) -> T::Output { +pub fn join<'lt, E: Effect, T: Joinable<'lt, E>>(x: T) -> ErasedEffective<'lt, T::Output, E> { x.join() } pub trait Joinable<'lt, E: Effect> { - type Output: Effective<'lt, Effect = E>; + type Output: Ss; - fn join(self) -> Self::Output; + fn join(self) -> ErasedEffective<'lt, Self::Output, E>; } impl<'lt, E: Effect> Joinable<'lt, E> for () { - type Output = ErasedEffective<'lt, (), E>; + type Output = (); - fn join(self) -> Self::Output { + fn join(self) -> ErasedEffective<'lt, (), E> { E::ready(()) } } @@ -671,10 +658,10 @@ impl<'lt, E: Effect, T0> Joinable<'lt, E> for (T0,) where T0: Effective<'lt, Effect = E>, { - type Output = T0; + type Output = (T0::Output,); - fn join(self) -> Self::Output { - self.0 + fn join(self) -> ErasedEffective<'lt, (T0::Output,), E> { + self.0.map(|x| (x,)) } } @@ -683,9 +670,9 @@ where T0: Effective<'lt, Effect = E>, T1: Effective<'lt, Effect = E>, { - type Output = <E as Join>::Two<'lt, T0, T1>; + type Output = (T0::Output, T1::Output); - fn join(self) -> Self::Output { + fn join(self) -> ErasedEffective<'lt, Self::Output, E> { <E as Join>::two(self) } } @@ -696,29 +683,34 @@ where T1: Effective<'lt, Effect = E>, T2: Effective<'lt, Effect = E>, { - type Output = <E as Join>::Three<'lt, T0, T1, T2>; + type Output = (T0::Output, T1::Output, T2::Output); - fn join(self) -> Self::Output { + fn join(self) -> ErasedEffective<'lt, Self::Output, E> { <E as Join>::three(self) } } -pub fn try_join<'lt, E: Effect, T: TryJoinable<'lt, E>>(x: T) -> T::Output { +pub fn try_join<'lt, E: Effect, T: TryJoinable<'lt, E>>( + x: T, +) -> ErasedEffective<'lt, Result<T::Ok, T::Err>, E> { x.join() } pub trait TryJoinable<'lt, E: Effect> { - type Output: TryEffective<'lt, Effect = E>; + type Ok: Ss; + type Err: Ss; - fn join(self) -> Self::Output; + fn join(self) -> ErasedEffective<'lt, Result<Self::Ok, Self::Err>, E>; } impl<'lt, E: Effect> TryJoinable<'lt, E> for () { - type Output = ErasedEffective<'lt, Result<(), ()>, E>; - - fn join(self) -> Self::Output { + fn join(self) -> ErasedEffective<'lt, Result<Self::Ok, Self::Err>, E> { E::ready(Ok(())) } + + type Ok = (); + + type Err = Never; } impl<'lt, E: Effect, F0, T0> TryJoinable<'lt, E> for (F0,) @@ -726,11 +718,13 @@ where F0: FnOnce() -> T0, T0: TryEffective<'lt, Effect = E>, { - type Output = T0; - - fn join(self) -> Self::Output { - self.0() + fn join(self) -> ErasedEffective<'lt, Result<Self::Ok, Self::Err>, E> { + self.0().map(|x| x.map(|x| (x,))) } + + type Ok = (T0::Ok,); + + type Err = T0::Err; } impl<'lt, E: Effect, F0, F1, T0, T1> TryJoinable<'lt, E> for (F0, F1) @@ -740,11 +734,13 @@ where T0: TryEffective<'lt, Effect = E>, T1: TryEffective<'lt, Err = T0::Err, Effect = E>, { - type Output = <E as TryJoin>::Two<'lt, T0, T1>; - - fn join(self) -> Self::Output { + fn join(self) -> ErasedEffective<'lt, Result<Self::Ok, Self::Err>, E> { <E as TryJoin>::two(self) } + + type Ok = (T0::Ok, T1::Ok); + + type Err = T0::Err; } impl<'lt, E: Effect, F0, F1, F2, T0, T1, T2> TryJoinable<'lt, E> for (F0, F1, F2) @@ -756,9 +752,11 @@ where T1: TryEffective<'lt, Err = T0::Err, Effect = E>, T2: TryEffective<'lt, Err = T0::Err, Effect = E>, { - type Output = <E as TryJoin>::Three<'lt, T0, T1, T2>; - - fn join(self) -> Self::Output { + fn join(self) -> ErasedEffective<'lt, Result<Self::Ok, Self::Err>, E> { <E as TryJoin>::three(self) } + + type Ok = (T0::Ok, T1::Ok, T2::Ok); + + type Err = T0::Err; } |