id_effect 0.13.0

Effect<A, E, R>: lazy Send effects over a typed capability list, run on a fiber runtime (Tokio executor), with layers, streams, schedules and coordination
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
//! [`Stream`]: a pull-based, chunked stream on the 0.7 kernel.
//!
//! A stream is an owned *pull*: a boxed `FnOnce(max) -> Effect<Step>` whose step is either the
//! end or one chunk plus the rest of the stream. Operators move their state (counters, buffers,
//! closures) into the rest they return, so nothing is shared between pulls and there is no lock
//! on the pull path (the 0.6 stream kept its state in an `Arc<Mutex<StreamState>>` locked twice
//! per pull). A failed pull ends the stream: there is no rest to pull again (fusing).
//!
//! The pull effect runs on the consumer's fiber under the consumer's environment `R`, so
//! effectful operators such as [`Stream::map_effect`] never clone `R` (it is shared by the
//! runloop), and interruption is the kernel's: a pull is interrupted like any other effect.

use std::collections::VecDeque;
use std::fmt;
use std::future::poll_fn;
use std::ops::ControlFlow;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::task::{Context, Poll};

use crate::capability::HNil;
use crate::concurrency::{Concurrency, fork_all};
use crate::coordination::{Queue, QueueError};
use crate::failure::{Cause, Never};
use crate::kernel::{
  AnyEnv, Effect, Instr, Node, erase_exit, fail, settle_instr, succeed, suspend, unerase_exit,
  yield_fiber,
};
use crate::runtime::FiberContext;
use crate::streaming::chunk::Chunk;

/// The chunk size a consumer asks for when it does not choose one ([`Stream::next_chunk`] and
/// the `run_*` consumers).
pub const DEFAULT_CHUNK_SIZE: usize = 64;

/// One pull's outcome: the end (`None`) or a chunk (possibly empty) and the rest of the stream.
pub(crate) type Step<A, E, R> = Option<(Vec<A>, Stream<A, E, R>)>;

type PullFn<A, E, R> = Box<dyn FnOnce(usize) -> Effect<Step<A, E, R>, E, R> + Send>;

/// The take-once failure slot a queue-backed source reports once its queue is drained.
pub(crate) type FailSlot<E> = Arc<Mutex<Option<E>>>;

/// Locks `m`, ignoring poisoning (the guarded data is always left consistent).
pub(crate) fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
  m.lock().unwrap_or_else(PoisonError::into_inner)
}

/// A lazy, pull-based stream of `A` that can fail with `E` and needs the capabilities in `R`.
///
/// Nothing runs until a consumer pulls ([`next_chunk`](Self::next_chunk) or a `run_*` method).
/// Each pull yields at most the requested number of items (default [`DEFAULT_CHUNK_SIZE`]), and
/// transforming operators pull one upstream chunk per downstream pull, so memory stays
/// proportional to the chunk size and infinite sources work.
///
/// ```
/// use id_effect::Stream;
/// use id_effect::Never;
///
/// let out = Stream::<u64, Never>::from_iterable(0..)
///   .map(|n| n * 3)
///   .filter(|n: &u64| n % 2 == 0)
///   .take(4)
///   .run_collect()
///   .block_on()
///   .into_result();
/// assert_eq!(out.ok(), Some(vec![0, 6, 12, 18]));
/// ```
pub struct Stream<A, E = Never, R = HNil> {
  repr: Repr<A, E, R>,
}

enum Repr<A, E, R> {
  Done,
  Pull(PullFn<A, E, R>),
}

impl<A, E, R> fmt::Debug for Stream<A, E, R> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.write_str(match self.repr {
      Repr::Done => "Stream::Done",
      Repr::Pull(_) => "Stream::Pull",
    })
  }
}

/// The result of one synchronous [`Stream::pull_stage`] step over an upstream chunk.
pub(crate) enum StageStep<B> {
  /// Emit these items and keep pulling upstream.
  Emit(Vec<B>),
  /// Emit these items and stop: upstream is dropped and never pulled again.
  Last(Vec<B>),
}

