From aa548ded39c7ba1927019c748c359523b21d59e8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 29 Oct 2022 14:38:51 +0200 Subject: refactor!: add dependency history type BREAKING CHANGE: Binding builders & configurators now take dependency history type arguments, the DetectedCircular variant of InjectableError now contains a dependency history field & the injectable traits take dependency history instead of a Vec --- src/provider/async.rs | 161 +++++++++++++++++++++++++++++------------------ src/provider/blocking.rs | 124 ++++++++++++++++++++++-------------- 2 files changed, 177 insertions(+), 108 deletions(-) (limited to 'src/provider') diff --git a/src/provider/async.rs b/src/provider/async.rs index 8d482cd..557617b 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -3,18 +3,24 @@ use std::sync::Arc; use async_trait::async_trait; +use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::injectable::InjectableError; use crate::interfaces::async_injectable::AsyncInjectable; use crate::ptr::{ThreadsafeSingletonPtr, TransientPtr}; #[derive(strum_macros::Display, Debug)] -pub enum AsyncProvidable +pub enum AsyncProvidable where - DIContainerType: IAsyncDIContainer, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { - Transient(TransientPtr>), - Singleton(ThreadsafeSingletonPtr>), + Transient(TransientPtr>), + Singleton( + ThreadsafeSingletonPtr< + dyn AsyncInjectable, + >, + ), #[cfg(feature = "factory")] Factory( crate::ptr::ThreadsafeFactoryPtr< @@ -37,22 +43,26 @@ where #[async_trait] #[cfg_attr(test, mockall::automock, allow(dead_code))] -pub trait IAsyncProvider: Send + Sync +pub trait IAsyncProvider: Send + Sync where - DIContainerType: IAsyncDIContainer, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { async fn provide( &self, di_container: &Arc, - dependency_history: Vec<&'static str>, - ) -> Result, InjectableError>; + dependency_history: DependencyHistoryType, + ) -> Result, InjectableError>; - fn do_clone(&self) -> Box>; + fn do_clone(&self) + -> Box>; } -impl Clone for Box> +impl Clone + for Box> where - DIContainerType: IAsyncDIContainer, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { fn clone(&self) -> Self { @@ -60,127 +70,148 @@ where } } -pub struct AsyncTransientTypeProvider -where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, +pub struct AsyncTransientTypeProvider< + InjectableType, + DIContainerType, + DependencyHistoryType, +> where + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { injectable_phantom: PhantomData, di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } -impl - AsyncTransientTypeProvider +impl + AsyncTransientTypeProvider where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { pub fn new() -> Self { Self { injectable_phantom: PhantomData, di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } } } #[async_trait] -impl IAsyncProvider - for AsyncTransientTypeProvider +impl + IAsyncProvider + for AsyncTransientTypeProvider where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, { async fn provide( &self, di_container: &Arc, - dependency_history: Vec<&'static str>, - ) -> Result, InjectableError> + dependency_history: DependencyHistoryType, + ) -> Result, InjectableError> { Ok(AsyncProvidable::Transient( InjectableType::resolve(di_container, dependency_history).await?, )) } - fn do_clone(&self) -> Box> + fn do_clone(&self) + -> Box> { Box::new(self.clone()) } } -impl Clone - for AsyncTransientTypeProvider +impl Clone + for AsyncTransientTypeProvider where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { fn clone(&self) -> Self { Self { injectable_phantom: self.injectable_phantom, - di_container_phantom: self.di_container_phantom, + di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } } } -pub struct AsyncSingletonProvider +pub struct AsyncSingletonProvider where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { singleton: ThreadsafeSingletonPtr, di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } -impl - AsyncSingletonProvider +impl + AsyncSingletonProvider where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { pub fn new(singleton: ThreadsafeSingletonPtr) -> Self { Self { singleton, di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } } } #[async_trait] -impl IAsyncProvider - for AsyncSingletonProvider +impl + IAsyncProvider + for AsyncSingletonProvider where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, { async fn provide( &self, _di_container: &Arc, - _dependency_history: Vec<&'static str>, - ) -> Result, InjectableError> + _dependency_history: DependencyHistoryType, + ) -> Result, InjectableError> { Ok(AsyncProvidable::Singleton(self.singleton.clone())) } - fn do_clone(&self) -> Box> + fn do_clone(&self) + -> Box> { Box::new(self.clone()) } } -impl Clone - for AsyncSingletonProvider +impl Clone + for AsyncSingletonProvider where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync, { fn clone(&self) -> Self { Self { singleton: self.singleton.clone(), di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } } } @@ -218,15 +249,17 @@ impl AsyncFactoryProvider #[cfg(feature = "factory")] #[async_trait] -impl IAsyncProvider for AsyncFactoryProvider +impl + IAsyncProvider for AsyncFactoryProvider where - DIContainerType: IAsyncDIContainer, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, { async fn provide( &self, _di_container: &Arc, - _dependency_history: Vec<&'static str>, - ) -> Result, InjectableError> + _dependency_history: DependencyHistoryType, + ) -> Result, InjectableError> { Ok(match self.variant { AsyncFactoryVariant::Normal => AsyncProvidable::Factory(self.factory.clone()), @@ -239,7 +272,8 @@ where }) } - fn do_clone(&self) -> Box> + fn do_clone(&self) + -> Box> { Box::new(self.clone()) } @@ -263,6 +297,7 @@ mod tests use std::error::Error; use super::*; + use crate::test_utils::mocks::MockDependencyHistory; use crate::test_utils::{mocks, subjects_async}; #[tokio::test] @@ -270,7 +305,8 @@ mod tests { let transient_type_provider = AsyncTransientTypeProvider::< subjects_async::UserManager, - mocks::async_di_container::MockAsyncDIContainer, + mocks::async_di_container::MockAsyncDIContainer, + mocks::MockDependencyHistory, >::new(); let di_container = mocks::async_di_container::MockAsyncDIContainer::new(); @@ -278,7 +314,7 @@ mod tests assert!( matches!( transient_type_provider - .provide(&Arc::new(di_container), vec![]) + .provide(&Arc::new(di_container), MockDependencyHistory::new()) .await?, AsyncProvidable::Transient(_) ), @@ -293,7 +329,8 @@ mod tests { let singleton_provider = AsyncSingletonProvider::< subjects_async::UserManager, - mocks::async_di_container::MockAsyncDIContainer, + mocks::async_di_container::MockAsyncDIContainer, + mocks::MockDependencyHistory, >::new(ThreadsafeSingletonPtr::new( subjects_async::UserManager {}, )); @@ -303,7 +340,7 @@ mod tests assert!( matches!( singleton_provider - .provide(&Arc::new(di_container), vec![]) + .provide(&Arc::new(di_container), MockDependencyHistory::new()) .await?, AsyncProvidable::Singleton(_) ), @@ -345,7 +382,9 @@ mod tests assert!( matches!( - factory_provider.provide(&di_container, vec![]).await?, + factory_provider + .provide(&di_container, mocks::MockDependencyHistory::new()) + .await?, AsyncProvidable::Factory(_) ), "The provided type is not a factory" @@ -354,7 +393,7 @@ mod tests assert!( matches!( default_factory_provider - .provide(&di_container, vec![]) + .provide(&di_container, MockDependencyHistory::new()) .await?, AsyncProvidable::DefaultFactory(_) ), @@ -364,7 +403,7 @@ mod tests assert!( matches!( async_default_factory_provider - .provide(&di_container, vec![]) + .provide(&di_container, MockDependencyHistory::new()) .await?, AsyncProvidable::AsyncDefaultFactory(_) ), 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 +pub enum Providable where - DIContainerType: IDIContainer, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { - Transient(TransientPtr>), - Singleton(SingletonPtr>), + Transient(TransientPtr>), + Singleton(SingletonPtr>), #[cfg(feature = "factory")] Factory(crate::ptr::FactoryPtr), #[cfg(feature = "factory")] @@ -22,52 +24,59 @@ where } #[cfg_attr(test, mockall::automock, allow(dead_code))] -pub trait IProvider +pub trait IProvider where - DIContainerType: IDIContainer, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { fn provide( &self, di_container: &Rc, - dependency_history: Vec<&'static str>, - ) -> Result, InjectableError>; + dependency_history: DependencyHistoryType, + ) -> Result, InjectableError>; } -pub struct TransientTypeProvider +pub struct TransientTypeProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, + InjectableType: Injectable, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { injectable_phantom: PhantomData, di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } -impl - TransientTypeProvider +impl + TransientTypeProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, + InjectableType: Injectable, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { pub fn new() -> Self { Self { injectable_phantom: PhantomData, di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } } } -impl IProvider - for TransientTypeProvider +impl + IProvider + for TransientTypeProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, + InjectableType: Injectable, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { fn provide( &self, di_container: &Rc, - dependency_history: Vec<&'static str>, - ) -> Result, InjectableError> + dependency_history: DependencyHistoryType, + ) -> Result, InjectableError> { Ok(Providable::Transient(InjectableType::resolve( di_container, @@ -76,40 +85,48 @@ where } } -pub struct SingletonProvider +pub struct SingletonProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, + InjectableType: Injectable, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { singleton: SingletonPtr, + di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } -impl SingletonProvider +impl + SingletonProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, + InjectableType: Injectable, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { pub fn new(singleton: SingletonPtr) -> Self { Self { singleton, di_container_phantom: PhantomData, + dependency_history_phantom: PhantomData, } } } -impl IProvider - for SingletonProvider +impl + IProvider + for SingletonProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, + InjectableType: Injectable, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { fn provide( &self, _di_container: &Rc, - _dependency_history: Vec<&'static str>, - ) -> Result, InjectableError> + _dependency_history: DependencyHistoryType, + ) -> Result, InjectableError> { Ok(Providable::Singleton(self.singleton.clone())) } @@ -138,15 +155,17 @@ impl FactoryProvider } #[cfg(feature = "factory")] -impl IProvider for FactoryProvider +impl + IProvider for FactoryProvider where - DIContainerType: IDIContainer, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { fn provide( &self, _di_container: &Rc, - _dependency_history: Vec<&'static str>, - ) -> Result, InjectableError> + _dependency_history: DependencyHistoryType, + ) -> Result, 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, >::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> { - 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, + >::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" -- cgit v1.2.3-18-g5258