aboutsummaryrefslogtreecommitdiff
path: root/src/di_container/asynchronous/binding/builder.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/di_container/asynchronous/binding/builder.rs')
-rw-r--r--src/di_container/asynchronous/binding/builder.rs94
1 files changed, 44 insertions, 50 deletions
diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs
index 8465c9a..db1b576 100644
--- a/src/di_container/asynchronous/binding/builder.rs
+++ b/src/di_container/asynchronous/binding/builder.rs
@@ -1,13 +1,17 @@
//! Binding builder for types inside of a [`AsyncDIContainer`].
use std::any::type_name;
use std::marker::PhantomData;
+use std::sync::Arc;
+use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
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::BindingOptions;
use crate::errors::async_di_container::AsyncBindingBuilderError;
+use crate::future::BoxFuture;
use crate::interfaces::async_injectable::AsyncInjectable;
+use crate::provider::r#async::{AsyncFunctionProvider, ProvidableFunctionKind};
+use crate::ptr::TransientPtr;
use crate::util::use_double;
use_double!(crate::dependency_history::DependencyHistory);
@@ -173,8 +177,10 @@ where
Interface: Fn<Args, Output = Return> + Send + Sync,
FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn<Args, Return> + Send + Sync,
{
+ use std::sync::Arc;
+
use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::AsyncFactoryVariant;
+ use crate::provider::r#async::ProvidableFunctionKind;
if self
.di_container
@@ -190,9 +196,9 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::r#async::AsyncFactoryProvider::new(
- crate::ptr::ThreadsafeFactoryPtr::new(factory_impl),
- AsyncFactoryVariant::Normal,
+ Box::new(crate::provider::r#async::AsyncFunctionProvider::new(
+ Arc::new(factory_impl),
+ ProvidableFunctionKind::UserCalled,
)),
);
@@ -270,9 +276,6 @@ where
+ Send
+ Sync,
{
- use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::AsyncFactoryVariant;
-
if self
.di_container
.has_binding::<Interface>(BindingOptions::new())
@@ -287,17 +290,17 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::r#async::AsyncFactoryProvider::new(
- crate::ptr::ThreadsafeFactoryPtr::new(factory_impl),
- AsyncFactoryVariant::Normal,
+ Box::new(AsyncFunctionProvider::new(
+ Arc::new(factory_impl),
+ ProvidableFunctionKind::UserCalled,
)),
);
Ok(AsyncBindingWhenConfigurator::new(self.di_container))
}
- /// Creates a binding of type `Interface` to a factory that takes no arguments
- /// inside of the associated [`AsyncDIContainer`].
+ /// Creates a binding of type `Interface` to a value resolved using the given
+ /// function.
///
/// # Errors
/// Will return Err if the associated [`AsyncDIContainer`] already have a binding
@@ -325,7 +328,7 @@ where
/// # {
/// # let mut di_container = AsyncDIContainer::new();
/// #
- /// di_container.bind::<dyn Foo>().to_default_factory(&|_| {
+ /// di_container.bind::<dyn Foo>().to_dynamic_value(&|_| {
/// Box::new(|| {
/// let bar = TransientPtr::new(Bar {
/// num: 42,
@@ -339,24 +342,20 @@ where
/// # Ok(())
/// # }
/// ```
- #[cfg(feature = "factory")]
- #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
- pub fn to_default_factory<Return, FactoryFunc>(
+ pub fn to_dynamic_value<Func>(
self,
- factory_func: &'static FactoryFunc,
+ func: &'static Func,
) -> Result<
AsyncBindingWhenConfigurator<'di_container, Interface>,
AsyncBindingBuilderError,
>
where
- Return: 'static + ?Sized,
- FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn<(), crate::ptr::TransientPtr<Return>>
+ Func: Fn(
+ &AsyncDIContainer,
+ ) -> Box<dyn Fn() -> TransientPtr<Interface> + Send + Sync>
+ Send
+ Sync,
{
- use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::AsyncFactoryVariant;
-
if self
.di_container
.has_binding::<Interface>(BindingOptions::new())
@@ -367,21 +366,21 @@ where
)));
}
- let factory_impl = ThreadsafeCastableFunction::new(factory_func);
+ let castable_func = ThreadsafeCastableFunction::new(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,
+ Box::new(AsyncFunctionProvider::new(
+ Arc::new(castable_func),
+ ProvidableFunctionKind::Instant,
)),
);
Ok(AsyncBindingWhenConfigurator::new(self.di_container))
}
- /// Creates a binding of factory type `Interface` to a async factory inside of the
- /// associated [`AsyncDIContainer`].
+ /// Creates a binding of type `Interface` to a value resolved using the given
+ /// async function.
///
/// # Errors
/// Will return Err if the associated [`AsyncDIContainer`] already have a binding
@@ -412,7 +411,7 @@ where
/// #
/// di_container
/// .bind::<dyn Foo>()
- /// .to_async_default_factory(&|_| {
+ /// .to_async_dynamic_value(&|_| {
/// Box::new(|| {
/// Box::pin(async {
/// let bar = TransientPtr::new(Bar {
@@ -430,24 +429,21 @@ where
/// # Ok(())
/// # }
/// ```
- #[cfg(feature = "factory")]
- #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
- pub fn to_async_default_factory<Return, FactoryFunc>(
+ pub fn to_async_dynamic_value<Func>(
self,
- factory_func: &'static FactoryFunc,
+ func: &'static Func,
) -> Result<
AsyncBindingWhenConfigurator<'di_container, Interface>,
AsyncBindingBuilderError,
>
where
- Return: 'static + ?Sized,
- FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn<(), crate::future::BoxFuture<'static, Return>>
- + Send
+ Func: Fn(
+ &AsyncDIContainer,
+ ) -> Box<
+ dyn Fn() -> BoxFuture<'static, TransientPtr<Interface>> + Send + Sync,
+ > + Send
+ Sync,
{
- use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::AsyncFactoryVariant;
-
if self
.di_container
.has_binding::<Interface>(BindingOptions::new())
@@ -458,13 +454,13 @@ where
)));
}
- let factory_impl = ThreadsafeCastableFunction::new(factory_func);
+ let castable_func = ThreadsafeCastableFunction::new(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,
+ Box::new(AsyncFunctionProvider::new(
+ Arc::new(castable_func),
+ ProvidableFunctionKind::AsyncInstant,
)),
);
@@ -599,8 +595,7 @@ mod tests
}
#[tokio::test]
- #[cfg(feature = "factory")]
- async fn can_bind_to_default_factory()
+ async fn can_bind_to_dynamic_value()
{
use crate::ptr::TransientPtr;
@@ -625,7 +620,7 @@ mod tests
);
binding_builder
- .to_default_factory(&|_| {
+ .to_dynamic_value(&|_| {
Box::new(|| {
let user_manager: TransientPtr<dyn subjects_async::IUserManager> =
TransientPtr::new(subjects_async::UserManager::new());
@@ -637,8 +632,7 @@ mod tests
}
#[tokio::test]
- #[cfg(feature = "factory")]
- async fn can_bind_to_async_default_factory()
+ async fn can_bind_to_async_dynamic_value()
{
use crate::ptr::TransientPtr;
use crate::test_utils::async_closure;
@@ -664,7 +658,7 @@ mod tests
);
binding_builder
- .to_async_default_factory(&|_| {
+ .to_async_dynamic_value(&|_| {
async_closure!(|| {
let user_manager: TransientPtr<dyn subjects_async::IUserManager> =
TransientPtr::new(subjects_async::UserManager::new());