runestick/
generator_state.rs1use crate::{FromValue, Mut, RawMut, RawRef, Ref, Shared, UnsafeFromValue, Value, VmError};
2
3#[derive(Debug)]
5pub enum GeneratorState {
6 Yielded(Value),
8 Complete(Value),
10}
11
12impl GeneratorState {
13 pub fn is_yielded(&self) -> bool {
15 matches!(self, Self::Yielded(..))
16 }
17
18 pub fn is_complete(&self) -> bool {
20 matches!(self, Self::Complete(..))
21 }
22}
23
24impl FromValue for Shared<GeneratorState> {
25 fn from_value(value: Value) -> Result<Self, VmError> {
26 Ok(value.into_generator_state()?)
27 }
28}
29
30impl FromValue for GeneratorState {
31 fn from_value(value: Value) -> Result<Self, VmError> {
32 let state = value.into_generator_state()?;
33 Ok(state.take()?)
34 }
35}
36
37impl UnsafeFromValue for &GeneratorState {
38 type Output = *const GeneratorState;
39 type Guard = RawRef;
40
41 fn from_value(value: Value) -> Result<(Self::Output, Self::Guard), VmError> {
42 let state = value.into_generator_state()?;
43 let (state, guard) = Ref::into_raw(state.into_ref()?);
44 Ok((state, guard))
45 }
46
47 unsafe fn unsafe_coerce(output: Self::Output) -> Self {
48 &*output
49 }
50}
51
52impl UnsafeFromValue for &mut GeneratorState {
53 type Output = *mut GeneratorState;
54 type Guard = RawMut;
55
56 fn from_value(value: Value) -> Result<(Self::Output, Self::Guard), VmError> {
57 let state = value.into_generator_state()?;
58 Ok(Mut::into_raw(state.into_mut()?))
59 }
60
61 unsafe fn unsafe_coerce(output: Self::Output) -> Self {
62 &mut *output
63 }
64}