aboutsummaryrefslogtreecommitdiff
path: root/src/provider/async.rs
blob: 93ae03ad661b3d50b7dc37b25f15c2f6bf99da83 (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
#![allow(clippy::module_name_repetitions)]
use std::marker::PhantomData;

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,
        >,
    ),
}

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

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: &AsyncDIContainer,
        dependency_history: Vec<&'static str>,
    ) -> Result<AsyncProvidable, InjectableError>
    {
        Ok(AsyncProvidable::Transient(
            InjectableType::resolve(di_container, dependency_history).await?,
        ))
    }
}

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: &AsyncDIContainer,
        _dependency_history: Vec<&'static str>,
    ) -> Result<AsyncProvidable, InjectableError>
    {
        Ok(AsyncProvidable::Singleton(self.singleton.clone()))
    }
}

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

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

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