impl<A, E, R> Stream<A, E, R>
where
  A: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
{
  // ---------------------------------------------------------------------------------------------
  // Representation
  // ---------------------------------------------------------------------------------------------

  /// A stream with no elements.
  pub fn empty() -> Self {
    Self { repr: Repr::Done }
  }

  /// A stream from its pull: `f(max)` is called on the first pull.
  pub(crate) fn from_step(
    f: impl FnOnce(usize) -> Effect<Step<A, E, R>, E, R> + Send + 'static,
  ) -> Self {
    Self {
      repr: Repr::Pull(Box::new(f)),
    }
  }

  /// Pulls one chunk of at most `max` (at least 1) items. The pull function runs now (it only
  /// builds the effect); callers that are not already running inside an effect wrap this in
  /// `suspend`.
  pub(crate) fn pull_raw(self, max: usize) -> Effect<Step<A, E, R>, E, R> {
    match self.repr {
      Repr::Done => succeed(None),
      Repr::Pull(f) => f(max.max(1)),
    }
  }

  /// Pulls the next non-empty chunk of at most `max` items (`0` counts as `1`) together with the
  /// rest of the stream, or `None` at the end. A failure ends the stream (there is no rest).
  pub fn next_chunk_with_size(self, max: usize) -> Effect<Option<(Chunk<A>, Self)>, E, R> {
    let max = max.max(1);
    suspend(move || self.next_chunk_now(max))
  }

  /// [`next_chunk_with_size`](Self::next_chunk_with_size) with [`DEFAULT_CHUNK_SIZE`].
  pub fn next_chunk(self) -> Effect<Option<(Chunk<A>, Self)>, E, R> {
    self.next_chunk_with_size(DEFAULT_CHUNK_SIZE)
  }

  fn next_chunk_now(self, max: usize) -> Effect<Option<(Chunk<A>, Self)>, E, R> {
    self.pull_raw(max).flat_map(move |step| match step {
      None => succeed(None),
      // Empty chunks are never surfaced: pull again, yielding so a source that keeps producing
      // nothing stays interruptible and fair.
      Some((items, rest)) if items.is_empty() => {
        yield_fiber().flat_map(move |()| rest.next_chunk_now(max))
      }
      Some((items, rest)) => succeed(Some((Chunk::from_vec(items), rest))),
    })
  }

  // ---------------------------------------------------------------------------------------------
  // Sources
  // ---------------------------------------------------------------------------------------------

  /// A one-element stream.
  pub fn succeed(a: A) -> Self {
    Self::from_vec(vec![a])
  }

  /// A stream that fails with `e` on its first pull.
  pub fn fail(e: E) -> Self {
    Self::from_step(move |_| fail(e))
  }

  /// The elements of `chunk`, served in chunks of the requested size.
  pub fn from_chunk(chunk: Chunk<A>) -> Self {
    Self::from_vec(chunk.into_vec())
  }

  /// The elements of `items`, served in chunks of the requested size.
  pub fn from_vec(items: Vec<A>) -> Self {
    Self::buffered(items.into())
  }

  pub(crate) fn buffered(mut buf: VecDeque<A>) -> Self {
    if buf.is_empty() {
      return Self::empty();
    }
    Self::from_step(move |n| {
      let out: Vec<A> = buf.drain(..n.min(buf.len())).collect();
      succeed(Some((out, Self::buffered(buf))))
    })
  }

  /// A stream over any iterator, advanced lazily (at most one chunk of items per pull), so
  /// infinite iterators are fine.
  pub fn from_iterable<I>(iter: I) -> Self
  where
    I: IntoIterator<Item = A>,
    I::IntoIter: Send + 'static,
  {
    Self::from_iter_state(iter.into_iter())
  }

  fn from_iter_state<It: Iterator<Item = A> + Send + 'static>(mut it: It) -> Self {
    Self::from_step(move |n| {
      let batch: Vec<A> = it.by_ref().take(n).collect();
      succeed((!batch.is_empty()).then(|| (batch, Self::from_iter_state(it))))
    })
  }

  /// Expands `init` with the pure generator `f` until it returns `None` (lazily, at most one
  /// chunk of items per pull).
  pub fn unfold<S, F>(init: S, mut f: F) -> Self
  where
    S: Send + 'static,
    F: FnMut(S) -> Option<(A, S)> + Send + 'static,
  {
    let mut state = Some(init);
    Self::from_iter_state(std::iter::from_fn(move || {
      let (a, s) = f(state.take()?)?;
      state = Some(s);
      Some(a)
    }))
  }

  /// Expands `init` with the effectful generator `f` until it returns `None`. Each pull runs `f`
  /// at most `max` times, one after the other.
  ///
  /// A pull waits for its **first** element only. After that it keeps the elements whose steps
  /// complete without waiting, and stops at the first step that would wait (a timer, I/O, an
  /// empty queue, any step that needs the fiber), handing over what it has; the next pull
  /// resumes that step where it stopped. So a generator whose steps are always ready (pure,
  /// synchronous or already-complete futures) fills whole chunks, and a live one (a socket, a
  /// subscription, a client stream) delivers each element as it arrives instead of stalling
  /// until `max` of them have. A step that fails without waiting fails the pull.
  ///
  /// ```
  /// use id_effect::{HNil, Never, Queue, Stream, succeed};
  ///
  /// let q = Queue::<u32>::unbounded();
  /// let _ = q.try_offer_all::<Never, HNil>([1, 2, 3]).block_on();
  /// // The queue stays open, so a fourth `take` would wait: the pull hands over the three
  /// // elements that are there instead of waiting for 64.
  /// let take = q.clone();
  /// let live = Stream::<u32, Never>::unfold_effect((), move |()| {
  ///   take.take().map(|n| Some((n, ()))).catch(|_| succeed(None))
  /// });
  /// let (chunk, _rest) = live.next_chunk().block_on().into_result().ok().flatten().unwrap();
  /// assert_eq!(chunk.into_vec(), vec![1, 2, 3]);
  /// ```
  pub fn unfold_effect<S, F>(init: S, mut f: F) -> Self
  where
    S: Send + 'static,
    F: FnMut(S) -> Effect<Option<(A, S)>, E, R> + Send + 'static,
  {
    Self::from_step(move |n| {
      let first = f(init);
      Self::unfold_pull(first, f, n)
    })
  }

  /// The rest of an [`unfold_effect`](Self::unfold_effect) stream whose next step `next` is
  /// already built (and possibly started: a pull stopped on it because it would wait).
  fn unfold_resume<S, F>(next: Effect<Option<(A, S)>, E, R>, f: F) -> Self
  where
    S: Send + 'static,
    F: FnMut(S) -> Effect<Option<(A, S)>, E, R> + Send + 'static,
  {
    Self::from_step(move |n| Self::unfold_pull(next, f, n))
  }

  /// One `unfold_effect` pull of at most `n` elements: waits for `next`, then gathers the steps
  /// that are ready without waiting.
  fn unfold_pull<S, F>(
    next: Effect<Option<(A, S)>, E, R>,
    f: F,
    n: usize,
  ) -> Effect<Step<A, E, R>, E, R>
  where
    S: Send + 'static,
    F: FnMut(S) -> Effect<Option<(A, S)>, E, R> + Send + 'static,
  {
    next.flat_map(move |next| match next {
      None => succeed(None),
      Some((a, s)) => Self::unfold_gather(s, f, n, vec![a]),
    })
  }

  /// Adds the generator's ready steps to `out` (non-empty) until it holds `n` elements, the
  /// generator ends, or a step would wait. Ready values are taken here without any allocation;
  /// the first step that is not a ready value moves the gathering into [`unfold_probe`].
  fn unfold_gather<S, F>(
    mut s: S,
    mut f: F,
    n: usize,
    mut out: Vec<A>,
  ) -> Effect<Step<A, E, R>, E, R>
  where
    S: Send + 'static,
    F: FnMut(S) -> Effect<Option<(A, S)>, E, R> + Send + 'static,
  {
    loop {
      if out.len() >= n {
        return succeed(Some((out, Self::unfold_effect(s, f))));
      }
      match f(s).node {
        Node::Pure(Ok(Some((a, next)))) => {
          out.push(a);
          s = next;
        }
        Node::Pure(Ok(None)) => return succeed(Some((out, Self::empty()))),
        Node::Pure(Err(c)) => return Effect::pure(Err(c)),
        node => return unfold_probe(Effect { node }, f, n, out),
      }
    }
  }

  /// An incremental source driven by a pull function: each pull runs `f(max)` once; `Ok(None)`
  /// ends the stream. `max` is a hint: a larger batch is split and served over the following
  /// pulls, and an empty batch is simply pulled again.
  ///
  /// ```
  /// use id_effect::{Stream, succeed};
  /// use id_effect::Never;
  ///
  /// let mut next = 0_u64;
  /// let naturals = Stream::<u64, Never>::from_pull(move |n| {
  ///   let batch: Vec<u64> = (next..next + n as u64).collect();
  ///   next += n as u64;
  ///   succeed(Some(batch))
  /// });
  /// let first = naturals.take(3).run_collect().block_on().into_result();
  /// assert_eq!(first.ok(), Some(vec![0, 1, 2]));
  /// ```
  pub fn from_pull<F>(f: F) -> Self
  where
    F: FnMut(usize) -> Effect<Option<Vec<A>>, E, R> + Send + 'static,
  {
    Self::pull_source(f, VecDeque::new())
  }

  fn pull_source<F>(mut f: F, mut buf: VecDeque<A>) -> Self
  where
    F: FnMut(usize) -> Effect<Option<Vec<A>>, E, R> + Send + 'static,
  {
    Self::from_step(move |n| {
      if !buf.is_empty() {
        let out: Vec<A> = buf.drain(..n.min(buf.len())).collect();
        return succeed(Some((out, Self::pull_source(f, buf))));
      }
      f(n).map(move |batch| {
        batch.map(|mut items| {
          let rest: VecDeque<A> = if items.len() > n {
            items.split_off(n).into()
          } else {
            VecDeque::new()
          };
          (items, Self::pull_source(f, rest))
        })
      })
    })
  }

  /// A **single-batch** stream: the first pull runs `effect` to obtain every element, which are
  /// then served in chunks. Use [`from_pull`](Self::from_pull) or
  /// [`unfold_effect`](Self::unfold_effect) for incremental effectful sources.
  pub fn from_effect(effect: Effect<Vec<A>, E, R>) -> Self {
    Self::from_step(move |n| effect.flat_map(move |items| Self::from_vec(items).pull_raw(n)))
  }

  /// The items offered to `queue`, until it is shut down and drained (each pull takes what is
  /// buffered, up to the requested size, suspending while the queue is empty).
  pub fn from_queue(queue: Queue<A>) -> Self {
    queue_source(queue, Arc::new(Mutex::new(None)), VecDeque::new(), |v| v)
  }

  /// Concatenation: every element of `self`, then every element of `second` (incremental, so
  /// either side may be long or unbounded).
  #[must_use]
  pub fn chain(self, second: Self) -> Self {
    if matches!(self.repr, Repr::Done) {
      return second;
    }
    Self::from_step(move |n| {
      self.pull_raw(n).flat_map(move |step| match step {
        None => second.pull_raw(n),
        Some((items, rest)) => succeed(Some((items, rest.chain(second)))),
      })
    })
  }

  /// Alias of [`chain`](Self::chain) (Effect.ts `Stream.concat`).
  #[must_use]
  pub fn concat(self, second: Self) -> Self {
    self.chain(second)
  }

  // ---------------------------------------------------------------------------------------------
  // Stages
  // ---------------------------------------------------------------------------------------------

  /// A pull stage over `self`: every downstream pull fetches **one** upstream chunk (of
  /// `want(&state, max)` items) and feeds it to `step` (`None` once upstream ends, so the step
  /// can flush). After [`StageStep::Last`] or the upstream end, upstream is dropped. Memory is
  /// one upstream chunk plus whatever `state` retains.
  pub(crate) fn pull_stage<B, S, W, F>(self, state: S, want: W, step: F) -> Stream<B, E, R>
  where
    B: Send + 'static,
    S: Send + 'static,
    W: Fn(&S, usize) -> usize + Send + 'static,
    F: FnMut(&mut S, Option<Vec<A>>) -> StageStep<B> + Send + 'static,
  {
    Stream::from_step(move |n| {
      let w = want(&state, n).max(1);
      self.pull_raw(w).map(move |res| {
        let (mut state, mut step) = (state, step);
        let (input, rest) = match res {
          Some((items, rest)) => (Some(items), Some(rest)),
          None => (None, None),
        };
        match (step(&mut state, input), rest) {
          (StageStep::Emit(out), Some(rest)) => Some((out, rest.pull_stage(state, want, step))),
          (StageStep::Emit(out) | StageStep::Last(out), _) => {
            (!out.is_empty()).then(|| (out, Stream::empty()))
          }
        }
      })
    })
  }

  /// [`pull_stage`](Self::pull_stage) without explicit state; asks upstream for the requested
  /// size.
  pub(crate) fn stage<B, F>(self, mut step: F) -> Stream<B, E, R>
  where
    B: Send + 'static,
    F: FnMut(Option<Vec<A>>) -> StageStep<B> + Send + 'static,
  {
    self.pull_stage((), |(), n| n, move |(), input| step(input))
  }

  /// Maps each element with the pure `f`, per upstream chunk, in parallel through the Compute
  /// Fabric when the chunk is large enough (ADR 0008); output order is preserved.
  pub fn map<B, F>(self, f: F) -> Stream<B, E, R>
  where
    B: Send + 'static,
    F: Fn(A) -> B + Send + Sync + 'static,
  {
    self.stage(move |input| StageStep::Emit(input.map_or_else(Vec::new, |v| bulk_map(v, &f))))
  }

  /// Maps each element in order with an `FnMut`, on the pulling fiber.
  pub fn map_serial<B, F>(self, mut f: F) -> Stream<B, E, R>
  where
    B: Send + 'static,
    F: FnMut(A) -> B + Send + 'static,
  {
    self.stage(move |input| {
      StageStep::Emit(input.map_or_else(Vec::new, |v| v.into_iter().map(&mut f).collect()))
    })
  }

  /// Keeps the elements satisfying `p` (Fabric-parallel per chunk, like [`map`](Self::map)).
  pub fn filter<P>(self, p: P) -> Self
  where
    P: Fn(&A) -> bool + Send + Sync + 'static,
  {
    self.stage(move |input| StageStep::Emit(input.map_or_else(Vec::new, |v| bulk_filter(v, &p))))
  }

  /// Keeps the elements satisfying `p`, in order on the pulling fiber.
  pub fn filter_serial<P>(self, mut p: P) -> Self
  where
    P: FnMut(&A) -> bool + Send + 'static,
  {
    self.stage(move |input| {
      StageStep::Emit(input.map_or_else(Vec::new, |v| v.into_iter().filter(|a| p(a)).collect()))
    })
  }

  /// The elements from the start while `p` holds; ends before the first element that fails it
  /// (upstream is not pulled again after that).
  pub fn take_while<P>(self, mut p: P) -> Self
  where
    P: FnMut(&A) -> bool + Send + 'static,
  {
    self.stage(move |input| {
      let Some(items) = input else {
        return StageStep::Emit(Vec::new());
      };
      let mut out = Vec::with_capacity(items.len());
      for item in items {
        if !p(&item) {
          return StageStep::Last(out);
        }
        out.push(item);
      }
      StageStep::Emit(out)
    })
  }

  /// Drops elements from the start while `p` holds, then yields the remainder.
  pub fn drop_while<P>(self, mut p: P) -> Self
  where
    P: FnMut(&A) -> bool + Send + 'static,
  {
    let mut skipping = true;
    self.stage(move |input| {
      let Some(items) = input else {
        return StageStep::Emit(Vec::new());
      };
      if !skipping {
        return StageStep::Emit(items);
      }
      let out: Vec<A> = items.into_iter().skip_while(|a| p(a)).collect();
      if !out.is_empty() {
        skipping = false;
      }
      StageStep::Emit(out)
    })
  }

  /// At most `n` elements from the start. Never asks upstream for more than the remaining count,
  /// and stops pulling upstream once `n` elements have been produced (so it ends infinite
  /// sources).
  pub fn take(self, n: usize) -> Self {
    if n == 0 {
      return Self::empty();
    }
    self.pull_stage(
      n,
      |remaining: &usize, want| want.min(*remaining),
      |remaining, input| {
        let Some(mut items) = input else {
          return StageStep::Emit(Vec::new());
        };
        items.truncate(*remaining);
        *remaining -= items.len();
        if *remaining == 0 {
          StageStep::Last(items)
        } else {
          StageStep::Emit(items)
        }
      },
    )
  }

  /// Groups the elements into vectors of `size` (the last may be shorter; `size == 0` yields an
  /// empty stream). Buffers at most one partial group between pulls.
  pub fn grouped(self, size: usize) -> Stream<Vec<A>, E, R> {
    if size == 0 {
      return Stream::empty();
    }
    let mut cur: Vec<A> = Vec::with_capacity(size);
    self.stage(move |input| {
      let Some(items) = input else {
        let last = std::mem::take(&mut cur);
        return StageStep::Emit(if last.is_empty() {
          Vec::new()
        } else {
          vec![last]
        });
      };
      let mut out: Vec<Vec<A>> = Vec::new();
      for item in items {
        cur.push(item);
        if cur.len() == size {
          out.push(std::mem::replace(&mut cur, Vec::with_capacity(size)));
        }
      }
      StageStep::Emit(out)
    })
  }

  /// Stateful map: emits `f(&mut state, item)` for each element.
  pub fn scan<S, B, F>(self, state: S, mut f: F) -> Stream<B, E, R>
  where
    S: Send + 'static,
    B: Send + 'static,
    F: FnMut(&mut S, A) -> B + Send + 'static,
  {
    self.pull_stage(
      state,
      |_, n| n,
      move |s, input| {
        StageStep::Emit(input.map_or_else(Vec::new, |v| v.into_iter().map(|a| f(s, a)).collect()))
      },
    )
  }

  /// FSM-style scan: `step(state, item)` returns the next state and an optional output; only
  /// the `Some` outputs are emitted.
  pub fn state_scan<S, B, F>(self, init: S, mut step: F) -> Stream<B, E, R>
  where
    S: Send + 'static,
    B: Send + 'static,
    F: FnMut(S, A) -> (S, Option<B>) + Send + 'static,
  {
    let mut state = Some(init);
    self.stage(move |input| {
      let mut out = Vec::new();
      for item in input.unwrap_or_default() {
        let Some(s) = state.take() else { break };
        let (next, emitted) = step(s, item);
        state = Some(next);
        out.extend(emitted);
      }
      StageStep::Emit(out)
    })
  }

  /// Sequential concatenation of the streams `f` builds from each element (Effect.ts
  /// `Stream.flatMap`): every element of `f(a1)`, then every element of `f(a2)`, and so on.
  pub fn flat_map<B, F>(self, f: F) -> Stream<B, E, R>
  where
    B: Send + 'static,
    F: FnMut(A) -> Stream<B, E, R> + Send + 'static,
  {
    flat_map_go(self, VecDeque::new(), None, f)
  }

  /// Maps each element with an effect, **sequentially** (ADR 0008 Amendment 2): one element's
  /// effect completes before the next one's starts, so output order and side-effect order both
  /// match stream order. A failure ends the stream with that error. `f` runs on the pulling
  /// fiber under its environment (never cloned).
  ///
  /// Use [`map_effect_par`](Self::map_effect_par) for bounded concurrency.
  pub fn map_effect<B, F>(self, f: F) -> Stream<B, E, R>
  where
    B: Send + 'static,
    F: FnMut(A) -> Effect<B, E, R> + Send + 'static,
  {
    Stream::from_step(move |n| {
      self.pull_raw(n).flat_map(move |step| match step {
        None => succeed(None),
        Some((items, rest)) => {
          let cap = items.len();
          seq_map(items.into_iter(), f, Vec::with_capacity(cap))
            .map(move |(out, f)| Some((out, rest.map_effect(f))))
        }
      })
    })
  }

  /// Maps each element with an effect, with at most `max(n, 1)` effects in flight
  /// (`Concurrency::Bounded(n)` over each upstream chunk, as child fibers of the pulling fiber).
  /// Output order matches stream order; side-effect order does not. The first failure
  /// interrupts the in-flight effects of the chunk and ends the stream with that error.
  pub fn map_effect_par<B, F>(self, n: usize, f: F) -> Stream<B, E, R>
  where
    B: Send + 'static,
    F: Fn(A) -> Effect<B, E, R> + Send + Sync + 'static,
  {
    self.map_effect_par_arc(n.max(1), Arc::new(f))
  }

  fn map_effect_par_arc<B, F>(self, limit: usize, f: Arc<F>) -> Stream<B, E, R>
  where
    B: Send + 'static,
    F: Fn(A) -> Effect<B, E, R> + Send + Sync + 'static,
  {
    Stream::from_step(move |n| {
      self.pull_raw(n).flat_map(move |step| match step {
        None => succeed(None),
        Some((items, rest)) => {
          let mode = if limit == 1 || items.len() <= 1 {
            Concurrency::Sequential
          } else {
            Concurrency::Bounded(limit)
          };
          let effects: Vec<Effect<B, E, R>> = items.into_iter().map(|a| f(a)).collect();
          fork_all(effects, mode).map(move |out| Some((out, rest.map_effect_par_arc(limit, f))))
        }
      })
    })
  }

  /// Transforms the typed error of every pull.
  pub fn map_err<E2, F>(self, f: F) -> Stream<A, E2, R>
  where
    E2: Send + 'static,
    F: Fn(E) -> E2 + Send + Sync + 'static,
  {
    self.map_err_arc(Arc::new(f))
  }

  fn map_err_arc<E2, F>(self, f: Arc<F>) -> Stream<A, E2, R>
  where
    E2: Send + 'static,
    F: Fn(E) -> E2 + Send + Sync + 'static,
  {
    match self.repr {
      Repr::Done => Stream::empty(),
      Repr::Pull(pull) => Stream::from_step(move |n| {
        let g = Arc::clone(&f);
        pull(n)
          .map_err(move |e| g(e))
          .map(move |step| step.map(|(items, rest)| (items, rest.map_err_arc(f))))
      }),
    }
  }

  /// Adds the item count of every chunk this stream yields to `counter` (combine with
  /// wall-clock samples for an items-per-second rate). Failed pulls count nothing.
  #[must_use]
  pub fn with_throughput_counter(self, counter: Arc<AtomicU64>) -> Self {
    match self.repr {
      Repr::Done => self,
      Repr::Pull(pull) => Self::from_step(move |n| {
        pull(n).map(move |step| {
          step.map(|(items, rest)| {
            counter.fetch_add(items.len() as u64, Ordering::Relaxed);
            (items, rest.with_throughput_counter(counter))
          })
        })
      }),
    }
  }

  // ---------------------------------------------------------------------------------------------
  // Consumers
  // ---------------------------------------------------------------------------------------------

  /// Folds whole chunks until the end or until `f` breaks; the core loop of every consumer.
  pub(crate) fn fold_chunks<B, F>(self, init: B, f: F) -> Effect<B, E, R>
  where
    B: Send + 'static,
    F: FnMut(B, Vec<A>) -> ControlFlow<B, B> + Send + 'static,
  {
    suspend(move || self.fold_chunks_now(init, f))
  }

  fn fold_chunks_now<B, F>(self, acc: B, mut f: F) -> Effect<B, E, R>
  where
    B: Send + 'static,
    F: FnMut(B, Vec<A>) -> ControlFlow<B, B> + Send + 'static,
  {
    self
      .pull_raw(DEFAULT_CHUNK_SIZE)
      .flat_map(move |step| match step {
        None => succeed(acc),
        Some((items, rest)) => {
          let empty = items.is_empty();
          match f(acc, items) {
            ControlFlow::Break(b) => succeed(b),
            ControlFlow::Continue(b) if empty => {
              yield_fiber().flat_map(move |()| rest.fold_chunks_now(b, f))
            }
            ControlFlow::Continue(b) => rest.fold_chunks_now(b, f),
          }
        }
      })
  }

  /// Drains the stream into a vector.
  pub fn run_collect(self) -> Effect<Vec<A>, E, R> {
    self.fold_chunks(Vec::new(), |mut acc, items| {
      acc.extend(items);
      ControlFlow::Continue(acc)
    })
  }

  /// Left fold with a pure step.
  pub fn run_fold<B, F>(self, init: B, mut f: F) -> Effect<B, E, R>
  where
    B: Send + 'static,
    F: FnMut(B, A) -> B + Send + 'static,
  {
    self.fold_chunks(init, move |acc, items| {
      ControlFlow::Continue(items.into_iter().fold(acc, &mut f))
    })
  }

  /// Left fold where each step is an effect (run one after the other).
  pub fn run_fold_effect<B, F>(self, init: B, f: F) -> Effect<B, E, R>
  where
    B: Send + 'static,
    F: FnMut(B, A) -> Effect<B, E, R> + Send + 'static,
  {
    suspend(move || self.fold_effect_now(init, f))
  }

  fn fold_effect_now<B, F>(self, acc: B, f: F) -> Effect<B, E, R>
  where
    B: Send + 'static,
    F: FnMut(B, A) -> Effect<B, E, R> + Send + 'static,
  {
    self
      .pull_raw(DEFAULT_CHUNK_SIZE)
      .flat_map(move |step| match step {
        None => succeed(acc),
        Some((items, rest)) => {
          let empty = items.is_empty();
          seq_fold(items.into_iter(), acc, f).flat_map(move |(acc, f)| {
            if empty {
              yield_fiber().flat_map(move |()| rest.fold_effect_now(acc, f))
            } else {
              rest.fold_effect_now(acc, f)
            }
          })
        }
      })
  }

  /// Runs `f` for each element, one after the other.
  pub fn run_for_each_effect<F>(self, mut f: F) -> Effect<(), E, R>
  where
    F: FnMut(A) -> Effect<(), E, R> + Send + 'static,
  {
    self.run_fold_effect((), move |(), a| f(a))
  }

  /// Alias of [`run_for_each_effect`](Self::run_for_each_effect).
  pub fn run_for_each<F>(self, f: F) -> Effect<(), E, R>
  where
    F: FnMut(A) -> Effect<(), E, R> + Send + 'static,
  {
    self.run_for_each_effect(f)
  }

  /// Binary reduction over the elements (`None` for an empty stream).
  pub fn run_reduce<F>(self, mut f: F) -> Effect<Option<A>, E, R>
  where
    F: FnMut(A, A) -> A + Send + 'static,
  {
    self.run_fold(None, move |acc, a| {
      Some(match acc {
        Some(cur) => f(cur, a),
        None => a,
      })
    })
  }

  /// Pulls the stream to its end, discarding the elements.
  pub fn run_drain(self) -> Effect<(), E, R> {
    self.fold_chunks((), |(), _| ControlFlow::Continue(()))
  }

  /// Runs `sink` (a [`Sink`](super::Sink) or a [`FoldSink`](super::FoldSink)) over this stream.
  pub fn run<B: Send + 'static>(self, sink: impl Into<super::Sink<B, A, E, R>>) -> Effect<B, E, R> {
    sink.into().run(self)
  }
}

