From 6d729a4d20944b990c341149729a810a2898cdff Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 18 Sep 2023 20:35:55 +0200 Subject: refactor: make threadsafe castable factory take DI container param --- macros/src/factory/build_declare_interfaces.rs | 12 +-- src/di_container/asynchronous/mod.rs | 26 +++--- src/private/castable_factory/threadsafe.rs | 109 ++++++++++++------------- src/private/factory.rs | 9 +- 4 files changed, 81 insertions(+), 75 deletions(-) diff --git a/macros/src/factory/build_declare_interfaces.rs b/macros/src/factory/build_declare_interfaces.rs index f256c7b..1e2d62e 100644 --- a/macros/src/factory/build_declare_interfaces.rs +++ b/macros/src/factory/build_declare_interfaces.rs @@ -12,19 +12,19 @@ pub fn build_declare_factory_interfaces( quote! { syrette::declare_interface!( syrette::private::castable_factory::threadsafe::ThreadsafeCastableFactory< - (std::sync::Arc,), - #factory_interface + #factory_interface, + syrette::di_container::asynchronous::AsyncDIContainer, > -> syrette::private::factory::IThreadsafeFactory< - (std::sync::Arc,), - #factory_interface + #factory_interface, + syrette::di_container::asynchronous::AsyncDIContainer, >, threadsafe_sharable = true ); syrette::declare_interface!( syrette::private::castable_factory::threadsafe::ThreadsafeCastableFactory< - (std::sync::Arc,), - #factory_interface + #factory_interface, + syrette::di_container::asynchronous::AsyncDIContainer, > -> syrette::private::any_factory::AnyThreadsafeFactory, threadsafe_sharable = true ); diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index c2b4f6f..e651d81 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -279,7 +279,7 @@ impl AsyncDIContainer use crate::private::factory::IThreadsafeFactory; let factory = factory_binding - .cast::,), Interface>>() + .cast::>() .map_err(|err| match err { CastError::NotArcCastable(_) => { AsyncDIContainerError::InterfaceNotAsync( @@ -306,11 +306,13 @@ impl AsyncDIContainer use crate::private::factory::IThreadsafeFactory; use crate::ptr::TransientPtr; + type DefaultFactoryFn = dyn IThreadsafeFactory< + dyn Fn<(), Output = TransientPtr> + Send + Sync, + AsyncDIContainer, + >; + let default_factory = Self::cast_factory_binding::< - dyn IThreadsafeFactory< - (Arc,), - dyn Fn<(), Output = TransientPtr> + Send + Sync, - >, + DefaultFactoryFn, >(binding, "default factory")?; Ok(SomePtr::Transient(default_factory(self.clone())())) @@ -321,13 +323,15 @@ impl AsyncDIContainer use crate::private::factory::IThreadsafeFactory; use crate::ptr::TransientPtr; + type AsyncDefaultFactoryFn = dyn IThreadsafeFactory< + dyn Fn<(), Output = BoxFuture<'static, TransientPtr>> + + Send + + Sync, + AsyncDIContainer, + >; + let async_default_factory = Self::cast_factory_binding::< - dyn IThreadsafeFactory< - (Arc,), - dyn Fn<(), Output = BoxFuture<'static, TransientPtr>> - + Send - + Sync, - >, + AsyncDefaultFactoryFn, >( binding, "async default factory" )?; diff --git a/src/private/castable_factory/threadsafe.rs b/src/private/castable_factory/threadsafe.rs index 5b19844..cb8a04b 100644 --- a/src/private/castable_factory/threadsafe.rs +++ b/src/private/castable_factory/threadsafe.rs @@ -1,26 +1,29 @@ use std::any::type_name; use std::fmt::Debug; -use std::marker::Tuple; +use std::sync::Arc; use crate::private::any_factory::{AnyFactory, AnyThreadsafeFactory}; use crate::private::factory::IThreadsafeFactory; use crate::ptr::TransientPtr; -pub struct ThreadsafeCastableFactory +pub struct ThreadsafeCastableFactory where - Args: Tuple + 'static, + DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { - func: &'static (dyn Fn> + Send + Sync), + func: &'static (dyn Fn<(Arc,), Output = TransientPtr> + + Send + + Sync), } -impl ThreadsafeCastableFactory +impl + ThreadsafeCastableFactory where - Args: Tuple + 'static, + DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { pub fn new( - func: &'static (dyn Fn> + func: &'static (dyn Fn<(Arc,), Output = TransientPtr> + Send + Sync), ) -> Self @@ -29,94 +32,83 @@ where } } -impl IThreadsafeFactory - for ThreadsafeCastableFactory +impl IThreadsafeFactory + for ThreadsafeCastableFactory where - Args: Tuple + 'static, + DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { } -impl Fn for ThreadsafeCastableFactory +impl Fn<(Arc,)> + for ThreadsafeCastableFactory where - Args: Tuple + 'static, + DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { - extern "rust-call" fn call(&self, args: Args) -> Self::Output + extern "rust-call" fn call(&self, args: (Arc,)) -> Self::Output { self.func.call(args) } } -impl FnMut - for ThreadsafeCastableFactory +impl FnMut<(Arc,)> + for ThreadsafeCastableFactory where - Args: Tuple + 'static, + DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { - extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output + extern "rust-call" fn call_mut(&mut self, args: (Arc,)) + -> Self::Output { self.call(args) } } -impl FnOnce - for ThreadsafeCastableFactory +impl FnOnce<(Arc,)> + for ThreadsafeCastableFactory where - Args: Tuple + 'static, + DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { type Output = TransientPtr; - extern "rust-call" fn call_once(self, args: Args) -> Self::Output + extern "rust-call" fn call_once(self, args: (Arc,)) -> Self::Output { self.call(args) } } -impl AnyFactory - for ThreadsafeCastableFactory +impl AnyFactory + for ThreadsafeCastableFactory where - Args: Tuple + 'static, + DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { } -impl AnyThreadsafeFactory - for ThreadsafeCastableFactory +impl AnyThreadsafeFactory + for ThreadsafeCastableFactory where - Args: Tuple + 'static, + DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { } -impl Debug for ThreadsafeCastableFactory +impl Debug + for ThreadsafeCastableFactory where - Args: Tuple + 'static, + DIContainerT: 'static, ReturnInterface: 'static + ?Sized, { #[cfg(not(tarpaulin_include))] fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut args = type_name::(); - - if args.len() < 2 { - return Err(std::fmt::Error); - } - - args = args - .get(1..args.len() - 1) - .map_or_else(|| Err(std::fmt::Error), Ok)?; - - if args.ends_with(',') { - args = args - .get(..args.len() - 1) - .map_or_else(|| Err(std::fmt::Error), Ok)?; - } - let ret = type_name::>(); - formatter.write_fmt(format_args!("ThreadsafeCastableFactory ({args}) -> {ret}",)) + formatter.write_fmt(format_args!( + "ThreadsafeCastableFactory (Arc) -> {ret} {{ ... }}", + )) } } @@ -124,6 +116,7 @@ where mod tests { use super::*; + use crate::di_container::asynchronous::MockAsyncDIContainer; #[derive(Debug, PartialEq, Eq)] struct Bacon @@ -134,11 +127,13 @@ mod tests #[test] fn can_call() { - let castable_factory = ThreadsafeCastableFactory::new(&|heal_amount| { - TransientPtr::new(Bacon { heal_amount }) + let castable_factory = ThreadsafeCastableFactory::new(&|_| { + TransientPtr::new(Bacon { heal_amount: 27 }) }); - let output = castable_factory.call((27,)); + let mock_di_container = Arc::new(MockAsyncDIContainer::new()); + + let output = castable_factory.call((mock_di_container,)); assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 })); } @@ -146,11 +141,13 @@ mod tests #[test] fn can_call_mut() { - let mut castable_factory = ThreadsafeCastableFactory::new(&|heal_amount| { - TransientPtr::new(Bacon { heal_amount }) + let mut castable_factory = ThreadsafeCastableFactory::new(&|_| { + TransientPtr::new(Bacon { heal_amount: 1092 }) }); - let output = castable_factory.call_mut((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 })); } @@ -158,11 +155,13 @@ mod tests #[test] fn can_call_once() { - let castable_factory = ThreadsafeCastableFactory::new(&|heal_amount| { - TransientPtr::new(Bacon { heal_amount }) + let castable_factory = ThreadsafeCastableFactory::new(&|_| { + TransientPtr::new(Bacon { heal_amount: 547 }) }); - let output = castable_factory.call_once((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 94e1023..730338f 100644 --- a/src/private/factory.rs +++ b/src/private/factory.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "async")] +use std::sync::Arc; + use crate::private::cast::CastFrom; use crate::ptr::TransientPtr; @@ -11,10 +14,10 @@ where /// Interface for a threadsafe factory. #[cfg(feature = "async")] -pub trait IThreadsafeFactory: - Fn> + crate::private::cast::CastFromArc +pub trait IThreadsafeFactory: + Fn<(Arc,), Output = TransientPtr> + + crate::private::cast::CastFromArc where - Args: std::marker::Tuple, ReturnInterface: 'static + ?Sized, { } -- cgit v1.2.3-18-g5258