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