impl<E, R> Stream<i64, E, R>
where
  E: Send + 'static,
  R: Send + Sync + 'static,
{
  /// The half-open range `[start, end_exclusive)`, produced lazily one chunk per pull.
  pub fn range(start: i64, end_exclusive: i64) -> Self {
    Self::from_iterable(start..end_exclusive)
  }
}

impl<A, R> Stream<A, Never, R>
where
  A: Send + 'static,
  R: Send + Sync + 'static,
{
  /// An infallible stream under any error type.
  pub fn widen<E: Send + 'static>(self) -> Stream<A, E, R> {
    self.map_err(|n| match n {})
  }
}

/// A source over `queue`: each pull takes what is buffered (up to the requested size, suspending
/// while the queue is empty) and `unpack`s it; once the queue is shut down and drained the stream
/// fails with the error in `failure`, if one was stored, and otherwise ends.
pub(crate) fn queue_source<T, A, E, R>(
  queue: Queue<T>,
  failure: FailSlot<E>,
  mut buf: VecDeque<A>,
  unpack: fn(Vec<T>) -> Vec<A>,
) -> Stream<A, E, R>
where
  T: Send + 'static,
  A: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
{
  Stream::from_step(move |n| {
    if !buf.is_empty() {
      let out: Vec<A> = buf.drain(..n.min(buf.len())).collect();
      return succeed(Some((out, queue_source(queue, failure, buf, unpack))));
    }
    queue
      .take_up_to::<R>(n)
      .map(Some)
      .catch(|_: QueueError| succeed(None))
      .flat_map(move |taken| match taken {
        Some(raw) => {
          let mut items = unpack(raw);
          let rest: VecDeque<A> = if items.len() > n {
            items.split_off(n).into()
          } else {
            VecDeque::new()
          };
          succeed(Some((items, queue_source(queue, failure, rest, unpack))))
        }
        None => match lock(&failure).take() {
          Some(e) => fail(e),
          None => succeed(None),
        },
      })
  })
}

