diff options
author | HampusM <hampus@hampusmat.com> | 2023-08-05 23:14:06 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-08-05 23:14:06 +0200 |
commit | 89c238f9c82ade2d7656e2bee76838a391609a88 (patch) | |
tree | 8771a893a9c83b06715c3af0fa2da3cd206b0716 /src/di_container/blocking/binding | |
parent | ddc666c55eec968f9a99408f3e3ad0f92d932179 (diff) |
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
Diffstat (limited to 'src/di_container/blocking/binding')
-rw-r--r-- | src/di_container/blocking/binding/builder.rs | 78 | ||||
-rw-r--r-- | src/di_container/blocking/binding/scope_configurator.rs | 79 | ||||
-rw-r--r-- | src/di_container/blocking/binding/when_configurator.rs | 17 |
3 files changed, 64 insertions, 110 deletions
diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs index 27151d7..991961c 100644 --- a/src/di_container/blocking/binding/builder.rs +++ b/src/di_container/blocking/binding/builder.rs @@ -5,40 +5,39 @@ use std::any::type_name; use std::marker::PhantomData; use std::rc::Rc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::binding::scope_configurator::BindingScopeConfigurator; #[cfg(feature = "factory")] use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator; use crate::di_container::blocking::IDIContainer; use crate::errors::di_container::BindingBuilderError; use crate::interfaces::injectable::Injectable; +use crate::util::use_dependency_history; + +use_dependency_history!(); /// Binding builder for type `Interface` inside a [`IDIContainer`]. /// /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer #[must_use = "No binding will be created if you don't use the binding builder"] -pub struct BindingBuilder<Interface, DIContainerType, DependencyHistoryType> +pub struct BindingBuilder<Interface, DIContainerType> where Interface: 'static + ?Sized, - DIContainerType: IDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { di_container: Rc<DIContainerType>, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData<Interface>, } -impl<Interface, DIContainerType, DependencyHistoryType> - BindingBuilder<Interface, DIContainerType, DependencyHistoryType> +impl<Interface, DIContainerType> BindingBuilder<Interface, DIContainerType> where Interface: 'static + ?Sized, - DIContainerType: IDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory + 'static, + DIContainerType: IDIContainer, { pub(crate) fn new( di_container: Rc<DIContainerType>, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, ) -> Self { Self { @@ -93,16 +92,11 @@ where pub fn to<Implementation>( self, ) -> Result< - BindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, - >, + BindingScopeConfigurator<Interface, Implementation, DIContainerType>, BindingBuilderError, > where - Implementation: Injectable<DIContainerType, DependencyHistoryType>, + Implementation: Injectable<DIContainerType>, { { if self.di_container.has_binding::<Interface>(None) { @@ -194,10 +188,7 @@ where pub fn to_factory<Args, Return, Func>( self, factory_func: &'static Func, - ) -> Result< - BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, - BindingBuilderError, - > + ) -> Result<BindingWhenConfigurator<Interface, DIContainerType>, BindingBuilderError> where Args: std::marker::Tuple + 'static, Return: 'static + ?Sized, @@ -283,10 +274,7 @@ where pub fn to_default_factory<Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, - ) -> Result< - BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, - BindingBuilderError, - > + ) -> Result<BindingWhenConfigurator<Interface, DIContainerType>, BindingBuilderError> where Return: 'static + ?Sized, FactoryFunc: Fn< @@ -326,6 +314,7 @@ mod tests use mockall::predicate::eq; use super::*; + use crate::dependency_history::MockDependencyHistory; use crate::test_utils::{mocks, subjects}; #[test] @@ -345,14 +334,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = BindingBuilder::< - dyn subjects::INumber, - mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>, - mocks::MockDependencyHistory, - >::new( - Rc::new(mock_di_container), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + BindingBuilder::< + dyn subjects::INumber, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(mock_di_container), MockDependencyHistory::new); binding_builder.to::<subjects::Number>()?; @@ -384,14 +370,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = BindingBuilder::< - IUserManagerFactory, - mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>, - mocks::MockDependencyHistory, - >::new( - Rc::new(mock_di_container), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + BindingBuilder::< + IUserManagerFactory, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(mock_di_container), MockDependencyHistory::new); binding_builder.to_factory(&|_| { Box::new(move |_num, _text| { @@ -430,14 +413,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = BindingBuilder::< - dyn subjects::IUserManager, - mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>, - mocks::MockDependencyHistory, - >::new( - Rc::new(mock_di_container), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + BindingBuilder::< + dyn subjects::IUserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(mock_di_container), MockDependencyHistory::new); binding_builder.to_default_factory(&|_| { Box::new(move || { diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs index f318dd6..0e2437f 100644 --- a/src/di_container/blocking/binding/scope_configurator.rs +++ b/src/di_container/blocking/binding/scope_configurator.rs @@ -4,51 +4,42 @@ use std::marker::PhantomData; use std::rc::Rc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator; use crate::di_container::blocking::IDIContainer; use crate::errors::di_container::BindingScopeConfiguratorError; use crate::interfaces::injectable::Injectable; use crate::provider::blocking::{SingletonProvider, TransientTypeProvider}; use crate::ptr::SingletonPtr; +use crate::util::use_dependency_history; + +use_dependency_history!(); /// Scope configurator for a binding for type `Interface` inside a [`IDIContainer`]. /// /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer -pub struct BindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, -> where +pub struct BindingScopeConfigurator<Interface, Implementation, DIContainerType> +where Interface: 'static + ?Sized, - Implementation: Injectable<DIContainerType, DependencyHistoryType>, - DIContainerType: IDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory, + Implementation: Injectable<DIContainerType>, + DIContainerType: IDIContainer, { di_container: Rc<DIContainerType>, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData<Interface>, implementation_phantom: PhantomData<Implementation>, } -impl<Interface, Implementation, DIContainerType, DependencyHistoryType> - BindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, - > +impl<Interface, Implementation, DIContainerType> + BindingScopeConfigurator<Interface, Implementation, DIContainerType> where Interface: 'static + ?Sized, - Implementation: Injectable<DIContainerType, DependencyHistoryType>, - DIContainerType: IDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory + 'static, + Implementation: Injectable<DIContainerType>, + DIContainerType: IDIContainer, { pub(crate) fn new( di_container: Rc<DIContainerType>, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, ) -> Self { Self { @@ -63,9 +54,8 @@ where /// /// This is the default. #[allow(clippy::must_use_candidate)] - pub fn in_transient_scope( - self, - ) -> BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType> + pub fn in_transient_scope(self) + -> BindingWhenConfigurator<Interface, DIContainerType> { self.set_in_transient_scope(); @@ -79,7 +69,7 @@ where pub fn in_singleton_scope( self, ) -> Result< - BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, + BindingWhenConfigurator<Interface, DIContainerType>, BindingScopeConfiguratorError, > { @@ -101,11 +91,7 @@ where { self.di_container.set_binding::<Interface>( None, - Box::new(TransientTypeProvider::< - Implementation, - DIContainerType, - DependencyHistoryType, - >::new()), + Box::new(TransientTypeProvider::<Implementation, DIContainerType>::new()), ); } } @@ -114,6 +100,7 @@ where mod tests { use super::*; + use crate::dependency_history::MockDependencyHistory; use crate::test_utils::{mocks, subjects}; #[test] @@ -127,15 +114,12 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_scope_configurator = BindingScopeConfigurator::< - dyn subjects::IUserManager, - subjects::UserManager, - mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>, - mocks::MockDependencyHistory, - >::new( - Rc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_scope_configurator = + BindingScopeConfigurator::< + dyn subjects::IUserManager, + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(di_container_mock), MockDependencyHistory::new); binding_scope_configurator.in_transient_scope(); } @@ -151,15 +135,12 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_scope_configurator = BindingScopeConfigurator::< - dyn subjects::IUserManager, - subjects::UserManager, - mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>, - mocks::MockDependencyHistory, - >::new( - Rc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_scope_configurator = + BindingScopeConfigurator::< + dyn subjects::IUserManager, + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(di_container_mock), MockDependencyHistory::new); assert!(binding_scope_configurator.in_singleton_scope().is_ok()); } diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs index 31b1b48..fcef377 100644 --- a/src/di_container/blocking/binding/when_configurator.rs +++ b/src/di_container/blocking/binding/when_configurator.rs @@ -5,38 +5,32 @@ use std::any::type_name; use std::marker::PhantomData; use std::rc::Rc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::IDIContainer; use crate::errors::di_container::BindingWhenConfiguratorError; /// When configurator for a binding for type `Interface` inside a [`IDIContainer`]. /// /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer -pub struct BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType> +pub struct BindingWhenConfigurator<Interface, DIContainerType> where Interface: 'static + ?Sized, - DIContainerType: IDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { di_container: Rc<DIContainerType>, interface_phantom: PhantomData<Interface>, - dependency_history_phantom: PhantomData<DependencyHistoryType>, } -impl<Interface, DIContainerType, DependencyHistoryType> - BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType> +impl<Interface, DIContainerType> BindingWhenConfigurator<Interface, DIContainerType> where Interface: 'static + ?Sized, - DIContainerType: IDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { pub(crate) fn new(di_container: Rc<DIContainerType>) -> Self { Self { di_container, interface_phantom: PhantomData, - dependency_history_phantom: PhantomData, } } @@ -97,8 +91,7 @@ mod tests let binding_when_configurator = BindingWhenConfigurator::< dyn subjects::INumber, - mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>, - mocks::MockDependencyHistory, + mocks::blocking_di_container::MockDIContainer, >::new(Rc::new(di_container_mock)); assert!(binding_when_configurator.when_named("cool").is_ok()); |