From b0a1af1e312f23eff7fe68ae17132ccded5cf31c Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 4 Oct 2023 11:10:36 +0200 Subject: refactor!: remove mutex in AsyncDIContainer BREAKING CHANGE: Multiple async DI container binding builder & binding configurator functions are no longer async --- src/di_container/asynchronous/binding/builder.rs | 160 +++++++++-------------- 1 file changed, 65 insertions(+), 95 deletions(-) (limited to 'src/di_container/asynchronous/binding/builder.rs') diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index db73ad2..5e77c24 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -24,7 +24,7 @@ pub struct AsyncBindingBuilder<'di_container, Interface> where Interface: 'static + ?Sized + Send + Sync, { - di_container: &'di_container AsyncDIContainer, + di_container: &'di_container mut AsyncDIContainer, dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, @@ -35,7 +35,7 @@ where Interface: 'static + ?Sized + Send + Sync, { pub(crate) fn new( - di_container: &'di_container AsyncDIContainer, + di_container: &'di_container mut AsyncDIContainer, dependency_history_factory: fn() -> DependencyHistory, ) -> Self { @@ -82,12 +82,12 @@ where /// # { /// # let mut di_container = AsyncDIContainer::new(); /// # - /// di_container.bind::().to::().await?; + /// di_container.bind::().to::()?; /// # /// # Ok(()) /// # } /// ``` - pub async fn to( + pub fn to( self, ) -> Result< AsyncBindingScopeConfigurator<'di_container, Interface, Implementation>, @@ -99,7 +99,6 @@ where if self .di_container .has_binding::(BindingOptions::new()) - .await { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< Interface, @@ -107,12 +106,12 @@ where ))); } - let binding_scope_configurator = AsyncBindingScopeConfigurator::new( + let mut binding_scope_configurator = AsyncBindingScopeConfigurator::new( self.di_container, self.dependency_history_factory, ); - binding_scope_configurator.set_in_transient_scope().await; + binding_scope_configurator.set_in_transient_scope(); Ok(binding_scope_configurator) } @@ -150,23 +149,20 @@ where /// # { /// # let mut di_container = AsyncDIContainer::new(); /// # - /// di_container - /// .bind::() - /// .to_factory(&|_| { - /// Box::new(|num, some_str| { - /// let bar = TransientPtr::new(Bar { num, some_str }); + /// di_container.bind::().to_factory(&|_| { + /// Box::new(|num, some_str| { + /// let bar = TransientPtr::new(Bar { num, some_str }); /// - /// bar as TransientPtr - /// }) + /// bar as TransientPtr /// }) - /// .await?; + /// })?; /// # /// # Ok(()) /// # } /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub async fn to_factory( + pub fn to_factory( self, factory_func: &'static FactoryFunc, ) -> Result< @@ -185,7 +181,6 @@ where if self .di_container .has_binding::(BindingOptions::new()) - .await { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< Interface, @@ -195,15 +190,13 @@ where let factory_impl = ThreadsafeCastableFactory::new(factory_func); - self.di_container - .set_binding::( - BindingOptions::new(), - Box::new(crate::provider::r#async::AsyncFactoryProvider::new( - crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), - AsyncFactoryVariant::Normal, - )), - ) - .await; + self.di_container.set_binding::( + BindingOptions::new(), + Box::new(crate::provider::r#async::AsyncFactoryProvider::new( + crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), + AsyncFactoryVariant::Normal, + )), + ); Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } @@ -246,27 +239,24 @@ where /// # { /// # let mut di_container = AsyncDIContainer::new(); /// # - /// di_container - /// .bind::() - /// .to_async_factory(&|_| { - /// Box::new(|num, some_str| { - /// Box::pin(async move { - /// let bar = TransientPtr::new(Bar { num, some_str }); + /// di_container.bind::().to_async_factory(&|_| { + /// Box::new(|num, some_str| { + /// Box::pin(async move { + /// let bar = TransientPtr::new(Bar { num, some_str }); /// - /// tokio::time::sleep(Duration::from_secs(2)).await; + /// tokio::time::sleep(Duration::from_secs(2)).await; /// - /// bar as TransientPtr - /// }) + /// bar as TransientPtr /// }) /// }) - /// .await?; + /// })?; /// # /// # Ok(()) /// # } /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub async fn to_async_factory( + pub fn to_async_factory( self, factory_func: &'static FactoryFunc, ) -> Result< @@ -290,7 +280,6 @@ where if self .di_container .has_binding::(BindingOptions::new()) - .await { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< Interface, @@ -300,15 +289,13 @@ where let factory_impl = ThreadsafeCastableFactory::new(factory_func); - self.di_container - .set_binding::( - BindingOptions::new(), - Box::new(crate::provider::r#async::AsyncFactoryProvider::new( - crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), - AsyncFactoryVariant::Normal, - )), - ) - .await; + self.di_container.set_binding::( + BindingOptions::new(), + Box::new(crate::provider::r#async::AsyncFactoryProvider::new( + crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), + AsyncFactoryVariant::Normal, + )), + ); Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } @@ -342,26 +329,23 @@ where /// # { /// # let mut di_container = AsyncDIContainer::new(); /// # - /// di_container - /// .bind::() - /// .to_default_factory(&|_| { - /// Box::new(|| { - /// let bar = TransientPtr::new(Bar { - /// num: 42, - /// some_str: "hello".to_string(), - /// }); + /// di_container.bind::().to_default_factory(&|_| { + /// Box::new(|| { + /// let bar = TransientPtr::new(Bar { + /// num: 42, + /// some_str: "hello".to_string(), + /// }); /// - /// bar as TransientPtr - /// }) + /// bar as TransientPtr /// }) - /// .await?; + /// })?; /// # /// # Ok(()) /// # } /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub async fn to_default_factory( + pub fn to_default_factory( self, factory_func: &'static FactoryFunc, ) -> Result< @@ -380,7 +364,6 @@ where if self .di_container .has_binding::(BindingOptions::new()) - .await { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< Interface, @@ -390,15 +373,13 @@ where let factory_impl = ThreadsafeCastableFactory::new(factory_func); - self.di_container - .set_binding::( - BindingOptions::new(), - Box::new(crate::provider::r#async::AsyncFactoryProvider::new( - crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), - AsyncFactoryVariant::Default, - )), - ) - .await; + self.di_container.set_binding::( + BindingOptions::new(), + Box::new(crate::provider::r#async::AsyncFactoryProvider::new( + crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), + AsyncFactoryVariant::Default, + )), + ); Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } @@ -448,15 +429,14 @@ where /// bar as TransientPtr /// }) /// }) - /// }) - /// .await?; + /// })?; /// # /// # Ok(()) /// # } /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub async fn to_async_default_factory( + pub fn to_async_default_factory( self, factory_func: &'static FactoryFunc, ) -> Result< @@ -475,7 +455,6 @@ where if self .di_container .has_binding::(BindingOptions::new()) - .await { return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::< Interface, @@ -485,15 +464,13 @@ where let factory_impl = ThreadsafeCastableFactory::new(factory_func); - self.di_container - .set_binding::( - BindingOptions::new(), - Box::new(crate::provider::r#async::AsyncFactoryProvider::new( - crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), - AsyncFactoryVariant::AsyncDefault, - )), - ) - .await; + self.di_container.set_binding::( + BindingOptions::new(), + Box::new(crate::provider::r#async::AsyncFactoryProvider::new( + crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), + AsyncFactoryVariant::AsyncDefault, + )), + ); Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } @@ -528,14 +505,11 @@ mod tests let binding_builder = AsyncBindingBuilder::::new( - &di_container_mock, + &mut di_container_mock, MockDependencyHistory::new, ); - binding_builder - .to::() - .await - .unwrap(); + binding_builder.to::().unwrap(); } #[tokio::test] @@ -570,7 +544,7 @@ mod tests .once(); let binding_builder = AsyncBindingBuilder::::new( - &di_container_mock, + &mut di_container_mock, MockDependencyHistory::new, ); @@ -583,7 +557,6 @@ mod tests user_manager }) }) - .await .unwrap(); } @@ -618,7 +591,7 @@ mod tests .once(); let binding_builder = AsyncBindingBuilder::::new( - &di_container_mock, + &mut di_container_mock, MockDependencyHistory::new, ); @@ -631,7 +604,6 @@ mod tests user_manager }) }) - .await .unwrap(); } @@ -662,7 +634,7 @@ mod tests let binding_builder = AsyncBindingBuilder::::new( - &di_container_mock, + &mut di_container_mock, MockDependencyHistory::new, ); @@ -675,7 +647,6 @@ mod tests user_manager }) }) - .await .unwrap(); } @@ -707,7 +678,7 @@ mod tests let binding_builder = AsyncBindingBuilder::::new( - &di_container_mock, + &mut di_container_mock, MockDependencyHistory::new, ); @@ -720,7 +691,6 @@ mod tests user_manager }) }) - .await .unwrap(); } } -- cgit v1.2.3-18-g5258