pub enum GeneratorState {
Yielded(Value),
Complete(Value),
}
Expand description
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(rune::to_value(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(rune::to_value(42i64)?).into_result()? {
GeneratorState::Complete(ret) => rune::from_value::<i64>(ret)?,
GeneratorState::Yielded(..) => panic!("generator yielded"),
};
assert_eq!(ret, 42);
Variants§
Implementations§
Source§impl GeneratorState
impl GeneratorState
Sourcepub fn is_yielded(&self) -> bool
pub fn is_yielded(&self) -> bool
Test if the state is yielded.
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Test if the state is complete.
Trait Implementations§
Source§impl Any for GeneratorState
impl Any for GeneratorState
Source§const ANY_TYPE_INFO: AnyTypeInfo = _
const ANY_TYPE_INFO: AnyTypeInfo = _
The compile-time type information know for the type.
Source§impl Debug for GeneratorState
impl Debug for GeneratorState
Source§impl InstallWith for GeneratorState
impl InstallWith for GeneratorState
Source§fn install_with(module: &mut Module) -> Result<(), ContextError>
fn install_with(module: &mut Module) -> Result<(), ContextError>
Hook to install more things into the module.
Source§impl IntoOutput for GeneratorState
impl IntoOutput for GeneratorState
Source§fn into_output(self) -> Result<Value, RuntimeError>
fn into_output(self) -> Result<Value, RuntimeError>
Coerce the current value into an output.
Source§impl MaybeTypeOf for GeneratorState
impl MaybeTypeOf for GeneratorState
Source§fn maybe_type_of() -> Result<DocType>
fn maybe_type_of() -> Result<DocType>
Type information for the given type.
Source§impl Named for GeneratorState
impl Named for GeneratorState
Source§impl TryClone for GeneratorState
impl TryClone for GeneratorState
Source§impl TryFrom<GeneratorState> for Value
impl TryFrom<GeneratorState> for Value
Source§impl TypeHash for GeneratorState
impl TypeHash for GeneratorState
Source§impl TypeOf for GeneratorState
impl TypeOf for GeneratorState
Source§const PARAMETERS: Hash = _
const PARAMETERS: Hash = _
Type parameters for the type. Read more
Source§const STATIC_TYPE_INFO: AnyTypeInfo = <Self as rune::Any>::ANY_TYPE_INFO
const STATIC_TYPE_INFO: AnyTypeInfo = <Self as rune::Any>::ANY_TYPE_INFO
Access diagnostical type information for the current type. Read more
Source§impl UnsafeToMut for GeneratorState
impl UnsafeToMut for GeneratorState
Source§type Guard = RawValueGuard
type Guard = RawValueGuard
The raw guard returned. Read more
Source§unsafe fn unsafe_to_mut<'a>(
value: Value,
) -> Result<(&'a mut Self, Self::Guard), RuntimeError>
unsafe fn unsafe_to_mut<'a>( value: Value, ) -> Result<(&'a mut Self, Self::Guard), RuntimeError>
Safety Read more
Source§impl UnsafeToRef for GeneratorState
impl UnsafeToRef for GeneratorState
Source§type Guard = RawValueGuard
type Guard = RawValueGuard
The raw guard returned. Read more
Source§unsafe fn unsafe_to_ref<'a>(
value: Value,
) -> Result<(&'a Self, Self::Guard), RuntimeError>
unsafe fn unsafe_to_ref<'a>( value: Value, ) -> Result<(&'a Self, Self::Guard), RuntimeError>
Safety Read more
Source§impl UnsafeToValue for &GeneratorState
impl UnsafeToValue for &GeneratorState
Source§type Guard = ValueRefGuard
type Guard = ValueRefGuard
The type used to guard the unsafe value conversion.
Source§unsafe fn unsafe_to_value(self) -> Result<(Value, Self::Guard), RuntimeError>
unsafe fn unsafe_to_value(self) -> Result<(Value, Self::Guard), RuntimeError>
Convert into a value. Read more
Source§impl UnsafeToValue for &mut GeneratorState
impl UnsafeToValue for &mut GeneratorState
Source§type Guard = ValueMutGuard
type Guard = ValueMutGuard
The type used to guard the unsafe value conversion.
Source§unsafe fn unsafe_to_value(self) -> Result<(Value, Self::Guard), RuntimeError>
unsafe fn unsafe_to_value(self) -> Result<(Value, Self::Guard), RuntimeError>
Convert into a value. Read more
Auto Trait Implementations§
impl Freeze for GeneratorState
impl !RefUnwindSafe for GeneratorState
impl !Send for GeneratorState
impl !Sync for GeneratorState
impl Unpin for GeneratorState
impl !UnwindSafe for GeneratorState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> FromValue for Twhere
T: AnyMarker,
impl<T> FromValue for Twhere
T: AnyMarker,
Source§fn from_value(value: Value) -> Result<T, RuntimeError>
fn from_value(value: Value) -> Result<T, RuntimeError>
Try to convert to the given type, from the given value.
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> TryToOwned for Twhere
T: TryClone,
impl<T> TryToOwned for Twhere
T: TryClone,
Source§impl<T> UnsafeToValue for Twhere
T: ToValue,
impl<T> UnsafeToValue for Twhere
T: ToValue,
Source§unsafe fn unsafe_to_value(
self,
) -> Result<(Value, <T as UnsafeToValue>::Guard), RuntimeError>
unsafe fn unsafe_to_value( self, ) -> Result<(Value, <T as UnsafeToValue>::Guard), RuntimeError>
Convert into a value. Read more