/// The gathering part of an [`Stream::unfold_effect`] pull once a step is not a ready value:
/// one never-waiting async step that runs `next`, and the generator's following steps, as far
/// as each goes without waiting (see [`run_ready`]), on the pulling fiber's environment and
/// waker. The first step that would wait ends the pull with `out`, and the rest of the stream
/// resumes that step.
fn unfold_probe<A, S, F, E, R>(
  next: Effect<Option<(A, S)>, E, R>,
  f: F,
  n: usize,
  out: Vec<A>,
) -> Effect<Step<A, E, R>, E, R>
where
  A: Send + 'static,
  S: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
  F: FnMut(S) -> Effect<Option<(A, S)>, E, R> + Send + 'static,
{
  Effect::from_instr(Instr::Async(Box::new(move |env, fiber| {
    let env = Arc::clone(env);
    let fiber = Arc::clone(fiber);
    let mut state = Some((next, f, out));
    Box::pin(poll_fn(move |cx| {
      let (next, f, out) = state
        .take()
        .expect("id_effect: unfold probe polled after completion (kernel bug)");
      let ready = ProbeCx {
        env: &env,
        fiber: &fiber,
        cx,
      };
      Poll::Ready(erase_exit(unfold_ready(next, f, n, out, ready)))
    }))
  })))
}

