rune/runtime/
call.rs

1use core::fmt;
2
3#[cfg(feature = "musli")]
4use musli::{Decode, Encode};
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8use crate as rune;
9use crate::alloc::prelude::*;
10use crate::runtime::{Future, Generator, Stream, Value, Vm, VmError};
11
12/// The calling convention of a function.
13#[derive(Debug, TryClone, Clone, Copy)]
14#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
15#[cfg_attr(feature = "musli", derive(Encode, Decode))]
16#[try_clone(copy)]
17#[non_exhaustive]
18pub enum Call {
19    /// Function is `async` and returns a future that must be await:ed to make
20    /// progress.
21    Async,
22    /// Functions are immediately called and control handed over.
23    Immediate,
24    /// Function produces a stream, also known as an async generator.
25    Stream,
26    /// Function produces a generator.
27    Generator,
28}
29
30impl Call {
31    /// Perform the call with the given virtual machine.
32    #[inline]
33    pub(crate) fn call_with_vm(self, vm: Vm) -> Result<Value, VmError> {
34        Ok(match self {
35            Call::Stream => Value::try_from(Stream::new(vm))?,
36            Call::Generator => Value::try_from(Generator::new(vm))?,
37            Call::Immediate => vm.complete()?,
38            Call::Async => {
39                let mut execution = vm.into_execution();
40                let future = Future::new(async move { execution.resume().await?.into_complete() })?;
41                Value::try_from(future)?
42            }
43        })
44    }
45}
46
47impl fmt::Display for Call {
48    #[inline]
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            Self::Immediate => {
52                write!(f, "immediate")
53            }
54            Self::Async => {
55                write!(f, "async")
56            }
57            Self::Stream => {
58                write!(f, "stream")
59            }
60            Self::Generator => {
61                write!(f, "generator")
62            }
63        }
64    }
65}