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/di_container.rs | |
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/di_container.rs')
-rw-r--r-- | src/di_container.rs | 22 |
1 files changed, 14 insertions, 8 deletions
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())())) } } } |