diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/di_container/blocking/mod.rs | 12 | ||||
-rw-r--r-- | src/private/castable_factory/blocking.rs | 85 | ||||
-rw-r--r-- | src/private/factory.rs | 9 |
3 files changed, 50 insertions, 56 deletions
diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index 1ee0294..702141a 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -209,7 +209,7 @@ impl DIContainer use crate::private::factory::IFactory; let factory = factory_binding - .cast::<dyn IFactory<(Rc<DIContainer>,), Interface>>() + .cast::<dyn IFactory<Interface, Self>>() .map_err(|_| DIContainerError::CastFailed { interface: type_name::<Interface>(), binding_kind: "factory", @@ -222,11 +222,13 @@ impl DIContainer use crate::private::factory::IFactory; use crate::ptr::TransientPtr; + type DefaultFactoryFn<Interface> = dyn IFactory< + dyn Fn<(), Output = TransientPtr<Interface>>, + DIContainer, + >; + let default_factory = factory_binding - .cast::<dyn IFactory< - (Rc<DIContainer>,), - dyn Fn<(), Output = TransientPtr<Interface>>, - >>() + .cast::<DefaultFactoryFn<Interface>>() .map_err(|_| DIContainerError::CastFailed { interface: type_name::<Interface>(), binding_kind: "default 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<Args, ReturnInterface> +pub struct CastableFactory<ReturnInterface, DIContainerT> where - Args: Tuple + 'static, ReturnInterface: 'static + ?Sized, + DIContainerT: 'static, { - func: &'static dyn Fn<Args, Output = TransientPtr<ReturnInterface>>, + func: &'static dyn Fn<(Rc<DIContainerT>,), Output = TransientPtr<ReturnInterface>>, } -impl<Args, ReturnInterface> CastableFactory<Args, ReturnInterface> +impl<ReturnInterface, DIContainerT> CastableFactory<ReturnInterface, DIContainerT> where - Args: Tuple + 'static, ReturnInterface: 'static + ?Sized, { pub fn new( - func: &'static dyn Fn<Args, Output = TransientPtr<ReturnInterface>>, + func: &'static dyn Fn< + (Rc<DIContainerT>,), + Output = TransientPtr<ReturnInterface>, + >, ) -> Self { Self { func } } } -impl<Args, ReturnInterface> IFactory<Args, ReturnInterface> - for CastableFactory<Args, ReturnInterface> +impl<ReturnInterface, DIContainerT> IFactory<ReturnInterface, DIContainerT> + for CastableFactory<ReturnInterface, DIContainerT> where - Args: Tuple + 'static, ReturnInterface: 'static + ?Sized, { } -impl<Args, ReturnInterface> Fn<Args> for CastableFactory<Args, ReturnInterface> +impl<ReturnInterface, DIContainerT> Fn<(Rc<DIContainerT>,)> + for CastableFactory<ReturnInterface, DIContainerT> 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<DIContainerT>,)) -> Self::Output { self.func.call(args) } } -impl<Args, ReturnInterface> FnMut<Args> for CastableFactory<Args, ReturnInterface> +impl<ReturnInterface, DIContainerT> FnMut<(Rc<DIContainerT>,)> + for CastableFactory<ReturnInterface, DIContainerT> 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<DIContainerT>,)) -> Self::Output { self.call(args) } } -impl<Args, ReturnInterface> FnOnce<Args> for CastableFactory<Args, ReturnInterface> +impl<ReturnInterface, DIContainerT> FnOnce<(Rc<DIContainerT>,)> + for CastableFactory<ReturnInterface, DIContainerT> where - Args: Tuple + 'static, ReturnInterface: 'static + ?Sized, { type Output = TransientPtr<ReturnInterface>; - extern "rust-call" fn call_once(self, args: Args) -> Self::Output + extern "rust-call" fn call_once(self, args: (Rc<DIContainerT>,)) -> Self::Output { self.call(args) } } -impl<Args, ReturnInterface> AnyFactory for CastableFactory<Args, ReturnInterface> +impl<ReturnInterface, DIContainerT> AnyFactory + for CastableFactory<ReturnInterface, DIContainerT> where - Args: Tuple + 'static, ReturnInterface: 'static + ?Sized, + DIContainerT: 'static, { } -impl<Args, ReturnInterface> Debug for CastableFactory<Args, ReturnInterface> +impl<ReturnInterface, DIContainerT> Debug + for CastableFactory<ReturnInterface, DIContainerT> 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::<Args>(); - - 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::<TransientPtr<ReturnInterface>>(); - formatter.write_fmt(format_args!("CastableFactory ({args}) -> {ret}")) + formatter.write_fmt(format_args!("CastableFactory (Rc<DIContainer>) -> {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 })); } diff --git a/src/private/factory.rs b/src/private/factory.rs index 84b00c6..af6df8a 100644 --- a/src/private/factory.rs +++ b/src/private/factory.rs @@ -1,13 +1,12 @@ -use std::marker::Tuple; +use std::rc::Rc; use crate::private::cast::CastFrom; use crate::ptr::TransientPtr; /// Interface for a factory. -pub trait IFactory<Args, ReturnInterface>: - Fn<Args, Output = TransientPtr<ReturnInterface>> + CastFrom +pub trait IFactory<ReturnInterface, DIContainerT>: + Fn<(Rc<DIContainerT>,), Output = TransientPtr<ReturnInterface>> + CastFrom where - Args: Tuple, ReturnInterface: 'static + ?Sized, { } @@ -17,7 +16,7 @@ where pub trait IThreadsafeFactory<Args, ReturnInterface>: Fn<Args, Output = TransientPtr<ReturnInterface>> + crate::private::cast::CastFromArc where - Args: Tuple, + Args: std::marker::Tuple, ReturnInterface: 'static + ?Sized, { } |