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/di_container/asynchronous/binding/builder.rs | 107 +++++++++------------ .../asynchronous/binding/scope_configurator.rs | 78 ++++++--------- .../asynchronous/binding/when_configurator.rs | 17 +--- 3 files changed, 79 insertions(+), 123 deletions(-) (limited to 'src/di_container/asynchronous/binding') diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index d33b840..45597e8 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -5,13 +5,15 @@ use std::any::type_name; use std::marker::PhantomData; use std::sync::Arc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::binding::scope_configurator::AsyncBindingScopeConfigurator; #[cfg(feature = "factory")] use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingWhenConfigurator; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::async_di_container::AsyncBindingBuilderError; use crate::interfaces::async_injectable::AsyncInjectable; +use crate::util::use_dependency_history; + +use_dependency_history!(); /// Alias for a threadsafe boxed function. #[cfg(feature = "factory")] @@ -22,28 +24,25 @@ pub type BoxFn = Box<(dyn Fn + Send + Sync) /// /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer #[must_use = "No binding will be created if you don't use the binding builder"] -pub struct AsyncBindingBuilder +pub struct AsyncBindingBuilder where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { di_container: Arc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, } -impl - AsyncBindingBuilder +impl AsyncBindingBuilder where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, + DIContainerType: IAsyncDIContainer, { pub(crate) fn new( di_container: Arc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, ) -> Self { Self { @@ -99,16 +98,11 @@ where pub async fn to( self, ) -> Result< - AsyncBindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, - >, + AsyncBindingScopeConfigurator, AsyncBindingBuilderError, > where - Implementation: AsyncInjectable, + Implementation: AsyncInjectable, { if self.di_container.has_binding::(None).await { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< @@ -182,7 +176,7 @@ where self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator, + AsyncBindingWhenConfigurator, AsyncBindingBuilderError, > where @@ -277,7 +271,7 @@ where self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator, + AsyncBindingWhenConfigurator, AsyncBindingBuilderError, > where @@ -370,7 +364,7 @@ where self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator, + AsyncBindingWhenConfigurator, AsyncBindingBuilderError, > where @@ -465,7 +459,7 @@ where self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator, + AsyncBindingWhenConfigurator, AsyncBindingBuilderError, > where @@ -510,14 +504,14 @@ mod tests use mockall::predicate::eq; use super::*; + use crate::dependency_history::MockDependencyHistory; use crate::test_utils::{mocks, subjects_async}; #[tokio::test] async fn can_bind_to() -> Result<(), Box> { - let mut di_container_mock = mocks::async_di_container::MockAsyncDIContainer::< - mocks::MockDependencyHistory, - >::new(); + let mut di_container_mock = + mocks::async_di_container::MockAsyncDIContainer::new(); di_container_mock .expect_has_binding::() @@ -531,14 +525,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = AsyncBindingBuilder::< - dyn subjects_async::IUserManager, - mocks::async_di_container::MockAsyncDIContainer, - mocks::MockDependencyHistory, - >::new( - Arc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + AsyncBindingBuilder::< + dyn subjects_async::IUserManager, + mocks::async_di_container::MockAsyncDIContainer, + >::new(Arc::new(di_container_mock), MockDependencyHistory::new); binding_builder.to::().await?; @@ -575,14 +566,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = AsyncBindingBuilder::< - IUserManagerFactory, - mocks::async_di_container::MockAsyncDIContainer, - mocks::MockDependencyHistory, - >::new( - Arc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + AsyncBindingBuilder::< + IUserManagerFactory, + mocks::async_di_container::MockAsyncDIContainer, + >::new(Arc::new(di_container_mock), MockDependencyHistory::new); binding_builder .to_factory(&|_| { @@ -624,14 +612,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = AsyncBindingBuilder::< - IUserManagerFactory, - mocks::async_di_container::MockAsyncDIContainer, - mocks::MockDependencyHistory, - >::new( - Arc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + AsyncBindingBuilder::< + IUserManagerFactory, + mocks::async_di_container::MockAsyncDIContainer, + >::new(Arc::new(di_container_mock), MockDependencyHistory::new); binding_builder .to_async_factory(&|_| { @@ -673,14 +658,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = AsyncBindingBuilder::< - dyn subjects_async::IUserManager, - mocks::async_di_container::MockAsyncDIContainer, - mocks::MockDependencyHistory, - >::new( - Arc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + AsyncBindingBuilder::< + dyn subjects_async::IUserManager, + mocks::async_di_container::MockAsyncDIContainer, + >::new(Arc::new(di_container_mock), MockDependencyHistory::new); binding_builder .to_default_factory(&|_| { @@ -723,14 +705,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = AsyncBindingBuilder::< - dyn subjects_async::IUserManager, - mocks::async_di_container::MockAsyncDIContainer, - mocks::MockDependencyHistory, - >::new( - Arc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + AsyncBindingBuilder::< + dyn subjects_async::IUserManager, + mocks::async_di_container::MockAsyncDIContainer, + >::new(Arc::new(di_container_mock), MockDependencyHistory::new); binding_builder .to_async_default_factory(&|_| { diff --git a/src/di_container/asynchronous/binding/scope_configurator.rs b/src/di_container/asynchronous/binding/scope_configurator.rs index 023bb5e..a4a684b 100644 --- a/src/di_container/asynchronous/binding/scope_configurator.rs +++ b/src/di_container/asynchronous/binding/scope_configurator.rs @@ -4,51 +4,42 @@ use std::marker::PhantomData; use std::sync::Arc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingWhenConfigurator; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::async_di_container::AsyncBindingScopeConfiguratorError; use crate::interfaces::async_injectable::AsyncInjectable; use crate::provider::r#async::{AsyncSingletonProvider, AsyncTransientTypeProvider}; use crate::ptr::ThreadsafeSingletonPtr; +use crate::util::use_dependency_history; + +use_dependency_history!(); /// Scope configurator for a binding for type `Interface` inside a [`IAsyncDIContainer`]. /// /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer -pub struct AsyncBindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, -> where +pub struct AsyncBindingScopeConfigurator +where Interface: 'static + ?Sized + Send + Sync, - Implementation: AsyncInjectable, - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + Implementation: AsyncInjectable, + DIContainerType: IAsyncDIContainer, { di_container: Arc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, implementation_phantom: PhantomData, } -impl - AsyncBindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, - > +impl + AsyncBindingScopeConfigurator where Interface: 'static + ?Sized + Send + Sync, - Implementation: AsyncInjectable, - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, + Implementation: AsyncInjectable, + DIContainerType: IAsyncDIContainer, { pub(crate) fn new( di_container: Arc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, ) -> Self { Self { @@ -64,7 +55,7 @@ where /// This is the default. pub async fn in_transient_scope( self, - ) -> AsyncBindingWhenConfigurator + ) -> AsyncBindingWhenConfigurator { self.set_in_transient_scope().await; @@ -78,7 +69,7 @@ where pub async fn in_singleton_scope( self, ) -> Result< - AsyncBindingWhenConfigurator, + AsyncBindingWhenConfigurator, AsyncBindingScopeConfiguratorError, > { @@ -107,11 +98,9 @@ where self.di_container .set_binding::( None, - Box::new(AsyncTransientTypeProvider::< - Implementation, - DIContainerType, - DependencyHistoryType, - >::new()), + Box::new( + AsyncTransientTypeProvider::::new(), + ), ) .await; } @@ -121,6 +110,7 @@ where mod tests { use super::*; + use crate::dependency_history::MockDependencyHistory; use crate::test_utils::{mocks, subjects_async}; #[tokio::test] @@ -135,15 +125,12 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_scope_configurator = AsyncBindingScopeConfigurator::< - dyn subjects_async::IUserManager, - subjects_async::UserManager, - mocks::async_di_container::MockAsyncDIContainer, - mocks::MockDependencyHistory, - >::new( - Arc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_scope_configurator = + AsyncBindingScopeConfigurator::< + dyn subjects_async::IUserManager, + subjects_async::UserManager, + mocks::async_di_container::MockAsyncDIContainer, + >::new(Arc::new(di_container_mock), MockDependencyHistory::new); binding_scope_configurator.in_transient_scope().await; } @@ -160,15 +147,12 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_scope_configurator = AsyncBindingScopeConfigurator::< - dyn subjects_async::IUserManager, - subjects_async::UserManager, - mocks::async_di_container::MockAsyncDIContainer, - mocks::MockDependencyHistory, - >::new( - Arc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_scope_configurator = + AsyncBindingScopeConfigurator::< + dyn subjects_async::IUserManager, + subjects_async::UserManager, + mocks::async_di_container::MockAsyncDIContainer, + >::new(Arc::new(di_container_mock), MockDependencyHistory::new); assert!(binding_scope_configurator .in_singleton_scope() diff --git a/src/di_container/asynchronous/binding/when_configurator.rs b/src/di_container/asynchronous/binding/when_configurator.rs index 39e1807..d08239e 100644 --- a/src/di_container/asynchronous/binding/when_configurator.rs +++ b/src/di_container/asynchronous/binding/when_configurator.rs @@ -5,38 +5,32 @@ use std::any::type_name; use std::marker::PhantomData; use std::sync::Arc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::async_di_container::AsyncBindingWhenConfiguratorError; /// When configurator for a binding for type `Interface` inside a [`IAsyncDIContainer`]. /// /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer -pub struct AsyncBindingWhenConfigurator +pub struct AsyncBindingWhenConfigurator where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { di_container: Arc, interface_phantom: PhantomData, - dependency_history_phantom: PhantomData, } -impl - AsyncBindingWhenConfigurator +impl AsyncBindingWhenConfigurator where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { pub(crate) fn new(di_container: Arc) -> Self { Self { di_container, interface_phantom: PhantomData, - dependency_history_phantom: PhantomData, } } @@ -99,8 +93,7 @@ mod tests let binding_when_configurator = AsyncBindingWhenConfigurator::< dyn subjects_async::INumber, - mocks::async_di_container::MockAsyncDIContainer, - mocks::MockDependencyHistory, + mocks::async_di_container::MockAsyncDIContainer, >::new(Arc::new(di_container_mock)); assert!(binding_when_configurator -- cgit v1.2.3-18-g5258