From de2e1349f459f7f69226b2decd366be690426ea7 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 18 Sep 2023 21:45:23 +0200 Subject: refactor: replace threadsafe castable factory Fn impl with method --- src/di_container/asynchronous/mod.rs | 8 ++-- src/lib.rs | 2 +- src/private/castable_factory/threadsafe.rs | 71 ++---------------------------- src/private/factory.rs | 4 +- 4 files changed, 12 insertions(+), 73 deletions(-) diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index e651d81..827364d 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -299,7 +299,9 @@ impl AsyncDIContainer } })?; - Ok(SomePtr::ThreadsafeFactory(factory(self.clone()).into())) + Ok(SomePtr::ThreadsafeFactory( + factory.call(self.clone()).into(), + )) } #[cfg(feature = "factory")] AsyncProvidable::DefaultFactory(binding) => { @@ -315,7 +317,7 @@ impl AsyncDIContainer DefaultFactoryFn, >(binding, "default factory")?; - Ok(SomePtr::Transient(default_factory(self.clone())())) + Ok(SomePtr::Transient(default_factory.call(self.clone())())) } #[cfg(feature = "factory")] AsyncProvidable::AsyncDefaultFactory(binding) => { @@ -337,7 +339,7 @@ impl AsyncDIContainer )?; Ok(SomePtr::Transient( - async_default_factory(self.clone())().await, + async_default_factory.call(self.clone())().await, )) } } diff --git a/src/lib.rs b/src/lib.rs index 562b886..7f4738d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -#![cfg_attr(feature = "factory", feature(unboxed_closures, fn_traits, tuple_trait))] +#![cfg_attr(feature = "factory", feature(unboxed_closures, tuple_trait))] #![cfg_attr(doc_cfg, feature(doc_cfg))] #![deny(clippy::all)] #![deny(clippy::pedantic)] 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,), Output = TransientPtr> + func: &'static (dyn Fn(Arc) -> TransientPtr + Send + Sync), } @@ -38,44 +38,9 @@ where DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { -} - -impl Fn<(Arc,)> - for ThreadsafeCastableFactory -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - extern "rust-call" fn call(&self, args: (Arc,)) -> Self::Output + fn call(&self, di_container: Arc) -> TransientPtr { - self.func.call(args) - } -} - -impl FnMut<(Arc,)> - for ThreadsafeCastableFactory -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - extern "rust-call" fn call_mut(&mut self, args: (Arc,)) - -> Self::Output - { - self.call(args) - } -} - -impl FnOnce<(Arc,)> - for ThreadsafeCastableFactory -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - type Output = TransientPtr; - - extern "rust-call" fn call_once(self, args: (Arc,)) -> 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 })); - } } diff --git a/src/private/factory.rs b/src/private/factory.rs index 730338f..c1672e1 100644 --- a/src/private/factory.rs +++ b/src/private/factory.rs @@ -15,9 +15,9 @@ where /// Interface for a threadsafe factory. #[cfg(feature = "async")] pub trait IThreadsafeFactory: - Fn<(Arc,), Output = TransientPtr> - + crate::private::cast::CastFromArc + crate::private::cast::CastFromArc where ReturnInterface: 'static + ?Sized, { + fn call(&self, di_container: Arc) -> TransientPtr; } -- cgit v1.2.3-18-g5258