aboutsummaryrefslogtreecommitdiff
path: root/src/provider/async.rs
blob: df96b27c9f6b87372a16d13ee311577df9bc61d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#![allow(clippy::module_name_repetitions)]
use std::marker::PhantomData;
use std::sync::Arc;

use async_trait::async_trait;

use crate::async_di_container::AsyncDIContainer;
use crate::errors::injectable::InjectableError;
use crate::interfaces::async_injectable::AsyncInjectable;
use crate::ptr::{ThreadsafeSingletonPtr, TransientPtr};

#[derive(strum_macros::Display, Debug)]
pub enum AsyncProvidable
{
    Transient(TransientPtr<dyn AsyncInjectable>),
    Singleton(ThreadsafeSingletonPtr<dyn AsyncInjectable>),
    #[cfg(feature = "factory")]
    Factory(
        crate::ptr::ThreadsafeFactoryPtr<
            dyn crate::interfaces::any_factory::AnyThreadsafeFactory,
        >,
    ),
    #[cfg(feature = "factory")]
    DefaultFactory(
        crate::ptr::ThreadsafeFactoryPtr<
            dyn crate::interfaces::any_factory::AnyThreadsafeFactory,
        >,
    ),
}

#[async_trait]
pub trait IAsyncProvider: Send + Sync
{
    async fn provide(
        &self,
        di_container: &Arc<AsyncDIContainer>,
        dependency_history: Vec<&'static str>,
    ) -> Result<AsyncProvidable, InjectableError>;

    fn do_clone(&self) -> Box<dyn IAsyncProvider>;
}

impl Clone for Box<dyn IAsyncProvider>
{
    fn clone(&self) -> Self
    {
        self.do_clone()
    }
}

pub struct AsyncTransientTypeProvider<InjectableType>
where
    InjectableType: AsyncInjectable,
{
    injectable_phantom: PhantomData<InjectableType>,
}

impl<InjectableType> AsyncTransientTypeProvider<InjectableType>
where
    InjectableType: AsyncInjectable,
{
    pub fn new() -> Self
    {
        Self {
            injectable_phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<InjectableType> IAsyncProvider for AsyncTransientTypeProvider<InjectableType>
where
    InjectableType: AsyncInjectable,
{
    async fn provide(
        &self,
        di_container: &Arc<AsyncDIContainer>,
        dependency_history: Vec<&'static str>,
    ) -> Result<AsyncProvidable, InjectableError>
    {
        Ok(AsyncProvidable::Transient(
            InjectableType::resolve(di_container, dependency_history).await?,
        ))
    }

    fn do_clone(&self) -> Box<dyn IAsyncProvider>
    {
        Box::new(self.clone())
    }
}

impl<InjectableType> Clone for AsyncTransientTypeProvider<InjectableType>
where
    InjectableType: AsyncInjectable,
{
    fn clone(&self) -> Self
    {
        Self {
            injectable_phantom: self.injectable_phantom,
        }
    }
}

pub struct AsyncSingletonProvider<InjectableType>
where
    InjectableType: AsyncInjectable,
{
    singleton: ThreadsafeSingletonPtr<InjectableType>,
}

impl<InjectableType> AsyncSingletonProvider<InjectableType>
where
    InjectableType: AsyncInjectable,
{
    pub fn new(singleton: ThreadsafeSingletonPtr<InjectableType>) -> Self
    {
        Self { singleton }
    }
}

#[async_trait]
impl<InjectableType> IAsyncProvider for AsyncSingletonProvider<InjectableType>
where
    InjectableType: AsyncInjectable,
{
    async fn provide(
        &self,
        _di_container: &Arc<AsyncDIContainer>,
        _dependency_history: Vec<&'static str>,
    ) -> Result<AsyncProvidable, InjectableError>
    {
        Ok(AsyncProvidable::Singleton(self.singleton.clone()))
    }

    fn do_clone(&self) -> Box<dyn IAsyncProvider>
    {
        Box::new(self.clone())
    }
}

impl<InjectableType> Clone for AsyncSingletonProvider<InjectableType>
where
    InjectableType: AsyncInjectable,
{
    fn clone(&self) -> Self
    {
        Self {
            singleton: self.singleton.clone(),
        }
    }
}

#[cfg(feature = "factory")]
pub struct AsyncFactoryProvider
{
    factory: crate::ptr::ThreadsafeFactoryPtr<
        dyn crate::interfaces::any_factory::AnyThreadsafeFactory,
    >,
    is_default_factory: bool,
}

#[cfg(feature = "factory")]
impl AsyncFactoryProvider
{
    pub fn new(
        factory: crate::ptr::ThreadsafeFactoryPtr<
            dyn crate::interfaces::any_factory::AnyThreadsafeFactory,
        >,
        is_default_factory: bool,
    ) -> Self
    {
        Self {
            factory,
            is_default_factory,
        }
    }
}

#[cfg(feature = "factory")]
#[async_trait]
impl IAsyncProvider for AsyncFactoryProvider
{
    async fn provide(
        &self,
        _di_container: &Arc<AsyncDIContainer>,
        _dependency_history: Vec<&'static str>,
    ) -> Result<AsyncProvidable, InjectableError>
    {
        Ok(if self.is_default_factory {
            AsyncProvidable::DefaultFactory(self.factory.clone())
        } else {
            AsyncProvidable::Factory(self.factory.clone())
        })
    }

    fn do_clone(&self) -> Box<dyn IAsyncProvider>
    {
        Box::new(self.clone())
    }
}

#[cfg(feature = "factory")]
impl Clone for AsyncFactoryProvider
{
    fn clone(&self) -> Self
    {
        Self {
            factory: self.factory.clone(),
            is_default_factory: self.is_default_factory.clone(),
        }
    }
}