use async_trait::async_trait;
use endbasic_core::*;
use futures_lite::future::yield_now;
use std::borrow::Cow;
use std::rc::Rc;
pub(super) struct AsyncIncrementFunction {
metadata: Rc<CallableMetadata>,
}
impl AsyncIncrementFunction {
pub(super) fn new() -> Rc<Self> {
Rc::from(Self {
metadata: CallableMetadataBuilder::new("ASYNC_INCREMENT")
.with_return_type(ExprType::Integer)
.with_async(true)
.with_syntax(&[(
&[SingularArgSyntax::RequiredValue(
RequiredValueSyntax {
name: Cow::Borrowed("value"),
vtype: ExprType::Integer,
},
ArgSepSyntax::End,
)],
None,
)])
.test_build(),
})
}
}
#[async_trait(?Send)]
impl Callable for AsyncIncrementFunction {
fn metadata(&self) -> Rc<CallableMetadata> {
self.metadata.clone()
}
async fn async_exec(&self, scope: Scope<'_>) -> CallResult<()> {
debug_assert_eq!(1, scope.nargs());
let incremented = scope.get_integer(0) + 1;
yield_now().await;
scope.return_integer(incremented)
}
}