use crate as rune;
use crate::alloc::fmt::TryWrite;
use crate::alloc::prelude::*;
use crate::runtime::generator::Iter;
use crate::runtime::{EnvProtocolCaller, Formatter, Generator, GeneratorState, Value, VmResult};
use crate::{ContextError, Module};
#[rune::module(::std::ops::generator)]
pub fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
{
m.ty::<Generator>()?;
m.function_meta(generator_next__meta)?;
m.function_meta(generator_resume__meta)?;
m.function_meta(generator_iter__meta)?;
m.function_meta(generator_into_iter__meta)?;
m.function_meta(generator_debug__meta)?;
m.function_meta(generator_clone__meta)?;
m.implement_trait::<Generator>(rune::item!(::std::clone::Clone))?;
m.ty::<Iter>()?.docs(docstring! {
})?;
m.function_meta(Iter::next__meta)?;
m.implement_trait::<Iter>(rune::item!(::std::iter::Iterator))?;
}
{
m.ty::<GeneratorState>()?.docs(docstring! {
})?;
m.function_meta(generator_state_partial_eq__meta)?;
m.implement_trait::<GeneratorState>(rune::item!(::std::cmp::PartialEq))?;
m.function_meta(generator_state_eq__meta)?;
m.implement_trait::<GeneratorState>(rune::item!(::std::cmp::Eq))?;
m.function_meta(generator_state_debug__meta)?;
m.function_meta(generator_state_clone__meta)?;
m.implement_trait::<GeneratorState>(rune::item!(::std::clone::Clone))?;
}
Ok(m)
}
#[rune::function(keep, instance, path = next)]
fn generator_next(this: &mut Generator) -> VmResult<Option<Value>> {
this.next()
}
#[rune::function(keep, instance, path = resume)]
fn generator_resume(this: &mut Generator, value: Value) -> VmResult<GeneratorState> {
this.resume(value)
}
#[rune::function(keep, instance, path = iter)]
#[inline]
fn generator_iter(this: Generator) -> Iter {
this.rune_iter()
}
#[rune::function(keep, instance, protocol = INTO_ITER)]
#[inline]
fn generator_into_iter(this: Generator) -> Iter {
this.rune_iter()
}
#[rune::function(keep, instance, protocol = DEBUG_FMT)]
fn generator_debug(this: &Generator, f: &mut Formatter) -> VmResult<()> {
vm_write!(f, "{this:?}")
}
#[rune::function(keep, instance, protocol = CLONE)]
fn generator_clone(this: &Generator) -> VmResult<Generator> {
VmResult::Ok(vm_try!(this.try_clone()))
}
#[rune::function(keep, instance, protocol = PARTIAL_EQ)]
fn generator_state_partial_eq(this: &GeneratorState, other: &GeneratorState) -> VmResult<bool> {
this.partial_eq_with(other, &mut EnvProtocolCaller)
}
#[rune::function(keep, instance, protocol = EQ)]
fn generator_state_eq(this: &GeneratorState, other: &GeneratorState) -> VmResult<bool> {
this.eq_with(other, &mut EnvProtocolCaller)
}
#[rune::function(keep, instance, protocol = DEBUG_FMT)]
fn generator_state_debug(this: &GeneratorState, f: &mut Formatter) -> VmResult<()> {
match this {
GeneratorState::Yielded(value) => {
vm_try!(vm_write!(f, "Yielded("));
vm_try!(value.debug_fmt_with(f, &mut EnvProtocolCaller));
vm_try!(vm_write!(f, ")"));
}
GeneratorState::Complete(value) => {
vm_try!(vm_write!(f, "Complete("));
vm_try!(value.debug_fmt_with(f, &mut EnvProtocolCaller));
vm_try!(vm_write!(f, ")"));
}
}
VmResult::Ok(())
}
#[rune::function(keep, instance, protocol = CLONE)]
fn generator_state_clone(this: &GeneratorState) -> VmResult<GeneratorState> {
VmResult::Ok(vm_try!(this.try_clone()))
}