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
use crate::{
    bind, intercept_input, map, map_input, map_output, receive, recieve_until, result, right,
    run_step, send, subroutine, suspend, tuple, Coroutine, StepResult,
};

/// A selection for which coroutine to route to
pub enum Select<A, B, C> {
    Left(A),
    Right(B),
    Both(C),
}

/// Represents the result of running the left and right coroutines
/// Returns whichever coroutine finished first
pub enum DispatchResult<'a, IA, IB, OA, OB, A, B> {
    Left {
        value: A,
        remaining: Coroutine<'a, IB, OB, B>,
    },
    Right {
        value: B,
        remaining: Coroutine<'a, IA, OA, A>,
    },
}

type DispatchRoutine<'a, IA, IB, IAB, OA, OB, A, B> = Coroutine<
    'a,
    Select<IA, IB, IAB>,
    UnicastSelect<OA, OB>,
    DispatchResult<'a, IA, IB, OA, OB, A, B>,
>;
/// Run two co-routines, sharing inputs depending on selector.
///
/// This can be thought of as running them almost in parralel.
/// Selector will route the input as needed to coroutines a and b
/// Values that can be sent to both, if the selector selects both
/// and the both value is cloneable and convertable
pub fn dispatch<'a, IA, IB, IAB, OA, OB, A, B>(
    first: Coroutine<'a, IA, OA, A>,
    second: Coroutine<'a, IB, OB, B>,
) -> DispatchRoutine<'a, IA, IB, IAB, OA, OB, A, B>
where
    IAB: Into<IA> + Into<IB> + Clone,
    OA: Send,
    OB: Send,
    A: Send,
    B: Send,
{
    let s1 = run_step(first);
    let s2 = run_step(second);

    match (s1, s2) {
        (StepResult::Done(value), StepResult::Done(b)) => {
            let ret = DispatchResult::Left {
                value,
                remaining: result(b),
            };
            result(ret)
        }
        (StepResult::Done(value), StepResult::Yield { output, next }) => {
            let remaining = *next;
            let race = DispatchResult::Left { value, remaining };
            right(send(UnicastSelect::Right(output)), result(race))
        }
        (StepResult::Done(value), StepResult::Next(next)) => {
            let remaining = suspend(next);
            result(DispatchResult::Left { value, remaining })
        }
        (StepResult::Yield { output, next }, StepResult::Done(value)) => {
            let remaining = *next;
            let race = DispatchResult::Right { value, remaining };
            right(send(UnicastSelect::Left(output)), result(race))
        }
        (
            StepResult::Yield {
                output: a,
                next: na,
            },
            StepResult::Yield {
                output: b,
                next: nb,
            },
        ) => {
            let send = tuple(send(UnicastSelect::Left(a)), send(UnicastSelect::Right(b)));
            let next = dispatch(*na, *nb);
            right(send, next)
        }
        (StepResult::Yield { output, next: a }, StepResult::Next(b)) => {
            let send = send(UnicastSelect::Left(output));
            let next = dispatch(*a, suspend(b));
            right(send, next)
        }
        (StepResult::Next(a), StepResult::Done(value)) => {
            let remaining = suspend(a);
            let race = DispatchResult::Right { value, remaining };
            result(race)
        }
        (StepResult::Next(a), StepResult::Yield { output, next }) => {
            let send = send(UnicastSelect::Right(output));
            let a = suspend(a);
            let b = *next;
            let next = dispatch(a, b);
            right(send, next)
        }
        (StepResult::Next(a), StepResult::Next(b)) => {
            let on_input = |input: Select<IA, IB, IAB>| match input {
                Select::Left(ia) => {
                    let a = a(ia);
                    dispatch(a, suspend(b))
                }
                Select::Right(ib) => {
                    let b = b(ib);
                    dispatch(suspend(a), b)
                }
                Select::Both(iab) => {
                    let ia = iab.clone().into();
                    let ib = iab.into();
                    let a = a(ia);
                    let b = b(ib);
                    dispatch(a, b)
                }
            };
            bind(receive(), on_input)
        }
    }
}

pub type BroadcastRoutine<'a, I, OA, OB, A, B> =
    Coroutine<'a, I, UnicastSelect<OA, OB>, DispatchResult<'a, I, I, OA, OB, A, B>>;
/// Sends inputs to both coroutines, and will emit outputs together
///
/// This is a more generic form of dispatch
/// in which we want inputs send to both routines always
pub fn broadcast<'a, I, OA, OB, A, B>(
    first: Coroutine<'a, I, OA, A>,
    second: Coroutine<'a, I, OB, B>,
) -> BroadcastRoutine<'a, I, OA, OB, A, B>
where
    I: Clone,
    A: Send,
    B: Send,
    OA: Send,
    OB: Send,
{
    fn both_selector<I>(input: I) -> Select<I, I, I> {
        Select::Both(input)
    }
    map_input(dispatch(first, second), both_selector)
}

