From 4fd0d6b4951b08a20d5378bca75561109dc6d036 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 18 Sep 2023 22:35:58 +0200 Subject: refactor!: make the async DI container not inside a Arc BREAKING CHANGE: The async DI container is no longer inside of a Arc. This affects AsyncBindingBuilder, AsyncBindingScopeConfigurator, AsyncBindingWhenConfigurator & AsyncInjectable --- src/di_container/asynchronous/mod.rs | 84 +++++++++++++----------------------- 1 file changed, 30 insertions(+), 54 deletions(-) (limited to 'src/di_container/asynchronous/mod.rs') diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index 827364d..4be232d 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -71,6 +71,7 @@ use_double!(crate::dependency_history::DependencyHistory); pub mod binding; /// Async dependency injection container. +#[derive(Default)] pub struct AsyncDIContainer { binding_storage: Mutex>>, @@ -80,11 +81,11 @@ impl AsyncDIContainer { /// Returns a new `AsyncDIContainer`. #[must_use] - pub fn new() -> Arc + pub fn new() -> Self { - Arc::new(Self { + Self { binding_storage: Mutex::new(DIContainerBindingStorage::new()), - }) + } } } @@ -93,7 +94,7 @@ impl AsyncDIContainer { /// Returns a new [`AsyncBindingBuilder`] for the given interface. #[allow(clippy::missing_panics_doc)] - pub fn bind(self: &mut Arc) -> AsyncBindingBuilder + pub fn bind(&mut self) -> AsyncBindingBuilder<'_, Interface> where Interface: 'static + ?Sized + Send + Sync, { @@ -101,7 +102,7 @@ impl AsyncDIContainer panic!("Bind function is unusable when testing"); #[cfg(not(test))] - AsyncBindingBuilder::new(self.clone(), DependencyHistory::new) + AsyncBindingBuilder::new(self, DependencyHistory::new) } /// Returns the type bound with `Interface`. @@ -112,7 +113,7 @@ impl AsyncDIContainer /// - Resolving the binding for `Interface` fails /// - Casting the binding for `Interface` fails pub async fn get( - self: &Arc, + &self, ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync, @@ -129,7 +130,7 @@ impl AsyncDIContainer /// - Resolving the binding for `Interface` fails /// - Casting the binding for `Interface` fails pub async fn get_named( - self: &Arc, + &self, name: &'static str, ) -> Result, AsyncDIContainerError> where @@ -177,7 +178,7 @@ impl AsyncDIContainer /// # }); /// ``` pub async fn get_bound( - self: &Arc, + &self, dependency_history: DependencyHistory, binding_options: BindingOptions<'static>, ) -> Result, AsyncDIContainerError> @@ -192,7 +193,7 @@ impl AsyncDIContainer } async fn has_binding( - self: &Arc, + &self, binding_options: BindingOptions<'static>, ) -> bool where @@ -205,7 +206,7 @@ impl AsyncDIContainer } async fn set_binding( - self: &Arc, + &self, binding_options: BindingOptions<'static>, provider: Box>, ) where @@ -218,7 +219,7 @@ impl AsyncDIContainer } async fn remove_binding( - self: &Arc, + &self, binding_options: BindingOptions<'static>, ) -> Option>> where @@ -234,7 +235,7 @@ impl AsyncDIContainer impl AsyncDIContainer { async fn handle_binding_providable( - self: &Arc, + &self, binding_providable: AsyncProvidable, ) -> Result, AsyncDIContainerError> where @@ -299,9 +300,7 @@ impl AsyncDIContainer } })?; - Ok(SomePtr::ThreadsafeFactory( - factory.call(self.clone()).into(), - )) + Ok(SomePtr::ThreadsafeFactory(factory.call(self).into())) } #[cfg(feature = "factory")] AsyncProvidable::DefaultFactory(binding) => { @@ -317,7 +316,7 @@ impl AsyncDIContainer DefaultFactoryFn, >(binding, "default factory")?; - Ok(SomePtr::Transient(default_factory.call(self.clone())())) + Ok(SomePtr::Transient(default_factory.call(self)())) } #[cfg(feature = "factory")] AsyncProvidable::AsyncDefaultFactory(binding) => { @@ -338,9 +337,7 @@ impl AsyncDIContainer binding, "async default factory" )?; - Ok(SomePtr::Transient( - async_default_factory.call(self.clone())().await, - )) + Ok(SomePtr::Transient(async_default_factory.call(self)().await)) } } } @@ -368,7 +365,7 @@ impl AsyncDIContainer } async fn get_binding_providable( - self: &Arc, + &self, binding_options: BindingOptions<'static>, dependency_history: DependencyHistory, ) -> Result, AsyncDIContainerError> @@ -644,21 +641,13 @@ mod tests let mut mock_provider = MockAsyncProvider::new(); mock_provider.expect_do_clone().returning(|| { - type FactoryFunc = Box< - (dyn Fn<(Vec,), Output = TransientPtr> + Send + Sync) - >; - let mut inner_mock_provider = MockAsyncProvider::new(); - let factory_func: &'static (dyn Fn< - (Arc,), - Output = FactoryFunc> + Send + Sync) = &|_| { + let factory_func = &|_: &AsyncDIContainer| { Box::new(|users| { - let user_manager: TransientPtr = - TransientPtr::new(UserManager::new(users)); - - user_manager - }) + TransientPtr::new(UserManager::new(users)) + as TransientPtr + }) as Box }; inner_mock_provider.expect_provide().returning(|_, _| { @@ -672,16 +661,11 @@ mod tests Box::new(inner_mock_provider) }); - { - di_container - .binding_storage - .lock() - .await - .set::( - BindingOptions::new(), - Box::new(mock_provider), - ); - } + di_container + .binding_storage + .lock() + .await + .set::(BindingOptions::new(), Box::new(mock_provider)); di_container .get::() @@ -743,21 +727,13 @@ mod tests let mut mock_provider = MockAsyncProvider::new(); mock_provider.expect_do_clone().returning(|| { - type FactoryFunc = Box< - (dyn Fn<(Vec,), Output = TransientPtr> + Send + Sync) - >; - let mut inner_mock_provider = MockAsyncProvider::new(); - let factory_func: &'static (dyn Fn< - (Arc,), - Output = FactoryFunc> + Send + Sync) = &|_| { + let factory_func = &|_: &AsyncDIContainer| { Box::new(|users| { - let user_manager: TransientPtr = - TransientPtr::new(UserManager::new(users)); - - user_manager - }) + TransientPtr::new(UserManager::new(users)) + as TransientPtr + }) as Box }; inner_mock_provider.expect_provide().returning(|_, _| { -- cgit v1.2.3-18-g5258