Skip to main content

cgp_handler/providers/
promote_ref.rs

1use core::ops::Deref;
2
3use cgp_core::prelude::*;
4
5use crate::{
6    AsyncComputer, AsyncComputerComponent, AsyncComputerRef, AsyncComputerRefComponent, Computer,
7    ComputerComponent, ComputerRef, ComputerRefComponent, Handler, HandlerComponent, HandlerRef,
8    HandlerRefComponent, TryComputer, TryComputerComponent, TryComputerRef,
9    TryComputerRefComponent,
10};
11
12pub struct PromoteRef<Provider>(pub PhantomData<Provider>);
13
14#[cgp_provider]
15impl<Context, Code, Input, Target, Provider> Handler<Context, Code, Input> for PromoteRef<Provider>
16where
17    Context: HasErrorType,
18    Provider: HandlerRef<Context, Code, Target>,
19    Input: Deref<Target = Target>,
20{
21    type Output = Provider::Output;
22
23    async fn handle(
24        context: &Context,
25        tag: PhantomData<Code>,
26        input: Input,
27    ) -> Result<Self::Output, Context::Error> {
28        Provider::handle_ref(context, tag, input.deref()).await
29    }
30}
31
32#[cgp_provider]
33impl<Context, Code, Input, Provider, Output> HandlerRef<Context, Code, Input>
34    for PromoteRef<Provider>
35where
36    Context: HasErrorType,
37    Provider: for<'a> Handler<Context, Code, &'a Input, Output = Output>,
38{
39    type Output = Output;
40
41    async fn handle_ref(
42        context: &Context,
43        tag: PhantomData<Code>,
44        input: &Input,
45    ) -> Result<Self::Output, Context::Error> {
46        Provider::handle(context, tag, input).await
47    }
48}
49
50#[cgp_provider]
51impl<Context, Code, Input, Target, Provider> AsyncComputer<Context, Code, Input>
52    for PromoteRef<Provider>
53where
54    Provider: AsyncComputerRef<Context, Code, Target>,
55    Input: Deref<Target = Target>,
56{
57    type Output = Provider::Output;
58
59    async fn compute_async(
60        context: &Context,
61        tag: PhantomData<Code>,
62        input: Input,
63    ) -> Self::Output {
64        Provider::compute_async_ref(context, tag, input.deref()).await
65    }
66}
67
68#[cgp_provider]
69impl<Context, Code, Input, Provider, Output> AsyncComputerRef<Context, Code, Input>
70    for PromoteRef<Provider>
71where
72    Provider: for<'a> AsyncComputer<Context, Code, &'a Input, Output = Output>,
73{
74    type Output = Output;
75
76    async fn compute_async_ref(
77        context: &Context,
78        tag: PhantomData<Code>,
79        input: &Input,
80    ) -> Self::Output {
81        Provider::compute_async(context, tag, input).await
82    }
83}
84
85#[cgp_provider]
86impl<Context, Code, Input, Target, Provider> TryComputer<Context, Code, Input>
87    for PromoteRef<Provider>
88where
89    Context: HasErrorType,
90    Provider: TryComputerRef<Context, Code, Target>,
91    Input: Deref<Target = Target>,
92{
93    type Output = Provider::Output;
94
95    fn try_compute(
96        context: &Context,
97        tag: PhantomData<Code>,
98        input: Input,
99    ) -> Result<Self::Output, Context::Error> {
100        Provider::try_compute_ref(context, tag, input.deref())
101    }
102}
103
104#[cgp_provider]
105impl<Context, Code, Input, Provider, Output> TryComputerRef<Context, Code, Input>
106    for PromoteRef<Provider>
107where
108    Context: HasErrorType,
109    Provider: for<'a> TryComputer<Context, Code, &'a Input, Output = Output>,
110{
111    type Output = Output;
112
113    fn try_compute_ref(
114        context: &Context,
115        tag: PhantomData<Code>,
116        input: &Input,
117    ) -> Result<Self::Output, Context::Error> {
118        Provider::try_compute(context, tag, input)
119    }
120}
121
122#[cgp_provider]
123impl<Context, Code, Input, Target, Provider> Computer<Context, Code, Input> for PromoteRef<Provider>
124where
125    Provider: ComputerRef<Context, Code, Target>,
126    Input: Deref<Target = Target>,
127{
128    type Output = Provider::Output;
129
130    fn compute(context: &Context, tag: PhantomData<Code>, input: Input) -> Self::Output {
131        Provider::compute_ref(context, tag, input.deref())
132    }
133}
134
135#[cgp_provider]
136impl<Context, Code, Input, Provider, Output> ComputerRef<Context, Code, Input>
137    for PromoteRef<Provider>
138where
139    Provider: for<'a> Computer<Context, Code, &'a Input, Output = Output>,
140{
141    type Output = Output;
142
143    fn compute_ref(context: &Context, tag: PhantomData<Code>, input: &Input) -> Self::Output {
144        Provider::compute(context, tag, input)
145    }
146}