/// What [`run_ready`] runs a step with: the pulling fiber's environment, the fiber (for its
/// interrupt flag) and the waker of the current poll.
struct ProbeCx<'a, 'b> {
  env: &'a AnyEnv,
  fiber: &'a FiberContext,
  cx: &'a mut Context<'b>,
}

/// The loop of [`unfold_probe`].
fn unfold_ready<A, S, F, E, R>(
  mut next: Effect<Option<(A, S)>, E, R>,
  mut f: F,
  n: usize,
  mut out: Vec<A>,
  mut ready: ProbeCx<'_, '_>,
) -> Result<Step<A, E, R>, Cause<E>>
where
  A: Send + 'static,
  S: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
  F: FnMut(S) -> Effect<Option<(A, S)>, E, R> + Send + 'static,
{
  loop {
    match run_ready(next, &mut ready) {
      Err(waiting) => return Ok(Some((out, Stream::unfold_resume(waiting, f)))),
      Ok(Ok(None)) => return Ok(Some((out, Stream::empty()))),
      Ok(Err(c)) => return Err(c),
      Ok(Ok(Some((a, s)))) => {
        out.push(a);
        if out.len() >= n {
          return Ok(Some((out, Stream::unfold_effect(s, f))));
        }
        next = f(s);
      }
    }
  }
}

