From 8651f84f205da7a89f2fc7333d1dd8de0d80a22b Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 17 Sep 2022 16:12:45 +0200 Subject: 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 --- src/provider/async.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) (limited to 'src/provider') 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, dependency_history: Vec<&'static str>, ) -> Result; + + fn do_clone(&self) -> Box; +} + +impl Clone for Box +{ + fn clone(&self) -> Self + { + self.do_clone() + } } pub struct AsyncTransientTypeProvider @@ -57,7 +68,7 @@ where { async fn provide( &self, - di_container: &AsyncDIContainer, + di_container: &Arc, dependency_history: Vec<&'static str>, ) -> Result { @@ -65,6 +76,23 @@ where InjectableType::resolve(di_container, dependency_history).await?, )) } + + fn do_clone(&self) -> Box + { + Box::new(self.clone()) + } +} + +impl Clone for AsyncTransientTypeProvider +where + InjectableType: AsyncInjectable, +{ + fn clone(&self) -> Self + { + Self { + injectable_phantom: self.injectable_phantom, + } + } } pub struct AsyncSingletonProvider @@ -91,12 +119,29 @@ where { async fn provide( &self, - _di_container: &AsyncDIContainer, + _di_container: &Arc, _dependency_history: Vec<&'static str>, ) -> Result { Ok(AsyncProvidable::Singleton(self.singleton.clone())) } + + fn do_clone(&self) -> Box + { + Box::new(self.clone()) + } +} + +impl Clone for AsyncSingletonProvider +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, _dependency_history: Vec<&'static str>, ) -> Result { Ok(AsyncProvidable::Factory(self.factory.clone())) } + + fn do_clone(&self) -> Box + { + Box::new(self.clone()) + } +} + +#[cfg(feature = "factory")] +impl Clone for AsyncFactoryProvider +{ + fn clone(&self) -> Self + { + Self { + factory: self.factory.clone(), + } + } } -- cgit v1.2.3-18-g5258