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/dependency_history.rs | 20 +-- src/di_container/asynchronous/binding/builder.rs | 107 ++++++-------- .../asynchronous/binding/scope_configurator.rs | 78 +++++------ .../asynchronous/binding/when_configurator.rs | 17 +-- src/di_container/asynchronous/mod.rs | 44 +++--- src/di_container/blocking/binding/builder.rs | 78 ++++------- .../blocking/binding/scope_configurator.rs | 79 ++++------- .../blocking/binding/when_configurator.rs | 17 +-- src/di_container/blocking/mod.rs | 43 +++--- src/interfaces/async_injectable.rs | 17 ++- src/interfaces/injectable.rs | 17 ++- src/lib.rs | 1 + src/provider/async.rs | 153 ++++++++------------ src/provider/blocking.rs | 124 +++++++---------- src/test_utils.rs | 155 ++++++++------------- src/util.rs | 15 ++ 16 files changed, 386 insertions(+), 579 deletions(-) create mode 100644 src/util.rs (limited to 'src') diff --git a/src/dependency_history.rs b/src/dependency_history.rs index 4e36a7b..cd4e77d 100644 --- a/src/dependency_history.rs +++ b/src/dependency_history.rs @@ -7,18 +7,6 @@ use std::fmt::{Debug, Display}; const BOLD_MODE: &str = "\x1b[1m"; const RESET_BOLD_MODE: &str = "\x1b[22m"; -/// Dependency history interface. -/// -/// **This trait is sealed and cannot be implemented for types outside this crate.** -pub trait IDependencyHistory: private::Sealed -{ - #[doc(hidden)] - fn push(&mut self); - - #[doc(hidden)] - fn contains(&self) -> bool; -} - /// Dependency history. #[derive(Clone, Debug)] pub struct DependencyHistory @@ -35,16 +23,18 @@ impl DependencyHistory } } -impl IDependencyHistory for DependencyHistory +#[cfg_attr(test, mockall::automock)] +impl DependencyHistory { #[doc(hidden)] - fn push(&mut self) + pub fn push(&mut self) { self.inner.push(type_name::()); } #[doc(hidden)] - fn contains(&self) -> bool + #[allow(clippy::must_use_candidate)] + pub fn contains(&self) -> bool { self.inner.contains(&type_name::()) } 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 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: - Sized + 'static + Send + Sync + details::DIContainerInternals -where - DependencyHistoryType: IDependencyHistory + Send + Sync, +pub trait IAsyncDIContainer: + Sized + 'static + Send + Sync + details::DIContainerInternals { /// Returns a new [`AsyncBindingBuilder`] for the given interface. - fn bind( - self: &mut Arc, - ) -> AsyncBindingBuilder + fn bind(self: &mut Arc) -> AsyncBindingBuilder where Interface: 'static + ?Sized + Send + Sync; @@ -121,7 +119,7 @@ where #[doc(hidden)] async fn get_bound( self: &Arc, - dependency_history: DependencyHistoryType, + dependency_history: DependencyHistory, name: Option<&'static str>, ) -> Result, AsyncDIContainerError> where @@ -131,8 +129,7 @@ where /// Async dependency injection container. pub struct AsyncDIContainer { - binding_storage: - Mutex>>, + binding_storage: Mutex>>, } impl AsyncDIContainer @@ -148,11 +145,9 @@ impl AsyncDIContainer } #[async_trait] -impl IAsyncDIContainer for AsyncDIContainer +impl IAsyncDIContainer for AsyncDIContainer { - fn bind( - self: &mut Arc, - ) -> AsyncBindingBuilder + fn bind(self: &mut Arc) -> AsyncBindingBuilder where Interface: 'static + ?Sized + Send + Sync, { @@ -205,7 +200,7 @@ impl IAsyncDIContainer for AsyncDIContainer } #[async_trait] -impl details::DIContainerInternals for AsyncDIContainer +impl details::DIContainerInternals for AsyncDIContainer { async fn has_binding(self: &Arc, name: Option<&'static str>) -> bool where @@ -217,7 +212,7 @@ impl details::DIContainerInternals for AsyncDIContainer async fn set_binding( self: &Arc, name: Option<&'static str>, - provider: Box>, + provider: Box>, ) where Interface: 'static + ?Sized, { @@ -230,7 +225,7 @@ impl details::DIContainerInternals for AsyncDIContainer async fn remove_binding( self: &Arc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> where Interface: 'static + ?Sized, { @@ -242,7 +237,7 @@ impl AsyncDIContainer { async fn handle_binding_providable( self: &Arc, - binding_providable: AsyncProvidable, + binding_providable: AsyncProvidable, ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync, @@ -369,7 +364,7 @@ impl AsyncDIContainer self: &Arc, name: Option<&'static str>, dependency_history: DependencyHistory, - ) -> Result, AsyncDIContainerError> + ) -> Result, 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 - where - DependencyHistoryType: IDependencyHistory, + pub trait DIContainerInternals { async fn has_binding( self: &Arc, @@ -426,14 +418,14 @@ pub(crate) mod details async fn set_binding( self: &Arc, name: Option<&'static str>, - provider: Box>, + provider: Box>, ) where Interface: 'static + ?Sized; async fn remove_binding( self: &Arc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> 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 +pub struct BindingBuilder where Interface: 'static + ?Sized, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { di_container: Rc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, } -impl - BindingBuilder +impl BindingBuilder where Interface: 'static + ?Sized, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory + 'static, + DIContainerType: IDIContainer, { pub(crate) fn new( di_container: Rc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, ) -> Self { Self { @@ -93,16 +92,11 @@ where pub fn to( self, ) -> Result< - BindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, - >, + BindingScopeConfigurator, BindingBuilderError, > where - Implementation: Injectable, + Implementation: Injectable, { { if self.di_container.has_binding::(None) { @@ -194,10 +188,7 @@ where pub fn to_factory( self, factory_func: &'static Func, - ) -> Result< - BindingWhenConfigurator, - BindingBuilderError, - > + ) -> Result, BindingBuilderError> where Args: std::marker::Tuple + 'static, Return: 'static + ?Sized, @@ -283,10 +274,7 @@ where pub fn to_default_factory( self, factory_func: &'static FactoryFunc, - ) -> Result< - BindingWhenConfigurator, - BindingBuilderError, - > + ) -> Result, 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, - >::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::()?; @@ -384,14 +370,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = BindingBuilder::< - IUserManagerFactory, - mocks::blocking_di_container::MockDIContainer, - 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, - >::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 +where Interface: 'static + ?Sized, - Implementation: Injectable, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + Implementation: Injectable, + DIContainerType: IDIContainer, { di_container: Rc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, implementation_phantom: PhantomData, } -impl - BindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, - > +impl + BindingScopeConfigurator where Interface: 'static + ?Sized, - Implementation: Injectable, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory + 'static, + Implementation: Injectable, + DIContainerType: IDIContainer, { pub(crate) fn new( di_container: Rc, - 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 + pub fn in_transient_scope(self) + -> BindingWhenConfigurator { self.set_in_transient_scope(); @@ -79,7 +69,7 @@ where pub fn in_singleton_scope( self, ) -> Result< - BindingWhenConfigurator, + BindingWhenConfigurator, BindingScopeConfiguratorError, > { @@ -101,11 +91,7 @@ where { self.di_container.set_binding::( None, - Box::new(TransientTypeProvider::< - Implementation, - DIContainerType, - DependencyHistoryType, - >::new()), + Box::new(TransientTypeProvider::::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, - >::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, - >::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 +pub struct BindingWhenConfigurator where Interface: 'static + ?Sized, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { di_container: Rc, interface_phantom: PhantomData, - dependency_history_phantom: PhantomData, } -impl - BindingWhenConfigurator +impl BindingWhenConfigurator where Interface: 'static + ?Sized, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { pub(crate) fn new(di_container: Rc) -> 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::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: - Sized + 'static + details::DIContainerInternals -where - DependencyHistoryType: IDependencyHistory, +pub trait IDIContainer: Sized + 'static + details::DIContainerInternals { /// Returns a new [`BindingBuilder`] for the given interface. - fn bind( - self: &mut Rc, - ) -> BindingBuilder + fn bind(self: &mut Rc) -> BindingBuilder where Interface: 'static + ?Sized; @@ -109,7 +106,7 @@ where #[doc(hidden)] fn get_bound( self: &Rc, - dependency_history: DependencyHistoryType, + dependency_history: DependencyHistory, name: Option<&'static str>, ) -> Result, DIContainerError> where @@ -119,8 +116,7 @@ where /// Blocking dependency injection container. pub struct DIContainer { - binding_storage: - RefCell>>, + binding_storage: RefCell>>, } impl DIContainer @@ -135,11 +131,9 @@ impl DIContainer } } -impl IDIContainer for DIContainer +impl IDIContainer for DIContainer { - fn bind( - self: &mut Rc, - ) -> BindingBuilder + fn bind(self: &mut Rc) -> BindingBuilder where Interface: 'static + ?Sized, { @@ -183,7 +177,7 @@ impl IDIContainer for DIContainer } } -impl details::DIContainerInternals for DIContainer +impl details::DIContainerInternals for DIContainer { fn has_binding(self: &Rc, name: Option<&'static str>) -> bool where @@ -195,7 +189,7 @@ impl details::DIContainerInternals for DIContainer fn set_binding( self: &Rc, name: Option<&'static str>, - provider: Box>, + provider: Box>, ) where Interface: 'static + ?Sized, { @@ -207,7 +201,7 @@ impl details::DIContainerInternals for DIContainer fn remove_binding( self: &Rc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> where Interface: 'static + ?Sized, { @@ -219,7 +213,7 @@ impl DIContainer { fn handle_binding_providable( #[cfg(feature = "factory")] self: &Rc, - binding_providable: Providable, + binding_providable: Providable, ) -> Result, DIContainerError> where Interface: 'static + ?Sized, @@ -278,7 +272,7 @@ impl DIContainer self: &Rc, name: Option<&'static str>, dependency_history: DependencyHistory, - ) -> Result, DIContainerError> + ) -> Result, 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 - where - DependencyHistoryType: IDependencyHistory, + pub trait DIContainerInternals { fn has_binding(self: &Rc, name: Option<&'static str>) -> bool where @@ -320,14 +311,14 @@ pub(crate) mod details fn set_binding( self: &Rc, name: Option<&'static str>, - provider: Box>, + provider: Box>, ) where Interface: 'static + ?Sized; fn remove_binding( self: &Rc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> where Interface: 'static + ?Sized; } diff --git a/src/interfaces/async_injectable.rs b/src/interfaces/async_injectable.rs index d8e7dfc..2364ae1 100644 --- a/src/interfaces/async_injectable.rs +++ b/src/interfaces/async_injectable.rs @@ -2,18 +2,19 @@ use std::fmt::Debug; use std::sync::Arc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::injectable::InjectableError; use crate::future::BoxFuture; use crate::private::cast::CastFromArc; use crate::ptr::TransientPtr; +use crate::util::use_dependency_history; + +use_dependency_history!(); /// Interface for structs that can be injected into or be injected to. -pub trait AsyncInjectable: CastFromArc +pub trait AsyncInjectable: CastFromArc where - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { /// Resolves the dependencies of the injectable. /// @@ -21,18 +22,16 @@ where /// Will return `Err` if resolving the dependencies fails. fn resolve<'di_container, 'fut>( di_container: &'di_container Arc, - dependency_history: DependencyHistoryType, + dependency_history: DependencyHistory, ) -> BoxFuture<'fut, Result, InjectableError>> where Self: Sized + 'fut, 'di_container: 'fut; } -impl Debug - for dyn AsyncInjectable +impl Debug for dyn AsyncInjectable where - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync, + DIContainerType: IAsyncDIContainer, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/src/interfaces/injectable.rs b/src/interfaces/injectable.rs index b70e13f..82b773a 100644 --- a/src/interfaces/injectable.rs +++ b/src/interfaces/injectable.rs @@ -2,17 +2,18 @@ use std::fmt::Debug; use std::rc::Rc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::IDIContainer; use crate::errors::injectable::InjectableError; use crate::private::cast::CastFrom; use crate::ptr::TransientPtr; +use crate::util::use_dependency_history; + +use_dependency_history!(); /// Interface for structs that can be injected into or be injected to. -pub trait Injectable: CastFrom +pub trait Injectable: CastFrom where - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { /// Resolves the dependencies of the injectable. /// @@ -20,17 +21,15 @@ where /// Will return `Err` if resolving the dependencies fails. fn resolve( di_container: &Rc, - dependency_history: DependencyHistoryType, + dependency_history: DependencyHistory, ) -> Result, InjectableError> where Self: Sized; } -impl Debug - for dyn Injectable +impl Debug for dyn Injectable where - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/src/lib.rs b/src/lib.rs index 4807f61..40bf6e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,6 +112,7 @@ pub use syrette_macros::{declare_interface, injectable, named}; pub mod private; mod provider; +mod util; #[cfg(test)] #[cfg(not(tarpaulin_include))] 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" diff --git a/src/test_utils.rs b/src/test_utils.rs index 78ad63b..2bc1671 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -7,12 +7,13 @@ pub mod subjects use syrette_macros::declare_interface; - use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::IDIContainer; use crate::interfaces::injectable::Injectable; use crate::private::cast::CastFromArc; use crate::ptr::TransientPtr; + use_dependency_history!(); + pub trait IUserManager { fn add_user(&self, user_id: i128); @@ -44,18 +45,17 @@ pub mod subjects } use crate as syrette; + use crate::util::use_dependency_history; declare_interface!(UserManager -> IUserManager); - impl - Injectable for UserManager + impl Injectable for UserManager where - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { fn resolve( _di_container: &Rc, - _dependency_history: DependencyHistoryType, + _dependency_history: DependencyHistory, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, @@ -115,15 +115,13 @@ pub mod subjects declare_interface!(Number -> INumber); - impl - Injectable for Number + impl Injectable for Number where - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { fn resolve( _di_container: &Rc, - _dependency_history: DependencyHistoryType, + _dependency_history: DependencyHistory, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, @@ -151,11 +149,12 @@ pub mod subjects_async use async_trait::async_trait; use syrette_macros::declare_interface; - use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::interfaces::async_injectable::AsyncInjectable; use crate::ptr::TransientPtr; + use_dependency_history!(); + pub trait IUserManager: Send + Sync { fn add_user(&self, user_id: i128); @@ -187,19 +186,18 @@ pub mod subjects_async } use crate as syrette; + use crate::util::use_dependency_history; declare_interface!(UserManager -> IUserManager); #[async_trait] - impl - AsyncInjectable for UserManager + impl AsyncInjectable for UserManager where - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, + DIContainerType: IAsyncDIContainer, { async fn resolve( _: &Arc, - _dependency_history: DependencyHistoryType, + _dependency_history: DependencyHistory, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, @@ -260,15 +258,13 @@ pub mod subjects_async declare_interface!(Number -> INumber, threadsafe_sharable = true); #[async_trait] - impl - AsyncInjectable for Number + impl AsyncInjectable for Number where - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, + DIContainerType: IAsyncDIContainer, { async fn resolve( _: &Arc, - _dependency_history: DependencyHistoryType, + _dependency_history: DependencyHistory, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, @@ -290,27 +286,22 @@ pub mod mocks use std::rc::Rc; use super::*; - use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::binding::builder::BindingBuilder; use crate::di_container::blocking::details::DIContainerInternals; use crate::di_container::blocking::IDIContainer; use crate::errors::di_container::DIContainerError; use crate::provider::blocking::IProvider; use crate::ptr::SomePtr; + use crate::util::use_dependency_history; + + use_dependency_history!(); mock! { - pub DIContainer - where - DependencyHistoryType: IDependencyHistory + 'static - {} + pub DIContainer {} - impl IDIContainer for - DIContainer - where - DependencyHistoryType: IDependencyHistory + 'static + impl IDIContainer for DIContainer { - fn bind(self: &mut Rc) -> - BindingBuilder + fn bind(self: &mut Rc) -> BindingBuilder where Interface: 'static + ?Sized; @@ -328,18 +319,14 @@ pub mod mocks #[doc(hidden)] fn get_bound( self: &Rc, - dependency_history: DependencyHistoryType, + dependency_history: DependencyHistory, name: Option<&'static str>, ) -> Result, DIContainerError> where Interface: 'static + ?Sized; } - impl DIContainerInternals< - DependencyHistoryType - > for DIContainer - where - DependencyHistoryType: IDependencyHistory + impl DIContainerInternals for DIContainer { fn has_binding(self: &Rc, name: Option<&'static str>) -> bool where @@ -349,14 +336,14 @@ pub mod mocks fn set_binding( self: &Rc, name: Option<&'static str>, - provider: Box>, + provider: Box>, ) where Interface: 'static + ?Sized; fn remove_binding( self: &Rc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> where Interface: 'static + ?Sized; } @@ -369,29 +356,24 @@ pub mod mocks use std::sync::Arc; use super::*; - use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder; use crate::di_container::asynchronous::details::DIContainerInternals; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::async_di_container::AsyncDIContainerError; use crate::provider::r#async::IAsyncProvider; use crate::ptr::SomePtr; + use crate::util::use_dependency_history; + + use_dependency_history!(); mock! { - pub AsyncDIContainer - where - DependencyHistoryType: IDependencyHistory + 'static + Send + Sync - {} + pub AsyncDIContainer {} #[async_trait::async_trait] - impl IAsyncDIContainer< - DependencyHistoryType - > for AsyncDIContainer - where - DependencyHistoryType: IDependencyHistory + 'static + Send + Sync + impl IAsyncDIContainer for AsyncDIContainer { fn bind(self: &mut Arc) -> - AsyncBindingBuilder + AsyncBindingBuilder where Interface: 'static + ?Sized + Send + Sync; @@ -411,7 +393,7 @@ pub mod mocks #[doc(hidden)] async fn get_bound( self: &Arc, - dependency_history: DependencyHistoryType, + dependency_history: DependencyHistory, name: Option<&'static str>, ) -> Result, AsyncDIContainerError> where @@ -419,11 +401,7 @@ pub mod mocks } #[async_trait::async_trait] - impl DIContainerInternals< - DependencyHistoryType - > for AsyncDIContainer - where - DependencyHistoryType: IDependencyHistory + 'static + Send + Sync + impl DIContainerInternals for AsyncDIContainer { async fn has_binding( self: &Arc, @@ -435,14 +413,14 @@ pub mod mocks async fn set_binding( self: &Arc, name: Option<&'static str>, - provider: Box>, + provider: Box>, ) where Interface: 'static + ?Sized; async fn remove_binding( self: &Arc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> where Interface: 'static + ?Sized; } @@ -457,34 +435,31 @@ pub mod mocks use async_trait::async_trait; use super::*; - use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::injectable::InjectableError; use crate::provider::r#async::{AsyncProvidable, IAsyncProvider}; + use crate::util::use_dependency_history; + + use_dependency_history!(); mock! { - pub AsyncProvider< - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync - > {} + pub AsyncProvider {} #[async_trait] impl< - DIContainerType: IAsyncDIContainer, - DependencyHistoryType: IDependencyHistory + Send + Sync - > IAsyncProvider for AsyncProvider< + DIContainerType: IAsyncDIContainer, + > IAsyncProvider for AsyncProvider< DIContainerType, - DependencyHistoryType > { async fn provide( &self, di_container: &Arc, - dependency_history: DependencyHistoryType - ) -> Result, InjectableError>; + dependency_history: DependencyHistory + ) -> Result, InjectableError>; fn do_clone(&self) -> - Box>; + Box>; } } } @@ -494,47 +469,31 @@ pub mod mocks use std::rc::Rc; use super::*; - use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::IDIContainer; use crate::errors::injectable::InjectableError; use crate::provider::blocking::{IProvider, Providable}; + use crate::util::use_dependency_history; + + use_dependency_history!(); mock! { - pub Provider + pub Provider where - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer {} - impl IProvider< - DIContainerType, - DependencyHistoryType - > for Provider - where - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory + impl IProvider for Provider + where + DIContainerType: IDIContainer, { fn provide( &self, di_container: &Rc, - dependency_history: DependencyHistoryType, - ) -> Result, InjectableError>; + dependency_history: DependencyHistory, + ) -> Result, InjectableError>; } } } - - mock! { - pub DependencyHistory {} - - impl crate::dependency_history::IDependencyHistory for DependencyHistory - { - fn push(&mut self); - - fn contains(&self) -> bool; - } - - impl crate::dependency_history::private::Sealed for DependencyHistory {} - } } #[cfg(feature = "async")] diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..2d2d911 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,15 @@ +#[cfg(not(test))] +macro_rules! use_dependency_history { + () => { + use $crate::dependency_history::DependencyHistory; + }; +} + +#[cfg(test)] +macro_rules! use_dependency_history { + () => { + use $crate::dependency_history::MockDependencyHistory as DependencyHistory; + }; +} + +pub(crate) use use_dependency_history; -- cgit v1.2.3-18-g5258