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
//! Parallel combinators on fibers: `zip_par`, `all` and `for_each_par` under a
//! [`Concurrency`] policy.
//!
//! A failure fails fast and interrupts the siblings; `Bounded(n)` caps the number in flight;
//! `for_each_par` keeps input order; `Sequential` behaves like a fold.
//!
//! Every combinator here is a thin layer over [`fork_all`], the one choke point for parallel
//! work (ADR 0011 "Parallel combinators"), so they share its semantics:
//!
//! - children are child fibers of the current fiber and inherit its `FiberRef`s;
//! - the first failure interrupts the in-flight siblings, starts no further ones, and waits
//! until every started child has completed (their finalizers have run) before failing;
//! - the result's cause is that first failure, with any *genuine* sibling failure (`Fail` or
//! `Die`) combined in as [`Cause::Both`](crate::Cause::Both); the interrupts of siblings
//! that were stopped because of the first failure are dropped (Effect.ts `Effect.all`);
//! - interrupting the caller interrupts every child.
//!
//! `Concurrency::Bounded(n)` is a purely local window. Only `Concurrency::Inherit` awaits
//! admission from the ambient [`Fabric`](crate::Fabric) (installed with `Fabric::provide`,
//! at `fork_all`'s single admission point); with no fabric it behaves as `Unbounded`.
//!
//! ```
//! use std::time::Duration;
//! use id_effect::Exit;
//! use id_effect::{Concurrency, Effect, all, for_each_par, sleep, succeed, zip_par};
//! use id_effect::Never;
//!
//! let pair: Effect<(u8, &str), Never> = zip_par(
//! sleep(Duration::from_millis(5)).then(succeed(1)),
//! succeed("two"),
//! );
//! assert!(matches!(pair.block_on(), Exit::Success((1, "two"))));
//!
//! let squares = for_each_par(1..=4_u32, |n| succeed::<_, Never, _>(n * n), Concurrency::Bounded(2));
//! assert!(matches!(squares.block_on(), Exit::Success(v) if v == [1, 4, 9, 16]));
//!
//! let effs: Vec<Effect<u32, Never>> = vec![succeed(1), succeed(2)];
//! assert!(matches!(all(effs, Concurrency::Sequential).block_on(), Exit::Success(v) if v == [1, 2]));
//! ```
use crate;
use crateEffect;
/// One side of a [`zip_with_par`]: both sides go through one homogeneous [`fork_all`].
/// Runs `left` and `right` concurrently (as two child fibers) and pairs their values.
///
/// The first failure interrupts the other side; see the [module docs](self) for the cause.
/// Runs `left` and `right` concurrently and combines their values with `f`.
///
/// The first failure interrupts the other side; see the [module docs](self) for the cause.
/// Runs `effects` under `concurrency` and collects their values in input order.
///
/// `Sequential` is a left fold on the current fiber (nothing is forked, it stops at the first
/// failure); the other modes fork one child fiber per effect. See the [module docs](self).
/// Maps every item of `items` to an effect with `f`, runs them under `concurrency`, and
/// collects the values in input order (not completion order).
///
/// `f` is called for every item up front, when the combinator is built; the effects themselves
/// run only when the result runs. See [`all`].