/// Sends inputs to both coroutines, and will emit outputs together
///
/// If one finishes first, the other will consume the inputs until it is finished.
/// Both routines must finish to return
/// Input must be cloneable as it will need to feed both
/// This is similar to broadcast, but continues running the 'last' routine
pub fn broadcast_until_finished<'a, I, O, A, B>(
    first: Coroutine<'a, I, O, A>,
    second: Coroutine<'a, I, O, B>,
) -> Coroutine<'a, I, O, (A, B)>
where
    I: Clone,
    A: Send,
    B: Send,
    O: Send,
{
    let rr = broadcast(first, second);
    let rr = map_output(rr, |input| match input {
        UnicastSelect::Left(a) => a,
        UnicastSelect::Right(a) => a,
    });
    let on_result = |res| match res {
        DispatchResult::Left { value, remaining } => map(remaining, |b| (value, b)),
        DispatchResult::Right { value, remaining } => map(remaining, |a| (a, value)),
    };
    bind(rr, on_result)
}

/// A more specific version of select, where messages are exclusive
///
/// This allows you to not have to deal with A or B being cloneable
pub enum UnicastSelect<A, B> {
    Left(A),
    Right(B),
}

/// Run two co-routines, sharing inputs depending on selector.
///
/// This can be thought of as running them almost in parralel.
/// Selector will route the input as needed to coroutines a and b
/// This variant must send inputs to either first or second
/// it does not share them. See dispatch if you need to share values
pub fn unicast<'a, IA, IB, OA, OB, A, B>(
    first: Coroutine<'a, IA, OA, A>,
    second: Coroutine<'a, IB, OB, B>,
) -> Coroutine<
    'a,
    UnicastSelect<IA, IB>,
    UnicastSelect<OA, OB>,
    DispatchResult<'a, IA, IB, OA, OB, A, B>,
>
where
    OA: Send,
    OB: Send,
    A: Send,
    B: Send,
{
    // Private never type. Used for some trickery in unicast to cover into
    // for a type never used
    #[derive(Clone)]
    enum Never {}

    // Allows us to provide an impl for Never to wrapped t
    // This is an exception, but is never constructed. So is safe
    struct Wrapped<T>(T);
    impl<T> From<Never> for Wrapped<T> {
        fn from(_: Never) -> Self {
            unreachable!("This should never be called")
        }
    }

    // Selector will never create Both, thus never and Into Impls are safe
    let selector_ = move |input| -> Select<Wrapped<IA>, Wrapped<IB>, Never> {
        match input {
            UnicastSelect::Left(l) => Select::Left(Wrapped(l)),
            UnicastSelect::Right(r) => Select::Right(Wrapped(r)),
        }
    };

    // We need to convert out of the 'wrapped' inputs. So we change normal inputs to wrapped ones
    let extract = |dr| match dr {
        DispatchResult::Left { value, remaining } => {
            let remaining = intercept_input(remaining, |input| result(Wrapped(input)));
            DispatchResult::Left { value, remaining }
        }
        DispatchResult::Right { value, remaining } => {
            let remaining = intercept_input(remaining, |input| result(Wrapped(input)));
            DispatchResult::Right { value, remaining }
        }
    };

    // Extract from the wrappers to pass to lower levels
    let first = intercept_input(first, |input: Wrapped<IA>| result(input.0));
    let second = intercept_input(second, |input: Wrapped<IB>| result(input.0));
    let both = map_input(dispatch(first, second), selector_);
    map(both, extract)
}

/// Unicast until both routines are completed
///
/// Unlike broadcast, there is an issue with completing unicast routines
/// If IA is finished, the rest of its inputs will be thrown away/ignored
/// Be careful, as this could cause the routines to never complete
pub fn unicast_until_finished<'a, IA, IB, O, A, B>(
    first: Coroutine<'a, IA, O, A>,
    second: Coroutine<'a, IB, O, B>,
) -> Coroutine<'a, UnicastSelect<IA, IB>, O, (A, B)>
where
    O: Send,
    A: Send,
    B: Send,
{
    let is_ib = move |input| match input {
        UnicastSelect::Left(_) => result(None),
        UnicastSelect::Right(b) => result(Some(b)),
    };
    let is_ia = move |input| match input {
        UnicastSelect::Left(a) => result(Some(a)),
        UnicastSelect::Right(_) => result(None),
    };

    // These ultimately throw away inputs that aren't for them
    let on_ib_input = move || recieve_until(is_ib);
    let on_ia_input = move || recieve_until(is_ia);

    // This will finish-of, the 'loser' coroutine. Thus getting the values tupled together
    let on_result = move |r| match r {
        DispatchResult::Left { value, remaining } => {
            let remaining = subroutine(on_ib_input, send, remaining);
            map(remaining, |b| (value, b))
        }
        DispatchResult::Right { value, remaining } => {
            let remaining = subroutine(on_ia_input, send, remaining);
            map(remaining, |a| (a, value))
        }
    };
    let ur = unicast(first, second);
    let ur = map_output(ur, |o| match o {
        UnicastSelect::Left(o) => o,
        UnicastSelect::Right(o) => o,
    });
    bind(ur, on_result)
}