diff options
author | HampusM <hampus@hampusmat.com> | 2022-09-17 16:12:45 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-09-17 16:12:45 +0200 |
commit | 8651f84f205da7a89f2fc7333d1dd8de0d80a22b (patch) | |
tree | a178427abb442e897d21f654db71cc8135236920 /src/provider | |
parent | c1e682c25c24be3174d44ceb95b0537c38299d0c (diff) |
refactor!: make async DI container be used inside of a Arc
BREAKING CHANGE: The async DI container is to be used inside of a Arc & it also no longer implements Default
Diffstat (limited to 'src/provider')
-rw-r--r-- | src/provider/async.rs | 69 |
1 files changed, 65 insertions, 4 deletions
diff --git a/src/provider/async.rs b/src/provider/async.rs index 93ae03a..1ddb614 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -1,5 +1,6 @@ #![allow(clippy::module_name_repetitions)] use std::marker::PhantomData; +use std::sync::Arc; use async_trait::async_trait; @@ -26,9 +27,19 @@ pub trait IAsyncProvider: Send + Sync { async fn provide( &self, - di_container: &AsyncDIContainer, + 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> @@ -57,7 +68,7 @@ where { async fn provide( &self, - di_container: &AsyncDIContainer, + di_container: &Arc<AsyncDIContainer>, dependency_history: Vec<&'static str>, ) -> Result<AsyncProvidable, InjectableError> { @@ -65,6 +76,23 @@ where 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> @@ -91,12 +119,29 @@ where { async fn provide( &self, - _di_container: &AsyncDIContainer, + _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")] @@ -126,10 +171,26 @@ impl IAsyncProvider for AsyncFactoryProvider { async fn provide( &self, - _di_container: &AsyncDIContainer, + _di_container: &Arc<AsyncDIContainer>, _dependency_history: Vec<&'static str>, ) -> Result<AsyncProvidable, InjectableError> { Ok(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(), + } + } } |