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
use crate as rune;
use crate::runtime::{ProtocolCaller, Value, VmResult};
use crate::Any;

/// The state of a generator.
///
/// ```
/// use rune::{Value, Vm};
/// use rune::runtime::{Generator, GeneratorState};
/// use std::sync::Arc;
///
/// let mut sources = rune::sources! {
///     entry => {
///         pub fn main() {
///             let n = yield 1;
///             let out = yield n + 1;
///             out
///         }
///     }
/// };
///
/// let unit = rune::prepare(&mut sources).build()?;
///
/// let mut vm = Vm::without_runtime(Arc::new(unit));
/// let mut execution = vm.execute(["main"], ())?;
///
/// // Initial resume doesn't take a value.
/// let first = match execution.resume().into_result()? {
///     GeneratorState::Yielded(first) => rune::from_value::<i64>(first)?,
///     GeneratorState::Complete(..) => panic!("generator completed"),
/// };
///
/// assert_eq!(first, 1);
///
/// // Additional resumes require a value.
/// let second = match execution.resume_with(Value::from(2i64)).into_result()? {
///     GeneratorState::Yielded(second) => rune::from_value::<i64>(second)?,
///     GeneratorState::Complete(..) => panic!("generator completed"),
/// };
///
/// assert_eq!(second, 3);
///
/// let ret = match execution.resume_with(Value::from(42i64)).into_result()? {
///     GeneratorState::Complete(ret) => rune::from_value::<i64>(ret)?,
///     GeneratorState::Yielded(..) => panic!("generator yielded"),
/// };
///
/// assert_eq!(ret, 42);
/// # Ok::<_, rune::support::Error>(())
/// ```
#[derive(Any, Debug)]
#[rune(builtin, static_type = GENERATOR_STATE_TYPE)]
pub enum GeneratorState {
    /// The generator yielded.
    Yielded(Value),
    /// The generator completed.
    Complete(Value),
}

impl GeneratorState {
    /// Test if the state is yielded.
    pub fn is_yielded(&self) -> bool {
        matches!(self, Self::Yielded(..))
    }

    /// Test if the state is complete.
    pub fn is_complete(&self) -> bool {
        matches!(self, Self::Complete(..))
    }

    pub(crate) fn partial_eq_with(
        &self,
        other: &Self,
        caller: &mut impl ProtocolCaller,
    ) -> VmResult<bool> {
        match (self, other) {
            (GeneratorState::Yielded(a), GeneratorState::Yielded(b)) => {
                Value::partial_eq_with(a, b, caller)
            }
            (GeneratorState::Complete(a), GeneratorState::Complete(b)) => {
                Value::partial_eq_with(a, b, caller)
            }
            _ => VmResult::Ok(false),
        }
    }

    pub(crate) fn eq_with(&self, other: &Self, caller: &mut impl ProtocolCaller) -> VmResult<bool> {
        match (self, other) {
            (GeneratorState::Yielded(a), GeneratorState::Yielded(b)) => {
                Value::eq_with(a, b, caller)
            }
            (GeneratorState::Complete(a), GeneratorState::Complete(b)) => {
                Value::eq_with(a, b, caller)
            }
            _ => VmResult::Ok(false),
        }
    }
}

from_value!(GeneratorState, into_generator_state);