anthill_di/
constructor.rs1use tokio::sync::RwLock;
2
3use crate::{
4 DependencyContext,
5 types::BuildDependencyResult
6};
7
8#[cfg(not(feature = "async-mode"))]
9pub trait Constructor where Self: Sized + 'static {
10 fn ctor(ctx: DependencyContext) -> BuildDependencyResult<Self>;
11}
12
13#[cfg(feature = "async-mode")]
14#[async_trait_with_sync::async_trait(Sync)]
15pub trait Constructor where Self: Sized + 'static {
16 async fn ctor(ctx: DependencyContext) -> BuildDependencyResult<Self>;
17}
18
19#[cfg(not(feature = "async-mode"))]
20impl <T: Constructor + Sized + 'static> Constructor for tokio::sync::RwLock<T> {
21 fn ctor(ctx: DependencyContext) -> BuildDependencyResult<Self> {
22 Ok(RwLock::new(T::ctor(ctx)?))
23 }
24}
25
26#[cfg(feature = "async-mode")]
27#[async_trait_with_sync::async_trait(Sync)]
28impl <T: Constructor + Sized + 'static> Constructor for tokio::sync::RwLock<T> {
29 async fn ctor(ctx: DependencyContext) -> BuildDependencyResult<Self> {
30 Ok(RwLock::new(T::ctor(ctx).await?))
31 }
32}
33
34#[cfg(not(feature = "async-mode"))]
35impl <T: Constructor + Sized + 'static> Constructor for std::sync::RwLock<T> {
36 fn ctor(ctx: DependencyContext) -> BuildDependencyResult<Self> {
37 Ok(std::sync::RwLock::new(T::ctor(ctx)?))
38 }
39}
40
41#[cfg(feature = "async-mode")]
42#[async_trait_with_sync::async_trait(Sync)]
43impl <T: Constructor + Sized + 'static> Constructor for std::sync::RwLock<T> {
44 async fn ctor(ctx: DependencyContext) -> BuildDependencyResult<Self> {
45 Ok(std::sync::RwLock::new(T::ctor(ctx).await?))
46 }
47}