diff options
-rw-r--r-- | examples/async/main.rs | 1 | ||||
-rw-r--r-- | macros/src/injectable/implementation.rs | 1 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/builder.rs | 142 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/scope_configurator.rs | 47 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/when_configurator.rs | 29 | ||||
-rw-r--r-- | src/di_container/asynchronous/mod.rs | 280 | ||||
-rw-r--r-- | src/di_container/asynchronous/prelude.rs | 2 | ||||
-rw-r--r-- | src/interfaces/async_injectable.rs | 5 | ||||
-rw-r--r-- | src/provider/async.rs | 123 | ||||
-rw-r--r-- | src/test_utils.rs | 102 |
10 files changed, 283 insertions, 449 deletions
diff --git a/examples/async/main.rs b/examples/async/main.rs index d051b94..cf946e4 100644 --- a/examples/async/main.rs +++ b/examples/async/main.rs @@ -8,7 +8,6 @@ mod food; mod interfaces; use anyhow::Result; -use syrette::di_container::asynchronous::prelude::*; use tokio::spawn; use crate::bootstrap::bootstrap; diff --git a/macros/src/injectable/implementation.rs b/macros/src/injectable/implementation.rs index 1efd1fa..935a8ae 100644 --- a/macros/src/injectable/implementation.rs +++ b/macros/src/injectable/implementation.rs @@ -250,7 +250,6 @@ impl<Dep: IDependency> InjectableImpl<Dep> Box::pin(async move { use std::any::type_name; - use syrette::di_container::asynchronous::IAsyncDIContainer; use syrette::errors::injectable::InjectableError; let self_type_name = type_name::<#self_type>(); diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index 83a1efb..7050847 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -1,6 +1,4 @@ -//! Binding builder for types inside of a [`IAsyncDIContainer`]. -//! -//! [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer +//! Binding builder for types inside of a [`AsyncDIContainer`]. use std::any::type_name; use std::marker::PhantomData; use std::sync::Arc; @@ -8,41 +6,37 @@ use std::sync::Arc; 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::di_container::BindingOptions; use crate::errors::async_di_container::AsyncBindingBuilderError; use crate::interfaces::async_injectable::AsyncInjectable; use crate::util::use_double; use_double!(crate::dependency_history::DependencyHistory); +use_double!(crate::di_container::asynchronous::AsyncDIContainer); /// Alias for a threadsafe boxed function. #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] pub type BoxFn<Args, Return> = Box<(dyn Fn<Args, Output = Return> + Send + Sync)>; -/// Binding builder for type `Interface` inside a [`IAsyncDIContainer`]. -/// -/// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer +/// Binding builder for type `Interface` inside a [`AsyncDIContainer`]. #[must_use = "No binding will be created if you don't use the binding builder"] -pub struct AsyncBindingBuilder<Interface, DIContainerType> +pub struct AsyncBindingBuilder<Interface> where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, { - di_container: Arc<DIContainerType>, + di_container: Arc<AsyncDIContainer>, dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData<Interface>, } -impl<Interface, DIContainerType> AsyncBindingBuilder<Interface, DIContainerType> +impl<Interface> AsyncBindingBuilder<Interface> where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, { pub(crate) fn new( - di_container: Arc<DIContainerType>, + di_container: Arc<AsyncDIContainer>, dependency_history_factory: fn() -> DependencyHistory, ) -> Self { @@ -54,13 +48,13 @@ where } /// Creates a binding of type `Interface` to type `Implementation` inside of the - /// associated [`IAsyncDIContainer`]. + /// associated [`AsyncDIContainer`]. /// /// The scope of the binding is transient. But that can be changed by using the /// returned [`AsyncBindingScopeConfigurator`] /// /// # Errors - /// Will return Err if the associated [`IAsyncDIContainer`] already have a binding for + /// Will return Err if the associated [`AsyncDIContainer`] already have a binding for /// the interface. /// /// # Examples @@ -94,16 +88,14 @@ where /// # Ok(()) /// # } /// ``` - /// - /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer pub async fn to<Implementation>( self, ) -> Result< - AsyncBindingScopeConfigurator<Interface, Implementation, DIContainerType>, + AsyncBindingScopeConfigurator<Interface, Implementation>, AsyncBindingBuilderError, > where - Implementation: AsyncInjectable<DIContainerType>, + Implementation: AsyncInjectable<AsyncDIContainer>, { if self .di_container @@ -127,10 +119,10 @@ where } /// Creates a binding of factory type `Interface` to a factory inside of the - /// associated [`IAsyncDIContainer`]. + /// associated [`AsyncDIContainer`]. /// /// # Errors - /// Will return Err if the associated [`IAsyncDIContainer`] already have a binding + /// Will return Err if the associated [`AsyncDIContainer`] already have a binding /// for the interface. /// /// # Examples @@ -173,23 +165,18 @@ where /// # Ok(()) /// # } /// ``` - /// - /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] pub async fn to_factory<Args, Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, - ) -> Result< - AsyncBindingWhenConfigurator<Interface, DIContainerType>, - AsyncBindingBuilderError, - > + ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingBuilderError> where Args: std::marker::Tuple + 'static, Return: 'static + ?Sized, Interface: Fn<Args, Output = Return> + Send + Sync, FactoryFunc: - Fn<(Arc<DIContainerType>,), Output = BoxFn<Args, Return>> + Send + Sync, + Fn<(Arc<AsyncDIContainer>,), Output = BoxFn<Args, Return>> + Send + Sync, { use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::provider::r#async::AsyncFactoryVariant; @@ -221,10 +208,10 @@ where } /// Creates a binding of factory type `Interface` to a async factory inside of the - /// associated [`IAsyncDIContainer`]. + /// associated [`AsyncDIContainer`]. /// /// # Errors - /// Will return Err if the associated [`IAsyncDIContainer`] already have a binding + /// Will return Err if the associated [`AsyncDIContainer`] already have a binding /// for the interface. /// /// # Examples @@ -276,24 +263,19 @@ where /// # Ok(()) /// # } /// ``` - /// - /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] pub async fn to_async_factory<Args, Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, - ) -> Result< - AsyncBindingWhenConfigurator<Interface, DIContainerType>, - AsyncBindingBuilderError, - > + ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingBuilderError> where Args: std::marker::Tuple + 'static, Return: 'static + ?Sized, Interface: Fn<Args, Output = crate::future::BoxFuture<'static, Return>> + Send + Sync, FactoryFunc: Fn< - (Arc<DIContainerType>,), + (Arc<AsyncDIContainer>,), Output = BoxFn<Args, crate::future::BoxFuture<'static, Return>>, > + Send + Sync, @@ -328,10 +310,10 @@ where } /// Creates a binding of type `Interface` to a factory that takes no arguments - /// inside of the associated [`IAsyncDIContainer`]. + /// inside of the associated [`AsyncDIContainer`]. /// /// # Errors - /// Will return Err if the associated [`IAsyncDIContainer`] already have a binding + /// Will return Err if the associated [`AsyncDIContainer`] already have a binding /// for the interface. /// /// # Examples @@ -373,21 +355,16 @@ where /// # Ok(()) /// # } /// ``` - /// - /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] pub async fn to_default_factory<Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, - ) -> Result< - AsyncBindingWhenConfigurator<Interface, DIContainerType>, - AsyncBindingBuilderError, - > + ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingBuilderError> where Return: 'static + ?Sized, FactoryFunc: Fn< - (Arc<DIContainerType>,), + (Arc<AsyncDIContainer>,), Output = BoxFn<(), crate::ptr::TransientPtr<Return>>, > + Send + Sync, @@ -422,10 +399,10 @@ where } /// Creates a binding of factory type `Interface` to a async factory inside of the - /// associated [`IAsyncDIContainer`]. + /// associated [`AsyncDIContainer`]. /// /// # Errors - /// Will return Err if the associated [`IAsyncDIContainer`] already have a binding + /// Will return Err if the associated [`AsyncDIContainer`] already have a binding /// for the interface. /// /// # Examples @@ -472,21 +449,16 @@ where /// # Ok(()) /// # } /// ``` - /// - /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] pub async fn to_async_default_factory<Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, - ) -> Result< - AsyncBindingWhenConfigurator<Interface, DIContainerType>, - AsyncBindingBuilderError, - > + ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingBuilderError> where Return: 'static + ?Sized, FactoryFunc: Fn< - (Arc<DIContainerType>,), + (Arc<AsyncDIContainer>,), Output = BoxFn<(), crate::future::BoxFuture<'static, Return>>, > + Send + Sync, @@ -530,13 +502,13 @@ mod tests use super::*; use crate::dependency_history::MockDependencyHistory; - use crate::test_utils::{mocks, subjects_async}; + use crate::di_container::asynchronous::MockAsyncDIContainer; + use crate::test_utils::subjects_async; #[tokio::test] async fn can_bind_to() -> Result<(), Box<dyn Error>> { - let mut di_container_mock = - mocks::async_di_container::MockAsyncDIContainer::new(); + let mut di_container_mock = MockAsyncDIContainer::new(); di_container_mock .expect_has_binding::<dyn subjects_async::IUserManager>() @@ -551,10 +523,10 @@ mod tests .once(); let binding_builder = - AsyncBindingBuilder::< - dyn subjects_async::IUserManager, - mocks::async_di_container::MockAsyncDIContainer, - >::new(Arc::new(di_container_mock), MockDependencyHistory::new); + AsyncBindingBuilder::<dyn subjects_async::IUserManager>::new( + Arc::new(di_container_mock), + MockDependencyHistory::new, + ); binding_builder.to::<subjects_async::UserManager>().await?; @@ -578,8 +550,7 @@ mod tests + Send + Sync; - let mut di_container_mock = - mocks::async_di_container::MockAsyncDIContainer::new(); + let mut di_container_mock = MockAsyncDIContainer::new(); di_container_mock .expect_has_binding::<IUserManagerFactory>() @@ -593,11 +564,10 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = - AsyncBindingBuilder::< - IUserManagerFactory, - mocks::async_di_container::MockAsyncDIContainer, - >::new(Arc::new(di_container_mock), MockDependencyHistory::new); + let binding_builder = AsyncBindingBuilder::<IUserManagerFactory>::new( + Arc::new(di_container_mock), + MockDependencyHistory::new, + ); binding_builder .to_factory(&|_| { @@ -629,8 +599,7 @@ mod tests TransientPtr<dyn subjects_async::IUserManager> > + Send + Sync; - let mut di_container_mock = - mocks::async_di_container::MockAsyncDIContainer::new(); + let mut di_container_mock = MockAsyncDIContainer::new(); di_container_mock .expect_has_binding::<IUserManagerFactory>() @@ -644,11 +613,10 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = - AsyncBindingBuilder::< - IUserManagerFactory, - mocks::async_di_container::MockAsyncDIContainer, - >::new(Arc::new(di_container_mock), MockDependencyHistory::new); + let binding_builder = AsyncBindingBuilder::<IUserManagerFactory>::new( + Arc::new(di_container_mock), + MockDependencyHistory::new, + ); binding_builder .to_async_factory(&|_| { @@ -675,8 +643,7 @@ mod tests declare_default_factory!(dyn subjects_async::IUserManager); - let mut di_container_mock = - mocks::async_di_container::MockAsyncDIContainer::new(); + let mut di_container_mock = MockAsyncDIContainer::new(); di_container_mock .expect_has_binding::<dyn subjects_async::IUserManager>() @@ -691,10 +658,10 @@ mod tests .once(); let binding_builder = - AsyncBindingBuilder::< - dyn subjects_async::IUserManager, - mocks::async_di_container::MockAsyncDIContainer, - >::new(Arc::new(di_container_mock), MockDependencyHistory::new); + AsyncBindingBuilder::<dyn subjects_async::IUserManager>::new( + Arc::new(di_container_mock), + MockDependencyHistory::new, + ); binding_builder .to_default_factory(&|_| { @@ -722,8 +689,7 @@ mod tests declare_default_factory!(dyn subjects_async::IUserManager, async = true); - let mut di_container_mock = - mocks::async_di_container::MockAsyncDIContainer::new(); + let mut di_container_mock = MockAsyncDIContainer::new(); di_container_mock .expect_has_binding::<dyn subjects_async::IUserManager>() @@ -738,10 +704,10 @@ mod tests .once(); let binding_builder = - AsyncBindingBuilder::< - dyn subjects_async::IUserManager, - mocks::async_di_container::MockAsyncDIContainer, - >::new(Arc::new(di_container_mock), MockDependencyHistory::new); + AsyncBindingBuilder::<dyn subjects_async::IUserManager>::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 f10bb48..3557202 100644 --- a/src/di_container/asynchronous/binding/scope_configurator.rs +++ b/src/di_container/asynchronous/binding/scope_configurator.rs @@ -1,11 +1,8 @@ -//! Scope configurator for a binding for types inside of a [`IAsyncDIContainer`]. -//! -//! [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer +//! Scope configurator for a binding for types inside of a [`AsyncDIContainer`]. use std::marker::PhantomData; use std::sync::Arc; use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingWhenConfigurator; -use crate::di_container::asynchronous::IAsyncDIContainer; use crate::di_container::BindingOptions; use crate::errors::async_di_container::AsyncBindingScopeConfiguratorError; use crate::interfaces::async_injectable::AsyncInjectable; @@ -14,32 +11,28 @@ use crate::ptr::ThreadsafeSingletonPtr; use crate::util::use_double; use_double!(crate::dependency_history::DependencyHistory); +use_double!(crate::di_container::asynchronous::AsyncDIContainer); -/// Scope configurator for a binding for type `Interface` inside a [`IAsyncDIContainer`]. -/// -/// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer -pub struct AsyncBindingScopeConfigurator<Interface, Implementation, DIContainerType> +/// Scope configurator for a binding for type `Interface` inside a [`AsyncDIContainer`]. +pub struct AsyncBindingScopeConfigurator<Interface, Implementation> where Interface: 'static + ?Sized + Send + Sync, - Implementation: AsyncInjectable<DIContainerType>, - DIContainerType: IAsyncDIContainer, + Implementation: AsyncInjectable<AsyncDIContainer>, { - di_container: Arc<DIContainerType>, + di_container: Arc<AsyncDIContainer>, dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData<Interface>, implementation_phantom: PhantomData<Implementation>, } -impl<Interface, Implementation, DIContainerType> - AsyncBindingScopeConfigurator<Interface, Implementation, DIContainerType> +impl<Interface, Implementation> AsyncBindingScopeConfigurator<Interface, Implementation> where Interface: 'static + ?Sized + Send + Sync, - Implementation: AsyncInjectable<DIContainerType>, - DIContainerType: IAsyncDIContainer, + Implementation: AsyncInjectable<AsyncDIContainer>, { pub(crate) fn new( - di_container: Arc<DIContainerType>, + di_container: Arc<AsyncDIContainer>, dependency_history_factory: fn() -> DependencyHistory, ) -> Self { @@ -54,9 +47,7 @@ where /// Configures the binding to be in a transient scope. /// /// This is the default. - pub async fn in_transient_scope( - self, - ) -> AsyncBindingWhenConfigurator<Interface, DIContainerType> + pub async fn in_transient_scope(self) -> AsyncBindingWhenConfigurator<Interface> { self.set_in_transient_scope().await; @@ -69,10 +60,7 @@ where /// Will return Err if resolving the implementation fails. pub async fn in_singleton_scope( self, - ) -> Result< - AsyncBindingWhenConfigurator<Interface, DIContainerType>, - AsyncBindingScopeConfiguratorError, - > + ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingScopeConfiguratorError> { let singleton: ThreadsafeSingletonPtr<Implementation> = ThreadsafeSingletonPtr::from( @@ -100,7 +88,7 @@ where .set_binding::<Interface>( BindingOptions::new(), Box::new( - AsyncTransientTypeProvider::<Implementation, DIContainerType>::new(), + AsyncTransientTypeProvider::<Implementation, AsyncDIContainer>::new(), ), ) .await; @@ -112,13 +100,13 @@ mod tests { use super::*; use crate::dependency_history::MockDependencyHistory; - use crate::test_utils::{mocks, subjects_async}; + use crate::di_container::asynchronous::MockAsyncDIContainer; + use crate::test_utils::subjects_async; #[tokio::test] async fn in_transient_scope_works() { - let mut di_container_mock = - mocks::async_di_container::MockAsyncDIContainer::new(); + let mut di_container_mock = MockAsyncDIContainer::new(); di_container_mock .expect_set_binding::<dyn subjects_async::IUserManager>() @@ -130,7 +118,6 @@ mod tests 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; @@ -139,8 +126,7 @@ mod tests #[tokio::test] async fn in_singleton_scope_works() { - let mut di_container_mock = - mocks::async_di_container::MockAsyncDIContainer::new(); + let mut di_container_mock = MockAsyncDIContainer::new(); di_container_mock .expect_set_binding::<dyn subjects_async::IUserManager>() @@ -152,7 +138,6 @@ mod tests 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 diff --git a/src/di_container/asynchronous/binding/when_configurator.rs b/src/di_container/asynchronous/binding/when_configurator.rs index 4521178..3c1de7c 100644 --- a/src/di_container/asynchronous/binding/when_configurator.rs +++ b/src/di_container/asynchronous/binding/when_configurator.rs @@ -1,33 +1,29 @@ -//! When configurator for a binding for types inside of a [`IAsyncDIContainer`]. -//! -//! [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer +//! When configurator for a binding for types inside of a [`AsyncDIContainer`]. use std::any::type_name; use std::marker::PhantomData; use std::sync::Arc; -use crate::di_container::asynchronous::IAsyncDIContainer; use crate::di_container::BindingOptions; use crate::errors::async_di_container::AsyncBindingWhenConfiguratorError; +use crate::util::use_double; -/// When configurator for a binding for type `Interface` inside a [`IAsyncDIContainer`]. -/// -/// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer -pub struct AsyncBindingWhenConfigurator<Interface, DIContainerType> +use_double!(crate::di_container::asynchronous::AsyncDIContainer); + +/// When configurator for a binding for type `Interface` inside a [`AsyncDIContainer`]. +pub struct AsyncBindingWhenConfigurator<Interface> where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, { - di_container: Arc<DIContainerType>, + di_container: Arc<AsyncDIContainer>, interface_phantom: PhantomData<Interface>, } -impl<Interface, DIContainerType> AsyncBindingWhenConfigurator<Interface, DIContainerType> +impl<Interface> AsyncBindingWhenConfigurator<Interface> where Interface: 'static + ?Sized + Send + Sync, - DIContainerType: IAsyncDIContainer, { - pub(crate) fn new(di_container: Arc<DIContainerType>) -> Self + pub(crate) fn new(di_container: Arc<AsyncDIContainer>) -> Self { Self { di_container, @@ -71,14 +67,14 @@ mod tests use mockall::predicate::eq; use super::*; + use crate::di_container::asynchronous::MockAsyncDIContainer; use crate::provider::r#async::MockIAsyncProvider; - use crate::test_utils::{mocks, subjects_async}; + use crate::test_utils::subjects_async; #[tokio::test] async fn when_named_works() { - let mut di_container_mock = - mocks::async_di_container::MockAsyncDIContainer::new(); + let mut di_container_mock = MockAsyncDIContainer::new(); di_container_mock .expect_remove_binding::<dyn subjects_async::INumber>() @@ -94,7 +90,6 @@ mod tests let binding_when_configurator = AsyncBindingWhenConfigurator::< dyn subjects_async::INumber, - 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 8d67b99..73b1382 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -55,13 +55,11 @@ use std::any::type_name; use std::sync::Arc; use async_lock::Mutex; -use async_trait::async_trait; use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder; use crate::di_container::binding_storage::DIContainerBindingStorage; use crate::di_container::BindingOptions; use crate::errors::async_di_container::AsyncDIContainerError; -use crate::future::BoxFuture; use crate::private::cast::arc::CastArc; use crate::private::cast::boxed::CastBox; use crate::private::cast::error::CastError; @@ -74,17 +72,39 @@ use_double!(crate::dependency_history::DependencyHistory); pub mod binding; pub mod prelude; -/// Async dependency injection container interface. -/// -/// **This trait is sealed and cannot be implemented for types outside this crate.** -#[async_trait] -pub trait IAsyncDIContainer: - Sized + 'static + Send + Sync + details::DIContainerInternals +/// Async dependency injection container. +pub struct AsyncDIContainer +{ + binding_storage: Mutex<DIContainerBindingStorage<dyn IAsyncProvider<Self>>>, +} + +impl AsyncDIContainer +{ + /// Returns a new `AsyncDIContainer`. + #[must_use] + pub fn new() -> Arc<Self> + { + Arc::new(Self { + binding_storage: Mutex::new(DIContainerBindingStorage::new()), + }) + } +} + +#[cfg_attr(test, mockall::automock)] +impl AsyncDIContainer { /// Returns a new [`AsyncBindingBuilder`] for the given interface. - fn bind<Interface>(self: &mut Arc<Self>) -> AsyncBindingBuilder<Interface, Self> + #[allow(clippy::missing_panics_doc)] + pub fn bind<Interface>(self: &mut Arc<Self>) -> AsyncBindingBuilder<Interface> where - Interface: 'static + ?Sized + Send + Sync; + Interface: 'static + ?Sized + Send + Sync, + { + #[cfg(test)] + panic!("Bind function is unusable when testing"); + + #[cfg(not(test))] + AsyncBindingBuilder::new(self.clone(), DependencyHistory::new) + } /// Returns the type bound with `Interface`. /// @@ -93,13 +113,15 @@ pub trait IAsyncDIContainer: /// - No binding for `Interface` exists /// - Resolving the binding for `Interface` fails /// - Casting the binding for `Interface` fails - fn get<'a, 'b, Interface>( - self: &'a Arc<Self>, - ) -> BoxFuture<'b, Result<SomePtr<Interface>, AsyncDIContainerError>> + pub async fn get<Interface>( + self: &Arc<Self>, + ) -> Result<SomePtr<Interface>, AsyncDIContainerError> where - Interface: 'static + 'b + ?Sized + Send + Sync, - 'a: 'b, - Self: 'b; + Interface: 'static + ?Sized + Send + Sync, + { + self.get_bound::<Interface>(DependencyHistory::new(), BindingOptions::new()) + .await + } /// Returns the type bound with `Interface` and the specified name. /// @@ -108,14 +130,19 @@ pub trait IAsyncDIContainer: /// - No binding for `Interface` with name `name` exists /// - Resolving the binding for `Interface` fails /// - Casting the binding for `Interface` fails - fn get_named<'a, 'b, Interface>( - self: &'a Arc<Self>, + pub async fn get_named<Interface>( + self: &Arc<Self>, name: &'static str, - ) -> BoxFuture<'b, Result<SomePtr<Interface>, AsyncDIContainerError>> + ) -> Result<SomePtr<Interface>, AsyncDIContainerError> where - Interface: 'static + 'b + ?Sized + Send + Sync, - 'a: 'b, - Self: 'b; + Interface: 'static + ?Sized + Send + Sync, + { + self.get_bound::<Interface>( + DependencyHistory::new(), + BindingOptions::new().name(name), + ) + .await + } /// Returns the type bound with `Interface` where the binding has the specified /// options. @@ -131,7 +158,6 @@ pub trait IAsyncDIContainer: /// # Examples /// ``` /// # use syrette::di_container::asynchronous::AsyncDIContainer; - /// # use syrette::di_container::asynchronous::IAsyncDIContainer; /// # use syrette::dependency_history::DependencyHistory; /// # use syrette::di_container::BindingOptions; /// # @@ -152,100 +178,21 @@ pub trait IAsyncDIContainer: /// # Ok::<_, Box<dyn std::error::Error>>(()) /// # }); /// ``` - fn get_bound<'this, 'fut, Interface>( - self: &'this Arc<Self>, + pub async fn get_bound<Interface>( + self: &Arc<Self>, dependency_history: DependencyHistory, binding_options: BindingOptions<'static>, - ) -> BoxFuture<'fut, Result<SomePtr<Interface>, AsyncDIContainerError>> - where - Interface: 'static + 'this + ?Sized + Send + Sync, - 'this: 'fut, - Self: 'fut; -} - -/// Async dependency injection container. -pub struct AsyncDIContainer -{ - binding_storage: Mutex<DIContainerBindingStorage<dyn IAsyncProvider<Self>>>, -} - -impl AsyncDIContainer -{ - /// Returns a new `AsyncDIContainer`. - #[must_use] - pub fn new() -> Arc<Self> - { - Arc::new(Self { - binding_storage: Mutex::new(DIContainerBindingStorage::new()), - }) - } -} - -#[async_trait] -impl IAsyncDIContainer for AsyncDIContainer -{ - fn bind<Interface>(self: &mut Arc<Self>) -> AsyncBindingBuilder<Interface, Self> + ) -> Result<SomePtr<Interface>, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync, { - AsyncBindingBuilder::new(self.clone(), DependencyHistory::new) - } - - fn get<'a, 'b, Interface>( - self: &'a Arc<Self>, - ) -> BoxFuture<'b, Result<SomePtr<Interface>, AsyncDIContainerError>> - where - Interface: 'static + 'b + ?Sized + Send + Sync, - 'a: 'b, - Self: 'b, - { - Box::pin(async { - self.get_bound::<Interface>(DependencyHistory::new(), BindingOptions::new()) - .await - }) - } - - fn get_named<'a, 'b, Interface>( - self: &'a Arc<Self>, - name: &'static str, - ) -> BoxFuture<'b, Result<SomePtr<Interface>, AsyncDIContainerError>> - where - Interface: 'static + 'b + ?Sized + Send + Sync, - 'a: 'b, - Self: 'b, - { - Box::pin(async { - self.get_bound::<Interface>( - DependencyHistory::new(), - BindingOptions::new().name(name), - ) - .await - }) - } + let binding_providable = self + .get_binding_providable::<Interface>(binding_options, dependency_history) + .await?; - fn get_bound<'this, 'fut, Interface>( - self: &'this Arc<Self>, - dependency_history: DependencyHistory, - binding_options: BindingOptions<'static>, - ) -> BoxFuture<'fut, Result<SomePtr<Interface>, AsyncDIContainerError>> - where - Interface: 'static + 'this + ?Sized + Send + Sync, - 'this: 'fut, - Self: 'fut, - { - Box::pin(async move { - let binding_providable = self - .get_binding_providable::<Interface>(binding_options, dependency_history) - .await?; - - self.handle_binding_providable(binding_providable).await - }) + self.handle_binding_providable(binding_providable).await } -} -#[async_trait] -impl details::DIContainerInternals for AsyncDIContainer -{ async fn has_binding<Interface>( self: &Arc<Self>, binding_options: BindingOptions<'static>, @@ -372,6 +319,7 @@ impl AsyncDIContainer } #[cfg(feature = "factory")] AsyncProvidable::AsyncDefaultFactory(binding) => { + use crate::future::BoxFuture; use crate::private::factory::IThreadsafeFactory; use crate::ptr::TransientPtr; @@ -452,41 +400,6 @@ impl AsyncDIContainer } } -pub(crate) mod details -{ - use std::sync::Arc; - - use async_trait::async_trait; - - use crate::di_container::BindingOptions; - use crate::provider::r#async::IAsyncProvider; - - #[async_trait] - pub trait DIContainerInternals - { - async fn has_binding<Interface>( - self: &Arc<Self>, - binding_options: BindingOptions<'static>, - ) -> bool - where - Interface: ?Sized + 'static; - - async fn set_binding<Interface>( - self: &Arc<Self>, - binding_options: BindingOptions<'static>, - provider: Box<dyn IAsyncProvider<Self>>, - ) where - Interface: 'static + ?Sized; - - async fn remove_binding<Interface>( - self: &Arc<Self>, - binding_options: BindingOptions<'static>, - ) -> Option<Box<dyn IAsyncProvider<Self>>> - where - Interface: 'static + ?Sized; - } -} - #[cfg(test)] mod tests { @@ -872,4 +785,83 @@ mod tests Ok(()) } + + #[tokio::test] + async fn has_binding_works() + { + let di_container = AsyncDIContainer::new(); + + // No binding is present yet + assert!( + !di_container + .has_binding::<subjects_async::Number>(BindingOptions::new()) + .await + ); + + di_container + .binding_storage + .lock() + .await + .set::<subjects_async::Number>( + BindingOptions::new(), + Box::new(MockAsyncProvider::new()), + ); + + assert!( + di_container + .has_binding::<subjects_async::Number>(BindingOptions::new()) + .await + ); + } + + #[tokio::test] + async fn set_binding_works() + { + let di_container = AsyncDIContainer::new(); + + di_container + .set_binding::<subjects_async::UserManager>( + BindingOptions::new(), + Box::new(MockAsyncProvider::new()), + ) + .await; + + assert!(di_container + .binding_storage + .lock() + .await + .has::<subjects_async::UserManager>(BindingOptions::new())); + } + + #[tokio::test] + async fn remove_binding_works() + { + let di_container = AsyncDIContainer::new(); + + di_container + .binding_storage + .lock() + .await + .set::<subjects_async::UserManager>( + BindingOptions::new(), + Box::new(MockAsyncProvider::new()), + ); + + assert!( + // Formatting is weird without this comment + di_container + .remove_binding::<subjects_async::UserManager>(BindingOptions::new()) + .await + .is_some() + ); + + assert!( + // Formatting is weird without this comment + !di_container + .binding_storage + .lock() + .await + .has::<subjects_async::UserManager>(BindingOptions::new()) + ); + } } diff --git a/src/di_container/asynchronous/prelude.rs b/src/di_container/asynchronous/prelude.rs index 50fc42b..afbb0a2 100644 --- a/src/di_container/asynchronous/prelude.rs +++ b/src/di_container/asynchronous/prelude.rs @@ -1,3 +1,3 @@ //! Commonly used types. -pub use crate::di_container::asynchronous::{AsyncDIContainer, IAsyncDIContainer}; +pub use crate::di_container::asynchronous::AsyncDIContainer; diff --git a/src/interfaces/async_injectable.rs b/src/interfaces/async_injectable.rs index 56b263d..c455970 100644 --- a/src/interfaces/async_injectable.rs +++ b/src/interfaces/async_injectable.rs @@ -2,7 +2,6 @@ use std::fmt::Debug; use std::sync::Arc; -use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::injectable::InjectableError; use crate::future::BoxFuture; use crate::private::cast::CastFromArc; @@ -13,8 +12,6 @@ use_double!(crate::dependency_history::DependencyHistory); /// Interface for structs that can be injected into or be injected to. pub trait AsyncInjectable<DIContainerType>: CastFromArc -where - DIContainerType: IAsyncDIContainer, { /// Resolves the dependencies of the injectable. /// @@ -30,8 +27,6 @@ where } impl<DIContainerType> Debug for dyn AsyncInjectable<DIContainerType> -where - DIContainerType: IAsyncDIContainer, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/src/provider/async.rs b/src/provider/async.rs index 2a241a7..ae633fd 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -3,7 +3,6 @@ use std::sync::Arc; use async_trait::async_trait; -use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::injectable::InjectableError; use crate::interfaces::async_injectable::AsyncInjectable; use crate::ptr::{ThreadsafeSingletonPtr, TransientPtr}; @@ -12,12 +11,10 @@ use crate::util::use_double; use_double!(crate::dependency_history::DependencyHistory); #[derive(strum_macros::Display, Debug)] -pub enum AsyncProvidable<DIContainerType> -where - DIContainerType: IAsyncDIContainer, +pub enum AsyncProvidable<DIContainerT> { - Transient(TransientPtr<dyn AsyncInjectable<DIContainerType>>), - Singleton(ThreadsafeSingletonPtr<dyn AsyncInjectable<DIContainerType>>), + Transient(TransientPtr<dyn AsyncInjectable<DIContainerT>>), + Singleton(ThreadsafeSingletonPtr<dyn AsyncInjectable<DIContainerT>>), #[cfg(feature = "factory")] Factory( crate::ptr::ThreadsafeFactoryPtr< @@ -40,22 +37,22 @@ where #[async_trait] #[cfg_attr(test, mockall::automock, allow(dead_code))] -pub trait IAsyncProvider<DIContainerType>: Send + Sync +pub trait IAsyncProvider<DIContainerT>: Send + Sync where - DIContainerType: IAsyncDIContainer, + DIContainerT: Send + Sync, { async fn provide( &self, - di_container: &Arc<DIContainerType>, + di_container: &Arc<DIContainerT>, dependency_history: DependencyHistory, - ) -> Result<AsyncProvidable<DIContainerType>, InjectableError>; + ) -> Result<AsyncProvidable<DIContainerT>, InjectableError>; - fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerType>>; + fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerT>>; } -impl<DIContainerType> Clone for Box<dyn IAsyncProvider<DIContainerType>> +impl<DIContainerT> Clone for Box<dyn IAsyncProvider<DIContainerT>> where - DIContainerType: IAsyncDIContainer, + DIContainerT: Send + Sync, { fn clone(&self) -> Self { @@ -63,20 +60,17 @@ where } } -pub struct AsyncTransientTypeProvider<InjectableType, DIContainerType> +pub struct AsyncTransientTypeProvider<InjectableT, DIContainerT> where - InjectableType: AsyncInjectable<DIContainerType>, - DIContainerType: IAsyncDIContainer, + InjectableT: AsyncInjectable<DIContainerT>, { - injectable_phantom: PhantomData<InjectableType>, - di_container_phantom: PhantomData<DIContainerType>, + injectable_phantom: PhantomData<InjectableT>, + di_container_phantom: PhantomData<DIContainerT>, } -impl<InjectableType, DIContainerType> - AsyncTransientTypeProvider<InjectableType, DIContainerType> +impl<InjectableT, DIContainerT> AsyncTransientTypeProvider<InjectableT, DIContainerT> where - InjectableType: AsyncInjectable<DIContainerType>, - DIContainerType: IAsyncDIContainer, + InjectableT: AsyncInjectable<DIContainerT>, { pub fn new() -> Self { @@ -88,34 +82,33 @@ where } #[async_trait] -impl<InjectableType, DIContainerType> IAsyncProvider<DIContainerType> - for AsyncTransientTypeProvider<InjectableType, DIContainerType> +impl<InjectableT, DIContainerT> IAsyncProvider<DIContainerT> + for AsyncTransientTypeProvider<InjectableT, DIContainerT> where - InjectableType: AsyncInjectable<DIContainerType>, - DIContainerType: IAsyncDIContainer, + InjectableT: AsyncInjectable<DIContainerT>, + DIContainerT: Send + Sync + 'static, { async fn provide( &self, - di_container: &Arc<DIContainerType>, + di_container: &Arc<DIContainerT>, dependency_history: DependencyHistory, - ) -> Result<AsyncProvidable<DIContainerType>, InjectableError> + ) -> Result<AsyncProvidable<DIContainerT>, InjectableError> { Ok(AsyncProvidable::Transient( - InjectableType::resolve(di_container, dependency_history).await?, + InjectableT::resolve(di_container, dependency_history).await?, )) } - fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerType>> + fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerT>> { Box::new(self.clone()) } } -impl<InjectableType, DIContainerType> Clone - for AsyncTransientTypeProvider<InjectableType, DIContainerType> +impl<InjectableT, DIContainerT> Clone + for AsyncTransientTypeProvider<InjectableT, DIContainerT> where - InjectableType: AsyncInjectable<DIContainerType>, - DIContainerType: IAsyncDIContainer, + InjectableT: AsyncInjectable<DIContainerT>, { fn clone(&self) -> Self { @@ -126,23 +119,20 @@ where } } -pub struct AsyncSingletonProvider<InjectableType, DIContainerType> +pub struct AsyncSingletonProvider<InjectableT, DIContainerT> where - InjectableType: AsyncInjectable<DIContainerType>, - DIContainerType: IAsyncDIContainer, + InjectableT: AsyncInjectable<DIContainerT>, { - singleton: ThreadsafeSingletonPtr<InjectableType>, + singleton: ThreadsafeSingletonPtr<InjectableT>, - di_container_phantom: PhantomData<DIContainerType>, + di_container_phantom: PhantomData<DIContainerT>, } -impl<InjectableType, DIContainerType> - AsyncSingletonProvider<InjectableType, DIContainerType> +impl<InjectableT, DIContainerT> AsyncSingletonProvider<InjectableT, DIContainerT> where - InjectableType: AsyncInjectable<DIContainerType>, - DIContainerType: IAsyncDIContainer, + InjectableT: AsyncInjectable<DIContainerT>, { - pub fn new(singleton: ThreadsafeSingletonPtr<InjectableType>) -> Self + pub fn new(singleton: ThreadsafeSingletonPtr<InjectableT>) -> Self { Self { singleton, @@ -152,32 +142,31 @@ where } #[async_trait] -impl<InjectableType, DIContainerType> IAsyncProvider<DIContainerType> - for AsyncSingletonProvider<InjectableType, DIContainerType> +impl<InjectableT, DIContainerT> IAsyncProvider<DIContainerT> + for AsyncSingletonProvider<InjectableT, DIContainerT> where - InjectableType: AsyncInjectable<DIContainerType>, - DIContainerType: IAsyncDIContainer, + InjectableT: AsyncInjectable<DIContainerT>, + DIContainerT: Send + Sync + 'static, { async fn provide( &self, - _di_container: &Arc<DIContainerType>, + _di_container: &Arc<DIContainerT>, _dependency_history: DependencyHistory, - ) -> Result<AsyncProvidable<DIContainerType>, InjectableError> + ) -> Result<AsyncProvidable<DIContainerT>, InjectableError> { Ok(AsyncProvidable::Singleton(self.singleton.clone())) } - fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerType>> + fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerT>> { Box::new(self.clone()) } } -impl<InjectableType, DIContainerType> Clone - for AsyncSingletonProvider<InjectableType, DIContainerType> +impl<InjectableT, DIContainerT> Clone + for AsyncSingletonProvider<InjectableT, DIContainerT> where - InjectableType: AsyncInjectable<DIContainerType>, - DIContainerType: IAsyncDIContainer, + InjectableT: AsyncInjectable<DIContainerT>, { fn clone(&self) -> Self { @@ -222,15 +211,15 @@ impl AsyncFactoryProvider #[cfg(feature = "factory")] #[async_trait] -impl<DIContainerType> IAsyncProvider<DIContainerType> for AsyncFactoryProvider +impl<DIContainerT> IAsyncProvider<DIContainerT> for AsyncFactoryProvider where - DIContainerType: IAsyncDIContainer, + DIContainerT: Send + Sync, { async fn provide( &self, - _di_container: &Arc<DIContainerType>, + _di_container: &Arc<DIContainerT>, _dependency_history: DependencyHistory, - ) -> Result<AsyncProvidable<DIContainerType>, InjectableError> + ) -> Result<AsyncProvidable<DIContainerT>, InjectableError> { Ok(match self.variant { AsyncFactoryVariant::Normal => AsyncProvidable::Factory(self.factory.clone()), @@ -243,7 +232,7 @@ where }) } - fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerType>> + fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerT>> { Box::new(self.clone()) } @@ -268,17 +257,18 @@ mod tests use super::*; use crate::dependency_history::MockDependencyHistory; - use crate::test_utils::{mocks, subjects_async}; + use crate::di_container::asynchronous::MockAsyncDIContainer; + use crate::test_utils::subjects_async; #[tokio::test] async fn async_transient_type_provider_works() -> Result<(), Box<dyn Error>> { let transient_type_provider = AsyncTransientTypeProvider::< subjects_async::UserManager, - mocks::async_di_container::MockAsyncDIContainer, + MockAsyncDIContainer, >::new(); - let di_container = mocks::async_di_container::MockAsyncDIContainer::new(); + let di_container = MockAsyncDIContainer::new(); assert!( matches!( @@ -298,12 +288,12 @@ mod tests { let singleton_provider = AsyncSingletonProvider::< subjects_async::UserManager, - mocks::async_di_container::MockAsyncDIContainer, + MockAsyncDIContainer, >::new(ThreadsafeSingletonPtr::new( subjects_async::UserManager {}, )); - let di_container = mocks::async_di_container::MockAsyncDIContainer::new(); + let di_container = MockAsyncDIContainer::new(); assert!( matches!( @@ -345,8 +335,7 @@ mod tests AsyncFactoryVariant::AsyncDefault, ); - let di_container = - Arc::new(mocks::async_di_container::MockAsyncDIContainer::new()); + let di_container = Arc::new(MockAsyncDIContainer::new()); assert!( matches!( diff --git a/src/test_utils.rs b/src/test_utils.rs index 95b7ce1..41e3753 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -144,7 +144,6 @@ pub mod subjects_async use async_trait::async_trait; use syrette_macros::declare_interface; - use crate::di_container::asynchronous::IAsyncDIContainer; use crate::interfaces::async_injectable::AsyncInjectable; use crate::ptr::TransientPtr; @@ -187,8 +186,6 @@ pub mod subjects_async #[async_trait] impl<DIContainerType> AsyncInjectable<DIContainerType> for UserManager - where - DIContainerType: IAsyncDIContainer, { async fn resolve( _: &Arc<DIContainerType>, @@ -254,8 +251,6 @@ pub mod subjects_async #[async_trait] impl<DIContainerType> AsyncInjectable<DIContainerType> for Number - where - DIContainerType: IAsyncDIContainer, { async fn resolve( _: &Arc<DIContainerType>, @@ -275,85 +270,6 @@ pub mod mocks #![allow(dead_code)] // Not all mock functions may be used #[cfg(feature = "async")] - pub mod async_di_container - { - use std::sync::Arc; - - use mockall::mock; - - use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder; - use crate::di_container::asynchronous::details::DIContainerInternals; - use crate::di_container::asynchronous::IAsyncDIContainer; - use crate::di_container::BindingOptions; - use crate::errors::async_di_container::AsyncDIContainerError; - use crate::provider::r#async::IAsyncProvider; - use crate::ptr::SomePtr; - use crate::util::use_double; - - use_double!(crate::dependency_history::DependencyHistory); - - mock! { - pub AsyncDIContainer {} - - #[async_trait::async_trait] - impl IAsyncDIContainer for AsyncDIContainer - { - fn bind<Interface>(self: &mut Arc<Self>) -> - AsyncBindingBuilder<Interface, Self> - where - Interface: 'static + ?Sized + Send + Sync; - - async fn get<Interface>( - self: &Arc<Self>, - ) -> Result<SomePtr<Interface>, AsyncDIContainerError> - where - Interface: 'static + ?Sized + Send + Sync; - - async fn get_named<Interface>( - self: &Arc<Self>, - name: &'static str, - ) -> Result<SomePtr<Interface>, AsyncDIContainerError> - where - Interface: 'static + ?Sized + Send + Sync; - - #[doc(hidden)] - async fn get_bound<Interface>( - self: &Arc<Self>, - dependency_history: DependencyHistory, - binding_options: BindingOptions<'static> - ) -> Result<SomePtr<Interface>, AsyncDIContainerError> - where - Interface: 'static + ?Sized + Send + Sync; - } - - #[async_trait::async_trait] - impl DIContainerInternals for AsyncDIContainer - { - async fn has_binding<Interface>( - self: &Arc<Self>, - binding_options: BindingOptions<'static>, - ) -> bool - where - Interface: ?Sized + 'static; - - async fn set_binding<Interface>( - self: &Arc<Self>, - binding_options: BindingOptions<'static>, - provider: Box<dyn IAsyncProvider<Self>>, - ) where - Interface: 'static + ?Sized; - - async fn remove_binding<Interface>( - self: &Arc<Self>, - binding_options: BindingOptions<'static>, - ) -> Option<Box<dyn IAsyncProvider<Self>>> - where - Interface: 'static + ?Sized; - } - } - } - - #[cfg(feature = "async")] pub mod async_provider { use std::sync::Arc; @@ -361,7 +277,6 @@ pub mod mocks use async_trait::async_trait; use mockall::mock; - use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::injectable::InjectableError; use crate::provider::r#async::{AsyncProvidable, IAsyncProvider}; use crate::util::use_double; @@ -369,23 +284,22 @@ pub mod mocks use_double!(crate::dependency_history::DependencyHistory); mock! { - pub AsyncProvider<DIContainerType: IAsyncDIContainer> {} + pub AsyncProvider<DIContainerT> {} #[async_trait] - impl< - DIContainerType: IAsyncDIContainer, - > IAsyncProvider<DIContainerType> for AsyncProvider< - DIContainerType, - > + impl<DIContainerT> IAsyncProvider<DIContainerT> + for AsyncProvider<DIContainerT> + where + DIContainerT: Send + Sync { async fn provide( &self, - di_container: &Arc<DIContainerType>, + di_container: &Arc<DIContainerT>, dependency_history: DependencyHistory - ) -> Result<AsyncProvidable<DIContainerType>, InjectableError>; + ) -> Result<AsyncProvidable<DIContainerT>, InjectableError>; fn do_clone(&self) -> - Box<dyn IAsyncProvider<DIContainerType>>; + Box<dyn IAsyncProvider<DIContainerT>>; } } } |