aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/di_container/asynchronous.rs33
-rw-r--r--src/di_container/asynchronous/binding/builder.rs50
-rw-r--r--src/di_container/blocking.rs21
-rw-r--r--src/di_container/blocking/binding/builder.rs37
-rw-r--r--src/lib.rs1
-rw-r--r--src/provider/async.rs60
-rw-r--r--src/provider/blocking.rs37
-rw-r--r--src/test_utils.rs4
8 files changed, 55 insertions, 188 deletions
diff --git a/src/di_container/asynchronous.rs b/src/di_container/asynchronous.rs
index a338c0a..6cb54f3 100644
--- a/src/di_container/asynchronous.rs
+++ b/src/di_container/asynchronous.rs
@@ -51,15 +51,17 @@
//! ```
use std::any::type_name;
+use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder;
use crate::di_container::binding_storage::DIContainerBindingStorage;
use crate::di_container::BindingOptions;
use crate::errors::async_di_container::AsyncDIContainerError;
+use crate::future::BoxFuture;
use crate::private::cast::arc::CastArc;
use crate::private::cast::boxed::CastBox;
use crate::private::cast::error::CastError;
-use crate::provider::r#async::{AsyncProvidable, IAsyncProvider};
-use crate::ptr::SomePtr;
+use crate::provider::r#async::{AsyncProvidable, IAsyncProvider, ProvidableFunctionKind};
+use crate::ptr::{SomePtr, TransientPtr};
use crate::util::use_double;
use_double!(crate::dependency_history::DependencyHistory);
@@ -347,12 +349,7 @@ impl AsyncDIContainer
))
}
#[cfg(feature = "factory")]
- AsyncProvidable::Function(
- func_bound,
- crate::provider::r#async::ProvidableFunctionKind::UserCalled,
- ) => {
- use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
-
+ AsyncProvidable::Function(func_bound, ProvidableFunctionKind::UserCalled) => {
let factory = func_bound
.as_any()
.downcast_ref::<ThreadsafeCastableFunction<Interface, Self>>()
@@ -363,14 +360,7 @@ impl AsyncDIContainer
Ok(SomePtr::ThreadsafeFactory(factory.call(self).into()))
}
- #[cfg(feature = "factory")]
- AsyncProvidable::Function(
- func_bound,
- crate::provider::r#async::ProvidableFunctionKind::Instant,
- ) => {
- use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::ptr::TransientPtr;
-
+ AsyncProvidable::Function(func_bound, ProvidableFunctionKind::Instant) => {
type Func<Interface> = ThreadsafeCastableFunction<
dyn Fn() -> TransientPtr<Interface> + Send + Sync,
AsyncDIContainer,
@@ -386,19 +376,12 @@ impl AsyncDIContainer
Ok(SomePtr::Transient(dynamic_val_func.call(self)()))
}
- #[cfg(feature = "factory")]
AsyncProvidable::Function(
func_bound,
- crate::provider::r#async::ProvidableFunctionKind::AsyncInstant,
+ ProvidableFunctionKind::AsyncInstant,
) => {
- use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::future::BoxFuture;
- use crate::ptr::TransientPtr;
-
type Func<Interface> = ThreadsafeCastableFunction<
- dyn Fn<(), Output = BoxFuture<'static, TransientPtr<Interface>>>
- + Send
- + Sync,
+ dyn Fn() -> BoxFuture<'static, TransientPtr<Interface>> + Send + Sync,
AsyncDIContainer,
>;
diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs
index 6f1281d..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);
@@ -272,11 +276,6 @@ where
+ Send
+ Sync,
{
- use std::sync::Arc;
-
- use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::ProvidableFunctionKind;
-
if self
.di_container
.has_binding::<Interface>(BindingOptions::new())
@@ -291,7 +290,7 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::r#async::AsyncFunctionProvider::new(
+ Box::new(AsyncFunctionProvider::new(
Arc::new(factory_impl),
ProvidableFunctionKind::UserCalled,
)),
@@ -343,9 +342,7 @@ where
/// # Ok(())
/// # }
/// ```
- #[cfg(feature = "factory")]
- #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
- pub fn to_dynamic_value<Return, Func>(
+ pub fn to_dynamic_value<Func>(
self,
func: &'static Func,
) -> Result<
@@ -353,16 +350,12 @@ where
AsyncBindingBuilderError,
>
where
- Return: 'static + ?Sized,
- Func: Fn(&AsyncDIContainer) -> BoxFn<(), crate::ptr::TransientPtr<Return>>
+ Func: Fn(
+ &AsyncDIContainer,
+ ) -> Box<dyn Fn() -> TransientPtr<Interface> + Send + Sync>
+ Send
+ Sync,
{
- use std::sync::Arc;
-
- use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::ProvidableFunctionKind;
-
if self
.di_container
.has_binding::<Interface>(BindingOptions::new())
@@ -377,7 +370,7 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::r#async::AsyncFunctionProvider::new(
+ Box::new(AsyncFunctionProvider::new(
Arc::new(castable_func),
ProvidableFunctionKind::Instant,
)),
@@ -436,9 +429,7 @@ where
/// # Ok(())
/// # }
/// ```
- #[cfg(feature = "factory")]
- #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
- pub fn to_async_dynamic_value<Return, Func>(
+ pub fn to_async_dynamic_value<Func>(
self,
func: &'static Func,
) -> Result<
@@ -446,16 +437,13 @@ where
AsyncBindingBuilderError,
>
where
- Return: 'static + ?Sized,
- Func: Fn(&AsyncDIContainer) -> BoxFn<(), crate::future::BoxFuture<'static, Return>>
- + Send
+ Func: Fn(
+ &AsyncDIContainer,
+ ) -> Box<
+ dyn Fn() -> BoxFuture<'static, TransientPtr<Interface>> + Send + Sync,
+ > + Send
+ Sync,
{
- use std::sync::Arc;
-
- use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::ProvidableFunctionKind;
-
if self
.di_container
.has_binding::<Interface>(BindingOptions::new())
@@ -470,7 +458,7 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::r#async::AsyncFunctionProvider::new(
+ Box::new(AsyncFunctionProvider::new(
Arc::new(castable_func),
ProvidableFunctionKind::AsyncInstant,
)),
@@ -607,7 +595,6 @@ mod tests
}
#[tokio::test]
- #[cfg(feature = "factory")]
async fn can_bind_to_dynamic_value()
{
use crate::ptr::TransientPtr;
@@ -645,7 +632,6 @@ mod tests
}
#[tokio::test]
- #[cfg(feature = "factory")]
async fn can_bind_to_async_dynamic_value()
{
use crate::ptr::TransientPtr;
diff --git a/src/di_container/blocking.rs b/src/di_container/blocking.rs
index 3d79ae7..0c464df 100644
--- a/src/di_container/blocking.rs
+++ b/src/di_container/blocking.rs
@@ -51,14 +51,15 @@
//! ```
use std::any::type_name;
+use crate::castable_function::CastableFunction;
use crate::di_container::binding_storage::DIContainerBindingStorage;
use crate::di_container::blocking::binding::builder::BindingBuilder;
use crate::di_container::BindingOptions;
use crate::errors::di_container::DIContainerError;
use crate::private::cast::boxed::CastBox;
use crate::private::cast::rc::CastRc;
-use crate::provider::blocking::{IProvider, Providable};
-use crate::ptr::SomePtr;
+use crate::provider::blocking::{IProvider, Providable, ProvidableFunctionKind};
+use crate::ptr::{SomePtr, TransientPtr};
use crate::util::use_double;
use_double!(crate::dependency_history::DependencyHistory);
@@ -284,12 +285,7 @@ impl DIContainer
})?,
)),
#[cfg(feature = "factory")]
- Providable::Function(
- func_bound,
- crate::provider::blocking::ProvidableFunctionKind::UserCalled,
- ) => {
- use crate::castable_function::CastableFunction;
-
+ Providable::Function(func_bound, ProvidableFunctionKind::UserCalled) => {
let factory = func_bound
.as_any()
.downcast_ref::<CastableFunction<Interface, Self>>()
@@ -300,14 +296,7 @@ impl DIContainer
Ok(SomePtr::Factory(factory.call(self).into()))
}
- #[cfg(feature = "factory")]
- Providable::Function(
- func_bound,
- crate::provider::blocking::ProvidableFunctionKind::Instant,
- ) => {
- use crate::castable_function::CastableFunction;
- use crate::ptr::TransientPtr;
-
+ Providable::Function(func_bound, ProvidableFunctionKind::Instant) => {
type Func<Interface> =
CastableFunction<dyn Fn() -> TransientPtr<Interface>, DIContainer>;
diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs
index 558db6e..596a2aa 100644
--- a/src/di_container/blocking/binding/builder.rs
+++ b/src/di_container/blocking/binding/builder.rs
@@ -1,13 +1,16 @@
//! Binding builder for types inside of a [`DIContainer`].
use std::any::type_name;
use std::marker::PhantomData;
+use std::rc::Rc;
+use crate::castable_function::CastableFunction;
use crate::di_container::blocking::binding::scope_configurator::BindingScopeConfigurator;
-#[cfg(feature = "factory")]
use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator;
use crate::di_container::BindingOptions;
use crate::errors::di_container::BindingBuilderError;
use crate::interfaces::injectable::Injectable;
+use crate::provider::blocking::{FunctionProvider, ProvidableFunctionKind};
+use crate::ptr::TransientPtr;
use crate::util::use_double;
use_double!(crate::dependency_history::DependencyHistory);
@@ -181,11 +184,6 @@ where
Interface: Fn<Args, Output = crate::ptr::TransientPtr<Return>>,
Func: Fn(&DIContainer) -> Box<Interface>,
{
- use std::rc::Rc;
-
- use crate::castable_function::CastableFunction;
- use crate::provider::blocking::ProvidableFunctionKind;
-
if self
.di_container
.has_binding::<Interface>(BindingOptions::new())
@@ -199,7 +197,7 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::blocking::FunctionProvider::new(
+ Box::new(FunctionProvider::new(
Rc::new(factory_impl),
ProvidableFunctionKind::UserCalled,
)),
@@ -248,35 +246,19 @@ where
/// # let mut di_container = DIContainer::new();
/// #
/// di_container.bind::<dyn IBuffer>().to_dynamic_value(&|_| {
- /// Box::new(|| {
- /// let buffer = TransientPtr::new(Buffer::<BUFFER_SIZE>::new());
- ///
- /// buffer as TransientPtr<dyn IBuffer>
- /// })
+ /// Box::new(|| TransientPtr::new(Buffer::<BUFFER_SIZE>::new()))
/// });
/// #
/// # Ok(())
/// # }
/// ```
- #[cfg(feature = "factory")]
- #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
- pub fn to_dynamic_value<Return, Func>(
+ pub fn to_dynamic_value<Func>(
self,
func: &'static Func,
) -> Result<BindingWhenConfigurator<'di_container, Interface>, BindingBuilderError>
where
- Return: 'static + ?Sized,
- Func: Fn(
- &DIContainer,
- ) -> crate::ptr::TransientPtr<
- dyn Fn<(), Output = crate::ptr::TransientPtr<Return>>,
- >,
+ Func: Fn(&DIContainer) -> TransientPtr<dyn Fn() -> TransientPtr<Interface>>,
{
- use std::rc::Rc;
-
- use crate::castable_function::CastableFunction;
- use crate::provider::blocking::ProvidableFunctionKind;
-
if self
.di_container
.has_binding::<Interface>(BindingOptions::new())
@@ -290,7 +272,7 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::blocking::FunctionProvider::new(
+ Box::new(FunctionProvider::new(
Rc::new(castable_func),
ProvidableFunctionKind::Instant,
)),
@@ -376,7 +358,6 @@ mod tests
}
#[test]
- #[cfg(feature = "factory")]
fn can_bind_to_dynamic_value()
{
use crate::ptr::TransientPtr;
diff --git a/src/lib.rs b/src/lib.rs
index d061be6..07c6d91 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -110,7 +110,6 @@ pub mod private;
mod provider;
mod util;
-#[cfg(feature = "factory")]
mod castable_function;
#[cfg(test)]
diff --git a/src/provider/async.rs b/src/provider/async.rs
index b011d7a..787ef06 100644
--- a/src/provider/async.rs
+++ b/src/provider/async.rs
@@ -1,7 +1,9 @@
use std::marker::PhantomData;
+use std::sync::Arc;
use async_trait::async_trait;
+use crate::castable_function::threadsafe::AnyThreadsafeCastableFunction;
use crate::errors::injectable::InjectableError;
use crate::interfaces::async_injectable::AsyncInjectable;
use crate::ptr::{ThreadsafeSingletonPtr, TransientPtr};
@@ -14,19 +16,16 @@ pub enum AsyncProvidable<DIContainerT>
{
Transient(TransientPtr<dyn AsyncInjectable<DIContainerT>>),
Singleton(ThreadsafeSingletonPtr<dyn AsyncInjectable<DIContainerT>>),
- #[cfg(feature = "factory")]
Function(
- std::sync::Arc<
- dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction,
- >,
+ Arc<dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction>,
ProvidableFunctionKind,
),
}
-#[cfg(feature = "factory")]
#[derive(Debug, Clone, Copy)]
pub enum ProvidableFunctionKind
{
+ #[cfg(feature = "factory")]
UserCalled,
Instant,
AsyncInstant,
@@ -174,22 +173,16 @@ where
}
}
-#[cfg(feature = "factory")]
pub struct AsyncFunctionProvider
{
- function: std::sync::Arc<
- dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction,
- >,
+ function: Arc<dyn AnyThreadsafeCastableFunction>,
providable_func_kind: ProvidableFunctionKind,
}
-#[cfg(feature = "factory")]
impl AsyncFunctionProvider
{
pub fn new(
- function: std::sync::Arc<
- dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction,
- >,
+ function: Arc<dyn AnyThreadsafeCastableFunction>,
providable_func_kind: ProvidableFunctionKind,
) -> Self
{
@@ -200,7 +193,6 @@ impl AsyncFunctionProvider
}
}
-#[cfg(feature = "factory")]
#[async_trait]
impl<DIContainerT> IAsyncProvider<DIContainerT> for AsyncFunctionProvider
where
@@ -224,7 +216,6 @@ where
}
}
-#[cfg(feature = "factory")]
impl Clone for AsyncFunctionProvider
{
fn clone(&self) -> Self
@@ -291,7 +282,6 @@ mod tests
}
#[tokio::test]
- #[cfg(feature = "factory")]
async fn function_provider_works()
{
use std::any::Any;
@@ -313,39 +303,15 @@ mod tests
impl AnyThreadsafeCastableFunction for FooFactory {}
- let user_called_func_provider = AsyncFunctionProvider::new(
- Arc::new(FooFactory),
- ProvidableFunctionKind::UserCalled,
- );
-
let instant_func_provider = AsyncFunctionProvider::new(
Arc::new(FooFactory),
ProvidableFunctionKind::Instant,
);
- let async_instant_func_provider = AsyncFunctionProvider::new(
- Arc::new(FooFactory),
- ProvidableFunctionKind::AsyncInstant,
- );
-
let di_container = MockAsyncDIContainer::new();
assert!(
matches!(
- user_called_func_provider
- .provide(&di_container, MockDependencyHistory::new())
- .await
- .unwrap(),
- AsyncProvidable::Function(_, ProvidableFunctionKind::UserCalled)
- ),
- concat!(
- "The provided type is not a AsyncProvidable::Function of kind ",
- "ProvidableFunctionKind::UserCalled"
- )
- );
-
- assert!(
- matches!(
instant_func_provider
.provide(&di_container, MockDependencyHistory::new())
.await
@@ -357,19 +323,5 @@ mod tests
"ProvidableFunctionKind::Instant"
)
);
-
- assert!(
- matches!(
- async_instant_func_provider
- .provide(&di_container, MockDependencyHistory::new())
- .await
- .unwrap(),
- AsyncProvidable::Function(_, ProvidableFunctionKind::AsyncInstant)
- ),
- concat!(
- "The provided type is not a AsyncProvidable::Function of kind ",
- "ProvidableFunctionKind::AsyncInstant"
- )
- );
}
}
diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs
index e7f113b..6b22ad0 100644
--- a/src/provider/blocking.rs
+++ b/src/provider/blocking.rs
@@ -1,5 +1,7 @@
use std::marker::PhantomData;
+use std::rc::Rc;
+use crate::castable_function::AnyCastableFunction;
use crate::errors::injectable::InjectableError;
use crate::interfaces::injectable::Injectable;
use crate::ptr::{SingletonPtr, TransientPtr};
@@ -12,19 +14,15 @@ pub enum Providable<DIContainerType>
{
Transient(TransientPtr<dyn Injectable<DIContainerType>>),
Singleton(SingletonPtr<dyn Injectable<DIContainerType>>),
- #[cfg(feature = "factory")]
- Function(
- std::rc::Rc<dyn crate::castable_function::AnyCastableFunction>,
- ProvidableFunctionKind,
- ),
+ Function(Rc<dyn AnyCastableFunction>, ProvidableFunctionKind),
}
-#[cfg(feature = "factory")]
#[derive(Debug, Clone, Copy)]
pub enum ProvidableFunctionKind
{
- Instant,
+ #[cfg(feature = "factory")]
UserCalled,
+ Instant,
}
#[cfg_attr(test, mockall::automock)]
@@ -114,18 +112,16 @@ where
}
}
-#[cfg(feature = "factory")]
pub struct FunctionProvider
{
- function: std::rc::Rc<dyn crate::castable_function::AnyCastableFunction>,
+ function: Rc<dyn AnyCastableFunction>,
providable_func_kind: ProvidableFunctionKind,
}
-#[cfg(feature = "factory")]
impl FunctionProvider
{
pub fn new(
- function: std::rc::Rc<dyn crate::castable_function::AnyCastableFunction>,
+ function: Rc<dyn AnyCastableFunction>,
providable_func_kind: ProvidableFunctionKind,
) -> Self
{
@@ -136,7 +132,6 @@ impl FunctionProvider
}
}
-#[cfg(feature = "factory")]
impl<DIContainerType> IProvider<DIContainerType> for FunctionProvider
{
fn provide(
@@ -201,7 +196,6 @@ mod tests
}
#[test]
- #[cfg(feature = "factory")]
fn function_provider_works()
{
use std::any::Any;
@@ -220,11 +214,6 @@ mod tests
}
}
- let user_called_func_provider = FunctionProvider::new(
- Rc::new(FooFactory),
- ProvidableFunctionKind::UserCalled,
- );
-
let instant_func_provider =
FunctionProvider::new(Rc::new(FooFactory), ProvidableFunctionKind::Instant);
@@ -232,18 +221,6 @@ mod tests
assert!(
matches!(
- user_called_func_provider
- .provide(&di_container, MockDependencyHistory::new()),
- Ok(Providable::Function(_, ProvidableFunctionKind::UserCalled))
- ),
- concat!(
- "The provided type is not a Providable::Function of kind ",
- "ProvidableFunctionKind::UserCalled"
- )
- );
-
- assert!(
- matches!(
instant_func_provider
.provide(&di_container, MockDependencyHistory::new()),
Ok(Providable::Function(_, ProvidableFunctionKind::Instant))
diff --git a/src/test_utils.rs b/src/test_utils.rs
index 491e9b4..176ffa9 100644
--- a/src/test_utils.rs
+++ b/src/test_utils.rs
@@ -301,7 +301,7 @@ pub mod mocks
}
}
-#[cfg(all(feature = "async", feature = "factory"))]
+#[cfg(all(feature = "async"))]
macro_rules! async_closure {
(|$($args: ident),*| { $($inner: stmt);* }) => {
Box::new(|$($args),*| {
@@ -315,5 +315,5 @@ macro_rules! async_closure {
};
}
-#[cfg(all(feature = "async", feature = "factory"))]
+#[cfg(all(feature = "async"))]
pub(crate) use async_closure;