aboutsummaryrefslogtreecommitdiff
path: root/src/di_container/asynchronous/binding
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-04 11:10:36 +0200
committerHampusM <hampus@hampusmat.com>2023-10-04 11:10:36 +0200
commitb0a1af1e312f23eff7fe68ae17132ccded5cf31c (patch)
tree316f9177fcc7d3d73051750d9d87a6e965b6a26c /src/di_container/asynchronous/binding
parent613bf4a8a094df6f048cc8b1fcf2fc425abddd99 (diff)
refactor!: remove mutex in AsyncDIContainer
BREAKING CHANGE: Multiple async DI container binding builder & binding configurator functions are no longer async
Diffstat (limited to 'src/di_container/asynchronous/binding')
-rw-r--r--src/di_container/asynchronous/binding/builder.rs160
-rw-r--r--src/di_container/asynchronous/binding/scope_configurator.rs52
-rw-r--r--src/di_container/asynchronous/binding/when_configurator.rs24
3 files changed, 96 insertions, 140 deletions
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());
}
}