diff options
author | HampusM <hampus@hampusmat.com> | 2023-09-18 21:45:23 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-09-18 21:45:23 +0200 |
commit | de2e1349f459f7f69226b2decd366be690426ea7 (patch) | |
tree | 8312e49ae6007e5226a6ed9e3262855e40b06095 /src/private/castable_factory/threadsafe.rs | |
parent | 6d729a4d20944b990c341149729a810a2898cdff (diff) |
refactor: replace threadsafe castable factory Fn impl with method
Diffstat (limited to 'src/private/castable_factory/threadsafe.rs')
-rw-r--r-- | src/private/castable_factory/threadsafe.rs | 71 |
1 files changed, 4 insertions, 67 deletions
diff --git a/src/private/castable_factory/threadsafe.rs b/src/private/castable_factory/threadsafe.rs index cb8a04b..6e608b1 100644 --- a/src/private/castable_factory/threadsafe.rs +++ b/src/private/castable_factory/threadsafe.rs @@ -11,7 +11,7 @@ where DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { - func: &'static (dyn Fn<(Arc<DIContainerT>,), Output = TransientPtr<ReturnInterface>> + func: &'static (dyn Fn(Arc<DIContainerT>) -> TransientPtr<ReturnInterface> + Send + Sync), } @@ -38,44 +38,9 @@ where DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { -} - -impl<ReturnInterface, DIContainerT> Fn<(Arc<DIContainerT>,)> - for ThreadsafeCastableFactory<ReturnInterface, DIContainerT> -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - extern "rust-call" fn call(&self, args: (Arc<DIContainerT>,)) -> Self::Output + fn call(&self, di_container: Arc<DIContainerT>) -> TransientPtr<ReturnInterface> { - self.func.call(args) - } -} - -impl<ReturnInterface, DIContainerT> FnMut<(Arc<DIContainerT>,)> - for ThreadsafeCastableFactory<ReturnInterface, DIContainerT> -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - extern "rust-call" fn call_mut(&mut self, args: (Arc<DIContainerT>,)) - -> Self::Output - { - self.call(args) - } -} - -impl<ReturnInterface, DIContainerT> FnOnce<(Arc<DIContainerT>,)> - for ThreadsafeCastableFactory<ReturnInterface, DIContainerT> -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - type Output = TransientPtr<ReturnInterface>; - - extern "rust-call" fn call_once(self, args: (Arc<DIContainerT>,)) -> Self::Output - { - self.call(args) + (self.func)(di_container) } } @@ -133,36 +98,8 @@ mod tests let mock_di_container = Arc::new(MockAsyncDIContainer::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 = ThreadsafeCastableFactory::new(&|_| { - TransientPtr::new(Bacon { heal_amount: 1092 }) - }); - - let mock_di_container = Arc::new(MockAsyncDIContainer::new()); - - let output = castable_factory.call_mut((mock_di_container,)); - - assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 1092 })); - } - - #[test] - fn can_call_once() - { - let castable_factory = ThreadsafeCastableFactory::new(&|_| { - TransientPtr::new(Bacon { heal_amount: 547 }) - }); - - let mock_di_container = Arc::new(MockAsyncDIContainer::new()); - - let output = castable_factory.call_once((mock_di_container,)); - - assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 547 })); - } } |