From 2a44ec3ffdcd78b23ac31b722b4312774d643c3a Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 24 Sep 2022 16:14:45 +0200 Subject: 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 --- src/async_di_container.rs | 17 ++++++++++++----- src/di_container.rs | 22 ++++++++++++++-------- 2 files changed, 26 insertions(+), 13 deletions(-) (limited to 'src') 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 @@ -361,8 +361,12 @@ where ) -> Result, AsyncBindingBuilderError> where Return: 'static + ?Sized, - FactoryFunc: Fn<(Arc,), Output = crate::ptr::TransientPtr> - + Send + FactoryFunc: Fn< + (Arc,), + Output = Box< + (dyn Fn<(), Output = crate::ptr::TransientPtr> + 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::,), Interface>>() + .cast::,), + dyn Fn<(), Output = TransientPtr> + 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 @@ -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( + pub fn to_default_factory( &self, - factory_func: &'static dyn Fn< - (Rc,), - Output = crate::ptr::TransientPtr, - >, + factory_func: &'static FactoryFunc, ) -> Result, BindingBuilderError> where Return: 'static + ?Sized, + FactoryFunc: Fn< + (Rc,), + Output = crate::ptr::TransientPtr< + dyn Fn<(), Output = crate::ptr::TransientPtr>, + >, + >, { { let bindings = self.di_container.bindings.borrow(); @@ -445,13 +448,16 @@ impl DIContainer use crate::interfaces::factory::IFactory; let default_factory = factory_binding - .cast::,), Interface>>() + .cast::,), + dyn Fn<(), Output = TransientPtr>, + >>() .map_err(|_| DIContainerError::CastFailed { interface: type_name::(), binding_kind: "default factory", })?; - Ok(SomePtr::Transient(default_factory(self.clone()))) + Ok(SomePtr::Transient(default_factory(self.clone())())) } } } -- cgit v1.2.3-18-g5258