From 89c238f9c82ade2d7656e2bee76838a391609a88 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 5 Aug 2023 23:14:06 +0200 Subject: refactor!: remove IDependencyHistory BREAKING CHANGE: IDependencyHistory has been removed as part of an effort to simplify the API. This affects IDIContainer, DIContainer, IAsyncDIContainer, AsyncDIContainer, Injectable, AsyncInjectable, BindingBuilder, AsyncBindingBuilder, BindingScopeConfigurator, BindingWhenConfigurator, AsyncBindingScopeConfigurator, AsyncBindingWhenConfigurator and DependencyHistory --- src/provider/async.rs | 153 +++++++++++++++++++---------------------------- src/provider/blocking.rs | 124 +++++++++++++++++--------------------- 2 files changed, 114 insertions(+), 163 deletions(-) (limited to 'src/provider') diff --git a/src/provider/async.rs b/src/provider/async.rs index da42c7b..38600c9 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -3,24 +3,21 @@ 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}; +use crate::util::use_dependency_history; + +use_dependency_history!(); #[derive(strum_macros::Display, Debug)] -pub enum AsyncProvidable +pub enum AsyncProvidable where - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { - Transient(TransientPtr>), - Singleton( - ThreadsafeSingletonPtr< - dyn AsyncInjectable, - >, - ), + Transient(TransientPtr>), + Singleton(ThreadsafeSingletonPtr>), #[cfg(feature = "factory")] Factory( crate::ptr::ThreadsafeFactoryPtr< @@ -43,26 +40,22 @@ where #[async_trait] #[cfg_attr(test, mockall::automock, allow(dead_code))] -pub trait IAsyncProvider: Send + Sync +pub trait IAsyncProvider: Send + Sync where - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { async fn provide( &self, di_container: &Arc, - dependency_history: DependencyHistoryType, - ) -> Result, InjectableError>; + dependency_history: DependencyHistory, + ) -> Result, InjectableError>; - fn do_clone(&self) - -> Box>; + fn do_clone(&self) -> Box>; } -impl Clone - for Box> +impl Clone for Box> where - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { fn clone(&self) -> Self { @@ -70,148 +63,127 @@ where } } -pub struct AsyncTransientTypeProvider< - InjectableType, - DIContainerType, - DependencyHistoryType, -> where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, +pub struct AsyncTransientTypeProvider +where + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, { injectable_phantom: PhantomData, di_container_phantom: PhantomData, - dependency_history_phantom: PhantomData, } -impl - AsyncTransientTypeProvider +impl + AsyncTransientTypeProvider where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, { 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, - DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, { async fn provide( &self, di_container: &Arc, - dependency_history: DependencyHistoryType, - ) -> Result, InjectableError> + dependency_history: DependencyHistory, + ) -> 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, - DependencyHistoryType: IDependencyHistory + Send + Sync, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, { fn clone(&self) -> Self { Self { injectable_phantom: self.injectable_phantom, di_container_phantom: PhantomData, - dependency_history_phantom: PhantomData, } } } -pub struct AsyncSingletonProvider +pub struct AsyncSingletonProvider where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, { singleton: ThreadsafeSingletonPtr, di_container_phantom: PhantomData, - dependency_history_phantom: PhantomData, } -impl - AsyncSingletonProvider +impl + AsyncSingletonProvider where - InjectableType: AsyncInjectable, - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, { 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, - DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, { async fn provide( &self, _di_container: &Arc, - _dependency_history: DependencyHistoryType, - ) -> Result, InjectableError> + _dependency_history: DependencyHistory, + ) -> 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, - DependencyHistoryType: IDependencyHistory + Send + Sync, + InjectableType: AsyncInjectable, + DIContainerType: IAsyncDIContainer, { fn clone(&self) -> Self { Self { singleton: self.singleton.clone(), di_container_phantom: PhantomData, - dependency_history_phantom: PhantomData, } } } @@ -249,17 +221,15 @@ impl AsyncFactoryProvider #[cfg(feature = "factory")] #[async_trait] -impl - IAsyncProvider for AsyncFactoryProvider +impl IAsyncProvider for AsyncFactoryProvider where - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, + DIContainerType: IAsyncDIContainer, { async fn provide( &self, _di_container: &Arc, - _dependency_history: DependencyHistoryType, - ) -> Result, InjectableError> + _dependency_history: DependencyHistory, + ) -> Result, InjectableError> { Ok(match self.variant { AsyncFactoryVariant::Normal => AsyncProvidable::Factory(self.factory.clone()), @@ -272,8 +242,7 @@ where }) } - fn do_clone(&self) - -> Box> + fn do_clone(&self) -> Box> { Box::new(self.clone()) } @@ -297,7 +266,7 @@ mod tests use std::error::Error; use super::*; - use crate::test_utils::mocks::MockDependencyHistory; + use crate::dependency_history::MockDependencyHistory; use crate::test_utils::{mocks, subjects_async}; #[tokio::test] @@ -305,8 +274,7 @@ mod tests { let transient_type_provider = AsyncTransientTypeProvider::< subjects_async::UserManager, - mocks::async_di_container::MockAsyncDIContainer, - mocks::MockDependencyHistory, + mocks::async_di_container::MockAsyncDIContainer, >::new(); let di_container = mocks::async_di_container::MockAsyncDIContainer::new(); @@ -329,8 +297,7 @@ mod tests { let singleton_provider = AsyncSingletonProvider::< subjects_async::UserManager, - mocks::async_di_container::MockAsyncDIContainer, - mocks::MockDependencyHistory, + mocks::async_di_container::MockAsyncDIContainer, >::new(ThreadsafeSingletonPtr::new( subjects_async::UserManager {}, )); @@ -383,7 +350,7 @@ mod tests assert!( matches!( factory_provider - .provide(&di_container, mocks::MockDependencyHistory::new()) + .provide(&di_container, MockDependencyHistory::new()) .await?, AsyncProvidable::Factory(_) ), diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs index ea506ab..a18e997 100644 --- a/src/provider/blocking.rs +++ b/src/provider/blocking.rs @@ -1,20 +1,21 @@ 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}; +use crate::util::use_dependency_history; + +use_dependency_history!(); #[derive(strum_macros::Display, Debug)] -pub enum Providable +pub enum Providable where - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { - Transient(TransientPtr>), - Singleton(SingletonPtr>), + Transient(TransientPtr>), + Singleton(SingletonPtr>), #[cfg(feature = "factory")] Factory(crate::ptr::FactoryPtr), #[cfg(feature = "factory")] @@ -22,35 +23,32 @@ where } #[cfg_attr(test, mockall::automock, allow(dead_code))] -pub trait IProvider +pub trait IProvider where - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { fn provide( &self, di_container: &Rc, - dependency_history: DependencyHistoryType, - ) -> Result, InjectableError>; + dependency_history: DependencyHistory, + ) -> Result, InjectableError>; } -pub struct TransientTypeProvider +pub struct TransientTypeProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + InjectableType: Injectable, + DIContainerType: IDIContainer, { injectable_phantom: PhantomData, di_container_phantom: PhantomData, - dependency_history_phantom: PhantomData, + dependency_history_phantom: PhantomData, } -impl - TransientTypeProvider +impl + TransientTypeProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + InjectableType: Injectable, + DIContainerType: IDIContainer, { pub fn new() -> Self { @@ -62,19 +60,17 @@ where } } -impl - IProvider - for TransientTypeProvider +impl IProvider + for TransientTypeProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + InjectableType: Injectable, + DIContainerType: IDIContainer, { fn provide( &self, di_container: &Rc, - dependency_history: DependencyHistoryType, - ) -> Result, InjectableError> + dependency_history: DependencyHistory, + ) -> Result, InjectableError> { Ok(Providable::Transient(InjectableType::resolve( di_container, @@ -83,24 +79,21 @@ where } } -pub struct SingletonProvider +pub struct SingletonProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + InjectableType: Injectable, + DIContainerType: IDIContainer, { singleton: SingletonPtr, di_container_phantom: PhantomData, - dependency_history_phantom: PhantomData, + dependency_history_phantom: PhantomData, } -impl - SingletonProvider +impl SingletonProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + InjectableType: Injectable, + DIContainerType: IDIContainer, { pub fn new(singleton: SingletonPtr) -> Self { @@ -112,19 +105,17 @@ where } } -impl - IProvider - for SingletonProvider +impl IProvider + for SingletonProvider where - InjectableType: Injectable, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + InjectableType: Injectable, + DIContainerType: IDIContainer, { fn provide( &self, _di_container: &Rc, - _dependency_history: DependencyHistoryType, - ) -> Result, InjectableError> + _dependency_history: DependencyHistory, + ) -> Result, InjectableError> { Ok(Providable::Singleton(self.singleton.clone())) } @@ -153,17 +144,15 @@ impl FactoryProvider } #[cfg(feature = "factory")] -impl - IProvider for FactoryProvider +impl IProvider for FactoryProvider where - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { fn provide( &self, _di_container: &Rc, - _dependency_history: DependencyHistoryType, - ) -> Result, InjectableError> + _dependency_history: DependencyHistory, + ) -> Result, InjectableError> { Ok(if self.is_default_factory { Providable::DefaultFactory(self.factory.clone()) @@ -179,6 +168,7 @@ mod tests use std::error::Error; use super::*; + use crate::dependency_history::MockDependencyHistory; use crate::test_utils::{mocks, subjects}; #[test] @@ -186,13 +176,12 @@ mod tests { let transient_type_provider = TransientTypeProvider::< subjects::UserManager, - mocks::blocking_di_container::MockDIContainer, - mocks::MockDependencyHistory, + mocks::blocking_di_container::MockDIContainer, >::new(); let di_container = mocks::blocking_di_container::MockDIContainer::new(); - let dependency_history_mock = mocks::MockDependencyHistory::new(); + let dependency_history_mock = MockDependencyHistory::new(); assert!( matches!( @@ -209,22 +198,18 @@ mod tests #[test] fn singleton_provider_works() -> Result<(), Box> { - let singleton_provider = SingletonProvider::< - subjects::UserManager, - mocks::blocking_di_container::MockDIContainer, - mocks::MockDependencyHistory, - >::new(SingletonPtr::new( - subjects::UserManager {}, - )); + let singleton_provider = + SingletonProvider::< + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(SingletonPtr::new(subjects::UserManager {})); let di_container = mocks::blocking_di_container::MockDIContainer::new(); assert!( matches!( - singleton_provider.provide( - &Rc::new(di_container), - mocks::MockDependencyHistory::new() - )?, + singleton_provider + .provide(&Rc::new(di_container), MockDependencyHistory::new())?, Providable::Singleton(_) ), "The provided type is not a singleton" @@ -253,8 +238,7 @@ mod tests assert!( matches!( - factory_provider - .provide(&di_container, mocks::MockDependencyHistory::new())?, + factory_provider.provide(&di_container, MockDependencyHistory::new())?, Providable::Factory(_) ), "The provided type is not a factory" @@ -263,7 +247,7 @@ mod tests assert!( matches!( default_factory_provider - .provide(&di_container, mocks::MockDependencyHistory::new())?, + .provide(&di_container, MockDependencyHistory::new())?, Providable::DefaultFactory(_) ), "The provided type is not a default factory" -- cgit v1.2.3-18-g5258