/// Runs `eff` as far as it goes **without waiting**: its ready values, synchronous steps and
/// `map`/`flat_map`/`catch` frames under the environment, and each plain future it reaches (a
/// [`from_async`](crate::from_async) step) polled once with the current waker. `Ok` is its
/// outcome; `Err` is the rest of `eff` from the step that would wait (a future that returned
/// `Pending`, kept with its progress, or a step that needs the fiber, such as a timer, a fork or
/// a mask region, not started), to run later on any fiber.
///
/// Like the runloop, it polls no future once the fiber is asked to stop: the future is kept
/// unpolled in the rest, and the runloop observes the interrupt when this step returns.
fn run_ready<T, E, R>(
  eff: Effect<T, E, R>,
  ready: &mut ProbeCx<'_, '_>,
) -> Result<Result<T, Cause<E>>, Effect<T, E, R>>
where
  T: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
{
  let mut instr = match eff.node {
    Node::Pure(r) => return Ok(r),
    node => Effect { node }.into_instr(),
  };
  loop {
    let rest = match settle_instr(instr, ready.env) {
      Ok(exit) => return Ok(unerase_exit(exit)),
      Err(rest) => rest,
    };
    if ready.fiber.should_interrupt() {
      return Err(Effect::from_instr(rest));
    }
    instr = match rest {
      Instr::Future(mut fut) => match fut.as_mut().poll(ready.cx) {
        Poll::Ready(exit) => Instr::Pure(exit),
        Poll::Pending => return Err(Effect::from_instr(Instr::Future(fut))),
      },
      Instr::Seq(mut body) => {
        let Instr::Future(fut) = &mut body.inner else {
          return Err(Effect::from_instr(Instr::Seq(body)));
        };
        match fut.as_mut().poll(ready.cx) {
          Poll::Ready(exit) => body.inner = Instr::Pure(exit),
          Poll::Pending => return Err(Effect::from_instr(Instr::Seq(body))),
        }
        Instr::Seq(body)
      }
      other => return Err(Effect::from_instr(other)),
    };
  }
}

