diff options
Diffstat (limited to 'src/provider/blocking.rs')
-rw-r--r-- | src/provider/blocking.rs | 124 |
1 files changed, 77 insertions, 47 deletions
diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs index e1e2aad..ebe0c37 100644 --- a/src/provider/blocking.rs +++ b/src/provider/blocking.rs @@ -1,18 +1,20 @@ use std::marker::PhantomData; use std::rc::Rc; +use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::IDIContainer; use crate::errors::injectable::InjectableError; use crate::interfaces::injectable::Injectable; use crate::ptr::{SingletonPtr, TransientPtr}; #[derive(strum_macros::Display, Debug)] -pub enum Providable<DIContainerType> +pub enum Providable<DIContainerType, DependencyHistoryType> where - DIContainerType: IDIContainer, + DIContainerType: IDIContainer<DependencyHistoryType>, + DependencyHistoryType: IDependencyHistory, { - Transient(TransientPtr<dyn Injectable<DIContainerType>>), - Singleton(SingletonPtr<dyn Injectable<DIContainerType>>), + Transient(TransientPtr<dyn Injectable<DIContainerType, DependencyHistoryType>>), + Singleton(SingletonPtr<dyn Injectable<DIContainerType, DependencyHistoryType>>), #[cfg(feature = "factory")] Factory(crate::ptr::FactoryPtr<dyn crate::interfaces::any_factory::AnyFactory>), #[cfg(feature = "factory")] @@ -22,52 +24,59 @@ where } #[cfg_attr(test, mockall::automock, allow(dead_code))] -pub trait IProvider<DIContainerType> +pub trait IProvider<DIContainerType, DependencyHistoryType> where - DIContainerType: IDIContainer, + DIContainerType: IDIContainer<DependencyHistoryType>, + DependencyHistoryType: IDependencyHistory, { fn provide( &self, di_container: &Rc<DIContainerType>, - dependency_history: Vec<&'static str>, - ) -> Result<Providable<DIContainerType>, InjectableError>; + dependency_history: DependencyHistoryType, + ) -> Result<Providable<DIContainerType, DependencyHistoryType>, InjectableError>; } -pub struct TransientTypeProvider<InjectableType, DIContainerType> +pub struct TransientTypeProvider<InjectableType, DIContainerType, DependencyHistoryType> where - InjectableType: Injectable<DIContainerType>, - DIContainerType: IDIContainer, + InjectableType: Injectable<DIContainerType, DependencyHistoryType>, + DIContainerType: IDIContainer<DependencyHistoryType>, + DependencyHistoryType: IDependencyHistory, { injectable_phantom: PhantomData<InjectableType>, di_container_phantom: PhantomData<DIContainerType>, + dependency_history_phantom: PhantomData<DependencyHistoryType>, } -impl<InjectableType, DIContainerType> - TransientTypeProvider<InjectableType, DIContainerType> +impl<InjectableType, DIContainerType, DependencyHistoryType> + TransientTypeProvider<InjectableType, DIContainerType, DependencyHistoryType> where - InjectableType: Injectable<DIContainerType>, - DIContainerType: IDIContainer, + InjectableType: Injectable<DIContainerType, DependencyHistoryType>, + DIContainerType: IDIContainer<DependencyHistoryType>, + DependencyHistoryType: IDependencyHistory, { pub fn new() -> Self { Self { injectable_phantom: PhantomData, di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } } } -impl<InjectableType, DIContainerType> IProvider<DIContainerType> - for TransientTypeProvider<InjectableType, DIContainerType> +impl<InjectableType, DIContainerType, DependencyHistoryType> + IProvider<DIContainerType, DependencyHistoryType> + for TransientTypeProvider<InjectableType, DIContainerType, DependencyHistoryType> where - InjectableType: Injectable<DIContainerType>, - DIContainerType: IDIContainer, + InjectableType: Injectable<DIContainerType, DependencyHistoryType>, + DIContainerType: IDIContainer<DependencyHistoryType>, + DependencyHistoryType: IDependencyHistory, { fn provide( &self, di_container: &Rc<DIContainerType>, - dependency_history: Vec<&'static str>, - ) -> Result<Providable<DIContainerType>, InjectableError> + dependency_history: DependencyHistoryType, + ) -> Result<Providable<DIContainerType, DependencyHistoryType>, InjectableError> { Ok(Providable::Transient(InjectableType::resolve( di_container, @@ -76,40 +85,48 @@ where } } -pub struct SingletonProvider<InjectableType, DIContainerType> +pub struct SingletonProvider<InjectableType, DIContainerType, DependencyHistoryType> where - InjectableType: Injectable<DIContainerType>, - DIContainerType: IDIContainer, + InjectableType: Injectable<DIContainerType, DependencyHistoryType>, + DIContainerType: IDIContainer<DependencyHistoryType>, + DependencyHistoryType: IDependencyHistory, { singleton: SingletonPtr<InjectableType>, + di_container_phantom: PhantomData<DIContainerType>, + dependency_history_phantom: PhantomData<DependencyHistoryType>, } -impl<InjectableType, DIContainerType> SingletonProvider<InjectableType, DIContainerType> +impl<InjectableType, DIContainerType, DependencyHistoryType> + SingletonProvider<InjectableType, DIContainerType, DependencyHistoryType> where - InjectableType: Injectable<DIContainerType>, - DIContainerType: IDIContainer, + InjectableType: Injectable<DIContainerType, DependencyHistoryType>, + DIContainerType: IDIContainer<DependencyHistoryType>, + DependencyHistoryType: IDependencyHistory, { pub fn new(singleton: SingletonPtr<InjectableType>) -> Self { Self { singleton, di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } } } -impl<InjectableType, DIContainerType> IProvider<DIContainerType> - for SingletonProvider<InjectableType, DIContainerType> +impl<InjectableType, DIContainerType, DependencyHistoryType> + IProvider<DIContainerType, DependencyHistoryType> + for SingletonProvider<InjectableType, DIContainerType, DependencyHistoryType> where - InjectableType: Injectable<DIContainerType>, - DIContainerType: IDIContainer, + InjectableType: Injectable<DIContainerType, DependencyHistoryType>, + DIContainerType: IDIContainer<DependencyHistoryType>, + DependencyHistoryType: IDependencyHistory, { fn provide( &self, _di_container: &Rc<DIContainerType>, - _dependency_history: Vec<&'static str>, - ) -> Result<Providable<DIContainerType>, InjectableError> + _dependency_history: DependencyHistoryType, + ) -> Result<Providable<DIContainerType, DependencyHistoryType>, InjectableError> { Ok(Providable::Singleton(self.singleton.clone())) } @@ -138,15 +155,17 @@ impl FactoryProvider } #[cfg(feature = "factory")] -impl<DIContainerType> IProvider<DIContainerType> for FactoryProvider +impl<DIContainerType, DependencyHistoryType> + IProvider<DIContainerType, DependencyHistoryType> for FactoryProvider where - DIContainerType: IDIContainer, + DIContainerType: IDIContainer<DependencyHistoryType>, + DependencyHistoryType: IDependencyHistory, { fn provide( &self, _di_container: &Rc<DIContainerType>, - _dependency_history: Vec<&'static str>, - ) -> Result<Providable<DIContainerType>, InjectableError> + _dependency_history: DependencyHistoryType, + ) -> Result<Providable<DIContainerType, DependencyHistoryType>, InjectableError> { Ok(if self.is_default_factory { Providable::DefaultFactory(self.factory.clone()) @@ -169,14 +188,18 @@ mod tests { let transient_type_provider = TransientTypeProvider::< subjects::UserManager, - mocks::blocking_di_container::MockDIContainer, + mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>, + mocks::MockDependencyHistory, >::new(); let di_container = mocks::blocking_di_container::MockDIContainer::new(); + let dependency_history_mock = mocks::MockDependencyHistory::new(); + assert!( matches!( - transient_type_provider.provide(&Rc::new(di_container), vec![])?, + transient_type_provider + .provide(&Rc::new(di_container), dependency_history_mock)?, Providable::Transient(_) ), "The provided type is not transient" @@ -188,17 +211,22 @@ mod tests #[test] fn singleton_provider_works() -> Result<(), Box<dyn Error>> { - let singleton_provider = - SingletonProvider::< - subjects::UserManager, - mocks::blocking_di_container::MockDIContainer, - >::new(SingletonPtr::new(subjects::UserManager {})); + let singleton_provider = SingletonProvider::< + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>, + mocks::MockDependencyHistory, + >::new(SingletonPtr::new( + subjects::UserManager {}, + )); let di_container = mocks::blocking_di_container::MockDIContainer::new(); assert!( matches!( - singleton_provider.provide(&Rc::new(di_container), vec![])?, + singleton_provider.provide( + &Rc::new(di_container), + mocks::MockDependencyHistory::new() + )?, Providable::Singleton(_) ), "The provided type is not a singleton" @@ -227,7 +255,8 @@ mod tests assert!( matches!( - factory_provider.provide(&di_container, vec![])?, + factory_provider + .provide(&di_container, mocks::MockDependencyHistory::new())?, Providable::Factory(_) ), "The provided type is not a factory" @@ -235,7 +264,8 @@ mod tests assert!( matches!( - default_factory_provider.provide(&di_container, vec![])?, + default_factory_provider + .provide(&di_container, mocks::MockDependencyHistory::new())?, Providable::DefaultFactory(_) ), "The provided type is not a default factory" |