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
//! Define traits that expose DI apis to user.

use async_trait::async_trait;

use crate::container::DIContainer;
use crate::globals::INSTANCE;
use crate::types::DI;

/// Represent DI target type.
/// It requires thread safety.
#[cfg(target_arch = "wasm32")]
pub trait DITarget: 'static {}
#[cfg(not(target_arch = "wasm32"))]
pub trait DITarget: Send + Sync + 'static {}

#[cfg(target_arch = "wasm32")]
impl<T: 'static> DITarget for T {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Send + Sync + 'static> DITarget for T {}

/// Add `di` methods for DI target types.
pub trait DIPortal {
    /// DI on a container.
    fn di_on(container: &DIContainer) -> DI<Self>
    where
        Self: Sized + DITarget,
    {
        container.get_or_init(|| Self::create_for_di(container))
    }

    /// DI on the global container.
    #[cfg(not(target_arch = "wasm32"))]
    fn di() -> DI<Self>
    where
        Self: Sized + DITarget,
    {
        Self::di_on(&INSTANCE)
    }
    #[cfg(target_arch = "wasm32")]
    fn di() -> DI<Self>
    where
        Self: Sized + DITarget,
    {
        Self::di_on(INSTANCE.with(|i| i.clone()).as_ref())
    }

    /// Create new instance for DI.
    fn create_for_di(container: &DIContainer) -> Self;
}

/// Add `di` methods for DI target types that needs async creation.
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait AsyncDIPortal {
    /// DI on a container.
    async fn di_on(container: &DIContainer) -> DI<Self>
    where
        Self: Sized + DITarget,
    {
        container
            .get_or_init_async(|| Self::create_for_di(container))
            .await
    }

    /// DI on the global container.
    #[cfg(not(target_arch = "wasm32"))]
    async fn di() -> DI<Self>
    where
        Self: Sized + DITarget,
    {
        Self::di_on(&INSTANCE).await
    }
    #[cfg(target_arch = "wasm32")]
    async fn di() -> DI<Self>
    where
        Self: Sized + DITarget,
    {
        Self::di_on(INSTANCE.with(|i| i.clone()).as_ref()).await
    }

    /// Create new instance for DI.
    async fn create_for_di(container: &DIContainer) -> Self;
}

/// Provides component instance for trait DI types.
pub trait DIProvider {
    /// Target trait type.
    type Output: ?Sized;

    /// DI on a container.
    fn di_on(container: &DIContainer) -> DI<Self::Output>;

    /// DI on the global container.
    #[cfg(not(target_arch = "wasm32"))]
    fn di() -> DI<Self::Output> {
        Self::di_on(&INSTANCE)
    }
    #[cfg(target_arch = "wasm32")]
    fn di() -> DI<Self::Output> {
        Self::di_on(INSTANCE.with(|i| i.clone()).as_ref())
    }
}

/// Provides component instance for trait DI types that needs async creation.
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait AsyncDIProvider {
    /// Target trait type.
    type Output: ?Sized;

    /// DI on a container.
    async fn di_on(container: &DIContainer) -> DI<Self::Output>;

    /// DI on the global container.
    #[cfg(not(target_arch = "wasm32"))]
    async fn di() -> DI<Self::Output> {
        Self::di_on(&INSTANCE).await
    }
    #[cfg(target_arch = "wasm32")]
    async fn di() -> DI<Self::Output> {
        Self::di_on(INSTANCE.with(|i| i.clone()).as_ref()).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    trait FooI {}

    mod sync_test {
        use super::*;

        struct Hoge;
        impl DIPortal for Hoge {
            fn create_for_di(_container: &DIContainer) -> Self {
                Hoge {}
            }
        }
        impl FooI for Hoge {}

        struct FooIProvider;
        impl DIProvider for FooIProvider {
            type Output = dyn FooI;
            fn di_on(container: &DIContainer) -> DI<Self::Output> {
                Hoge::di_on(container)
            }
        }

        #[test]
        #[allow(non_snake_case)]
        fn test_same_instance_for_DIPortal() {
            let c = DIContainer::new();
            let hoge1 = Hoge::di_on(&c).as_ref() as *const _;
            let hoge2 = Hoge::di_on(&c).as_ref() as *const _;
            assert!(std::ptr::eq(hoge1, hoge2));
        }

        #[test]
        #[allow(non_snake_case)]
        fn test_same_instance_for_DIProvider() {
            let c = DIContainer::new();
            let foo1 = FooIProvider::di_on(&c).as_ref() as *const _;
            let foo2 = FooIProvider::di_on(&c).as_ref() as *const _;
            assert!(std::ptr::eq(foo1, foo2));
        }
    }

    mod async_test {
        use super::*;

        struct AsyncHoge;
        #[async_trait]
        impl AsyncDIPortal for AsyncHoge {
            async fn create_for_di(_container: &DIContainer) -> Self {
                AsyncHoge {}
            }
        }
        impl FooI for AsyncHoge {}

        struct AsyncFooIProvider;
        #[async_trait]
        impl AsyncDIProvider for AsyncFooIProvider {
            type Output = dyn FooI;
            async fn di_on(container: &DIContainer) -> DI<Self::Output> {
                AsyncHoge::di_on(container).await
            }
        }

        #[tokio::test]
        #[allow(non_snake_case)]
        async fn test_same_instance_for_AsyncDIPortal() {
            let c = DIContainer::new();
            let hoge1 = AsyncHoge::di_on(&c).await.as_ref() as *const _;
            let hoge2 = AsyncHoge::di_on(&c).await.as_ref() as *const _;
            println!("check !!! {:?} {:?}", hoge1, hoge2);
            assert!(std::ptr::eq(hoge1, hoge2));
        }

        #[tokio::test]
        #[allow(non_snake_case)]
        async fn test_same_instance_for_AsyncDIProvider() {
            let c = DIContainer::new();
            let foo1 = AsyncFooIProvider::di_on(&c).await.as_ref() as *const _;
            let foo2 = AsyncFooIProvider::di_on(&c).await.as_ref() as *const _;
            println!("check !!! {:?} {:?}", foo1, foo2);
            assert!(std::ptr::eq(foo1, foo2));
        }
    }
}