From 3e3a853615e97ee7c3aef09736eae8170c6fd78e Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 17 Sep 2023 13:08:23 +0200 Subject: refactor: replace castable factory Fn impl with method --- src/di_container/blocking/mod.rs | 4 +- src/private/castable_factory/blocking.rs | 63 ++------------------------------ src/private/factory.rs | 4 +- 3 files changed, 7 insertions(+), 64 deletions(-) (limited to 'src') 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 IFactory where ReturnInterface: 'static + ?Sized, { -} - -impl Fn<(Rc,)> - for CastableFactory -where - ReturnInterface: 'static + ?Sized, -{ - extern "rust-call" fn call(&self, args: (Rc,)) -> Self::Output - { - self.func.call(args) - } -} - -impl FnMut<(Rc,)> - for CastableFactory -where - ReturnInterface: 'static + ?Sized, -{ - extern "rust-call" fn call_mut(&mut self, args: (Rc,)) -> Self::Output - { - self.call(args) - } -} - -impl FnOnce<(Rc,)> - for CastableFactory -where - ReturnInterface: 'static + ?Sized, -{ - type Output = TransientPtr; - - extern "rust-call" fn call_once(self, args: (Rc,)) -> Self::Output + fn call(&self, di_container: Rc) -> TransientPtr { - 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: - Fn<(Rc,), Output = TransientPtr> + CastFrom +pub trait IFactory: CastFrom where ReturnInterface: 'static + ?Sized, { + fn call(&self, di_container: Rc) -> TransientPtr; } /// Interface for a threadsafe factory. -- cgit v1.2.3-18-g5258