rune/runtime/
call.rs
1use core::fmt;
2
3use musli::{Decode, Encode};
4use serde::{Deserialize, Serialize};
5
6use crate as rune;
7use crate::alloc::prelude::*;
8use crate::runtime::{Future, Generator, Stream, Value, Vm, VmResult};
9
10#[derive(Debug, TryClone, Clone, Copy, Serialize, Deserialize, Encode, Decode)]
12#[try_clone(copy)]
13#[non_exhaustive]
14pub enum Call {
15 Async,
18 Immediate,
20 Stream,
22 Generator,
24}
25
26impl Call {
27 #[inline]
29 pub(crate) fn call_with_vm(self, vm: Vm) -> VmResult<Value> {
30 VmResult::Ok(match self {
31 Call::Stream => vm_try!(Value::try_from(Stream::new(vm))),
32 Call::Generator => vm_try!(Value::try_from(Generator::new(vm))),
33 Call::Immediate => vm_try!(vm.complete()),
34 Call::Async => {
35 let mut execution = vm.into_execution();
36 let future = vm_try!(Future::new(async move { execution.async_complete().await }));
37 vm_try!(Value::try_from(future))
38 }
39 })
40 }
41}
42
43impl fmt::Display for Call {
44 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
45 match self {
46 Self::Immediate => {
47 write!(fmt, "immediate")?;
48 }
49 Self::Async => {
50 write!(fmt, "async")?;
51 }
52 Self::Stream => {
53 write!(fmt, "stream")?;
54 }
55 Self::Generator => {
56 write!(fmt, "generator")?;
57 }
58 }
59
60 Ok(())
61 }
62}