From 75ca777bbeb618e14b1cf8854ebb37b7a2c884b5 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 16 Sep 2023 16:04:47 +0200 Subject: refactor: make castable factory take DI container param --- src/private/castable_factory/blocking.rs | 85 +++++++++++++++----------------- 1 file changed, 39 insertions(+), 46 deletions(-) (limited to 'src/private/castable_factory') diff --git a/src/private/castable_factory/blocking.rs b/src/private/castable_factory/blocking.rs index bf0dde8..24c653e 100644 --- a/src/private/castable_factory/blocking.rs +++ b/src/private/castable_factory/blocking.rs @@ -1,109 +1,95 @@ use std::any::type_name; use std::fmt::Debug; -use std::marker::Tuple; +use std::rc::Rc; use crate::private::any_factory::AnyFactory; use crate::private::factory::IFactory; use crate::ptr::TransientPtr; -pub struct CastableFactory +pub struct CastableFactory where - Args: Tuple + 'static, ReturnInterface: 'static + ?Sized, + DIContainerT: 'static, { - func: &'static dyn Fn>, + func: &'static dyn Fn<(Rc,), Output = TransientPtr>, } -impl CastableFactory +impl CastableFactory where - Args: Tuple + 'static, ReturnInterface: 'static + ?Sized, { pub fn new( - func: &'static dyn Fn>, + func: &'static dyn Fn< + (Rc,), + Output = TransientPtr, + >, ) -> Self { Self { func } } } -impl IFactory - for CastableFactory +impl IFactory + for CastableFactory where - Args: Tuple + 'static, ReturnInterface: 'static + ?Sized, { } -impl Fn for CastableFactory +impl Fn<(Rc,)> + for CastableFactory where - Args: Tuple + 'static, ReturnInterface: 'static + ?Sized, { - extern "rust-call" fn call(&self, args: Args) -> Self::Output + extern "rust-call" fn call(&self, args: (Rc,)) -> Self::Output { self.func.call(args) } } -impl FnMut for CastableFactory +impl FnMut<(Rc,)> + for CastableFactory where - Args: Tuple + '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: (Rc,)) -> Self::Output { self.call(args) } } -impl FnOnce for CastableFactory +impl FnOnce<(Rc,)> + for CastableFactory where - Args: Tuple + '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: (Rc,)) -> Self::Output { self.call(args) } } -impl AnyFactory for CastableFactory +impl AnyFactory + for CastableFactory where - Args: Tuple + 'static, ReturnInterface: 'static + ?Sized, + DIContainerT: 'static, { } -impl Debug for CastableFactory +impl Debug + for CastableFactory where - Args: Tuple + '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!("CastableFactory ({args}) -> {ret}")) + formatter.write_fmt(format_args!("CastableFactory (Rc) -> {ret}")) } } @@ -111,6 +97,7 @@ where mod tests { use super::*; + use crate::di_container::blocking::MockDIContainer; #[derive(Debug, PartialEq, Eq)] struct Bacon @@ -122,9 +109,11 @@ mod tests fn can_call() { let castable_factory = - CastableFactory::new(&|heal_amount| TransientPtr::new(Bacon { heal_amount })); + CastableFactory::new(&|_| TransientPtr::new(Bacon { heal_amount: 27 })); - let output = castable_factory.call((27,)); + let mock_di_container = Rc::new(MockDIContainer::new()); + + let output = castable_factory.call((mock_di_container,)); assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 })); } @@ -133,9 +122,11 @@ mod tests fn can_call_mut() { let mut castable_factory = - CastableFactory::new(&|heal_amount| TransientPtr::new(Bacon { heal_amount })); + CastableFactory::new(&|_| TransientPtr::new(Bacon { heal_amount: 103 })); + + let mock_di_container = Rc::new(MockDIContainer::new()); - let output = castable_factory.call_mut((103,)); + let output = castable_factory.call_mut((mock_di_container,)); assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 103 })); } @@ -144,9 +135,11 @@ mod tests fn can_call_once() { let castable_factory = - CastableFactory::new(&|heal_amount| TransientPtr::new(Bacon { heal_amount })); + CastableFactory::new(&|_| TransientPtr::new(Bacon { heal_amount: 19 })); + + let mock_di_container = Rc::new(MockDIContainer::new()); - let output = castable_factory.call_once((19,)); + let output = castable_factory.call_once((mock_di_container,)); assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 19 })); } -- cgit v1.2.3-18-g5258