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 --- src/private/castable_factory/threadsafe.rs | 109 ++++++++++++++--------------- 1 file changed, 54 insertions(+), 55 deletions(-) (limited to 'src/private/castable_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 })); } -- cgit v1.2.3-18-g5258