forked from typelevel/fs2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPull.scala
1385 lines (1228 loc) · 56.9 KB
/
Pull.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Functional Streams for Scala
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package fs2
import scala.annotation.{nowarn, tailrec}
import scala.concurrent.duration.FiniteDuration
import scala.util.control.NonFatal
import cats.{Eval => _, _}
import cats.effect.kernel._
import cats.syntax.all._
import fs2.internal._
import Resource.ExitCase
import Pull._
/** A purely functional data structure that describes a process. This process
* may evaluate actions in an effect type F, emit any number of output values
* of type O (or None), and may a) terminate with a single result of type R;
* or b) terminate abnormally by raising (inside the effect `F`) an exception,
* or c) terminate because it was cancelled by another process,
* or d) not terminate.
*
* Like types from other effect libraries, pulls are pure and immutable values.
* They preserve referential transparency.
*
* === Chunking ===
*
* The output values of a pull are emitted not one by one, but in chunks.
* A `Chunk` is an immutable sequence with constant-time indexed lookup. For example,
* a pull `p: Pull[F, Byte, R]` internally operates and emits `Chunk[Byte]`
* values, which can wrap unboxed byte arrays -- avoiding boxing/unboxing costs.
* The `Pull` API provides mechanisms for working at both the chunk level and
* the individual element level. Generally, working at the chunk level will
* result in better performance but at the cost of more complex implementations
*
* A pull only emits non-empty chunks.
*
* However, chunks are not merely an operational matter of efficiency. Each
* pull is emitted from a chunk atomically, which is to say, any errors or
* interruptions in a pull can only happen between chunks, not within a
* chunk. For instance, if creating a new chunk of values fails (raises an
* uncaught exception) while creating an intermediate value, then it fails
* to create the entire chunk and previous values are discarded.
*
* === Evaluation ===
*
* Like other functional effect types (e.g. `cats.effect.IO`), a pull
* describes a _process_ or _computation_. It is not a running process nor a
* handle for the result of a spawned, running process, like `scala.concurrent.Future`.
*
* A pull can be converted to a stream and then compiled to an effectful value.
* For a `Pull[F, O, Unit]`, the result of compilation is a combination, via the
* monad instance of `F`, of all the actions in the effect `F` present in the pull.
* The result of that `F` action is the result of combining the outputs emitted by
* the pull, in the order it emits them, using a _fold_ function. Depending on that
* function, outputs may be collected into a list (or vector or array or ...),
* combined together into a single value, or just discarded altogether (by _draining_
* the pull).
*
* Compilation is pull-based, rather than push-based (hence the name of the datatype).
* It is the compilation process itself, that determines when the evaluation
* of each single effect can proceed or is held back. Effects and outputs later
* in the pull are not performed or emitted, _unless and until_ the compilation
* calls for them.
*
* === Resource scoping ===
*
* The effects in a `Pull` may operate on resources, which must be retained during
* the execution of the pull, may be shared by several pulls, and must be
* properly finalised when no longer needed, regardless of whether the pull completed
* successfully or not. A pull tracks its resources using '''scopes''', which register
* how many pulls are actively using each resource, and finalises resources when no
* longer used.
*
* Some operations of the `Pull` API can be used to introduce new resource scopes,
* or resource boundaries.
*
* === Functional typeclasses ===
*
* The `Pull` data structure is a "free" implementation of `Monad` and has an instance
* for `cats.effect.kernel.Sync`.
*
* For any types `F[_]` and `O`, a `Pull[F, O, *]` holds the following laws:
*
* - `pure >=> f == f`
* - `f >=> pure == f`
* - `(f >=> g) >=> h == f >=> (g >=> h)`
* where `f >=> g` is defined as `a => a flatMap f flatMap g`
* - `handleErrorWith(raiseError(e))(f) == f(e)`
*
* @tparam F[_] the type of effect that can be performed by this pull.
* An effect type of `Nothing`, also known in `fs2` by the alias `Pure`,
* indicates that this pull perform no effectful actions.
* _Note_: `Nothing` is a polykinded type, so it can also be
* applied as an argument to the type parameter `F[_]`.
* @tparam O The outputs emitted by this Pull. An output type of `Nothing` means
* that this pull does not emit any outputs.
* @tparam R The type of result returned by this Pull upon successful termination.
* An output type of `Nothing` indicates that this pull cannot terminate
* successfully: it may fail, be cancelled, or never terminate.
*/
sealed abstract class Pull[+F[_], +O, +R] {
/** Applies the result of this pull to `f` and returns the result.
*
* This method returns a new composed pull, which will do as follows:
*
* - If `this` pull succeeds with a result `r` of type R, the `f` function
* is applied to `r`, to build a new pull `f(r)`, and the result pull
* starts running that new pull. The composed pull will terminate (or not)
* just as the new pull `f(r)` does.
* - If `this` pull fails or is interrupted, then the composed pull
* terminates with that same failure or interruption.
* - If evaluating `f(r)` to build the pull throws an exception, the result
* is a pull that fails with that exception.
*
* The composed pull emits all outputs emitted by `this` pull,
* and if successful will start emitting the outputs from the generated pull.
*
* This operation does not modify resource scope boundaries. The generated
* `post` pull starts running on the same scope in which `this` pull ended,
* and the composed pull will end on the same scope in which `post` pull does.
*/
def flatMap[F2[x] >: F[x], O2 >: O, R2](f: R => Pull[F2, O2, R2]): Pull[F2, O2, R2] =
new Bind[F2, O2, R, R2](this) {
def cont(e: Terminal[R]): Pull[F2, O2, R2] =
e match {
case Succeeded(r) =>
try f(r)
catch { case NonFatal(e) => Fail(e) }
case res @ Interrupted(_, _) => res
case res @ Fail(_) => res
}
}
/** Lazily appends the given `post` pull, to be run after `this` pull.
*
* - If `this` pull succeeds, then its result is discarded, the `post`
* pull is built, and starts running. The result of `post` is
* the result of the composed pull.
*
* - If `this` pull raises an error or is interrupted, the `post` argument is
* not evaluated and the composed pull ends just as `this` pull did.
*
* In both cases, the effectful actions and outputs of the appended pull
* consists of those outputs and actions from the first pull, followed by
* those from the `post` pull, in the same order as they would come out of each pull.
*
* Since the `post` argument is lazy, this method can be used to build lazy
* pulls, which are not built in memory until after the prefix has run.
* This allows defining pulls recursively.
*
* This operation does not add or remove any resource scope boundaries.
* The `post` pull runs on the same scope in which `this` pull ended.
* The composed pull ends on whatever scope the `post` pull does.
*
* This is equivalent to `.flatMap(_ => post)`
*/
def >>[F2[x] >: F[x], O2 >: O, S](post: => Pull[F2, O2, S]): Pull[F2, O2, S] =
new Bind[F2, O2, R, S](this) {
def cont(r: Terminal[R]): Pull[F2, O2, S] =
r match {
case _: Succeeded[?] => post
case r: Interrupted => r
case r: Fail => r
}
}
/** Alias for `flatMap(r => Pull.eval(f(r)))`. */
def evalMap[F2[x] >: F[x], R2](f: R => F2[R2]): Pull[F2, O, R2] =
flatMap(r => Pull.eval(f(r)))
/** Allows to recover from any error raised by the evaluation of this pull.
* This method returns a composed pull with the following semantics:
* - If an error occurs, the supplied function is used to build a new handler
* pull, and it starts running it. However, the pull cannot be resumed from
* the point at which the error arose.
* - If no error is raised, the resulting pull just does what `this` pull does.
*/
def handleErrorWith[F2[x] >: F[x], O2 >: O, R2 >: R](
handler: Throwable => Pull[F2, O2, R2]
): Pull[F2, O2, R2] =
new Bind[F2, O2, R2, R2](this) {
def cont(term: Terminal[R2]): Pull[F2, O2, R2] =
term match {
case Fail(e) =>
try handler(e)
catch { case NonFatal(e) => Fail(e) }
case other => other
}
}
/** Run `post` after `this`, regardless of errors during `this`:
*
* - If `this` pull terminates successfully, then its result is discarded
* and the `post` pull is run. However the `post` pull ends, be it in
* success, error, interruption, is how the combined pull ends.
*
* - If `this` pull fails, the `post` pull is run next. If the `post` pull
* ends, fails, or is interrupted, that is how the combined pull ends.
* However, if the `post` pull succeeds, then the combined `onComplete` pull
* fails again with the error that was raised from `this` pull.
*
* - If `this` pull is interrupted, then the `post` pull is never run
* and the combined pull ends with that same interruption.
*/
def onComplete[F2[x] >: F[x], O2 >: O, R2](post: => Pull[F2, O2, R2]): Pull[F2, O2, R2] =
handleErrorWith(e => post >> Fail(e)) >> post
/** Short-hand for `(this: Pull[F2, P, S])`. Used to assist with type inference. */
def covaryAll[F2[x] >: F[x], O2 >: O, R2 >: R]: Pull[F2, O2, R2] = this
/** Short-hand for `(this: Pull[F2, O, R])`. Used to assist with type inference. */
def covary[F2[x] >: F[x]]: Pull[F2, O, R] = this
/** Short-hand for `(this: Pull[F, O2, R])`. Used to assist with type inference. */
def covaryOutput[O2 >: O]: Pull[F, O2, R] = this
/** Short-hand for `(this: Pull[F, O, R2])`. Used to assist with type inference. */
def covaryResult[R2 >: R]: Pull[F, O, R2] = this
/** Returns a pull with the result wrapped in `Right`,
* or an error wrapped in `Left` if the pull has raised an error.
* If `this` pull is interrupted, the attempted pull ends the same way.
*/
def attempt: Pull[F, O, Either[Throwable, R]] =
map(r => Right(r)).handleErrorWith(t => Succeeded(Left(t)))
/** Maps the result of this pull with the `f` mapping function.
*
* If `this` pull ends in success with a result `r`, then the function `f`
* is applied to its result `r`, and the image `f(r)` is the result of the
* mapped pull. However, if the evaluation of `f(r)` throws an error, the
* mapped pull fails with that error.
*
* Note: for some simple cases of Pull, the `map` function may be eagerly
* applied, or discarded, _before_ the pull starts being run.
*
* If `this` pull terminates abnormally, so does the mapped pull.
*/
def map[S](f: R => S): Pull[F, O, S] =
new Bind[F, O, R, S](this) {
def cont(r: Terminal[R]) = r.map(f)
}
/** Discards the result of this pull.
*
* If `this` pull ends in success, its result is discarded and the _voided_
* pull returns the unit `()` value. Otherwise, the voided pull just does
* the same as `this` pull does.
*
* Alias for `this.map(_ => ())`.
*/
def void: Pull[F, O, Unit] = as(())
/** Replaces the result of this pull with the given constant value.
* If `this` pull succeeds, then its result is discarded and the resulting
* pull succeeds with the `s` value as its result.
* Otherwise, if `this` pull fails or is interrupted, then the result pull
* ends the same way.
*
* Alias for `_.map(_ => o2)`.
*
* @tparam S The type of the constant,
* @param s The new result value of the pull
*/
def as[S](s: S): Pull[F, O, S] = map(_ => s)
/** Leases all resources that are currently open, canceling the lease at the
* termination of this pull.
*/
def lease: Pull[F, O, R] =
Pull.bracketCase[F, O, Lease[F], R](
Pull.getScope[F].evalMap(_.lease),
_ => this,
(l, _) => Pull.eval(l.cancel).rethrow
)
}
object Pull extends PullLowPriority {
implicit final class StreamPullOps[F[_], O](private val self: Pull[F, O, Unit]) extends AnyVal {
/** Interprets this pull to produce a stream. This method introduces a resource
* scope, to ensure any resources acquired by this pull are released in due
* course, even if the resulting stream does not terminate successfully.
*
* May only be called on pulls which return a `Unit` result type. Use
* `p.void.stream` to explicitly ignore the result type of the pull.
*/
def stream: Stream[F, O] = new Stream(Pull.scope(self))
/** Interpret this `Pull` to produce a `Stream` without introducing a scope.
*
* Only use this if you know a scope is not needed. Scope introduction is
*
* generally harmless and the risk of not introducing a scope is a memory leak
* in streams that otherwise would execute in constant memory.
*
* May only be called on pulls whose result type is `Unit`.
* Use `p.void.stream` to explicitly ignore the result of a pull.
*/
def streamNoScope: Stream[F, O] = new Stream(self)
private[fs2] def flatMapOutput[F2[x] >: F[x], O2](
f: O => Pull[F2, O2, Unit]
): Pull[F2, O2, Unit] =
self match {
case a: AlgEffect[F, Unit] => a
case r: Terminal[?] => r
case _ => FlatMapOutput(self, f)
}
private[fs2] def unconsFlatMap[F2[x] >: F[x], O2](
f: Chunk[O] => Pull[F2, O2, Unit]
): Pull[F2, O2, Unit] =
uncons.flatMap {
case None => Pull.done
case Some((hd, tl)) => f(hd) >> tl.unconsFlatMap(f)
}
/* Pull transformation that takes the given stream (pull), unrolls it until it either:
* - Reaches the end of the stream, and returns None; or
* - Reaches an Output action, and emits Some pair with
* the non-empty chunk of values and the rest of the stream.
*/
private[fs2] def uncons: Pull[F, Nothing, Option[(Chunk[O], Pull[F, O, Unit])]] =
self match {
case Succeeded(_) => Succeeded(None)
case Output(vals) => Succeeded(Some(vals -> unit))
case ff: Fail => ff
case it: Interrupted => it
case _ => Uncons(self)
}
}
private[this] val unit: Terminal[Unit] = Succeeded(())
private[fs2] val outUnit: Pull[Pure, Unit, Unit] = Output(Chunk.unit)
/** A pull that performs no effects, emits no outputs, and
* always terminates successfully with a unit result.
*/
val done: Pull[Nothing, Nothing, Unit] = unit
/** Creates an pull that performs no effects, emits no outputs,
* and terminates successfully with the supplied value as its result.
*/
def pure[F[_], R](r: R): Pull[F, Nothing, R] = Succeeded(r)
/** Lifts a throwable error into an atomic pull that emits no outputs and
* fails with the given error, without any result.
*
* The `F` type must be explicitly provided (e.g., via `raiseError[IO]`
* or `raiseError[Fallible]`).
*/
def raiseError[F[_]: RaiseThrowable](err: Throwable): Pull[F, Nothing, Nothing] = Fail(err)
/** Creates a pull that evaluates the supplied effect `fr`, emits no
* outputs, and terminates with the result of the effect.
* If the `fr` effect fails with an error, the new pull fails with that error.
*/
def eval[F[_], R](fr: F[R]): Pull[F, Nothing, R] = Eval[F, R](fr)
/** Creates a pull that waits for the duration `d` */
def sleep[F[_]](d: FiniteDuration)(implicit t: Temporal[F]): Pull[F, Nothing, Unit] =
Pull.eval(t.sleep(d))
/** Lifts the given output value `O` into a pull that performs no
* effects, emits that single output in a singleton chunk, and always
* terminates successfully with a unit result.
*
* _Note_: using singleton chunks is not efficient. If possible,
* use the chunk-based `output` method instead.
*/
def output1[F[_], O](o: O): Pull[F, O, Unit] = Output(Chunk.singleton(o))
/** Lifts the given optional value `O` into a pull that performs no
* effects, emits the content of that option, and always
* terminates successfully with a unit result.
*/
def outputOption1[F[_], O](opt: Option[O]): Pull[F, O, Unit] =
opt.map(output1).getOrElse(done)
/** Creates a pull that emits the elements of the given chunk.
* The new pull performs no effects and terminates successfully with a unit result.
*/
def output[F[_], O](os: Chunk[O]): Pull[F, O, Unit] =
if (os.isEmpty) Pull.done else Output[O](os)
private[fs2] def acquire[F[_], R](
resource: F[R],
release: (R, ExitCase) => F[Unit]
): Pull[F, Nothing, R] =
Acquire(resource, release, cancelable = false)
private[fs2] def acquireCancelable[F[_], R](
resource: Poll[F] => F[R],
release: (R, ExitCase) => F[Unit]
)(implicit F: MonadCancel[F, ?]): Pull[F, Nothing, R] =
Acquire(F.uncancelable(resource), release, cancelable = true)
/** Like [[eval]] but if the effectful value fails, the exception is returned
* in a `Left` instead of failing the pull.
*/
def attemptEval[F[_], R](fr: F[R]): Pull[F, Nothing, Either[Throwable, R]] =
Eval[F, R](fr)
.map(r => Right(r): Either[Throwable, R])
.handleErrorWith(t => Succeeded[Either[Throwable, R]](Left(t)))
def bracketCase[F[_], O, A, B](
acquire: Pull[F, O, A],
use: A => Pull[F, O, B],
release: (A, ExitCase) => Pull[F, O, Unit]
): Pull[F, O, B] =
acquire.flatMap { a =>
val used =
try use(a)
catch { case NonFatal(t) => Fail(t) }
transformWith(used) { result =>
val exitCase: ExitCase = result match {
case Succeeded(_) => ExitCase.Succeeded
case Fail(err) => ExitCase.Errored(err)
case Interrupted(_, _) => ExitCase.Canceled
}
transformWith(release(a, exitCase)) {
case Fail(t2) =>
result match {
case Fail(tres) => Fail(CompositeFailure(tres, t2))
case result => result
}
case _ => result
}
}
}
/** Extends the scope of the currently open resources to the specified stream,
* preventing them from being finalized until after `s` completes execution,
* even if the returned pull is converted to a stream, compiled, and
* evaluated before `s` is compiled and evaluated.
*/
def extendScopeTo[F[_], O](
s: Stream[F, O]
)(implicit F: MonadError[F, Throwable]): Pull[F, Nothing, Stream[F, O]] =
Pull.getScope[F].map(scope => Stream.bracket(scope.lease)(_.cancel.rethrow) *> s)
/** Repeatedly uses the output of the pull as input for the next step of the
* pull. Halts when a step terminates with `None` or `Pull.raiseError`.
*/
def loop[F[_], O, R](f: R => Pull[F, O, Option[R]]): R => Pull[F, O, Unit] =
(r: R) =>
f(r).flatMap {
case None => Pull.done
case Some(s) => loop(f)(s)
}
/** Intantiates with a state. Repeatedly uses the left value of the result of
* the pull as input for the next step. The Pull terminates when a step terminates with
* `Right` or `Pull.raiseError`.
*/
def loopEither[F[_], O, S, R](f: S => Pull[F, O, Either[S, R]]): S => Pull[F, O, R] =
(s: S) =>
f(s).flatMap {
case Left(ns) => loopEither(f)(ns)
case Right(r) => Pull.pure(r)
}
private[fs2] def fail[F[_]](err: Throwable): Pull[F, Nothing, Nothing] = Fail(err)
final class PartiallyAppliedFromEither[F[_]] {
def apply[A](either: Either[Throwable, A])(implicit ev: RaiseThrowable[F]): Pull[F, A, Unit] =
either.fold(raiseError[F], output1)
}
/** Lifts an Either[Throwable, A] to an effectful Pull[F, A, Unit].
*
* @example {{{
* scala> import cats.effect.SyncIO, scala.util.Try
* scala> Pull.fromEither[SyncIO](Right(42)).stream.compile.toList.unsafeRunSync()
* res0: List[Int] = List(42)
* scala> Try(Pull.fromEither[SyncIO](Left(new RuntimeException)).stream.compile.toList.unsafeRunSync())
* res1: Try[List[Nothing]] = Failure(java.lang.RuntimeException)
* }}}
*/
def fromEither[F[x]] = new PartiallyAppliedFromEither[F]
/** Gets the current scope, allowing manual leasing or interruption.
* This is a low-level method and generally should not be used by user code.
*/
private[fs2] def getScope[F[_]]: Pull[F, Nothing, Scope[F]] = GetScope[F]()
/** Returns a pull that evaluates the supplied by-name each time the pull is
* used, allowing use of a mutable value in pull computations.
*/
def suspend[F[_], O, R](p: => Pull[F, O, R]): Pull[F, O, R] =
new Bind[F, O, Unit, R](unit) {
def cont(r: Terminal[Unit]): Pull[F, O, R] = p
}
/** An abstraction for writing `Pull` computations that can timeout
* while reading from a `Stream`.
*
* A `Pull.Timed` is not created or intepreted directly, but by
* calling [[Stream.ToPull.timed]].
*
* {{{
* yourStream.pull.timed(tp => ...).stream
* }}}
*
* The argument to `timed` is a `Pull.Timed[F, O] => Pull[F, O2, R]`
* function, which describes the pulling logic and is often recursive,
* with shape:
*
* {{{
* def go(timedPull: Pull.Timed[F, A]): Pull[F, B, Unit] =
* timedPull.uncons.flatMap {
* case Some((Right(chunk), next)) => doSomething >> go(next)
* case Some((Left(_), next)) => doSomethingElse >> go(next)
* case None => Pull.done
* }
* }}}
*
* Where `doSomething` and `doSomethingElse` are `Pull` computations
* such as `Pull.output`, in addition to `Pull.Timed.timeout`.
*
* See below for detailed descriptions of `timeout` and `uncons`, and
* look at the [[Stream.ToPull.timed]] scaladoc for an example of usage.
*/
trait Timed[F[_], O] {
type Timeout
/** Waits for either a chunk of elements to be available in the
* source stream, or a timeout to trigger. Whichever happens
* first is provided as the resource of the returned pull,
* alongside a new timed pull that can be used for awaiting
* again. A `None` is returned as the resource of the pull upon
* reaching the end of the stream.
*
* Receiving a timeout is not a fatal event: the evaluation of the
* current chunk is not interrupted, and the next timed pull is
* still returned for further iteration. The lifetime of timeouts
* is handled by explicit calls to the `timeout` method: `uncons`
* does not start, restart or cancel any timeouts.
*
* Note that the type of timeouts is existential in `Pull.Timed`
* (hidden, basically) so you cannot do anything on it except for
* pattern matching, which is best done as a `Left(_)` case.
*/
def uncons: Pull[F, Nothing, Option[(Either[Timeout, Chunk[O]], Pull.Timed[F, O])]]
/** Asynchronously starts a timeout that will be received by
* `uncons` after `t`, and immediately returns.
*
* Timeouts are resettable: if `timeout` executes whilst a
* previous timeout is pending, it will cancel it before starting
* the new one, so that there is at most one timeout in flight at
* any given time. The implementation guards against stale
* timeouts: after resetting a timeout, a subsequent `uncons` is
* guaranteed to never receive an old one.
*
* Timeouts can be reset to any `t`, longer or shorter than the
* previous timeout, but a duration of 0 is treated specially, in
* that it will cancel a pending timeout but not start a new one.
*
* Note:
* If for some reason you insert a pause in between `uncons` and
* `timeout`, such as:
* {{{
* timedPull.timeout(n.millis) >>
* Pull.eval(IO.sleep(m.millis)) >>
* timedPull.uncons.flatMap { ...
* }}}
*
* you should be aware that an invocation of `timeout` that
* happens before the very first `uncons` will start the timeout
* simultaneously with the very first `uncons`. Subsequent
* invocations of `timeout` start the timeout immediately
* instead.
*
* This is an implementation detail which should not affect most
* cases, given that usually there is no need to sleep in between
* `timeout` and the very first call to `uncons`.
*/
def timeout(t: FiniteDuration): Pull[F, Nothing, Unit]
}
/** `Sync` instance for `Pull`. */
implicit def syncInstance[F[_]: Sync, O]: Sync[Pull[F, O, *]] =
new PullSyncInstance[F, O]
/** `FunctionK` instance for `F ~> Pull[F, Nothing, *]`
*
* @example {{{
* scala> import cats.Id
* scala> Pull.functionKInstance[Id](42).flatMap(Pull.output1).stream.compile.toList
* res0: cats.Id[List[Int]] = List(42)
* }}}
*/
implicit def functionKInstance[F[_]]: F ~> Pull[F, Nothing, *] =
new (F ~> Pull[F, Nothing, *]) {
def apply[X](fx: F[X]) = Pull.eval(fx)
}
/* Implementation notes:
*
* A Pull can be one of the following:
* - A Terminal - the end result of pulling. This may have ended in:
* - Succeeded with a result of type R.
* - Failed with an exception
* - Interrupted from another thread with a known `scopeId`
*
* - A Bind, that binds a first computation(another Pull) with a method to _continue_
* the computation from the result of the first one `step`.
*
* - A single Action, which can be one of following:
*
* - Eval (or lift) an effectful operation of type `F[R]`
* - Output some values of type O.
* - Acquire a new resource and add its cleanup to the current scope.
* - Open, Close, or Access to the resource scope.
* - side-Step or fork to a different computation
*/
/* A Terminal indicates how a pull evaluation ended.
* A pull may have succeeded with a result, failed with an exception,
* or interrupted from another concurrent pull.
*/
private sealed abstract class Terminal[+R]
extends Pull[Nothing, Nothing, R]
with ViewL[Nothing, Nothing]
private final case class Succeeded[+R](r: R) extends Terminal[R] {
override def map[R2](f: R => R2): Terminal[R2] =
try Succeeded(f(r))
catch { case NonFatal(err) => Fail(err) }
}
private final case class Fail(error: Throwable) extends Terminal[Nothing] {
override def map[R](f: Nothing => R): Terminal[R] = this
}
/** Signals that pull evaluation was interrupted.
*
* @param context Any user specific context that needs to be captured
* during interruption for eventual resume of the operation.
*
* @param deferredError Any errors, accumulated during resume of the interruption.
* Instead throwing errors immediately during interruption,
* signalling of the errors may be deferred until the Interruption resumes.
*/
private final case class Interrupted(context: Unique.Token, deferredError: Option[Throwable])
extends Terminal[Nothing] {
override def map[R](f: Nothing => R): Terminal[R] = this
}
private sealed trait ContP[-Y, +F[_], +O, +X] extends (Terminal[Y] => Pull[F, O, X]) {
def apply(r: Terminal[Y]): Pull[F, O, X] = cont(r)
protected def cont(r: Terminal[Y]): Pull[F, O, X]
}
private object IdContP extends ContP[Unit, Nothing, Nothing, Unit] {
def cont(r: Terminal[Unit]): Pull[Nothing, Nothing, Unit] = r
}
private abstract class Bind[+F[_], +O, X, +R](val step: Pull[F, O, X])
extends Pull[F, O, R]
with ContP[X, F, O, R] {
def cont(r: Terminal[X]): Pull[F, O, R]
def delegate: Bind[F, O, X, R] = this
}
/* Unrolled view of a `Pull` structure. */
private sealed trait ViewL[+F[_], +O]
// This class is not created by the combinators in the public Pull API, only during compilation
private class DelegateBind[F[_], O, Y](
step: Pull[F, O, Y],
override val delegate: Bind[F, O, Y, Unit]
) extends Bind[F, O, Y, Unit](step) {
def cont(yr: Terminal[Y]): Pull[F, O, Unit] = delegate.cont(yr)
}
private def bindView[F[_], O, Y](
fmoc: Pull[F, O, Unit],
view: Cont[Unit, F, O]
): Pull[F, O, Unit] =
view match {
case IdContP => fmoc
case bv: Bind[F, O, Unit, Unit] @unchecked =>
fmoc match {
case r: Terminal[Unit] =>
try bv(r)
catch { case NonFatal(e) => Fail(e) }
case _ => new DelegateBind[F, O, Unit](fmoc, bv.delegate)
}
case _ =>
new Bind[F, O, Unit, Unit](fmoc) {
def cont(r: Terminal[Unit]) = view(r)
}
}
// This class is not created by combinators in public Pull API, only during compilation
private class BindBind[F[_], O, X, Y](
step: Pull[F, O, X],
val bb: Bind[F, O, X, Y],
val del: Bind[F, O, Y, Unit]
) extends Bind[F, O, X, Unit](step) {
def cont(tx: Terminal[X]): Pull[F, O, Unit] =
try bindBindAux(bb.cont(tx), del)
catch { case NonFatal(e) => Fail(e) }
}
@tailrec @nowarn("cat=unchecked")
private def bindBindAux[F[_], O, X](
py: Pull[F, O, X],
del: Bind[F, O, X, Unit]
): Pull[F, O, Unit] =
py match {
case ty: Terminal[X] =>
del match {
case cici: BindBind[F, O, ?, X] =>
bindBindAux(cici.bb.cont(ty), cici.del)
case _ => del.cont(ty)
}
case x => new DelegateBind(x, del)
}
/* An action is an instruction that can perform effects in `F`
* to generate by-product outputs of type `O`.
*
* Each operation also generates an output of type `R` that is used
* as control information for the rest of the interpretation or compilation.
*/
private sealed abstract class Action[+F[_], +O, +R] extends Pull[F, O, R] with ViewL[F, O]
/* An action that emits a non-empty chunk of outputs. */
private final case class Output[+O](values: Chunk[O]) extends Action[Nothing, O, Unit]
/* A translation point, that wraps an inner stream written in another effect. */
private final case class Translate[G[_], F[_], +O](
stream: Pull[G, O, Unit],
fk: G ~> F
) extends Action[F, O, Unit]
private final case class FlatMapOutput[+F[_], O, +P](
stream: Pull[F, O, Unit],
fun: O => Pull[F, P, Unit]
) extends Action[F, P, Unit]
/* Steps through the given inner stream, until the first `Output` is reached.
* It returns the possible `uncons`.
* Yields to head in form of chunk, then id of the scope that was active
* after step evaluated and tail of the `stream`.
*
* @param stream Stream to step
*/
private final case class Uncons[+F[_], +O](stream: Pull[F, O, Unit])
extends Action[Nothing, Nothing, Option[(Chunk[O], Pull[F, O, Unit])]]
/** Steps through the stream, providing a `stepLeg`.
* Yields to head in form of chunk, then id of the scope that was active after step evaluated and tail of the `stream`.
*
* @param stream Stream to step
* @param scope Scope has to be changed before this step is evaluated, id of the scope must be supplied
*/
private final case class StepLeg[+F[_], +O](stream: Pull[F, O, Unit], scope: Unique.Token)
extends Action[Nothing, Nothing, Option[Stream.StepLeg[F, O]]]
/* The `AlgEffect` trait is for operations on the `F` effect that create no `O` output. */
private sealed abstract class AlgEffect[+F[_], R] extends Action[F, Nothing, R]
private final case class Eval[+F[_], R](value: F[R]) extends AlgEffect[F, R]
private final case class Acquire[+F[_], R](
resource: F[R],
release: (R, ExitCase) => F[Unit],
cancelable: Boolean
) extends AlgEffect[F, R]
private final case class InScope[+F[_], +O](
stream: Pull[F, O, Unit],
useInterruption: Boolean
) extends Action[F, O, Unit]
private final case class InterruptWhen[+F[_]](haltOnSignal: F[Either[Throwable, Unit]])
extends AlgEffect[F, Unit]
// `InterruptedScope` contains id of the scope currently being interrupted
// together with any errors accumulated during interruption process
private abstract class CloseScope extends AlgEffect[Nothing, Unit] {
def scopeId: Unique.Token
def interruption: Option[Interrupted]
def exitCase: ExitCase
}
private final case class SucceedScope(scopeId: Unique.Token) extends CloseScope {
def exitCase: ExitCase = ExitCase.Succeeded
def interruption: Option[Interrupted] = None
}
private final case class CanceledScope(scopeId: Unique.Token, inter: Interrupted)
extends CloseScope {
def exitCase: ExitCase = ExitCase.Canceled
def interruption: Option[Interrupted] = Some(inter)
}
private final case class FailedScope(scopeId: Unique.Token, err: Throwable) extends CloseScope {
def exitCase: ExitCase = ExitCase.Errored(err)
def interruption: Option[Interrupted] = None
}
private final case class GetScope[F[_]]() extends AlgEffect[Nothing, Scope[F]]
/** Ignores current stepLeg head, goes on with remaining data */
private[fs2] def stepLeg[F[_], O](
leg: Stream.StepLeg[F, O]
): Pull[F, Nothing, Option[Stream.StepLeg[F, O]]] =
StepLeg[F, O](leg.next, leg.scopeId)
/** Wraps supplied pull in new scope, that will be opened before this pull is evaluated
* and closed once this pull either finishes its evaluation or when it fails.
*/
private[fs2] def scope[F[_], O](s: Pull[F, O, Unit]): Pull[F, O, Unit] = InScope(s, false)
/** Like `scope` but allows this scope to be interrupted.
* Note that this may fail with `Interrupted` when interruption occurred
*/
private[fs2] def interruptScope[F[_], O](s: Pull[F, O, Unit]): Pull[F, O, Unit] = InScope(s, true)
private[fs2] def interruptWhen[F[_], O](
haltOnSignal: F[Either[Throwable, Unit]]
): Pull[F, O, Unit] = InterruptWhen(haltOnSignal)
private type Cont[-Y, +G[_], +O] = Terminal[Y] => Pull[G, O, Unit]
private[this] type Nought[A] = Any
/* Left-folds the output of a stream in to a single value of type `B`.
*
* === Interruption ===
*
* Interruption of the stream is implemented cooperatively between `Pull` and `Scope`.
* Unlike interruption of an `F[_]: MonadCancelThrow` type (e.g. `IO`), stream interruption
* needs to find the recovery point where stream evaluation continues.
*/
private[fs2] def compile[F[_], O, B](
stream: Pull[F, O, Unit],
initScope: Scope[F],
extendLastTopLevelScope: Boolean,
init: B
)(foldChunk: (B, Chunk[O]) => B)(implicit
F: MonadError[F, Throwable]
): F[B] = {
var contP: ContP[Nothing, Nought, Any, Unit] = null
def getCont(): Cont[Any, Nothing, Nothing] = contP.asInstanceOf[Cont[Any, Nothing, Nothing]]
@tailrec
def viewL[G[_], X](free: Pull[G, X, Unit]): ViewL[G, X] =
free match {
case e: Action[G, X, Unit] =>
contP = IdContP
e
case b: Bind[G, X, y, Unit] =>
type Y = y
b.step match {
case c: Bind[G, X, z, Y] =>
viewL(new BindBind[G, X, z, Y](c.step, c.delegate, b.delegate))
case e: Action[G, X, ?] =>
contP = b.delegate
e
case r: Terminal[Y] => viewL(b.cont(r))
}
case r: Terminal[Unit] => r
}
/* Inject interruption to the tail used in `flatMap`. Assures that close of the scope
* is invoked if at the flatMap tail, otherwise switches evaluation to `interrupted` path. */
def interruptBoundary[G[_], X](
stream: Pull[G, X, Unit],
interruption: Interrupted
): Pull[G, X, Unit] =
viewL(stream) match {
case cs: CloseScope =>
// Inner scope is getting closed b/c a parent was interrupted
val cl: Pull[G, X, Unit] = CanceledScope(cs.scopeId, interruption)
transformWith(cl)(getCont())
case _: Action[G, X, y] =>
// all other actions, roll the interruption forwards
getCont()(interruption)
case interrupted: Interrupted => interrupted // impossible
case _: Succeeded[?] => interruption
case failed: Fail =>
val errs = interruption.deferredError.toList :+ failed.error
Fail(CompositeFailure.fromList(errs).getOrElse(failed.error))
}
trait Run[-G[_], -X, +End] {
def done(scope: Scope[F]): End
def out(head: Chunk[X], scope: Scope[F], tail: Pull[G, X, Unit]): End
def interrupted(inter: Interrupted): End
def fail(e: Throwable): End
}
type CallRun[+G[_], +X, End] = Run[G, X, End] => End
object TheBuildR extends Run[Nothing, Nothing, F[CallRun[Nothing, Nothing, F[Nothing]]]] {
def fail(e: Throwable) = F.raiseError(e)
def done(scope: Scope[F]) = F.pure(_.done(scope))
def out(head: Chunk[Nothing], scope: Scope[F], tail: Pull[Nothing, Nothing, Unit]) =
F.pure(_.out(head, scope, tail))
def interrupted(i: Interrupted) = F.pure(_.interrupted(i))
}
def buildR[G[_], X, End]: Run[G, X, F[CallRun[G, X, F[End]]]] =
TheBuildR.asInstanceOf[Run[G, X, F[CallRun[G, X, F[End]]]]]
def go[G[_], X, End](
scope: Scope[F],
extendedTopLevelScope: Option[Scope[F]],
translation: G ~> F,
runner: Run[G, X, F[End]],
stream: Pull[G, X, Unit]
): F[End] = {
def interruptGuard(scope: Scope[F], view: Cont[Nothing, G, X])(next: => F[End]): F[End] =
scope.isInterrupted.flatMap {
case None => next
case Some(outcome) =>
val result = outcome match {
case Outcome.Errored(err) => Fail(err)
case Outcome.Canceled() => Interrupted(scope.id, None)
case Outcome.Succeeded(scopeId) => Interrupted(scopeId, None)
}
go(scope, extendedTopLevelScope, translation, runner, view(result))
}
def goErr(err: Throwable, view: Cont[Nothing, G, X]): F[End] =
go(scope, extendedTopLevelScope, translation, runner, view(Fail(err)))
class ViewRunner(val view: Cont[Unit, G, X]) extends Run[G, X, F[End]] {
private val prevRunner = runner
def done(doneScope: Scope[F]): F[End] =
go(doneScope, extendedTopLevelScope, translation, prevRunner, view(unit))
def out(head: Chunk[X], scope: Scope[F], tail: Pull[G, X, Unit]): F[End] = {
@tailrec
def outLoop(acc: Pull[G, X, Unit], pred: Run[G, X, F[End]]): F[End] =
// bit of an ugly hack to avoid a stack overflow when these accummulate
pred match {
case vrun: ViewRunner @unchecked => outLoop(bindView(acc, vrun.view), vrun.prevRunner)
case _ => pred.out(head, scope, acc)
}
outLoop(tail, this)
}
def interrupted(inter: Interrupted): F[End] =
go(scope, extendedTopLevelScope, translation, prevRunner, view(inter))
def fail(e: Throwable): F[End] = goErr(e, view)
}
class TranslateRunner[H[_]](fk: H ~> G, view: Cont[Unit, G, X]) extends Run[H, X, F[End]] {
def done(doneScope: Scope[F]): F[End] =
go(doneScope, extendedTopLevelScope, translation, runner, view(unit))
def out(head: Chunk[X], scope: Scope[F], tail: Pull[H, X, Unit]): F[End] = {
val next = bindView(Translate(tail, fk), view)
runner.out(head, scope, next)
}
def interrupted(inter: Interrupted): F[End] =
go(scope, extendedTopLevelScope, translation, runner, view(inter))
def fail(e: Throwable): F[End] = goErr(e, view)
}
abstract class StepRunR[Y, S](view: Cont[Option[S], G, X]) extends Run[G, Y, F[End]] {
def done(scope: Scope[F]): F[End] =
interruptGuard(scope, view) {
go(scope, extendedTopLevelScope, translation, runner, view(Succeeded(None)))