Diffstat (limited to 'src/effect/blocking.rs')
| -rw-r--r-- | src/effect/blocking.rs | 151 |
1 files changed, 72 insertions, 79 deletions
diff --git a/src/effect/blocking.rs b/src/effect/blocking.rs index 11c3b7d..c504445 100644 --- a/src/effect/blocking.rs +++ b/src/effect/blocking.rs @@ -35,17 +35,10 @@ impl<T: Ss, B: BlockOn> Erased::Hkt<T, Blocking<B>> for Value<T, B> { impl<B: BlockOn> Effect for Blocking<B> { type Erased<T: Ss> = Value<T, B>; - // type Ready<'a, T: Ss + 'a> = Value<T, B>; - fn ready<'a, T: Ss + 'a>(value: T) -> ErasedEffective<'a, T, Self> { Value(value, Default::default()) } - // type FromFuture<'a, F: Ss + 'a> = Value<F::Output, B> - // where - // F: Future, - // F::Output: Ss + 'a; - fn from_future<'a, F: Ss + 'a>(future: F) -> ErasedEffective<'a, F::Output, Self> where F: Future, @@ -56,7 +49,7 @@ impl<B: BlockOn> Effect for Blocking<B> { } impl<'lt, U: Ss + 'lt, B: BlockOn> Effective<'lt> for Value<U, B> { - fn into_erased<X>(self) -> ErasedEffective<'lt, Self::Output, Self::Effect, X> { + fn cast<'wrap, X>(self) -> ErasedEffective<'wrap, Self::Output, Self::Effect, X> { self } @@ -70,74 +63,74 @@ impl<'lt, U: Ss + 'lt, B: BlockOn> Effective<'lt> for Value<U, B> { core::future::ready(self.0) } - // type Loop<'ctx: 'a, 'a, T: Ss + 'a, F: Ss + 'a> - // = Value<(U, T), B> - // where - // F: for<'b> FnMut(&'b mut Self::Output) -> ErasedEffective<'b, ControlFlow<T>, Self::Effect>, - // 'lt: 'a; - - fn r#loop<'ctx: 'lt, 'wrap, T: Ss, F: Ss>( + #[inline(always)] + fn r#do< + 'ctx: 'lt, + 'wrap, + Pre: Ss, + Ctx: Ss, + Owned: Ss, + First: Ss, + FirstOutput: Ss, + FirstPost: Ss, + Done: Ss, + Extra: Ss, + Repeat: Ss, + RepeatOutput: Ss, + RepeatPost: Ss, + Post: Ss, + Return: Ss, + >( self, - mut cb: F, - ) -> ErasedEffective<'wrap, (Self::Output, T), Self::Effect> + pre: Pre, + first: First, + first_post: FirstPost, + mut repeat: Repeat, + mut repeat_post: RepeatPost, + post: Post, + ) -> ErasedEffective<'wrap, Return, Self::Effect> where - F: for<'b> FnMut(&'b mut Self::Output) -> ErasedEffective<'b, ControlFlow<T>, Self::Effect>, - { - let mut this = self.0; - - loop { - if let ControlFlow::Break(value) = cb(&mut this).0 { - return Value((this, value), Default::default()); - } - } - } - - // type Map<'a, T: Ss + 'a, F: Ss + 'a> = Value<T, B> - // where - // F: FnOnce(Self::Output) -> T, - // 'lt: 'a; - - fn map<'wrap, T: Ss, F: Ss>(self, cb: F) -> ErasedEffective<'wrap, T, Self::Effect> - where - F: FnOnce(Self::Output) -> T, + Pre: FnOnce(Self::Output) -> (Ctx, ControlFlow<Done, Owned>), + First: for<'b> FnOnce( + &'b mut Ctx, + Owned, + ) -> ErasedEffective<'b, FirstOutput, Self::Effect, &'wrap ()>, + FirstPost: for<'b> FnOnce(&'b mut Ctx, FirstOutput) -> ControlFlow<Done, Extra>, + Repeat: for<'b> FnMut( + &'b mut Ctx, + &'b mut Extra, + ) + -> ErasedEffective<'b, RepeatOutput, Self::Effect, &'wrap ()>, + RepeatPost: for<'b> FnMut(&'b mut Ctx, &'b mut Extra, RepeatOutput) -> ControlFlow<Done>, + Post: FnOnce(Ctx, Option<Extra>, Done) -> Return, 'lt: 'wrap, { - Value(cb(self.0), Default::default()) - } - - // type Then<'a, T: Ss + 'a, V: Ss + 'a, F: Ss + 'a> = Value<T, B> - // where - // F: FnOnce(Self::Output) -> V, - // V: Effective<'a, Output = T, Effect = Self::Effect>, - // 'lt: 'a; + let (ctx, done, extra) = match pre(self.0) { + (mut ctx, ControlFlow::Continue(owned)) => { + let first_output = first(&mut ctx, owned).0; + + let (done, extra) = match first_post(&mut ctx, first_output) { + ControlFlow::Continue(mut extra) => loop { + let repeat_output = repeat(&mut ctx, &mut extra).0; + + match repeat_post(&mut ctx, &mut extra, repeat_output) { + ControlFlow::Continue(()) => {} + ControlFlow::Break(done) => break (done, Some(extra)), + } + }, + ControlFlow::Break(done) => (done, None), + }; + + (ctx, done, extra) + } + (ctx, ControlFlow::Break(done)) => (ctx, done, None), + }; - fn then<'a, T: Ss, F: Ss>(self, cb: F) -> ErasedEffective<'a, T, Self::Effect> - where - F: FnOnce(Self::Output) -> ErasedEffective<'a, T, Self::Effect>, - 'lt: 'a, - { - cb(self.0).into_erased::<()>() + Value(post(ctx, extra, done), Default::default()) } - // type AsCtx<'ctx: 'a, 'a, T: Ss + 'a, F: Ss + 'a> = Value<(U, T), B> - // where - // F: for<'b> FnOnce(&'b mut Self::Output) -> ErasedEffective<'b, T, Self::Effect, (&'b mut Self::Output, &'ctx ())>, - // 'lt: 'a; - - fn as_ctx<'ctx: 'lt, 'wrap, T: Ss, S: Ss, C: Ss, K: Ss, F: Ss, A: Ss, R: Ss>( - self, - split: S, - cb: F, - after: A, - ) -> ErasedEffective<'wrap, R, Self::Effect> - where - S: FnOnce(Self::Output) -> (C, K), - F: for<'b> FnOnce(&'b mut C, K) -> ErasedEffective<'b, T, Self::Effect>, - A: FnOnce(C, T) -> R, - { - let (mut this, owned) = split(self.0); - let result = cb(&mut this, owned).0; - Value(after(this, result), Default::default()) + fn ready(value: Self::Output) -> Self { + Value(value, Default::default()) } } @@ -160,8 +153,8 @@ impl<B: BlockOn> Join for Blocking<B> { T0: Effective<'a, Effect = Self::Effect>, T1: Effective<'a, Effect = Self::Effect>, { - let v0 = cb.0.into_erased::<()>().0; - let v1 = cb.1.into_erased::<()>().0; + let v0 = cb.0.cast::<()>().0; + let v1 = cb.1.cast::<()>().0; Value((v0, v1), Default::default()) } @@ -174,9 +167,9 @@ impl<B: BlockOn> Join for Blocking<B> { T1: Effective<'a, Effect = Self::Effect>, T2: Effective<'a, Effect = Self::Effect>, { - let v0 = cb.0.into_erased::<()>().0; - let v1 = cb.1.into_erased::<()>().0; - let v2 = cb.2.into_erased::<()>().0; + let v0 = cb.0.cast::<()>().0; + let v1 = cb.1.cast::<()>().0; + let v2 = cb.2.cast::<()>().0; Value((v0, v1, v2), Default::default()) } @@ -205,12 +198,12 @@ impl<B: BlockOn> TryJoin for Blocking<B> { F0: FnOnce() -> T0, F1: FnOnce() -> T1, { - let v0 = match (cb.0)().into_erased::<()>().0 { + let v0 = match (cb.0)().cast::<()>().0 { Ok(v) => v, Err(err) => return Value(Err(err), Default::default()), }; - let v1 = match (cb.1)().into_erased::<()>().0 { + let v1 = match (cb.1)().cast::<()>().0 { Ok(v) => v, Err(err) => return Value(Err(err), Default::default()), }; @@ -229,17 +222,17 @@ impl<B: BlockOn> TryJoin for Blocking<B> { F1: FnOnce() -> T1, F2: FnOnce() -> T2, { - let v0 = match (cb.0)().into_erased::<()>().0 { + let v0 = match (cb.0)().cast::<()>().0 { Ok(v) => v, Err(err) => return Value(Err(err), Default::default()), }; - let v1 = match (cb.1)().into_erased::<()>().0 { + let v1 = match (cb.1)().cast::<()>().0 { Ok(v) => v, Err(err) => return Value(Err(err), Default::default()), }; - let v2 = match (cb.2)().into_erased::<()>().0 { + let v2 = match (cb.2)().cast::<()>().0 { Ok(v) => v, Err(err) => return Value(Err(err), Default::default()), }; |