diff options
Diffstat (limited to 'src/di_container')
-rw-r--r-- | src/di_container/asynchronous/binding/builder.rs | 107 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/scope_configurator.rs | 78 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/when_configurator.rs | 17 | ||||
-rw-r--r-- | src/di_container/asynchronous/mod.rs | 44 | ||||
-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 | ||||
-rw-r--r-- | src/di_container/blocking/mod.rs | 43 |
8 files changed, 178 insertions, 285 deletions
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<Args, Return> = Box<(dyn Fn<Args, Output = Return> + 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<Interface, DIContainerType, DependencyHistoryType> +pub struct AsyncBindingBuilder<Interface, DIContainerType> where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { di_container: Arc<DIContainerType>, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData<Interface>, } -impl<Interface, DIContainerType, DependencyHistoryType> - AsyncBindingBuilder<Interface, DIContainerType, DependencyHistoryType> +impl<Interface, DIContainerType> AsyncBindingBuilder<Interface, DIContainerType> where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, + DIContainerType: IAsyncDIContainer, { pub(crate) fn new( di_container: Arc<DIContainerType>, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, ) -> Self { Self { @@ -99,16 +98,11 @@ where pub async fn to<Implementation>( self, ) -> Result< - AsyncBindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, - >, + AsyncBindingScopeConfigurator<Interface, Implementation, DIContainerType>, AsyncBindingBuilderError, > where - Implementation: AsyncInjectable<DIContainerType, DependencyHistoryType>, + Implementation: AsyncInjectable<DIContainerType>, { if self.di_container.has_binding::<Interface>(None).await { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< @@ -182,7 +176,7 @@ where self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, + AsyncBindingWhenConfigurator<Interface, DIContainerType>, AsyncBindingBuilderError, > where @@ -277,7 +271,7 @@ where self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, + AsyncBindingWhenConfigurator<Interface, DIContainerType>, AsyncBindingBuilderError, > where @@ -370,7 +364,7 @@ where self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, + AsyncBindingWhenConfigurator<Interface, DIContainerType>, AsyncBindingBuilderError, > where @@ -465,7 +459,7 @@ where self, factory_func: &'static FactoryFunc, ) -> Result< - AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, + AsyncBindingWhenConfigurator<Interface, DIContainerType>, 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<dyn Error>> { - 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::<dyn subjects_async::IUserManager>() @@ -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>, - 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::<subjects_async::UserManager>().await?; @@ -575,14 +566,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = AsyncBindingBuilder::< - IUserManagerFactory, - mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>, - 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>, - 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>, - 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>, - 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<Interface, Implementation, DIContainerType> +where Interface: 'static + ?Sized + Send + Sync, - Implementation: AsyncInjectable<DIContainerType, DependencyHistoryType>, - DIContainerType: IAsyncDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory + Send + Sync, + Implementation: AsyncInjectable<DIContainerType>, + DIContainerType: IAsyncDIContainer, { di_container: Arc<DIContainerType>, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData<Interface>, implementation_phantom: PhantomData<Implementation>, } -impl<Interface, Implementation, DIContainerType, DependencyHistoryType> - AsyncBindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, - > +impl<Interface, Implementation, DIContainerType> + AsyncBindingScopeConfigurator<Interface, Implementation, DIContainerType> where Interface: 'static + ?Sized + Send + Sync, - Implementation: AsyncInjectable<DIContainerType, DependencyHistoryType>, - DIContainerType: IAsyncDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, + Implementation: AsyncInjectable<DIContainerType>, + DIContainerType: IAsyncDIContainer, { pub(crate) fn new( di_container: Arc<DIContainerType>, - 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<Interface, DIContainerType, DependencyHistoryType> + ) -> AsyncBindingWhenConfigurator<Interface, DIContainerType> { self.set_in_transient_scope().await; @@ -78,7 +69,7 @@ where pub async fn in_singleton_scope( self, ) -> Result< - AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, + AsyncBindingWhenConfigurator<Interface, DIContainerType>, AsyncBindingScopeConfiguratorError, > { @@ -107,11 +98,9 @@ where self.di_container .set_binding::<Interface>( None, - Box::new(AsyncTransientTypeProvider::< - Implementation, - DIContainerType, - DependencyHistoryType, - >::new()), + Box::new( + AsyncTransientTypeProvider::<Implementation, DIContainerType>::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>, - 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>, - 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<Interface, DIContainerType, DependencyHistoryType> +pub struct AsyncBindingWhenConfigurator<Interface, DIContainerType> where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { di_container: Arc<DIContainerType>, interface_phantom: PhantomData<Interface>, - dependency_history_phantom: PhantomData<DependencyHistoryType>, } -impl<Interface, DIContainerType, DependencyHistoryType> - AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType> +impl<Interface, DIContainerType> AsyncBindingWhenConfigurator<Interface, DIContainerType> where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer<DependencyHistoryType>, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { pub(crate) fn new(di_container: Arc<DIContainerType>) -> 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::MockDependencyHistory, + mocks::async_di_container::MockAsyncDIContainer, >::new(Arc::new(di_container_mock)); assert!(binding_when_configurator diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index b603e88..927f549 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -57,7 +57,6 @@ use std::sync::Arc; use async_lock::Mutex; use async_trait::async_trait; -use crate::dependency_history::{DependencyHistory, IDependencyHistory}; use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder; use crate::di_container::binding_storage::DIContainerBindingStorage; use crate::errors::async_di_container::AsyncDIContainerError; @@ -67,6 +66,9 @@ use crate::private::cast::boxed::CastBox; use crate::private::cast::error::CastError; use crate::provider::r#async::{AsyncProvidable, IAsyncProvider}; use crate::ptr::{SomePtr, TransientPtr}; +use crate::util::use_dependency_history; + +use_dependency_history!(); pub mod binding; pub mod prelude; @@ -75,15 +77,11 @@ pub mod prelude; /// /// **This trait is sealed and cannot be implemented for types outside this crate.** #[async_trait] -pub trait IAsyncDIContainer<DependencyHistoryType>: - Sized + 'static + Send + Sync + details::DIContainerInternals<DependencyHistoryType> -where - DependencyHistoryType: IDependencyHistory + Send + Sync, +pub trait IAsyncDIContainer: + Sized + 'static + Send + Sync + details::DIContainerInternals { /// Returns a new [`AsyncBindingBuilder`] for the given interface. - fn bind<Interface>( - self: &mut Arc<Self>, - ) -> AsyncBindingBuilder<Interface, Self, DependencyHistoryType> + fn bind<Interface>(self: &mut Arc<Self>) -> AsyncBindingBuilder<Interface, Self> where Interface: 'static + ?Sized + Send + Sync; @@ -121,7 +119,7 @@ where #[doc(hidden)] async fn get_bound<Interface>( self: &Arc<Self>, - dependency_history: DependencyHistoryType, + dependency_history: DependencyHistory, name: Option<&'static str>, ) -> Result<SomePtr<Interface>, AsyncDIContainerError> where @@ -131,8 +129,7 @@ where /// Async dependency injection container. pub struct AsyncDIContainer { - binding_storage: - Mutex<DIContainerBindingStorage<dyn IAsyncProvider<Self, DependencyHistory>>>, + binding_storage: Mutex<DIContainerBindingStorage<dyn IAsyncProvider<Self>>>, } impl AsyncDIContainer @@ -148,11 +145,9 @@ impl AsyncDIContainer } #[async_trait] -impl IAsyncDIContainer<DependencyHistory> for AsyncDIContainer +impl IAsyncDIContainer for AsyncDIContainer { - fn bind<Interface>( - self: &mut Arc<Self>, - ) -> AsyncBindingBuilder<Interface, Self, DependencyHistory> + fn bind<Interface>(self: &mut Arc<Self>) -> AsyncBindingBuilder<Interface, Self> where Interface: 'static + ?Sized + Send + Sync, { @@ -205,7 +200,7 @@ impl IAsyncDIContainer<DependencyHistory> for AsyncDIContainer } #[async_trait] -impl details::DIContainerInternals<DependencyHistory> for AsyncDIContainer +impl details::DIContainerInternals for AsyncDIContainer { async fn has_binding<Interface>(self: &Arc<Self>, name: Option<&'static str>) -> bool where @@ -217,7 +212,7 @@ impl details::DIContainerInternals<DependencyHistory> for AsyncDIContainer async fn set_binding<Interface>( self: &Arc<Self>, name: Option<&'static str>, - provider: Box<dyn IAsyncProvider<Self, DependencyHistory>>, + provider: Box<dyn IAsyncProvider<Self>>, ) where Interface: 'static + ?Sized, { @@ -230,7 +225,7 @@ impl details::DIContainerInternals<DependencyHistory> for AsyncDIContainer async fn remove_binding<Interface>( self: &Arc<Self>, name: Option<&'static str>, - ) -> Option<Box<dyn IAsyncProvider<Self, DependencyHistory>>> + ) -> Option<Box<dyn IAsyncProvider<Self>>> where Interface: 'static + ?Sized, { @@ -242,7 +237,7 @@ impl AsyncDIContainer { async fn handle_binding_providable<Interface>( self: &Arc<Self>, - binding_providable: AsyncProvidable<Self, DependencyHistory>, + binding_providable: AsyncProvidable<Self>, ) -> Result<SomePtr<Interface>, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync, @@ -369,7 +364,7 @@ impl AsyncDIContainer self: &Arc<Self>, name: Option<&'static str>, dependency_history: DependencyHistory, - ) -> Result<AsyncProvidable<Self, DependencyHistory>, AsyncDIContainerError> + ) -> Result<AsyncProvidable<Self>, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync, { @@ -408,13 +403,10 @@ pub(crate) mod details use async_trait::async_trait; - use crate::dependency_history::IDependencyHistory; use crate::provider::r#async::IAsyncProvider; #[async_trait] - pub trait DIContainerInternals<DependencyHistoryType> - where - DependencyHistoryType: IDependencyHistory, + pub trait DIContainerInternals { async fn has_binding<Interface>( self: &Arc<Self>, @@ -426,14 +418,14 @@ pub(crate) mod details async fn set_binding<Interface>( self: &Arc<Self>, name: Option<&'static str>, - provider: Box<dyn IAsyncProvider<Self, DependencyHistoryType>>, + provider: Box<dyn IAsyncProvider<Self>>, ) where Interface: 'static + ?Sized; async fn remove_binding<Interface>( self: &Arc<Self>, name: Option<&'static str>, - ) -> Option<Box<dyn IAsyncProvider<Self, DependencyHistoryType>>> + ) -> Option<Box<dyn IAsyncProvider<Self>>> where Interface: 'static + ?Sized; } 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()); diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index cd7065c..5781583 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -54,7 +54,6 @@ use std::any::type_name; use std::cell::RefCell; use std::rc::Rc; -use crate::dependency_history::{DependencyHistory, IDependencyHistory}; use crate::di_container::binding_storage::DIContainerBindingStorage; use crate::di_container::blocking::binding::builder::BindingBuilder; use crate::errors::di_container::DIContainerError; @@ -62,6 +61,9 @@ use crate::private::cast::boxed::CastBox; use crate::private::cast::rc::CastRc; use crate::provider::blocking::{IProvider, Providable}; use crate::ptr::SomePtr; +use crate::util::use_dependency_history; + +use_dependency_history!(); pub mod binding; pub mod prelude; @@ -69,15 +71,10 @@ pub mod prelude; /// Blocking dependency injection container interface. /// /// **This trait is sealed and cannot be implemented for types outside this crate.** -pub trait IDIContainer<DependencyHistoryType>: - Sized + 'static + details::DIContainerInternals<DependencyHistoryType> -where - DependencyHistoryType: IDependencyHistory, +pub trait IDIContainer: Sized + 'static + details::DIContainerInternals { /// Returns a new [`BindingBuilder`] for the given interface. - fn bind<Interface>( - self: &mut Rc<Self>, - ) -> BindingBuilder<Interface, Self, DependencyHistoryType> + fn bind<Interface>(self: &mut Rc<Self>) -> BindingBuilder<Interface, Self> where Interface: 'static + ?Sized; @@ -109,7 +106,7 @@ where #[doc(hidden)] fn get_bound<Interface>( self: &Rc<Self>, - dependency_history: DependencyHistoryType, + dependency_history: DependencyHistory, name: Option<&'static str>, ) -> Result<SomePtr<Interface>, DIContainerError> where @@ -119,8 +116,7 @@ where /// Blocking dependency injection container. pub struct DIContainer { - binding_storage: - RefCell<DIContainerBindingStorage<dyn IProvider<Self, DependencyHistory>>>, + binding_storage: RefCell<DIContainerBindingStorage<dyn IProvider<Self>>>, } impl DIContainer @@ -135,11 +131,9 @@ impl DIContainer } } -impl IDIContainer<DependencyHistory> for DIContainer +impl IDIContainer for DIContainer { - fn bind<Interface>( - self: &mut Rc<Self>, - ) -> BindingBuilder<Interface, Self, DependencyHistory> + fn bind<Interface>(self: &mut Rc<Self>) -> BindingBuilder<Interface, Self> where Interface: 'static + ?Sized, { @@ -183,7 +177,7 @@ impl IDIContainer<DependencyHistory> for DIContainer } } -impl details::DIContainerInternals<DependencyHistory> for DIContainer +impl details::DIContainerInternals for DIContainer { fn has_binding<Interface>(self: &Rc<Self>, name: Option<&'static str>) -> bool where @@ -195,7 +189,7 @@ impl details::DIContainerInternals<DependencyHistory> for DIContainer fn set_binding<Interface>( self: &Rc<Self>, name: Option<&'static str>, - provider: Box<dyn IProvider<Self, DependencyHistory>>, + provider: Box<dyn IProvider<Self>>, ) where Interface: 'static + ?Sized, { @@ -207,7 +201,7 @@ impl details::DIContainerInternals<DependencyHistory> for DIContainer fn remove_binding<Interface>( self: &Rc<Self>, name: Option<&'static str>, - ) -> Option<Box<dyn IProvider<Self, DependencyHistory>>> + ) -> Option<Box<dyn IProvider<Self>>> where Interface: 'static + ?Sized, { @@ -219,7 +213,7 @@ impl DIContainer { fn handle_binding_providable<Interface>( #[cfg(feature = "factory")] self: &Rc<Self>, - binding_providable: Providable<Self, DependencyHistory>, + binding_providable: Providable<Self>, ) -> Result<SomePtr<Interface>, DIContainerError> where Interface: 'static + ?Sized, @@ -278,7 +272,7 @@ impl DIContainer self: &Rc<Self>, name: Option<&'static str>, dependency_history: DependencyHistory, - ) -> Result<Providable<Self, DependencyHistory>, DIContainerError> + ) -> Result<Providable<Self>, DIContainerError> where Interface: 'static + ?Sized, { @@ -306,12 +300,9 @@ pub(crate) mod details { use std::rc::Rc; - use crate::dependency_history::IDependencyHistory; use crate::provider::blocking::IProvider; - pub trait DIContainerInternals<DependencyHistoryType> - where - DependencyHistoryType: IDependencyHistory, + pub trait DIContainerInternals { fn has_binding<Interface>(self: &Rc<Self>, name: Option<&'static str>) -> bool where @@ -320,14 +311,14 @@ pub(crate) mod details fn set_binding<Interface>( self: &Rc<Self>, name: Option<&'static str>, - provider: Box<dyn IProvider<Self, DependencyHistoryType>>, + provider: Box<dyn IProvider<Self>>, ) where Interface: 'static + ?Sized; fn remove_binding<Interface>( self: &Rc<Self>, name: Option<&'static str>, - ) -> Option<Box<dyn IProvider<Self, DependencyHistoryType>>> + ) -> Option<Box<dyn IProvider<Self>>> where Interface: 'static + ?Sized; } |