diff options
author | HampusM <hampus@hampusmat.com> | 2023-10-04 11:10:36 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-10-04 11:10:36 +0200 |
commit | b0a1af1e312f23eff7fe68ae17132ccded5cf31c (patch) | |
tree | 316f9177fcc7d3d73051750d9d87a6e965b6a26c | |
parent | 613bf4a8a094df6f048cc8b1fcf2fc425abddd99 (diff) |
refactor!: remove mutex in AsyncDIContainer
BREAKING CHANGE: Multiple async DI container binding builder & binding configurator functions are no longer async
-rw-r--r-- | Cargo.lock | 16 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | examples/async-factory/main.rs | 18 | ||||
-rw-r--r-- | examples/async/bootstrap.rs | 31 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/builder.rs | 160 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/scope_configurator.rs | 52 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/when_configurator.rs | 24 | ||||
-rw-r--r-- | src/di_container/asynchronous/mod.rs | 216 |
8 files changed, 190 insertions, 330 deletions
@@ -44,15 +44,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854" [[package]] -name = "async-lock" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa24f727524730b077666307f2734b4a1a1c57acb79193127dcc8914d5242dd7" -dependencies = [ - "event-listener", -] - -[[package]] name = "async-trait" version = "0.1.72" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -124,12 +115,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] -name = "event-listener" -version = "2.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" - -[[package]] name = "float-cmp" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -515,7 +500,6 @@ version = "0.5.1" dependencies = [ "ahash", "anyhow", - "async-lock", "async-trait", "linkme", "mockall", @@ -18,7 +18,7 @@ rustdoc-args = ["--cfg", "doc_cfg", "--html-in-header", "docs-style.html"] default = ["prevent-circular"] factory = ["syrette_macros/factory"] prevent-circular = ["syrette_macros/prevent-circular"] -async = ["dep:async-trait", "dep:async-lock", "syrette_macros/async"] +async = ["dep:async-trait", "syrette_macros/async"] [[example]] name = "factory" @@ -46,7 +46,6 @@ strum = "0.24.1" strum_macros = "0.24.3" paste = "1.0.8" async-trait = { version = "0.1.57", optional = true } -async-lock = { version = "2.6.0", optional = true } [dev_dependencies] mockall = "0.11.4" diff --git a/examples/async-factory/main.rs b/examples/async-factory/main.rs index f0365a1..83f79f0 100644 --- a/examples/async-factory/main.rs +++ b/examples/async-factory/main.rs @@ -75,18 +75,15 @@ async fn main() -> Result<()> { let mut di_container = AsyncDIContainer::new(); - di_container - .bind::<IFooFactory>() - .to_async_factory(&|_| { - Box::new(|cnt| { - Box::pin(async move { - let foo_ptr = Box::new(Foo::new(cnt)); + di_container.bind::<IFooFactory>().to_async_factory(&|_| { + Box::new(|cnt| { + Box::pin(async move { + let foo_ptr = Box::new(Foo::new(cnt)); - foo_ptr as Box<dyn IFoo> - }) + foo_ptr as Box<dyn IFoo> }) }) - .await?; + })?; di_container .bind::<dyn IPerson>() @@ -101,8 +98,7 @@ async fn main() -> Result<()> person as TransientPtr<dyn IPerson> }) }) - }) - .await?; + })?; let foo_factory = di_container .get::<IFooFactory>() diff --git a/examples/async/bootstrap.rs b/examples/async/bootstrap.rs index d26876a..6fbe831 100644 --- a/examples/async/bootstrap.rs +++ b/examples/async/bootstrap.rs @@ -18,34 +18,27 @@ pub async fn bootstrap() -> Result<AsyncDIContainer, anyhow::Error> di_container .bind::<dyn IDog>() - .to::<Dog>() - .await? + .to::<Dog>()? .in_singleton_scope() .await?; - di_container - .bind::<dyn ICat>() - .to_default_factory(&|_| { - Box::new(|| { - let cat: TransientPtr<dyn ICat> = TransientPtr::new(Cat::new()); + di_container.bind::<dyn ICat>().to_default_factory(&|_| { + Box::new(|| { + let cat: TransientPtr<dyn ICat> = TransientPtr::new(Cat::new()); - cat - }) + cat }) - .await?; + })?; - di_container.bind::<dyn IHuman>().to::<Human>().await?; + di_container.bind::<dyn IHuman>().to::<Human>()?; - di_container - .bind::<IFoodFactory>() - .to_factory(&|_| { - Box::new(|| { - let food: Box<dyn IFood> = Box::new(Food::new()); + di_container.bind::<IFoodFactory>().to_factory(&|_| { + Box::new(|| { + let food: Box<dyn IFood> = Box::new(Food::new()); - food - }) + food }) - .await?; + })?; Ok(di_container) } 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<Interface>, @@ -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::<dyn Foo>().to::<Bar>().await?; + /// di_container.bind::<dyn Foo>().to::<Bar>()?; /// # /// # Ok(()) /// # } /// ``` - pub async fn to<Implementation>( + pub fn to<Implementation>( self, ) -> Result< AsyncBindingScopeConfigurator<'di_container, Interface, Implementation>, @@ -99,7 +99,6 @@ where if self .di_container .has_binding::<Interface>(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::<FooFactory>() - /// .to_factory(&|_| { - /// Box::new(|num, some_str| { - /// let bar = TransientPtr::new(Bar { num, some_str }); + /// di_container.bind::<FooFactory>().to_factory(&|_| { + /// Box::new(|num, some_str| { + /// let bar = TransientPtr::new(Bar { num, some_str }); /// - /// bar as TransientPtr<dyn Foo> - /// }) + /// bar as TransientPtr<dyn Foo> /// }) - /// .await?; + /// })?; /// # /// # Ok(()) /// # } /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub async fn to_factory<Args, Return, FactoryFunc>( + pub fn to_factory<Args, Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, ) -> Result< @@ -185,7 +181,6 @@ where if self .di_container .has_binding::<Interface>(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::<Interface>( - BindingOptions::new(), - Box::new(crate::provider::r#async::AsyncFactoryProvider::new( - crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), - AsyncFactoryVariant::Normal, - )), - ) - .await; + self.di_container.set_binding::<Interface>( + 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::<FooFactory>() - /// .to_async_factory(&|_| { - /// Box::new(|num, some_str| { - /// Box::pin(async move { - /// let bar = TransientPtr::new(Bar { num, some_str }); + /// di_container.bind::<FooFactory>().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<dyn Foo> - /// }) + /// bar as TransientPtr<dyn Foo> /// }) /// }) - /// .await?; + /// })?; /// # /// # Ok(()) /// # } /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub async fn to_async_factory<Args, Return, FactoryFunc>( + pub fn to_async_factory<Args, Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, ) -> Result< @@ -290,7 +280,6 @@ where if self .di_container .has_binding::<Interface>(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::<Interface>( - BindingOptions::new(), - Box::new(crate::provider::r#async::AsyncFactoryProvider::new( - crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), - AsyncFactoryVariant::Normal, - )), - ) - .await; + self.di_container.set_binding::<Interface>( + 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::<dyn Foo>() - /// .to_default_factory(&|_| { - /// Box::new(|| { - /// let bar = TransientPtr::new(Bar { - /// num: 42, - /// some_str: "hello".to_string(), - /// }); + /// di_container.bind::<dyn Foo>().to_default_factory(&|_| { + /// Box::new(|| { + /// let bar = TransientPtr::new(Bar { + /// num: 42, + /// some_str: "hello".to_string(), + /// }); /// - /// bar as TransientPtr<dyn Foo> - /// }) + /// bar as TransientPtr<dyn Foo> /// }) - /// .await?; + /// })?; /// # /// # Ok(()) /// # } /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub async fn to_default_factory<Return, FactoryFunc>( + pub fn to_default_factory<Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, ) -> Result< @@ -380,7 +364,6 @@ where if self .di_container .has_binding::<Interface>(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::<Interface>( - BindingOptions::new(), - Box::new(crate::provider::r#async::AsyncFactoryProvider::new( - crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), - AsyncFactoryVariant::Default, - )), - ) - .await; + self.di_container.set_binding::<Interface>( + 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<dyn Foo> /// }) /// }) - /// }) - /// .await?; + /// })?; /// # /// # Ok(()) /// # } /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub async fn to_async_default_factory<Return, FactoryFunc>( + pub fn to_async_default_factory<Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, ) -> Result< @@ -475,7 +455,6 @@ where if self .di_container .has_binding::<Interface>(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::<Interface>( - BindingOptions::new(), - Box::new(crate::provider::r#async::AsyncFactoryProvider::new( - crate::ptr::ThreadsafeFactoryPtr::new(factory_impl), - AsyncFactoryVariant::AsyncDefault, - )), - ) - .await; + self.di_container.set_binding::<Interface>( + 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::<dyn subjects_async::IUserManager>::new( - &di_container_mock, + &mut di_container_mock, MockDependencyHistory::new, ); - binding_builder - .to::<subjects_async::UserManager>() - .await - .unwrap(); + binding_builder.to::<subjects_async::UserManager>().unwrap(); } #[tokio::test] @@ -570,7 +544,7 @@ mod tests .once(); let binding_builder = AsyncBindingBuilder::<IUserManagerFactory>::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::<IUserManagerFactory>::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::<dyn subjects_async::IUserManager>::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::<dyn subjects_async::IUserManager>::new( - &di_container_mock, + &mut di_container_mock, MockDependencyHistory::new, ); @@ -720,7 +691,6 @@ mod tests user_manager }) }) - .await .unwrap(); } } diff --git a/src/di_container/asynchronous/binding/scope_configurator.rs b/src/di_container/asynchronous/binding/scope_configurator.rs index b63e644..6f72cf0 100644 --- a/src/di_container/asynchronous/binding/scope_configurator.rs +++ b/src/di_container/asynchronous/binding/scope_configurator.rs @@ -18,7 +18,7 @@ where Interface: 'static + ?Sized + Send + Sync, Implementation: AsyncInjectable<AsyncDIContainer>, { - di_container: &'di_container AsyncDIContainer, + di_container: &'di_container mut AsyncDIContainer, dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData<Interface>, @@ -32,7 +32,7 @@ where Implementation: AsyncInjectable<AsyncDIContainer>, { pub(crate) fn new( - di_container: &'di_container AsyncDIContainer, + di_container: &'di_container mut AsyncDIContainer, dependency_history_factory: fn() -> DependencyHistory, ) -> Self { @@ -69,19 +69,18 @@ where /// /// di_container /// .bind::<Authenticator>() - /// .to::<Authenticator>() - /// .await? - /// .in_transient_scope() - /// .await; + /// .to::<Authenticator>()? + /// .in_transient_scope(); /// # /// # Ok(()) /// # } /// ``` - pub async fn in_transient_scope( - self, + #[allow(clippy::must_use_candidate)] + pub fn in_transient_scope( + mut self, ) -> AsyncBindingWhenConfigurator<'di_container, Interface> { - self.set_in_transient_scope().await; + self.set_in_transient_scope(); AsyncBindingWhenConfigurator::new(self.di_container) } @@ -127,8 +126,7 @@ where /// /// di_container /// .bind::<AudioManager>() - /// .to::<AudioManager>() - /// .await? + /// .to::<AudioManager>()? /// .in_singleton_scope() /// .await; /// @@ -168,26 +166,22 @@ where .map_err(AsyncBindingScopeConfiguratorError::SingletonResolveFailed)?, ); - self.di_container - .set_binding::<Interface>( - BindingOptions::new(), - Box::new(AsyncSingletonProvider::new(singleton)), - ) - .await; + self.di_container.set_binding::<Interface>( + BindingOptions::new(), + Box::new(AsyncSingletonProvider::new(singleton)), + ); Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } - pub(crate) async fn set_in_transient_scope(&self) + pub(crate) fn set_in_transient_scope(&mut self) { - self.di_container - .set_binding::<Interface>( - BindingOptions::new(), - Box::new( - AsyncTransientTypeProvider::<Implementation, AsyncDIContainer>::new(), - ), - ) - .await; + self.di_container.set_binding::<Interface>( + BindingOptions::new(), + Box::new( + AsyncTransientTypeProvider::<Implementation, AsyncDIContainer>::new(), + ), + ); } } @@ -214,9 +208,9 @@ mod tests AsyncBindingScopeConfigurator::< dyn subjects_async::IUserManager, subjects_async::UserManager, - >::new(&di_container_mock, MockDependencyHistory::new); + >::new(&mut di_container_mock, MockDependencyHistory::new); - binding_scope_configurator.in_transient_scope().await; + binding_scope_configurator.in_transient_scope(); } #[tokio::test] @@ -234,7 +228,7 @@ mod tests AsyncBindingScopeConfigurator::< dyn subjects_async::IUserManager, subjects_async::UserManager, - >::new(&di_container_mock, MockDependencyHistory::new); + >::new(&mut 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 bc8e97f..5cec278 100644 --- a/src/di_container/asynchronous/binding/when_configurator.rs +++ b/src/di_container/asynchronous/binding/when_configurator.rs @@ -13,7 +13,7 @@ pub struct AsyncBindingWhenConfigurator<'di_container, Interface> where Interface: 'static + ?Sized + Send + Sync, { - di_container: &'di_container AsyncDIContainer, + di_container: &'di_container mut AsyncDIContainer, interface_phantom: PhantomData<Interface>, } @@ -22,7 +22,7 @@ impl<'di_container, Interface> AsyncBindingWhenConfigurator<'di_container, Inter where Interface: 'static + ?Sized + Send + Sync, { - pub(crate) fn new(di_container: &'di_container AsyncDIContainer) -> Self + pub(crate) fn new(di_container: &'di_container mut AsyncDIContainer) -> Self { Self { di_container, @@ -56,17 +56,14 @@ where /// /// di_container /// .bind::<Kitten>() - /// .to::<Kitten>() - /// .await? + /// .to::<Kitten>()? /// .in_transient_scope() - /// .await - /// .when_named("Billy") - /// .await?; + /// .when_named("Billy")?; /// # /// # Ok(()) /// # } /// ``` - pub async fn when_named( + pub fn when_named( self, name: &'static str, ) -> Result<(), AsyncBindingWhenConfiguratorError> @@ -74,7 +71,6 @@ where let binding = self .di_container .remove_binding::<Interface>(BindingOptions::new()) - .await .map_or_else( || { Err(AsyncBindingWhenConfiguratorError::BindingNotFound( @@ -85,8 +81,7 @@ where )?; self.di_container - .set_binding::<Interface>(BindingOptions::new().name(name), binding) - .await; + .set_binding::<Interface>(BindingOptions::new().name(name), binding); Ok(()) } @@ -121,11 +116,8 @@ mod tests let binding_when_configurator = AsyncBindingWhenConfigurator::< dyn subjects_async::INumber, - >::new(&di_container_mock); + >::new(&mut di_container_mock); - assert!(binding_when_configurator - .when_named("awesome") - .await - .is_ok()); + assert!(binding_when_configurator.when_named("awesome").is_ok()); } } diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index 13d4df0..7acdf4e 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -39,8 +39,7 @@ //! //! di_container //! .bind::<dyn IDatabaseService>() -//! .to::<DatabaseService>() -//! .await?; +//! .to::<DatabaseService>()?; //! //! let database_service = di_container //! .get::<dyn IDatabaseService>() @@ -53,8 +52,6 @@ use std::any::type_name; use std::sync::Arc; -use async_lock::Mutex; - use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder; use crate::di_container::binding_storage::DIContainerBindingStorage; use crate::di_container::BindingOptions; @@ -74,7 +71,7 @@ pub mod binding; #[derive(Default)] pub struct AsyncDIContainer { - binding_storage: Mutex<DIContainerBindingStorage<dyn IAsyncProvider<Self>>>, + binding_storage: DIContainerBindingStorage<dyn IAsyncProvider<Self>>, } impl AsyncDIContainer @@ -84,7 +81,7 @@ impl AsyncDIContainer pub fn new() -> Self { Self { - binding_storage: Mutex::new(DIContainerBindingStorage::new()), + binding_storage: DIContainerBindingStorage::new(), } } } @@ -113,7 +110,7 @@ impl AsyncDIContainer /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// let mut di_container = AsyncDIContainer::new(); /// - /// di_container.bind::<DiskWriter>().to::<DiskWriter>().await?; + /// di_container.bind::<DiskWriter>().to::<DiskWriter>()?; /// # /// # Ok(()) /// # } @@ -157,10 +154,7 @@ impl AsyncDIContainer /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// let mut di_container = AsyncDIContainer::new(); /// - /// di_container - /// .bind::<DeviceManager>() - /// .to::<DeviceManager>() - /// .await?; + /// di_container.bind::<DeviceManager>().to::<DeviceManager>()?; /// /// let device_manager = di_container.get::<DeviceManager>().await?.transient(); /// # @@ -206,12 +200,9 @@ impl AsyncDIContainer /// /// di_container /// .bind::<DeviceManager>() - /// .to::<DeviceManager>() - /// .await? + /// .to::<DeviceManager>()? /// .in_transient_scope() - /// .await - /// .when_named("usb") - /// .await; + /// .when_named("usb"); /// /// let device_manager = di_container /// .get_named::<DeviceManager>("usb") @@ -284,43 +275,32 @@ impl AsyncDIContainer self.handle_binding_providable(binding_providable).await } - async fn has_binding<Interface>( - &self, - binding_options: BindingOptions<'static>, - ) -> bool + fn has_binding<Interface>(&self, binding_options: BindingOptions<'static>) -> bool where Interface: ?Sized + 'static, { - self.binding_storage - .lock() - .await - .has::<Interface>(binding_options) + self.binding_storage.has::<Interface>(binding_options) } - async fn set_binding<Interface>( - &self, + fn set_binding<Interface>( + &mut self, binding_options: BindingOptions<'static>, provider: Box<dyn IAsyncProvider<Self>>, ) where Interface: 'static + ?Sized, { self.binding_storage - .lock() - .await .set::<Interface>(binding_options, provider); } - async fn remove_binding<Interface>( - &self, + fn remove_binding<Interface>( + &mut self, binding_options: BindingOptions<'static>, ) -> Option<Box<dyn IAsyncProvider<Self>>> where Interface: 'static + ?Sized, { - self.binding_storage - .lock() - .await - .remove::<Interface>(binding_options) + self.binding_storage.remove::<Interface>(binding_options) } } @@ -464,24 +444,19 @@ impl AsyncDIContainer where Interface: 'static + ?Sized + Send + Sync, { - let provider; - - { - let bindings_lock = self.binding_storage.lock().await; - - provider = bindings_lock - .get::<Interface>(binding_options.clone()) - .map_or_else( - || { - Err(AsyncDIContainerError::BindingNotFound { - interface: type_name::<Interface>(), - name: binding_options.name, - }) - }, - Ok, - )? - .clone(); - } + let provider = self + .binding_storage + .get::<Interface>(binding_options.clone()) + .map_or_else( + || { + Err(AsyncDIContainerError::BindingNotFound { + interface: type_name::<Interface>(), + name: binding_options.name, + }) + }, + Ok, + )? + .clone(); provider .provide(self, dependency_history) @@ -504,7 +479,7 @@ mod tests #[tokio::test] async fn can_get() { - let di_container = AsyncDIContainer::new(); + let mut di_container = AsyncDIContainer::new(); let mut mock_provider = MockAsyncProvider::new(); @@ -520,16 +495,12 @@ mod tests Box::new(inner_mock_provider) }); - { - di_container - .binding_storage - .lock() - .await - .set::<dyn subjects_async::IUserManager>( - BindingOptions::new(), - Box::new(mock_provider), - ); - } + di_container + .binding_storage + .set::<dyn subjects_async::IUserManager>( + BindingOptions::new(), + Box::new(mock_provider), + ); di_container .get::<dyn subjects_async::IUserManager>() @@ -542,7 +513,7 @@ mod tests #[tokio::test] async fn can_get_named() { - let di_container = AsyncDIContainer::new(); + let mut di_container = AsyncDIContainer::new(); let mut mock_provider = MockAsyncProvider::new(); @@ -558,16 +529,12 @@ mod tests Box::new(inner_mock_provider) }); - { - di_container - .binding_storage - .lock() - .await - .set::<dyn subjects_async::IUserManager>( - BindingOptions::new().name("special"), - Box::new(mock_provider), - ); - } + di_container + .binding_storage + .set::<dyn subjects_async::IUserManager>( + BindingOptions::new().name("special"), + Box::new(mock_provider), + ); di_container .get_named::<dyn subjects_async::IUserManager>("special") @@ -580,7 +547,7 @@ mod tests #[tokio::test] async fn can_get_singleton() { - let di_container = AsyncDIContainer::new(); + let mut di_container = AsyncDIContainer::new(); let mut mock_provider = MockAsyncProvider::new(); @@ -600,16 +567,12 @@ mod tests Box::new(inner_mock_provider) }); - { - di_container - .binding_storage - .lock() - .await - .set::<dyn subjects_async::INumber>( - BindingOptions::new(), - Box::new(mock_provider), - ); - } + di_container + .binding_storage + .set::<dyn subjects_async::INumber>( + BindingOptions::new(), + Box::new(mock_provider), + ); let first_number_rc = di_container .get::<dyn subjects_async::INumber>() @@ -633,7 +596,7 @@ mod tests #[tokio::test] async fn can_get_singleton_named() { - let di_container = AsyncDIContainer::new(); + let mut di_container = AsyncDIContainer::new(); let mut mock_provider = MockAsyncProvider::new(); @@ -653,16 +616,12 @@ mod tests Box::new(inner_mock_provider) }); - { - di_container - .binding_storage - .lock() - .await - .set::<dyn subjects_async::INumber>( - BindingOptions::new().name("cool"), - Box::new(mock_provider), - ); - } + di_container + .binding_storage + .set::<dyn subjects_async::INumber>( + BindingOptions::new().name("cool"), + Box::new(mock_provider), + ); let first_number_rc = di_container .get_named::<dyn subjects_async::INumber>("cool") @@ -730,7 +689,7 @@ mod tests type IUserManagerFactory = dyn Fn(Vec<i128>) -> TransientPtr<dyn IUserManager> + Send + Sync; - let di_container = AsyncDIContainer::new(); + let mut di_container = AsyncDIContainer::new(); let mut mock_provider = MockAsyncProvider::new(); @@ -757,8 +716,6 @@ mod tests di_container .binding_storage - .lock() - .await .set::<IUserManagerFactory>(BindingOptions::new(), Box::new(mock_provider)); di_container @@ -816,7 +773,7 @@ mod tests type IUserManagerFactory = dyn Fn(Vec<i128>) -> TransientPtr<dyn IUserManager> + Send + Sync; - let di_container = AsyncDIContainer::new(); + let mut di_container = AsyncDIContainer::new(); let mut mock_provider = MockAsyncProvider::new(); @@ -841,16 +798,10 @@ mod tests Box::new(inner_mock_provider) }); - { - di_container - .binding_storage - .lock() - .await - .set::<IUserManagerFactory>( - BindingOptions::new().name("special"), - Box::new(mock_provider), - ); - } + di_container.binding_storage.set::<IUserManagerFactory>( + BindingOptions::new().name("special"), + Box::new(mock_provider), + ); di_container .get_named::<IUserManagerFactory>("special") @@ -863,59 +814,43 @@ mod tests #[tokio::test] async fn has_binding_works() { - let di_container = AsyncDIContainer::new(); + let mut di_container = AsyncDIContainer::new(); // No binding is present yet assert!( - !di_container - .has_binding::<subjects_async::Number>(BindingOptions::new()) - .await + !di_container.has_binding::<subjects_async::Number>(BindingOptions::new()) ); - 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 + di_container.binding_storage.set::<subjects_async::Number>( + BindingOptions::new(), + Box::new(MockAsyncProvider::new()), ); + + assert!(di_container.has_binding::<subjects_async::Number>(BindingOptions::new())); } #[tokio::test] async fn set_binding_works() { - let di_container = AsyncDIContainer::new(); + let mut di_container = AsyncDIContainer::new(); - di_container - .set_binding::<subjects_async::UserManager>( - BindingOptions::new(), - Box::new(MockAsyncProvider::new()), - ) - .await; + di_container.set_binding::<subjects_async::UserManager>( + BindingOptions::new(), + Box::new(MockAsyncProvider::new()), + ); 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(); + let mut di_container = AsyncDIContainer::new(); di_container .binding_storage - .lock() - .await .set::<subjects_async::UserManager>( BindingOptions::new(), Box::new(MockAsyncProvider::new()), @@ -925,7 +860,6 @@ mod tests // Formatting is weird without this comment di_container .remove_binding::<subjects_async::UserManager>(BindingOptions::new()) - .await .is_some() ); @@ -933,8 +867,6 @@ mod tests // Formatting is weird without this comment !di_container .binding_storage - .lock() - .await .has::<subjects_async::UserManager>(BindingOptions::new()) ); } |