/// Runs `f` over `items` one effect after the other, collecting the outputs; hands `f` back.
fn seq_map<A, B, E, R, F>(
  mut items: std::vec::IntoIter<A>,
  mut f: F,
  mut out: Vec<B>,
) -> Effect<(Vec<B>, F), E, R>
where
  A: Send + 'static,
  B: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
  F: FnMut(A) -> Effect<B, E, R> + Send + 'static,
{
  match items.next() {
    None => succeed((out, f)),
    Some(a) => f(a).flat_map(move |b| {
      out.push(b);
      seq_map(items, f, out)
    }),
  }
}

/// Folds `items` with the effectful `f`, one step after the other; hands `f` back.
fn seq_fold<A, B, E, R, F>(
  mut items: std::vec::IntoIter<A>,
  acc: B,
  mut f: F,
) -> Effect<(B, F), E, R>
where
  A: Send + 'static,
  B: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
  F: FnMut(B, A) -> Effect<B, E, R> + Send + 'static,
{
  match items.next() {
    None => succeed((acc, f)),
    Some(a) => f(acc, a).flat_map(move |acc| seq_fold(items, acc, f)),
  }
}

/// The state machine of [`Stream::flat_map`]: the current inner stream, the outer elements not
/// expanded yet, and the rest of the outer stream.
fn flat_map_go<A, B, E, R, F>(
  outer: Stream<A, E, R>,
  mut pending: VecDeque<A>,
  inner: Option<Stream<B, E, R>>,
  mut f: F,
) -> Stream<B, E, R>
where
  A: Send + 'static,
  B: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
  F: FnMut(A) -> Stream<B, E, R> + Send + 'static,
{
  Stream::from_step(move |n| {
    if let Some(inner) = inner {
      return inner.pull_raw(n).flat_map(move |step| match step {
        Some((items, rest)) => succeed(Some((items, flat_map_go(outer, pending, Some(rest), f)))),
        None => flat_map_go(outer, pending, None, f).pull_raw(n),
      });
    }
    if let Some(a) = pending.pop_front() {
      // Recursing once is bounded: the `inner` branch above only builds the inner pull.
      let inner = f(a);
      return flat_map_go(outer, pending, Some(inner), f).pull_raw(n);
    }
    outer.pull_raw(n).flat_map(move |step| match step {
      None => succeed(None),
      Some((items, rest)) => flat_map_go(rest, items.into(), None, f).pull_raw(n),
    })
  })
}

/// Pure bulk map: Fabric-parallel (Rayon) when the chunk is large enough, else in order.
fn bulk_map<A: Send, B: Send, F: Fn(A) -> B + Sync>(items: Vec<A>, f: &F) -> Vec<B> {
  #[cfg(feature = "compute")]
  if crate::compute::should_parallelize(items.len()) {
    use rayon::prelude::*;
    return crate::compute::install_parallel(|| items.into_par_iter().map(f).collect());
  }
  items.into_iter().map(f).collect()
}

/// Pure bulk filter: Fabric-parallel (Rayon) when the chunk is large enough, else in order.
fn bulk_filter<A: Send, P: Fn(&A) -> bool + Sync>(items: Vec<A>, p: &P) -> Vec<A> {
  #[cfg(feature = "compute")]
  if crate::compute::should_parallelize(items.len()) {
    use rayon::prelude::*;
    return crate::compute::install_parallel(|| items.into_par_iter().filter(|a| p(a)).collect());
  }
  items.into_iter().filter(|a| p(a)).collect()
}