Skip to main content

cgp_handler/components/
async_computer.rs

1use core::marker::PhantomData;
2
3use cgp::component::UseDelegate;
4use cgp::prelude::*;
5
6use crate::UseInputDelegate;
7
8#[async_trait]
9#[cgp_component(AsyncComputer)]
10#[prefix(@cgp.extra.handler in DefaultNamespace)]
11#[derive_delegate(UseDelegate<Code>)]
12#[derive_delegate(UseInputDelegate<Input>)]
13pub trait CanComputeAsync<Code, Input> {
14    type Output;
15
16    async fn compute_async(&self, _code: PhantomData<Code>, input: Input) -> Self::Output;
17}
18
19#[async_trait]
20#[cgp_component(AsyncComputerRef)]
21#[prefix(@cgp.extra.handler in DefaultNamespace)]
22#[derive_delegate(UseDelegate<Code>)]
23#[derive_delegate(UseInputDelegate<Input>)]
24pub trait CanComputeAsyncRef<Code, Input> {
25    type Output;
26
27    async fn compute_async_ref(&self, _code: PhantomData<Code>, input: &Input) -> Self::Output;
28}
29
30#[cgp_provider]
31impl<Context, Code, Input, Tag, Output> AsyncComputer<Context, Code, Input> for UseField<Tag>
32where
33    Context: HasField<Tag>,
34    Context::Value: CanComputeAsync<Code, Input, Output = Output>,
35{
36    type Output = Output;
37
38    async fn compute_async(context: &Context, code: PhantomData<Code>, input: Input) -> Output {
39        context
40            .get_field(PhantomData)
41            .compute_async(code, input)
42            .await
43    }
44}