diff options
author | HampusM <hampus@hampusmat.com> | 2022-09-24 16:14:45 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-09-24 16:14:45 +0200 |
commit | 2a44ec3ffdcd78b23ac31b722b4312774d643c3a (patch) | |
tree | d3056a1fffe6311f863c4d683cc3444507c3e7d8 /src | |
parent | 695f90bf900015df1e2728445f833dabced838a9 (diff) |
refactor!: remove repetition of declaring factory interfaces
BREAKING CHANGE: The to_default_factory method of the blocking and async DI containers now expect a function returning another function
Diffstat (limited to 'src')
-rw-r--r-- | src/async_di_container.rs | 17 | ||||
-rw-r--r-- | src/di_container.rs | 22 |
2 files changed, 26 insertions, 13 deletions
diff --git a/src/async_di_container.rs b/src/async_di_container.rs index c67900e..d90cc0b 100644 --- a/src/async_di_container.rs +++ b/src/async_di_container.rs @@ -78,7 +78,7 @@ use crate::provider::r#async::{ AsyncTransientTypeProvider, IAsyncProvider, }; -use crate::ptr::{SomeThreadsafePtr, ThreadsafeSingletonPtr}; +use crate::ptr::{SomeThreadsafePtr, ThreadsafeSingletonPtr, TransientPtr}; /// When configurator for a binding for type 'Interface' inside a [`AsyncDIContainer`]. pub struct AsyncBindingWhenConfigurator<Interface> @@ -361,8 +361,12 @@ where ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingBuilderError> where Return: 'static + ?Sized, - FactoryFunc: Fn<(Arc<AsyncDIContainer>,), Output = crate::ptr::TransientPtr<Return>> - + Send + FactoryFunc: Fn< + (Arc<AsyncDIContainer>,), + Output = Box< + (dyn Fn<(), Output = crate::ptr::TransientPtr<Return>> + Send + Sync), + >, + > + Send + Sync, { let mut bindings_lock = self.di_container.bindings.lock().await; @@ -531,7 +535,10 @@ impl AsyncDIContainer use crate::interfaces::factory::IFactory; let default_factory = default_factory_binding - .cast::<dyn IFactory<(Arc<AsyncDIContainer>,), Interface>>() + .cast::<dyn IFactory< + (Arc<AsyncDIContainer>,), + dyn Fn<(), Output = TransientPtr<Interface>> + Send + Sync, + >>() .map_err(|err| match err { CastError::NotArcCastable(_) => { AsyncDIContainerError::InterfaceNotAsync( @@ -546,7 +553,7 @@ impl AsyncDIContainer } })?; - Ok(SomeThreadsafePtr::Transient(default_factory(self.clone()))) + Ok(SomeThreadsafePtr::Transient(default_factory(self.clone())())) } } } diff --git a/src/di_container.rs b/src/di_container.rs index 2907969..45c3be8 100644 --- a/src/di_container.rs +++ b/src/di_container.rs @@ -71,7 +71,7 @@ use crate::provider::blocking::{ SingletonProvider, TransientTypeProvider, }; -use crate::ptr::{SingletonPtr, SomePtr}; +use crate::ptr::{SingletonPtr, SomePtr, TransientPtr}; /// When configurator for a binding for type 'Interface' inside a [`DIContainer`]. pub struct BindingWhenConfigurator<Interface> @@ -294,15 +294,18 @@ where /// Will return Err if the associated [`DIContainer`] already have a binding for /// the interface. #[cfg(feature = "factory")] - pub fn to_default_factory<Return>( + pub fn to_default_factory<Return, FactoryFunc>( &self, - factory_func: &'static dyn Fn< - (Rc<DIContainer>,), - Output = crate::ptr::TransientPtr<Return>, - >, + factory_func: &'static FactoryFunc, ) -> Result<BindingWhenConfigurator<Interface>, BindingBuilderError> where Return: 'static + ?Sized, + FactoryFunc: Fn< + (Rc<DIContainer>,), + Output = crate::ptr::TransientPtr< + dyn Fn<(), Output = crate::ptr::TransientPtr<Return>>, + >, + >, { { let bindings = self.di_container.bindings.borrow(); @@ -445,13 +448,16 @@ impl DIContainer use crate::interfaces::factory::IFactory; let default_factory = factory_binding - .cast::<dyn IFactory<(Rc<DIContainer>,), Interface>>() + .cast::<dyn IFactory< + (Rc<DIContainer>,), + dyn Fn<(), Output = TransientPtr<Interface>>, + >>() .map_err(|_| DIContainerError::CastFailed { interface: type_name::<Interface>(), binding_kind: "default factory", })?; - Ok(SomePtr::Transient(default_factory(self.clone()))) + Ok(SomePtr::Transient(default_factory(self.clone())())) } } } |