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/di_container.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src/di_container.rs') 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