diff options
author | HampusM <hampus@hampusmat.com> | 2023-09-17 13:08:23 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-09-17 13:08:23 +0200 |
commit | 3e3a853615e97ee7c3aef09736eae8170c6fd78e (patch) | |
tree | dec1ed4cb85868ea2931f072150e3cd9b0ab32aa | |
parent | 75ca777bbeb618e14b1cf8854ebb37b7a2c884b5 (diff) |
refactor: replace castable factory Fn impl with method
-rw-r--r-- | src/di_container/blocking/mod.rs | 4 | ||||
-rw-r--r-- | src/private/castable_factory/blocking.rs | 63 | ||||
-rw-r--r-- | src/private/factory.rs | 4 |
3 files changed, 7 insertions, 64 deletions
diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index 702141a..09e7db1 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -215,7 +215,7 @@ impl DIContainer binding_kind: "factory", })?; - Ok(SomePtr::Factory(factory(self.clone()).into())) + Ok(SomePtr::Factory(factory.call(self.clone()).into())) } #[cfg(feature = "factory")] Providable::DefaultFactory(factory_binding) => { @@ -234,7 +234,7 @@ impl DIContainer binding_kind: "default factory", })?; - Ok(SomePtr::Transient(default_factory(self.clone())())) + Ok(SomePtr::Transient(default_factory.call(self.clone())())) } } } diff --git a/src/private/castable_factory/blocking.rs b/src/private/castable_factory/blocking.rs index 24c653e..0761b7e 100644 --- a/src/private/castable_factory/blocking.rs +++ b/src/private/castable_factory/blocking.rs @@ -34,40 +34,9 @@ impl<ReturnInterface, DIContainerT> IFactory<ReturnInterface, DIContainerT> where ReturnInterface: 'static + ?Sized, { -} - -impl<ReturnInterface, DIContainerT> Fn<(Rc<DIContainerT>,)> - for CastableFactory<ReturnInterface, DIContainerT> -where - ReturnInterface: 'static + ?Sized, -{ - extern "rust-call" fn call(&self, args: (Rc<DIContainerT>,)) -> Self::Output - { - self.func.call(args) - } -} - -impl<ReturnInterface, DIContainerT> FnMut<(Rc<DIContainerT>,)> - for CastableFactory<ReturnInterface, DIContainerT> -where - ReturnInterface: 'static + ?Sized, -{ - extern "rust-call" fn call_mut(&mut self, args: (Rc<DIContainerT>,)) -> Self::Output - { - self.call(args) - } -} - -impl<ReturnInterface, DIContainerT> FnOnce<(Rc<DIContainerT>,)> - for CastableFactory<ReturnInterface, DIContainerT> -where - ReturnInterface: 'static + ?Sized, -{ - type Output = TransientPtr<ReturnInterface>; - - extern "rust-call" fn call_once(self, args: (Rc<DIContainerT>,)) -> Self::Output + fn call(&self, di_container: Rc<DIContainerT>) -> TransientPtr<ReturnInterface> { - self.call(args) + (self.func)(di_container) } } @@ -113,34 +82,8 @@ mod tests let mock_di_container = Rc::new(MockDIContainer::new()); - let output = castable_factory.call((mock_di_container,)); + let output = castable_factory.call(mock_di_container); assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 })); } - - #[test] - fn can_call_mut() - { - let mut castable_factory = - CastableFactory::new(&|_| TransientPtr::new(Bacon { heal_amount: 103 })); - - let mock_di_container = Rc::new(MockDIContainer::new()); - - let output = castable_factory.call_mut((mock_di_container,)); - - assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 103 })); - } - - #[test] - fn can_call_once() - { - let castable_factory = - CastableFactory::new(&|_| TransientPtr::new(Bacon { heal_amount: 19 })); - - let mock_di_container = Rc::new(MockDIContainer::new()); - - let output = castable_factory.call_once((mock_di_container,)); - - assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 19 })); - } } diff --git a/src/private/factory.rs b/src/private/factory.rs index af6df8a..23b4e8f 100644 --- a/src/private/factory.rs +++ b/src/private/factory.rs @@ -4,11 +4,11 @@ use crate::private::cast::CastFrom; use crate::ptr::TransientPtr; /// Interface for a factory. -pub trait IFactory<ReturnInterface, DIContainerT>: - Fn<(Rc<DIContainerT>,), Output = TransientPtr<ReturnInterface>> + CastFrom +pub trait IFactory<ReturnInterface, DIContainerT>: CastFrom where ReturnInterface: 'static + ?Sized, { + fn call(&self, di_container: Rc<DIContainerT>) -> TransientPtr<ReturnInterface>; } /// Interface for a threadsafe factory. |