Skip to content

Commit fd1d154

Browse files
committed
Revert "Pull - Cut ties with BindBind, no need for the BuildR"
This reverts commit f63dd03.
1 parent 389aea8 commit fd1d154

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

core/shared/src/main/scala/fs2/Pull.scala

+30-17
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,9 @@ object Pull extends PullLowPriority {
654654
def cont(r: Terminal[Unit]): Pull[Pure, INothing, Unit] = r
655655
}
656656

657-
private abstract class Bind[+F[_], +O, X, +R](misstep: Pull[F, O, X])
657+
private abstract class Bind[+F[_], +O, X, +R](val step: Pull[F, O, X])
658658
extends Pull[F, O, R]
659659
with ContP[X, F, O, R] {
660-
def step: Pull[F, O, X] = misstep
661660
def cont(r: Terminal[X]): Pull[F, O, R]
662661
def delegate: Bind[F, O, X, R] = this
663662
}
@@ -694,12 +693,12 @@ object Pull extends PullLowPriority {
694693

695694
// This class is not created by combinators in public Pull API, only during compilation
696695
private class BindBind[F[_], O, X, Y](
697-
var innerBind: Bind[F, O, X, Y],
698-
var endBind: Bind[F, O, Y, Unit]
699-
) extends Bind[F, O, X, Unit](null) {
700-
override def step: Pull[F, O, X] = innerBind.step
701-
def cont(xterm: Terminal[X]): Pull[F, O, Unit] =
702-
try bindBindAux(xterm, this)
696+
step: Pull[F, O, X],
697+
val bb: Bind[F, O, X, Y],
698+
val del: Bind[F, O, Y, Unit]
699+
) extends Bind[F, O, X, Unit](step) {
700+
def cont(tx: Terminal[X]): Pull[F, O, Unit] =
701+
try bindBindAux(bb.cont(tx), del)
703702
catch { case NonFatal(e) => Fail(e) }
704703
}
705704

@@ -712,12 +711,7 @@ object Pull extends PullLowPriority {
712711
case ty: Terminal[_] =>
713712
del match {
714713
case cici: BindBind[F, O, r, Y] =>
715-
val innerBind = cici.innerBind
716-
val endBind = cici.endBind
717-
cici.innerBind = null
718-
cici.endBind = null
719-
val nextStep = innerBind.cont(ty)
720-
bindBindAux[F, O, r, Y](nextStep, endBind)
714+
bindBindAux[F, O, r, Y](cici.bb.cont(ty), cici.del)
721715
case _ => del.cont(ty)
722716
}
723717
case x => new DelegateBind(x, del)
@@ -871,7 +865,7 @@ object Pull extends PullLowPriority {
871865
case b: Bind[G, X, y, Unit] =>
872866
b.step match {
873867
case c: Bind[G, X, x, _] =>
874-
viewL(new BindBind[G, X, x, y](c, b.delegate))
868+
viewL(new BindBind[G, X, x, y](c.step, c.delegate, b.delegate))
875869
case e: Action[G, X, y2] =>
876870
contP = b.delegate
877871
e
@@ -910,6 +904,21 @@ object Pull extends PullLowPriority {
910904
def interrupted(inter: Interrupted): End
911905
def fail(e: Throwable): End
912906
}
907+
type CallRun[+G[_], +X, End] = Run[G, X, End] => End
908+
909+
object TheBuildR extends Run[Pure, INothing, F[CallRun[Pure, Nothing, F[INothing]]]] {
910+
type TheRun = Run[Pure, INothing, F[INothing]]
911+
def fail(e: Throwable) = F.raiseError(e)
912+
def done(scope: Scope[F]) =
913+
F.pure((cont: TheRun) => cont.done(scope))
914+
def out(head: Chunk[INothing], scope: Scope[F], tail: Pull[Pure, INothing, Unit]) =
915+
F.pure((cont: TheRun) => cont.out(head, scope, tail))
916+
def interrupted(i: Interrupted) =
917+
F.pure((cont: TheRun) => cont.interrupted(i))
918+
}
919+
920+
def buildR[G[_], X, End]: Run[G, X, F[CallRun[G, X, F[End]]]] =
921+
TheBuildR.asInstanceOf[Run[G, X, F[CallRun[G, X, F[End]]]]]
913922

914923
def go[G[_], X, End](
915924
scope: Scope[F],
@@ -1205,13 +1214,17 @@ object Pull extends PullLowPriority {
12051214
case u: Uncons[G, y] @unchecked =>
12061215
val v = getCont[Option[(Chunk[y], Pull[G, y, Unit])], G, X]
12071216
// a Uncons is run on the same scope, without shifting.
1208-
F.unit >> go(scope, extendedTopLevelScope, translation, new UnconsRunR(v), u.stream)
1217+
val runr = buildR[G, y, End]
1218+
F.unit >> go(scope, extendedTopLevelScope, translation, runr, u.stream).attempt
1219+
.flatMap(_.fold(goErr(_, v), _.apply(new UnconsRunR(v))))
12091220

12101221
case s: StepLeg[G, y] @unchecked =>
12111222
val v = getCont[Option[Stream.StepLeg[G, y]], G, X]
1223+
val runr = buildR[G, y, End]
12121224
scope
12131225
.shiftScope(s.scope, s.toString)
1214-
.flatMap(go(_, extendedTopLevelScope, translation, new StepLegRunR(v), s.stream))
1226+
.flatMap(go(_, extendedTopLevelScope, translation, runr, s.stream).attempt)
1227+
.flatMap(_.fold(goErr(_, v), _.apply(new StepLegRunR(v))))
12151228

12161229
case _: GetScope[_] =>
12171230
go(scope, extendedTopLevelScope, translation, runner, getCont(Succeeded(scope)))

0 commit comments

Comments
 (0)