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
//! A sample executor that consumes an iterator
//!
//! It's not necessary to use this, as run_step is all you need if rolling your own
//! but it's a good reference, and is fairly generally useable

use crate::*;

pub enum IteratorExecutorResult<'a, Iter, Input, Output, Result> {
    /// The coroutine has finished
    Completed {
        /// The final result of the coroutine
        result: Result,
        /// What remains of the input
        remaining: Iter,
    },
    /// We ran out of inputs, returns a coroutine to continue when more inputs are
    /// available
    OutOfInputs(Coroutine<'a, Input, Output, Result>),
}

/// Consumes a coroutine and runs it with the iterated events
/// This may run to completion, or may consume all the inputs
/// The on_output function is called whenever a output is produced
/// Note: it's expected that shouldn't ever panic
/// ```
/// use bicoro::*;
/// use bicoro::executor::*;
///
/// // sample coroutine
/// let co : Coroutine<i32,i32,()> = receive().and_then(|i| send(i));
///
/// // inputs to send
/// let inputs = vec![1];
/// let mut outputs = vec![];
/// let on_output = |output:i32| outputs.push(output);
///
/// let exec = execute_from_iter(co,on_output,inputs.into_iter());
///
/// assert!(matches!(exec, IteratorExecutorResult::Completed{ result: (),..}));
/// assert_eq!(outputs, vec![1]);
/// ```
pub fn execute_from_iter<'a, Iter, Input: 'a, Output: 'a, OnOutput, Result: 'a>(
    mut routine: Coroutine<'a, Input, Output, Result>,
    mut on_output: OnOutput,
    mut events: Iter,
) -> IteratorExecutorResult<'a, Iter, Input, Output, Result>
where
    Iter: Iterator<Item = Input>,
    OnOutput: FnMut(Output),
{
    loop {
        match run_step(routine) {
            StepResult::Done(result) => {
                return IteratorExecutorResult::Completed {
                    result,
                    remaining: events,
                }
            }
            StepResult::Yield { output, next } => {
                on_output(output);
                routine = *next;
            }
            StepResult::Next(next) => {
                if let Some(event) = events.next() {
                    routine = next(event);
                } else {
                    let next = suspend(next);
                    return IteratorExecutorResult::OutOfInputs(next);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    type Co<I, O, R> = Coroutine<'static, I, O, R>;
    #[test]
    fn not_enough_input_data() {
        let test: Co<i32, (), i32> = receive();
        let inputs = vec![];
        let mut output = vec![];
        let on_output = |o| output.push(o);

        let exec = execute_from_iter(test, on_output, inputs.into_iter());

        assert!(matches!(exec, IteratorExecutorResult::OutOfInputs(_)));
        assert_eq!(output, vec![]);
    }

    #[test]
    fn instantly_completes() {
        let test: Co<(), (), i32> = result(1);
        let inputs = vec![];
        let mut output = vec![];
        let on_output = |o| output.push(o);

        let exec = execute_from_iter(test, on_output, inputs.into_iter());

        assert!(matches!(
            exec,
            IteratorExecutorResult::Completed { result: 1, .. }
        ));
        assert_eq!(output, vec![]);
    }

    #[test]
    fn send_writes_to_vec() {
        let test: Co<(), i32, ()> = send(1);
        let inputs = vec![];
        let mut output = vec![];
        let on_output = |o| output.push(o);

        let exec = execute_from_iter(test, on_output, inputs.into_iter());

        assert!(matches!(
            exec,
            IteratorExecutorResult::Completed { result: (), .. }
        ));
        assert_eq!(output, vec![1]);
    }

    #[test]
    fn send_writes_multiple_to_vec() {
        let test: Co<(), i32, ()> = send(1).and_then(|()| send(2));
        let inputs = vec![];
        let mut output = vec![];
        let on_output = |o| output.push(o);

        let exec = execute_from_iter(test, on_output, inputs.into_iter());

        assert!(matches!(
            exec,
            IteratorExecutorResult::Completed { result: (), .. }
        ));
        assert_eq!(output, vec![1, 2]);
    }
}