aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-10-29 14:38:51 +0200
committerHampusM <hampus@hampusmat.com>2022-10-29 14:40:11 +0200
commitaa548ded39c7ba1927019c748c359523b21d59e8 (patch)
tree779d104f85009dd831e6af6e7a523258a1ab5be9 /src
parentda94fd3b7dd2265f10957d0f5276881beb057d82 (diff)
refactor!: add dependency history type
BREAKING CHANGE: Binding builders & configurators now take dependency history type arguments, the DetectedCircular variant of InjectableError now contains a dependency history field & the injectable traits take dependency history instead of a Vec
Diffstat (limited to 'src')
-rw-r--r--src/dependency_history.rs186
-rw-r--r--src/dependency_trace.rs18
-rw-r--r--src/di_container/asynchronous/binding/builder.rs102
-rw-r--r--src/di_container/asynchronous/binding/scope_configurator.rs75
-rw-r--r--src/di_container/asynchronous/binding/when_configurator.rs18
-rw-r--r--src/di_container/asynchronous/mod.rs182
-rw-r--r--src/di_container/blocking/binding/builder.rs69
-rw-r--r--src/di_container/blocking/binding/scope_configurator.rs69
-rw-r--r--src/di_container/blocking/binding/when_configurator.rs18
-rw-r--r--src/di_container/blocking/mod.rs146
-rw-r--r--src/errors/injectable.rs7
-rw-r--r--src/interfaces/async_injectable.rs14
-rw-r--r--src/interfaces/injectable.rs14
-rw-r--r--src/lib.rs4
-rw-r--r--src/provider/async.rs161
-rw-r--r--src/provider/blocking.rs124
-rw-r--r--src/test_utils.rs179
17 files changed, 879 insertions, 507 deletions
diff --git a/src/dependency_history.rs b/src/dependency_history.rs
new file mode 100644
index 0000000..4e36a7b
--- /dev/null
+++ b/src/dependency_history.rs
@@ -0,0 +1,186 @@
+//! Dependency history.
+
+use std::any::type_name;
+use std::collections::HashSet;
+use std::fmt::{Debug, Display};
+
+const BOLD_MODE: &str = "\x1b[1m";
+const RESET_BOLD_MODE: &str = "\x1b[22m";
+
+/// Dependency history interface.
+///
+/// **This trait is sealed and cannot be implemented for types outside this crate.**
+pub trait IDependencyHistory: private::Sealed
+{
+ #[doc(hidden)]
+ fn push<Dependency: 'static + ?Sized>(&mut self);
+
+ #[doc(hidden)]
+ fn contains<Dependency: 'static + ?Sized>(&self) -> bool;
+}
+
+/// Dependency history.
+#[derive(Clone, Debug)]
+pub struct DependencyHistory
+{
+ inner: Vec<&'static str>,
+}
+
+impl DependencyHistory
+{
+ #[must_use]
+ pub(crate) fn new() -> Self
+ {
+ Self { inner: vec![] }
+ }
+}
+
+impl IDependencyHistory for DependencyHistory
+{
+ #[doc(hidden)]
+ fn push<Dependency: 'static + ?Sized>(&mut self)
+ {
+ self.inner.push(type_name::<Dependency>());
+ }
+
+ #[doc(hidden)]
+ fn contains<Dependency: 'static + ?Sized>(&self) -> bool
+ {
+ self.inner.contains(&type_name::<Dependency>())
+ }
+}
+
+impl Display for DependencyHistory
+{
+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
+ {
+ let mut found_items = HashSet::new();
+
+ let opt_dupe_item = self.inner.iter().find(|item| {
+ if found_items.contains(item) {
+ return true;
+ }
+
+ found_items.insert(*item);
+
+ false
+ });
+
+ for (index, item) in self.inner.iter().enumerate() {
+ let mut item_is_dupe = false;
+
+ if let Some(dupe_item) = opt_dupe_item {
+ if *item == *dupe_item {
+ formatter
+ .write_fmt(format_args!("{BOLD_MODE}{item}{RESET_BOLD_MODE}"))?;
+
+ item_is_dupe = true;
+ }
+ }
+
+ if !item_is_dupe {
+ formatter.write_str(item)?;
+ }
+
+ if index != self.inner.len() - 1 {
+ formatter.write_str(" -> ")?;
+ }
+ }
+
+ if opt_dupe_item.is_some() {
+ formatter.write_str(" -> ...")?;
+ }
+
+ Ok(())
+ }
+}
+
+impl Default for DependencyHistory
+{
+ fn default() -> Self
+ {
+ Self::new()
+ }
+}
+
+impl private::Sealed for DependencyHistory {}
+
+pub(crate) mod private
+{
+ pub trait Sealed {}
+}
+
+#[cfg(test)]
+mod tests
+{
+ use super::*;
+ use crate::test_utils::subjects;
+
+ #[test]
+ fn can_push()
+ {
+ let mut dependency_history = DependencyHistory::new();
+
+ dependency_history.push::<dyn subjects::INumber>();
+
+ assert!(dependency_history
+ .inner
+ .contains(&type_name::<dyn subjects::INumber>()));
+ }
+
+ #[test]
+ fn contains_works()
+ {
+ let mut dependency_history = DependencyHistory::new();
+
+ dependency_history
+ .inner
+ .push(type_name::<dyn subjects::IUserManager>());
+
+ assert!(dependency_history.contains::<dyn subjects::IUserManager>());
+
+ assert!(!dependency_history.contains::<dyn subjects::INumber>());
+ }
+
+ #[test]
+ fn display_works()
+ {
+ trait Ninja {}
+ trait Katana {}
+ trait Blade {}
+
+ let mut dependency_history = DependencyHistory::new();
+
+ dependency_history.inner.push(type_name::<dyn Ninja>());
+ dependency_history.inner.push(type_name::<dyn Katana>());
+ dependency_history.inner.push(type_name::<dyn Blade>());
+
+ assert_eq!(
+ dependency_history.to_string(),
+ format!(
+ "{} -> {} -> {}",
+ type_name::<dyn Ninja>(),
+ type_name::<dyn Katana>(),
+ type_name::<dyn Blade>()
+ )
+ );
+
+ dependency_history.inner.push(type_name::<dyn Katana>());
+
+ assert_eq!(
+ dependency_history.to_string(),
+ format!(
+ concat!(
+ "{} -> {bold_mode}{}{reset_bold_mode} -> {} -> ",
+ "{bold_mode}{}{reset_bold_mode} -> ...",
+ ),
+ type_name::<dyn Ninja>(),
+ type_name::<dyn Katana>(),
+ type_name::<dyn Blade>(),
+ type_name::<dyn Katana>(),
+ bold_mode = BOLD_MODE,
+ reset_bold_mode = RESET_BOLD_MODE
+ )
+ );
+ }
+}
diff --git a/src/dependency_trace.rs b/src/dependency_trace.rs
deleted file mode 100644
index 86906f4..0000000
--- a/src/dependency_trace.rs
+++ /dev/null
@@ -1,18 +0,0 @@
-#[must_use]
-pub fn create_dependency_trace(
- dependency_history: &[&'static str],
- err_dependency: &'static str,
-) -> String
-{
- dependency_history
- .iter()
- .map(|dep| {
- if dep == &err_dependency {
- format!("\x1b[1m{}\x1b[22m", dep)
- } else {
- (*dep).to_string()
- }
- })
- .collect::<Vec<_>>()
- .join(" -> ")
-}
diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs
index f334cd7..3d03562 100644
--- a/src/di_container/asynchronous/binding/builder.rs
+++ b/src/di_container/asynchronous/binding/builder.rs
@@ -5,6 +5,7 @@ use std::any::type_name;
use std::marker::PhantomData;
use std::sync::Arc;
+use crate::dependency_history::IDependencyHistory;
use crate::di_container::asynchronous::binding::scope_configurator::AsyncBindingScopeConfigurator;
#[cfg(feature = "factory")]
use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingWhenConfigurator;
@@ -20,24 +21,33 @@ pub type BoxFn<Args, Return> = Box<(dyn Fn<Args, Output = Return> + Send + Sync)
/// Binding builder for type `Interface` inside a [`IAsyncDIContainer`].
///
/// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer
-pub struct AsyncBindingBuilder<Interface, DIContainerType>
+pub struct AsyncBindingBuilder<Interface, DIContainerType, DependencyHistoryType>
where
Interface: 'static + ?Sized + Send + Sync,
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
di_container: Arc<DIContainerType>,
+ dependency_history_factory: fn() -> DependencyHistoryType,
+
interface_phantom: PhantomData<Interface>,
}
-impl<Interface, DIContainerType> AsyncBindingBuilder<Interface, DIContainerType>
+impl<Interface, DIContainerType, DependencyHistoryType>
+ AsyncBindingBuilder<Interface, DIContainerType, DependencyHistoryType>
where
Interface: 'static + ?Sized + Send + Sync,
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync + 'static,
{
- pub(crate) fn new(di_container: Arc<DIContainerType>) -> Self
+ pub(crate) fn new(
+ di_container: Arc<DIContainerType>,
+ dependency_history_factory: fn() -> DependencyHistoryType,
+ ) -> Self
{
Self {
di_container,
+ dependency_history_factory,
interface_phantom: PhantomData,
}
}
@@ -88,11 +98,16 @@ where
pub async fn to<Implementation>(
&self,
) -> Result<
- AsyncBindingScopeConfigurator<Interface, Implementation, DIContainerType>,
+ AsyncBindingScopeConfigurator<
+ Interface,
+ Implementation,
+ DIContainerType,
+ DependencyHistoryType,
+ >,
AsyncBindingBuilderError,
>
where
- Implementation: AsyncInjectable<DIContainerType>,
+ Implementation: AsyncInjectable<DIContainerType, DependencyHistoryType>,
{
if self.di_container.has_binding::<Interface>(None).await {
return Err(AsyncBindingBuilderError::BindingAlreadyExists(type_name::<
@@ -101,8 +116,10 @@ where
)));
}
- let binding_scope_configurator =
- AsyncBindingScopeConfigurator::new(self.di_container.clone());
+ let binding_scope_configurator = AsyncBindingScopeConfigurator::new(
+ self.di_container.clone(),
+ self.dependency_history_factory,
+ );
binding_scope_configurator.in_transient_scope().await;
@@ -164,7 +181,7 @@ where
&self,
factory_func: &'static FactoryFunc,
) -> Result<
- AsyncBindingWhenConfigurator<Interface, DIContainerType>,
+ AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>,
AsyncBindingBuilderError,
>
where
@@ -257,7 +274,7 @@ where
&self,
factory_func: &'static FactoryFunc,
) -> Result<
- AsyncBindingWhenConfigurator<Interface, DIContainerType>,
+ AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>,
AsyncBindingBuilderError,
>
where
@@ -350,7 +367,7 @@ where
&self,
factory_func: &'static FactoryFunc,
) -> Result<
- AsyncBindingWhenConfigurator<Interface, DIContainerType>,
+ AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>,
AsyncBindingBuilderError,
>
where
@@ -444,7 +461,7 @@ where
&self,
factory_func: &'static FactoryFunc,
) -> Result<
- AsyncBindingWhenConfigurator<Interface, DIContainerType>,
+ AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>,
AsyncBindingBuilderError,
>
where
@@ -489,13 +506,14 @@ mod tests
use mockall::predicate::eq;
use super::*;
- use crate::test_utils::mocks::async_di_container::MockAsyncDIContainer;
- use crate::test_utils::subjects_async;
+ use crate::test_utils::{mocks, subjects_async};
#[tokio::test]
async fn can_bind_to() -> Result<(), Box<dyn Error>>
{
- let mut di_container_mock = MockAsyncDIContainer::new();
+ let mut di_container_mock = mocks::async_di_container::MockAsyncDIContainer::<
+ mocks::MockDependencyHistory,
+ >::new();
di_container_mock
.expect_has_binding::<dyn subjects_async::IUserManager>()
@@ -511,8 +529,12 @@ mod tests
let binding_builder = AsyncBindingBuilder::<
dyn subjects_async::IUserManager,
- MockAsyncDIContainer,
- >::new(Arc::new(di_container_mock));
+ mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Arc::new(di_container_mock),
+ mocks::MockDependencyHistory::new,
+ );
binding_builder.to::<subjects_async::UserManager>().await?;
@@ -534,7 +556,8 @@ mod tests
subjects_async::Number,
) -> dyn subjects_async::IUserManager;
- let mut di_container_mock = MockAsyncDIContainer::new();
+ let mut di_container_mock =
+ mocks::async_di_container::MockAsyncDIContainer::new();
di_container_mock
.expect_has_binding::<IUserManagerFactory>()
@@ -550,8 +573,12 @@ mod tests
let binding_builder = AsyncBindingBuilder::<
IUserManagerFactory,
- MockAsyncDIContainer,
- >::new(Arc::new(di_container_mock));
+ mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Arc::new(di_container_mock),
+ mocks::MockDependencyHistory::new,
+ );
binding_builder
.to_factory(&|_| {
@@ -577,7 +604,8 @@ mod tests
#[factory(async = true)]
type IUserManagerFactory = dyn Fn(String) -> dyn subjects_async::IUserManager;
- let mut di_container_mock = MockAsyncDIContainer::new();
+ let mut di_container_mock =
+ mocks::async_di_container::MockAsyncDIContainer::new();
di_container_mock
.expect_has_binding::<IUserManagerFactory>()
@@ -593,8 +621,12 @@ mod tests
let binding_builder = AsyncBindingBuilder::<
IUserManagerFactory,
- MockAsyncDIContainer,
- >::new(Arc::new(di_container_mock));
+ mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Arc::new(di_container_mock),
+ mocks::MockDependencyHistory::new,
+ );
binding_builder
.to_async_factory(&|_| {
@@ -621,7 +653,8 @@ mod tests
declare_default_factory!(dyn subjects_async::IUserManager);
- let mut di_container_mock = MockAsyncDIContainer::new();
+ let mut di_container_mock =
+ mocks::async_di_container::MockAsyncDIContainer::new();
di_container_mock
.expect_has_binding::<dyn subjects_async::IUserManager>()
@@ -637,8 +670,12 @@ mod tests
let binding_builder = AsyncBindingBuilder::<
dyn subjects_async::IUserManager,
- MockAsyncDIContainer,
- >::new(Arc::new(di_container_mock));
+ mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Arc::new(di_container_mock),
+ mocks::MockDependencyHistory::new,
+ );
binding_builder
.to_default_factory(&|_| {
@@ -665,7 +702,8 @@ mod tests
declare_default_factory!(dyn subjects_async::IUserManager, async = true);
- let mut di_container_mock = MockAsyncDIContainer::new();
+ let mut di_container_mock =
+ mocks::async_di_container::MockAsyncDIContainer::new();
di_container_mock
.expect_has_binding::<dyn subjects_async::IUserManager>()
@@ -681,8 +719,12 @@ mod tests
let binding_builder = AsyncBindingBuilder::<
dyn subjects_async::IUserManager,
- MockAsyncDIContainer,
- >::new(Arc::new(di_container_mock));
+ mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Arc::new(di_container_mock),
+ mocks::MockDependencyHistory::new,
+ );
binding_builder
.to_async_default_factory(&|_| {
diff --git a/src/di_container/asynchronous/binding/scope_configurator.rs b/src/di_container/asynchronous/binding/scope_configurator.rs
index e2e916a..b5923ec 100644
--- a/src/di_container/asynchronous/binding/scope_configurator.rs
+++ b/src/di_container/asynchronous/binding/scope_configurator.rs
@@ -4,6 +4,7 @@
use std::marker::PhantomData;
use std::sync::Arc;
+use crate::dependency_history::IDependencyHistory;
use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingWhenConfigurator;
use crate::di_container::asynchronous::IAsyncDIContainer;
use crate::errors::async_di_container::AsyncBindingScopeConfiguratorError;
@@ -14,28 +15,45 @@ use crate::ptr::ThreadsafeSingletonPtr;
/// Scope configurator for a binding for type 'Interface' inside a [`IAsyncDIContainer`].
///
/// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer
-pub struct AsyncBindingScopeConfigurator<Interface, Implementation, DIContainerType>
-where
+pub struct AsyncBindingScopeConfigurator<
+ Interface,
+ Implementation,
+ DIContainerType,
+ DependencyHistoryType,
+> where
Interface: 'static + ?Sized + Send + Sync,
- Implementation: AsyncInjectable<DIContainerType>,
- DIContainerType: IAsyncDIContainer,
+ Implementation: AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
di_container: Arc<DIContainerType>,
+ dependency_history_factory: fn() -> DependencyHistoryType,
+
interface_phantom: PhantomData<Interface>,
implementation_phantom: PhantomData<Implementation>,
}
-impl<Interface, Implementation, DIContainerType>
- AsyncBindingScopeConfigurator<Interface, Implementation, DIContainerType>
+impl<Interface, Implementation, DIContainerType, DependencyHistoryType>
+ AsyncBindingScopeConfigurator<
+ Interface,
+ Implementation,
+ DIContainerType,
+ DependencyHistoryType,
+ >
where
Interface: 'static + ?Sized + Send + Sync,
- Implementation: AsyncInjectable<DIContainerType>,
- DIContainerType: IAsyncDIContainer,
+ Implementation: AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync + 'static,
{
- pub(crate) fn new(di_container: Arc<DIContainerType>) -> Self
+ pub(crate) fn new(
+ di_container: Arc<DIContainerType>,
+ dependency_history_factory: fn() -> DependencyHistoryType,
+ ) -> Self
{
Self {
di_container,
+ dependency_history_factory,
interface_phantom: PhantomData,
implementation_phantom: PhantomData,
}
@@ -46,14 +64,16 @@ where
/// This is the default.
pub async fn in_transient_scope(
&self,
- ) -> AsyncBindingWhenConfigurator<Interface, DIContainerType>
+ ) -> AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>
{
self.di_container
.set_binding::<Interface>(
None,
- Box::new(
- AsyncTransientTypeProvider::<Implementation, DIContainerType>::new(),
- ),
+ Box::new(AsyncTransientTypeProvider::<
+ Implementation,
+ DIContainerType,
+ DependencyHistoryType,
+ >::new()),
)
.await;
@@ -67,17 +87,18 @@ where
pub async fn in_singleton_scope(
&self,
) -> Result<
- AsyncBindingWhenConfigurator<Interface, DIContainerType>,
+ AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>,
AsyncBindingScopeConfiguratorError,
>
{
let singleton: ThreadsafeSingletonPtr<Implementation> =
ThreadsafeSingletonPtr::from(
- Implementation::resolve(&self.di_container, Vec::new())
- .await
- .map_err(
- AsyncBindingScopeConfiguratorError::SingletonResolveFailed,
- )?,
+ Implementation::resolve(
+ &self.di_container,
+ (self.dependency_history_factory)(),
+ )
+ .await
+ .map_err(AsyncBindingScopeConfiguratorError::SingletonResolveFailed)?,
);
self.di_container
@@ -112,8 +133,12 @@ mod tests
let binding_scope_configurator = AsyncBindingScopeConfigurator::<
dyn subjects_async::IUserManager,
subjects_async::UserManager,
- mocks::async_di_container::MockAsyncDIContainer,
- >::new(Arc::new(di_container_mock));
+ mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Arc::new(di_container_mock),
+ mocks::MockDependencyHistory::new,
+ );
binding_scope_configurator.in_transient_scope().await;
}
@@ -133,8 +158,12 @@ mod tests
let binding_scope_configurator = AsyncBindingScopeConfigurator::<
dyn subjects_async::IUserManager,
subjects_async::UserManager,
- mocks::async_di_container::MockAsyncDIContainer,
- >::new(Arc::new(di_container_mock));
+ mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Arc::new(di_container_mock),
+ mocks::MockDependencyHistory::new,
+ );
assert!(matches!(
binding_scope_configurator.in_singleton_scope().await,
diff --git a/src/di_container/asynchronous/binding/when_configurator.rs b/src/di_container/asynchronous/binding/when_configurator.rs
index 9a1505b..4d56347 100644
--- a/src/di_container/asynchronous/binding/when_configurator.rs
+++ b/src/di_container/asynchronous/binding/when_configurator.rs
@@ -5,31 +5,38 @@ use std::any::type_name;
use std::marker::PhantomData;
use std::sync::Arc;
+use crate::dependency_history::IDependencyHistory;
use crate::di_container::asynchronous::IAsyncDIContainer;
use crate::errors::async_di_container::AsyncBindingWhenConfiguratorError;
/// When configurator for a binding for type 'Interface' inside a [`IAsyncDIContainer`].
///
/// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer
-pub struct AsyncBindingWhenConfigurator<Interface, DIContainerType>
+pub struct AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>
where
Interface: 'static + ?Sized + Send + Sync,
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
di_container: Arc<DIContainerType>,
+
interface_phantom: PhantomData<Interface>,
+ dependency_history_phantom: PhantomData<DependencyHistoryType>,
}
-impl<Interface, DIContainerType> AsyncBindingWhenConfigurator<Interface, DIContainerType>
+impl<Interface, DIContainerType, DependencyHistoryType>
+ AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>
where
Interface: 'static + ?Sized + Send + Sync,
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
pub(crate) fn new(di_container: Arc<DIContainerType>) -> Self
{
Self {
di_container,
interface_phantom: PhantomData,
+ dependency_history_phantom: PhantomData,
}
}
@@ -92,7 +99,8 @@ mod tests
let binding_when_configurator = AsyncBindingWhenConfigurator::<
dyn subjects_async::INumber,
- mocks::async_di_container::MockAsyncDIContainer,
+ mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
>::new(Arc::new(di_container_mock));
assert!(matches!(
diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs
index 128fbbe..99a33be 100644
--- a/src/di_container/asynchronous/mod.rs
+++ b/src/di_container/asynchronous/mod.rs
@@ -57,6 +57,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use tokio::sync::Mutex;
+use crate::dependency_history::{DependencyHistory, IDependencyHistory};
use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder;
use crate::di_container::binding_storage::DIContainerBindingStorage;
use crate::errors::async_di_container::AsyncDIContainerError;
@@ -71,12 +72,16 @@ pub mod prelude;
/// Dependency injection container interface.
#[async_trait]
-pub trait IAsyncDIContainer:
- Sized + 'static + Send + Sync + details::DIContainerInternals
+pub trait IAsyncDIContainer<DependencyHistoryType>:
+ Sized + 'static + Send + Sync + details::DIContainerInternals<DependencyHistoryType>
+where
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
/// Returns a new [`AsyncBindingBuilder`] for the given interface.
#[must_use]
- fn bind<Interface>(self: &mut Arc<Self>) -> AsyncBindingBuilder<Interface, Self>
+ fn bind<Interface>(
+ self: &mut Arc<Self>,
+ ) -> AsyncBindingBuilder<Interface, Self, DependencyHistoryType>
where
Interface: 'static + ?Sized + Send + Sync;
@@ -114,7 +119,7 @@ pub trait IAsyncDIContainer:
#[doc(hidden)]
async fn get_bound<Interface>(
self: &Arc<Self>,
- dependency_history: Vec<&'static str>,
+ dependency_history: DependencyHistoryType,
name: Option<&'static str>,
) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>
where
@@ -124,7 +129,8 @@ pub trait IAsyncDIContainer:
/// Dependency injection container.
pub struct AsyncDIContainer
{
- binding_storage: Mutex<DIContainerBindingStorage<dyn IAsyncProvider<Self>>>,
+ binding_storage:
+ Mutex<DIContainerBindingStorage<dyn IAsyncProvider<Self, DependencyHistory>>>,
}
impl AsyncDIContainer
@@ -140,14 +146,16 @@ impl AsyncDIContainer
}
#[async_trait]
-impl IAsyncDIContainer for AsyncDIContainer
+impl IAsyncDIContainer<DependencyHistory> for AsyncDIContainer
{
#[must_use]
- fn bind<Interface>(self: &mut Arc<Self>) -> AsyncBindingBuilder<Interface, Self>
+ fn bind<Interface>(
+ self: &mut Arc<Self>,
+ ) -> AsyncBindingBuilder<Interface, Self, DependencyHistory>
where
Interface: 'static + ?Sized + Send + Sync,
{
- AsyncBindingBuilder::new(self.clone())
+ AsyncBindingBuilder::new(self.clone(), DependencyHistory::new)
}
fn get<'a, 'b, Interface>(
@@ -158,7 +166,10 @@ impl IAsyncDIContainer for AsyncDIContainer
'a: 'b,
Self: 'b,
{
- Box::pin(async { self.get_bound::<Interface>(Vec::new(), None).await })
+ Box::pin(async {
+ self.get_bound::<Interface>(DependencyHistory::new(), None)
+ .await
+ })
}
fn get_named<'a, 'b, Interface>(
@@ -170,12 +181,15 @@ impl IAsyncDIContainer for AsyncDIContainer
'a: 'b,
Self: 'b,
{
- Box::pin(async { self.get_bound::<Interface>(Vec::new(), Some(name)).await })
+ Box::pin(async {
+ self.get_bound::<Interface>(DependencyHistory::new(), Some(name))
+ .await
+ })
}
async fn get_bound<Interface>(
self: &Arc<Self>,
- dependency_history: Vec<&'static str>,
+ dependency_history: DependencyHistory,
name: Option<&'static str>,
) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>
where
@@ -190,7 +204,7 @@ impl IAsyncDIContainer for AsyncDIContainer
}
#[async_trait]
-impl details::DIContainerInternals for AsyncDIContainer
+impl details::DIContainerInternals<DependencyHistory> for AsyncDIContainer
{
async fn has_binding<Interface>(self: &Arc<Self>, name: Option<&'static str>) -> bool
where
@@ -202,7 +216,7 @@ impl details::DIContainerInternals for AsyncDIContainer
async fn set_binding<Interface>(
self: &Arc<Self>,
name: Option<&'static str>,
- provider: Box<dyn IAsyncProvider<Self>>,
+ provider: Box<dyn IAsyncProvider<Self, DependencyHistory>>,
) where
Interface: 'static + ?Sized,
{
@@ -215,7 +229,7 @@ impl details::DIContainerInternals for AsyncDIContainer
async fn remove_binding<Interface>(
self: &Arc<Self>,
name: Option<&'static str>,
- ) -> Option<Box<dyn IAsyncProvider<Self>>>
+ ) -> Option<Box<dyn IAsyncProvider<Self, DependencyHistory>>>
where
Interface: 'static + ?Sized,
{
@@ -227,7 +241,7 @@ impl AsyncDIContainer
{
async fn handle_binding_providable<Interface>(
self: &Arc<Self>,
- binding_providable: AsyncProvidable<Self>,
+ binding_providable: AsyncProvidable<Self, DependencyHistory>,
) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>
where
Interface: 'static + ?Sized + Send + Sync,
@@ -344,8 +358,8 @@ impl AsyncDIContainer
async fn get_binding_providable<Interface>(
self: &Arc<Self>,
name: Option<&'static str>,
- dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<Self>, AsyncDIContainerError>
+ dependency_history: DependencyHistory,
+ ) -> Result<AsyncProvidable<Self, DependencyHistory>, AsyncDIContainerError>
where
Interface: 'static + ?Sized + Send + Sync,
{
@@ -384,10 +398,13 @@ pub(crate) mod details
use async_trait::async_trait;
+ use crate::dependency_history::IDependencyHistory;
use crate::provider::r#async::IAsyncProvider;
#[async_trait]
- pub trait DIContainerInternals
+ pub trait DIContainerInternals<DependencyHistoryType>
+ where
+ DependencyHistoryType: IDependencyHistory,
{
async fn has_binding<Interface>(
self: &Arc<Self>,
@@ -399,14 +416,14 @@ pub(crate) mod details
async fn set_binding<Interface>(
self: &Arc<Self>,
name: Option<&'static str>,
- provider: Box<dyn IAsyncProvider<Self>>,
+ provider: Box<dyn IAsyncProvider<Self, DependencyHistoryType>>,
) where
Interface: 'static + ?Sized;
async fn remove_binding<Interface>(
self: &Arc<Self>,
name: Option<&'static str>,
- ) -> Option<Box<dyn IAsyncProvider<Self>>>
+ ) -> Option<Box<dyn IAsyncProvider<Self, DependencyHistoryType>>>
where
Interface: 'static + ?Sized;
}
@@ -417,39 +434,20 @@ mod tests
{
use std::error::Error;
- use async_trait::async_trait;
- use mockall::mock;
-
use super::*;
- use crate::errors::injectable::InjectableError;
use crate::ptr::{ThreadsafeSingletonPtr, TransientPtr};
+ use crate::test_utils::mocks::async_provider::MockAsyncProvider;
use crate::test_utils::subjects_async;
#[tokio::test]
async fn can_get() -> Result<(), Box<dyn Error>>
{
- mock! {
- Provider {}
-
- #[async_trait]
- impl IAsyncProvider<AsyncDIContainer> for Provider
- {
- async fn provide(
- &self,
- di_container: &Arc<AsyncDIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<AsyncDIContainer>, InjectableError>;
-
- fn do_clone(&self) -> Box<dyn IAsyncProvider<AsyncDIContainer>>;
- }
- }
-
let di_container = AsyncDIContainer::new();
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = MockAsyncProvider::new();
mock_provider.expect_do_clone().returning(|| {
- let mut inner_mock_provider = MockProvider::new();
+ let mut inner_mock_provider = MockAsyncProvider::new();
inner_mock_provider.expect_provide().returning(|_, _| {
Ok(AsyncProvidable::Transient(TransientPtr::new(
@@ -479,28 +477,12 @@ mod tests
#[tokio::test]
async fn can_get_named() -> Result<(), Box<dyn Error>>
{
- mock! {
- Provider {}
-
- #[async_trait]
- impl IAsyncProvider<AsyncDIContainer> for Provider
- {
- async fn provide(
- &self,
- di_container: &Arc<AsyncDIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<AsyncDIContainer>, InjectableError>;
-
- fn do_clone(&self) -> Box<dyn IAsyncProvider<AsyncDIContainer>>;
- }
- }
-
let di_container = AsyncDIContainer::new();
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = MockAsyncProvider::new();
mock_provider.expect_do_clone().returning(|| {
- let mut inner_mock_provider = MockProvider::new();
+ let mut inner_mock_provider = MockAsyncProvider::new();
inner_mock_provider.expect_provide().returning(|_, _| {
Ok(AsyncProvidable::Transient(TransientPtr::new(
@@ -533,32 +515,16 @@ mod tests
#[tokio::test]
async fn can_get_singleton() -> Result<(), Box<dyn Error>>
{
- mock! {
- Provider {}
-
- #[async_trait]
- impl IAsyncProvider<AsyncDIContainer> for Provider
- {
- async fn provide(
- &self,
- di_container: &Arc<AsyncDIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<AsyncDIContainer>, InjectableError>;
-
- fn do_clone(&self) -> Box<dyn IAsyncProvider<AsyncDIContainer>>;
- }
- }
-
let di_container = AsyncDIContainer::new();
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = MockAsyncProvider::new();
let mut singleton = ThreadsafeSingletonPtr::new(subjects_async::Number::new());
ThreadsafeSingletonPtr::get_mut(&mut singleton).unwrap().num = 2820;
mock_provider.expect_do_clone().returning(move || {
- let mut inner_mock_provider = MockProvider::new();
+ let mut inner_mock_provider = MockAsyncProvider::new();
let singleton_clone = singleton.clone();
@@ -597,32 +563,16 @@ mod tests
#[tokio::test]
async fn can_get_singleton_named() -> Result<(), Box<dyn Error>>
{
- mock! {
- Provider {}
-
- #[async_trait]
- impl IAsyncProvider<AsyncDIContainer> for Provider
- {
- async fn provide(
- &self,
- di_container: &Arc<AsyncDIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<AsyncDIContainer>, InjectableError>;
-
- fn do_clone(&self) -> Box<dyn IAsyncProvider<AsyncDIContainer>>;
- }
- }
-
let di_container = AsyncDIContainer::new();
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = MockAsyncProvider::new();
let mut singleton = ThreadsafeSingletonPtr::new(subjects_async::Number::new());
ThreadsafeSingletonPtr::get_mut(&mut singleton).unwrap().num = 2820;
mock_provider.expect_do_clone().returning(move || {
- let mut inner_mock_provider = MockProvider::new();
+ let mut inner_mock_provider = MockAsyncProvider::new();
let singleton_clone = singleton.clone();
@@ -707,32 +657,16 @@ mod tests
#[crate::factory(threadsafe = true)]
type IUserManagerFactory = dyn Fn(Vec<i128>) -> dyn IUserManager;
- mock! {
- Provider {}
-
- #[async_trait]
- impl IAsyncProvider<AsyncDIContainer> for Provider
- {
- async fn provide(
- &self,
- di_container: &Arc<AsyncDIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<AsyncDIContainer>, InjectableError>;
-
- fn do_clone(&self) -> Box<dyn IAsyncProvider<AsyncDIContainer>>;
- }
- }
-
let di_container = AsyncDIContainer::new();
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = MockAsyncProvider::new();
mock_provider.expect_do_clone().returning(|| {
type FactoryFunc = Box<
(dyn Fn<(Vec<i128>,), Output = TransientPtr<dyn IUserManager>> + Send + Sync)
>;
- let mut inner_mock_provider = MockProvider::new();
+ let mut inner_mock_provider = MockAsyncProvider::new();
let factory_func: &'static (dyn Fn<
(Arc<AsyncDIContainer>,),
@@ -818,32 +752,16 @@ mod tests
#[crate::factory(threadsafe = true)]
type IUserManagerFactory = dyn Fn(Vec<i128>) -> dyn IUserManager;
- mock! {
- Provider {}
-
- #[async_trait]
- impl IAsyncProvider<AsyncDIContainer> for Provider
- {
- async fn provide(
- &self,
- di_container: &Arc<AsyncDIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<AsyncDIContainer>, InjectableError>;
-
- fn do_clone(&self) -> Box<dyn IAsyncProvider<AsyncDIContainer>>;
- }
- }
-
let di_container = AsyncDIContainer::new();
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = MockAsyncProvider::new();
mock_provider.expect_do_clone().returning(|| {
type FactoryFunc = Box<
(dyn Fn<(Vec<i128>,), Output = TransientPtr<dyn IUserManager>> + Send + Sync)
>;
- let mut inner_mock_provider = MockProvider::new();
+ let mut inner_mock_provider = MockAsyncProvider::new();
let factory_func: &'static (dyn Fn<
(Arc<AsyncDIContainer>,),
diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs
index e1c1561..7aa1755 100644
--- a/src/di_container/blocking/binding/builder.rs
+++ b/src/di_container/blocking/binding/builder.rs
@@ -5,6 +5,7 @@ use std::any::type_name;
use std::marker::PhantomData;
use std::rc::Rc;
+use crate::dependency_history::IDependencyHistory;
use crate::di_container::blocking::binding::scope_configurator::BindingScopeConfigurator;
#[cfg(feature = "factory")]
use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator;
@@ -15,24 +16,33 @@ use crate::interfaces::injectable::Injectable;
/// Binding builder for type `Interface` inside a [`IDIContainer`].
///
/// [`IDIContainer`]: crate::di_container::blocking::IDIContainer
-pub struct BindingBuilder<Interface, DIContainerType>
+pub struct BindingBuilder<Interface, DIContainerType, DependencyHistoryType>
where
Interface: 'static + ?Sized,
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
di_container: Rc<DIContainerType>,
+ dependency_history_factory: fn() -> DependencyHistoryType,
+
interface_phantom: PhantomData<Interface>,
}
-impl<Interface, DIContainerType> BindingBuilder<Interface, DIContainerType>
+impl<Interface, DIContainerType, DependencyHistoryType>
+ BindingBuilder<Interface, DIContainerType, DependencyHistoryType>
where
Interface: 'static + ?Sized,
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + 'static,
{
- pub(crate) fn new(di_container: Rc<DIContainerType>) -> Self
+ pub(crate) fn new(
+ di_container: Rc<DIContainerType>,
+ dependency_history_factory: fn() -> DependencyHistoryType,
+ ) -> Self
{
Self {
di_container,
+ dependency_history_factory,
interface_phantom: PhantomData,
}
}
@@ -82,11 +92,16 @@ where
pub fn to<Implementation>(
&self,
) -> Result<
- BindingScopeConfigurator<Interface, Implementation, DIContainerType>,
+ BindingScopeConfigurator<
+ Interface,
+ Implementation,
+ DIContainerType,
+ DependencyHistoryType,
+ >,
BindingBuilderError,
>
where
- Implementation: Injectable<DIContainerType>,
+ Implementation: Injectable<DIContainerType, DependencyHistoryType>,
{
{
if self.di_container.has_binding::<Interface>(None) {
@@ -97,8 +112,10 @@ where
}
}
- let binding_scope_configurator =
- BindingScopeConfigurator::new(self.di_container.clone());
+ let binding_scope_configurator = BindingScopeConfigurator::new(
+ self.di_container.clone(),
+ self.dependency_history_factory,
+ );
binding_scope_configurator.in_transient_scope();
@@ -176,7 +193,10 @@ where
pub fn to_factory<Args, Return, Func>(
&self,
factory_func: &'static Func,
- ) -> Result<BindingWhenConfigurator<Interface, DIContainerType>, BindingBuilderError>
+ ) -> Result<
+ BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>,
+ BindingBuilderError,
+ >
where
Args: 'static,
Return: 'static + ?Sized,
@@ -262,7 +282,10 @@ where
pub fn to_default_factory<Return, FactoryFunc>(
&self,
factory_func: &'static FactoryFunc,
- ) -> Result<BindingWhenConfigurator<Interface, DIContainerType>, BindingBuilderError>
+ ) -> Result<
+ BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>,
+ BindingBuilderError,
+ >
where
Return: 'static + ?Sized,
FactoryFunc: Fn<
@@ -323,8 +346,12 @@ mod tests
let binding_builder = BindingBuilder::<
dyn subjects::INumber,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(mock_di_container));
+ mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Rc::new(mock_di_container),
+ mocks::MockDependencyHistory::new,
+ );
binding_builder.to::<subjects::Number>()?;
@@ -358,8 +385,12 @@ mod tests
let binding_builder = BindingBuilder::<
IUserManagerFactory,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(mock_di_container));
+ mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Rc::new(mock_di_container),
+ mocks::MockDependencyHistory::new,
+ );
binding_builder.to_factory(&|_| {
Box::new(move |_num, _text| {
@@ -400,8 +431,12 @@ mod tests
let binding_builder = BindingBuilder::<
dyn subjects::IUserManager,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(mock_di_container));
+ mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Rc::new(mock_di_container),
+ mocks::MockDependencyHistory::new,
+ );
binding_builder.to_default_factory(&|_| {
Box::new(move || {
diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs
index dc33cbc..6c6c32d 100644
--- a/src/di_container/blocking/binding/scope_configurator.rs
+++ b/src/di_container/blocking/binding/scope_configurator.rs
@@ -4,6 +4,7 @@
use std::marker::PhantomData;
use std::rc::Rc;
+use crate::dependency_history::IDependencyHistory;
use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator;
use crate::di_container::blocking::IDIContainer;
use crate::errors::di_container::BindingScopeConfiguratorError;
@@ -14,28 +15,45 @@ use crate::ptr::SingletonPtr;
/// Scope configurator for a binding for type 'Interface' inside a [`IDIContainer`].
///
/// [`IDIContainer`]: crate::di_container::blocking::IDIContainer
-pub struct BindingScopeConfigurator<Interface, Implementation, DIContainerType>
-where
+pub struct BindingScopeConfigurator<
+ Interface,
+ Implementation,
+ DIContainerType,
+ DependencyHistoryType,
+> where
Interface: 'static + ?Sized,
- Implementation: Injectable<DIContainerType>,
- DIContainerType: IDIContainer,
+ Implementation: Injectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
di_container: Rc<DIContainerType>,
+ dependency_history_factory: fn() -> DependencyHistoryType,
+
interface_phantom: PhantomData<Interface>,
implementation_phantom: PhantomData<Implementation>,
}
-impl<Interface, Implementation, DIContainerType>
- BindingScopeConfigurator<Interface, Implementation, DIContainerType>
+impl<Interface, Implementation, DIContainerType, DependencyHistoryType>
+ BindingScopeConfigurator<
+ Interface,
+ Implementation,
+ DIContainerType,
+ DependencyHistoryType,
+ >
where
Interface: 'static + ?Sized,
- Implementation: Injectable<DIContainerType>,
- DIContainerType: IDIContainer,
+ Implementation: Injectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + 'static,
{
- pub(crate) fn new(di_container: Rc<DIContainerType>) -> Self
+ pub(crate) fn new(
+ di_container: Rc<DIContainerType>,
+ dependency_history_factory: fn() -> DependencyHistoryType,
+ ) -> Self
{
Self {
di_container,
+ dependency_history_factory,
interface_phantom: PhantomData,
implementation_phantom: PhantomData,
}
@@ -47,11 +65,15 @@ where
#[allow(clippy::must_use_candidate)]
pub fn in_transient_scope(
&self,
- ) -> BindingWhenConfigurator<Interface, DIContainerType>
+ ) -> BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>
{
self.di_container.set_binding::<Interface>(
None,
- Box::new(TransientTypeProvider::<Implementation, DIContainerType>::new()),
+ Box::new(TransientTypeProvider::<
+ Implementation,
+ DIContainerType,
+ DependencyHistoryType,
+ >::new()),
);
BindingWhenConfigurator::new(self.di_container.clone())
@@ -64,13 +86,16 @@ where
pub fn in_singleton_scope(
&self,
) -> Result<
- BindingWhenConfigurator<Interface, DIContainerType>,
+ BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>,
BindingScopeConfiguratorError,
>
{
let singleton: SingletonPtr<Implementation> = SingletonPtr::from(
- Implementation::resolve(&self.di_container, Vec::new())
- .map_err(BindingScopeConfiguratorError::SingletonResolveFailed)?,
+ Implementation::resolve(
+ &self.di_container,
+ (self.dependency_history_factory)(),
+ )
+ .map_err(BindingScopeConfiguratorError::SingletonResolveFailed)?,
);
self.di_container
@@ -100,8 +125,12 @@ mod tests
let binding_scope_configurator = BindingScopeConfigurator::<
dyn subjects::IUserManager,
subjects::UserManager,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(di_container_mock));
+ mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Rc::new(di_container_mock),
+ mocks::MockDependencyHistory::new,
+ );
binding_scope_configurator.in_transient_scope();
}
@@ -120,8 +149,12 @@ mod tests
let binding_scope_configurator = BindingScopeConfigurator::<
dyn subjects::IUserManager,
subjects::UserManager,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(di_container_mock));
+ mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(
+ Rc::new(di_container_mock),
+ mocks::MockDependencyHistory::new,
+ );
assert!(matches!(
binding_scope_configurator.in_singleton_scope(),
diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs
index 49c9d9e..f93806b 100644
--- a/src/di_container/blocking/binding/when_configurator.rs
+++ b/src/di_container/blocking/binding/when_configurator.rs
@@ -5,31 +5,38 @@ use std::any::type_name;
use std::marker::PhantomData;
use std::rc::Rc;
+use crate::dependency_history::IDependencyHistory;
use crate::di_container::blocking::IDIContainer;
use crate::errors::di_container::BindingWhenConfiguratorError;
/// When configurator for a binding for type 'Interface' inside a [`IDIContainer`].
///
/// [`IDIContainer`]: crate::di_container::blocking::IDIContainer
-pub struct BindingWhenConfigurator<Interface, DIContainerType>
+pub struct BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>
where
Interface: 'static + ?Sized,
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
di_container: Rc<DIContainerType>,
+
interface_phantom: PhantomData<Interface>,
+ dependency_history_phantom: PhantomData<DependencyHistoryType>,
}
-impl<Interface, DIContainerType> BindingWhenConfigurator<Interface, DIContainerType>
+impl<Interface, DIContainerType, DependencyHistoryType>
+ BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>
where
Interface: 'static + ?Sized,
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
pub(crate) fn new(di_container: Rc<DIContainerType>) -> Self
{
Self {
di_container,
interface_phantom: PhantomData,
+ dependency_history_phantom: PhantomData,
}
}
@@ -90,7 +97,8 @@ mod tests
let binding_when_configurator = BindingWhenConfigurator::<
dyn subjects::INumber,
- mocks::blocking_di_container::MockDIContainer,
+ mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
>::new(Rc::new(di_container_mock));
assert!(matches!(
diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs
index bf77aba..6338118 100644
--- a/src/di_container/blocking/mod.rs
+++ b/src/di_container/blocking/mod.rs
@@ -54,6 +54,7 @@ use std::any::type_name;
use std::cell::RefCell;
use std::rc::Rc;
+use crate::dependency_history::{DependencyHistory, IDependencyHistory};
use crate::di_container::binding_storage::DIContainerBindingStorage;
use crate::di_container::blocking::binding::builder::BindingBuilder;
use crate::errors::di_container::DIContainerError;
@@ -65,10 +66,15 @@ pub mod binding;
pub mod prelude;
/// Blocking dependency injection container interface.
-pub trait IDIContainer: Sized + 'static + details::DIContainerInternals
+pub trait IDIContainer<DependencyHistoryType>:
+ Sized + 'static + details::DIContainerInternals<DependencyHistoryType>
+where
+ DependencyHistoryType: IDependencyHistory,
{
/// Returns a new [`BindingBuilder`] for the given interface.
- fn bind<Interface>(self: &mut Rc<Self>) -> BindingBuilder<Interface, Self>
+ fn bind<Interface>(
+ self: &mut Rc<Self>,
+ ) -> BindingBuilder<Interface, Self, DependencyHistoryType>
where
Interface: 'static + ?Sized;
@@ -100,7 +106,7 @@ pub trait IDIContainer: Sized + 'static + details::DIContainerInternals
#[doc(hidden)]
fn get_bound<Interface>(
self: &Rc<Self>,
- dependency_history: Vec<&'static str>,
+ dependency_history: DependencyHistoryType,
name: Option<&'static str>,
) -> Result<SomePtr<Interface>, DIContainerError>
where
@@ -110,7 +116,8 @@ pub trait IDIContainer: Sized + 'static + details::DIContainerInternals
/// Blocking dependency injection container.
pub struct DIContainer
{
- binding_storage: RefCell<DIContainerBindingStorage<dyn IProvider<Self>>>,
+ binding_storage:
+ RefCell<DIContainerBindingStorage<dyn IProvider<Self, DependencyHistory>>>,
}
impl DIContainer
@@ -125,21 +132,23 @@ impl DIContainer
}
}
-impl IDIContainer for DIContainer
+impl IDIContainer<DependencyHistory> for DIContainer
{
#[must_use]
- fn bind<Interface>(self: &mut Rc<Self>) -> BindingBuilder<Interface, Self>
+ fn bind<Interface>(
+ self: &mut Rc<Self>,
+ ) -> BindingBuilder<Interface, Self, DependencyHistory>
where
Interface: 'static + ?Sized,
{
- BindingBuilder::<Interface, Self>::new(self.clone())
+ BindingBuilder::new(self.clone(), DependencyHistory::new)
}
fn get<Interface>(self: &Rc<Self>) -> Result<SomePtr<Interface>, DIContainerError>
where
Interface: 'static + ?Sized,
{
- self.get_bound::<Interface>(Vec::new(), None)
+ self.get_bound::<Interface>(DependencyHistory::new(), None)
}
fn get_named<Interface>(
@@ -149,13 +158,13 @@ impl IDIContainer for DIContainer
where
Interface: 'static + ?Sized,
{
- self.get_bound::<Interface>(Vec::new(), Some(name))
+ self.get_bound::<Interface>(DependencyHistory::new(), Some(name))
}
#[doc(hidden)]
fn get_bound<Interface>(
self: &Rc<Self>,
- dependency_history: Vec<&'static str>,
+ dependency_history: DependencyHistory,
name: Option<&'static str>,
) -> Result<SomePtr<Interface>, DIContainerError>
where
@@ -168,7 +177,7 @@ impl IDIContainer for DIContainer
}
}
-impl details::DIContainerInternals for DIContainer
+impl details::DIContainerInternals<DependencyHistory> for DIContainer
{
fn has_binding<Interface>(self: &Rc<Self>, name: Option<&'static str>) -> bool
where
@@ -180,7 +189,7 @@ impl details::DIContainerInternals for DIContainer
fn set_binding<Interface>(
self: &Rc<Self>,
name: Option<&'static str>,
- provider: Box<dyn IProvider<Self>>,
+ provider: Box<dyn IProvider<Self, DependencyHistory>>,
) where
Interface: 'static + ?Sized,
{
@@ -192,7 +201,7 @@ impl details::DIContainerInternals for DIContainer
fn remove_binding<Interface>(
self: &Rc<Self>,
name: Option<&'static str>,
- ) -> Option<Box<dyn IProvider<Self>>>
+ ) -> Option<Box<dyn IProvider<Self, DependencyHistory>>>
where
Interface: 'static + ?Sized,
{
@@ -204,7 +213,7 @@ impl DIContainer
{
fn handle_binding_providable<Interface>(
self: &Rc<Self>,
- binding_providable: Providable<Self>,
+ binding_providable: Providable<Self, DependencyHistory>,
) -> Result<SomePtr<Interface>, DIContainerError>
where
Interface: 'static + ?Sized,
@@ -262,8 +271,8 @@ impl DIContainer
fn get_binding_providable<Interface>(
self: &Rc<Self>,
name: Option<&'static str>,
- dependency_history: Vec<&'static str>,
- ) -> Result<Providable<Self>, DIContainerError>
+ dependency_history: DependencyHistory,
+ ) -> Result<Providable<Self, DependencyHistory>, DIContainerError>
where
Interface: 'static + ?Sized,
{
@@ -291,9 +300,12 @@ pub(crate) mod details
{
use std::rc::Rc;
+ use crate::dependency_history::IDependencyHistory;
use crate::provider::blocking::IProvider;
- pub trait DIContainerInternals
+ pub trait DIContainerInternals<DependencyHistoryType>
+ where
+ DependencyHistoryType: IDependencyHistory,
{
fn has_binding<Interface>(self: &Rc<Self>, name: Option<&'static str>) -> bool
where
@@ -302,14 +314,14 @@ pub(crate) mod details
fn set_binding<Interface>(
self: &Rc<Self>,
name: Option<&'static str>,
- provider: Box<dyn IProvider<Self>>,
+ provider: Box<dyn IProvider<Self, DependencyHistoryType>>,
) where
Interface: 'static + ?Sized;
fn remove_binding<Interface>(
self: &Rc<Self>,
name: Option<&'static str>,
- ) -> Option<Box<dyn IProvider<Self>>>
+ ) -> Option<Box<dyn IProvider<Self, DependencyHistoryType>>>
where
Interface: 'static + ?Sized;
}
@@ -320,33 +332,16 @@ mod tests
{
use std::error::Error;
- use mockall::mock;
-
use super::*;
- use crate::errors::injectable::InjectableError;
- use crate::provider::blocking::IProvider;
use crate::ptr::{SingletonPtr, TransientPtr};
- use crate::test_utils::subjects;
+ use crate::test_utils::{mocks, subjects};
#[test]
fn can_get() -> Result<(), Box<dyn Error>>
{
- mock! {
- Provider {}
-
- impl IProvider<DIContainer> for Provider
- {
- fn provide(
- &self,
- di_container: &Rc<DIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<Providable<DIContainer>, InjectableError>;
- }
- }
-
let di_container = DIContainer::new();
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = mocks::blocking_provider::MockProvider::new();
mock_provider.expect_provide().returning(|_, _| {
Ok(Providable::Transient(TransientPtr::new(
@@ -369,22 +364,9 @@ mod tests
#[test]
fn can_get_named() -> Result<(), Box<dyn Error>>
{
- mock! {
- Provider {}
-
- impl IProvider<DIContainer> for Provider
- {
- fn provide(
- &self,
- di_container: &Rc<DIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<Providable<DIContainer>, InjectableError>;
- }
- }
-
let di_container = DIContainer::new();
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = mocks::blocking_provider::MockProvider::new();
mock_provider.expect_provide().returning(|_, _| {
Ok(Providable::Transient(TransientPtr::new(
@@ -407,22 +389,9 @@ mod tests
#[test]
fn can_get_singleton() -> Result<(), Box<dyn Error>>
{
- mock! {
- Provider {}
-
- impl IProvider<DIContainer> for Provider
- {
- fn provide(
- &self,
- di_container: &Rc<DIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<Providable<DIContainer>, InjectableError>;
- }
- }
-
let di_container = DIContainer::new();
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = mocks::blocking_provider::MockProvider::new();
let mut singleton = SingletonPtr::new(subjects::Number::new());
@@ -452,22 +421,9 @@ mod tests
#[test]
fn can_get_singleton_named() -> Result<(), Box<dyn Error>>
{
- mock! {
- Provider {}
-
- impl IProvider<DIContainer> for Provider
- {
- fn provide(
- &self,
- di_container: &Rc<DIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<Providable<DIContainer>, InjectableError>;
- }
- }
-
let di_container = DIContainer::new();
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = mocks::blocking_provider::MockProvider::new();
let mut singleton = SingletonPtr::new(subjects::Number::new());
@@ -552,19 +508,6 @@ mod tests
#[crate::factory]
type IUserManagerFactory = dyn Fn(Vec<i128>) -> dyn IUserManager;
- mock! {
- Provider {}
-
- impl IProvider<DIContainer> for Provider
- {
- fn provide(
- &self,
- di_container: &Rc<DIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<Providable<DIContainer>, InjectableError>;
- }
- }
-
let di_container = DIContainer::new();
let factory_func: &'static FactoryFunc = &|_: Rc<DIContainer>| {
@@ -576,7 +519,7 @@ mod tests
})
};
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = mocks::blocking_provider::MockProvider::new();
mock_provider.expect_provide().returning_st(|_, _| {
Ok(Providable::Factory(FactoryPtr::new(CastableFactory::new(
@@ -649,19 +592,6 @@ mod tests
#[crate::factory]
type IUserManagerFactory = dyn Fn(Vec<i128>) -> dyn IUserManager;
- mock! {
- Provider {}
-
- impl IProvider<DIContainer> for Provider
- {
- fn provide(
- &self,
- di_container: &Rc<DIContainer>,
- dependency_history: Vec<&'static str>,
- ) -> Result<Providable<DIContainer>, InjectableError>;
- }
- }
-
let di_container = DIContainer::new();
let factory_func: &'static FactoryFunc = &|_: Rc<DIContainer>| {
@@ -673,7 +603,7 @@ mod tests
})
};
- let mut mock_provider = MockProvider::new();
+ let mut mock_provider = mocks::blocking_provider::MockProvider::new();
mock_provider.expect_provide().returning_st(|_, _| {
Ok(Providable::Factory(FactoryPtr::new(CastableFactory::new(
diff --git a/src/errors/injectable.rs b/src/errors/injectable.rs
index df71f48..f132063 100644
--- a/src/errors/injectable.rs
+++ b/src/errors/injectable.rs
@@ -2,6 +2,7 @@
//!
//! [`Injectable`]: crate::interfaces::injectable::Injectable
+use crate::dependency_history::DependencyHistory;
use crate::errors::di_container::DIContainerError;
/// Error type for structs that implement [`Injectable`].
@@ -35,10 +36,10 @@ pub enum InjectableError
affected: &'static str,
},
/// Detected circular dependencies.
- #[error("Detected circular dependencies. {dependency_trace}")]
+ #[error("Detected circular dependencies. {dependency_history}")]
DetectedCircular
{
- /// A visual trace of dependencies.
- dependency_trace: String,
+ /// History of dependencies.
+ dependency_history: DependencyHistory,
},
}
diff --git a/src/interfaces/async_injectable.rs b/src/interfaces/async_injectable.rs
index fb7f8ba..4e614a3 100644
--- a/src/interfaces/async_injectable.rs
+++ b/src/interfaces/async_injectable.rs
@@ -2,6 +2,7 @@
use std::fmt::Debug;
use std::sync::Arc;
+use crate::dependency_history::IDependencyHistory;
use crate::di_container::asynchronous::IAsyncDIContainer;
use crate::errors::injectable::InjectableError;
use crate::future::BoxFuture;
@@ -9,9 +10,10 @@ use crate::libs::intertrait::CastFromSync;
use crate::ptr::TransientPtr;
/// Interface for structs that can be injected into or be injected to.
-pub trait AsyncInjectable<DIContainerType>: CastFromSync
+pub trait AsyncInjectable<DIContainerType, DependencyHistoryType>: CastFromSync
where
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
/// Resolves the dependencies of the injectable.
///
@@ -19,16 +21,18 @@ where
/// Will return `Err` if resolving the dependencies fails.
fn resolve<'di_container, 'fut>(
di_container: &'di_container Arc<DIContainerType>,
- dependency_history: Vec<&'static str>,
+ dependency_history: DependencyHistoryType,
) -> BoxFuture<'fut, Result<TransientPtr<Self>, InjectableError>>
where
Self: Sized + 'fut,
'di_container: 'fut;
}
-impl<DIContainerType> Debug for dyn AsyncInjectable<DIContainerType>
+impl<DIContainerType, DependencyHistoryType> Debug
+ for dyn AsyncInjectable<DIContainerType, DependencyHistoryType>
where
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
diff --git a/src/interfaces/injectable.rs b/src/interfaces/injectable.rs
index 132935d..6130d2b 100644
--- a/src/interfaces/injectable.rs
+++ b/src/interfaces/injectable.rs
@@ -2,15 +2,17 @@
use std::fmt::Debug;
use std::rc::Rc;
+use crate::dependency_history::IDependencyHistory;
use crate::di_container::blocking::IDIContainer;
use crate::errors::injectable::InjectableError;
use crate::libs::intertrait::CastFrom;
use crate::ptr::TransientPtr;
/// Interface for structs that can be injected into or be injected to.
-pub trait Injectable<DIContainerType>: CastFrom
+pub trait Injectable<DIContainerType, DependencyHistoryType>: CastFrom
where
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
/// Resolves the dependencies of the injectable.
///
@@ -18,15 +20,17 @@ where
/// Will return `Err` if resolving the dependencies fails.
fn resolve(
di_container: &Rc<DIContainerType>,
- dependency_history: Vec<&'static str>,
+ dependency_history: DependencyHistoryType,
) -> Result<TransientPtr<Self>, InjectableError>
where
Self: Sized;
}
-impl<DIContainerType> Debug for dyn Injectable<DIContainerType>
+impl<DIContainerType, DependencyHistoryType> Debug
+ for dyn Injectable<DIContainerType, DependencyHistoryType>
where
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
diff --git a/src/lib.rs b/src/lib.rs
index 6827768..a117ccc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -9,6 +9,7 @@
//!
//! Syrette is a collection of utilities useful for performing dependency injection.
+pub mod dependency_history;
pub mod di_container;
pub mod errors;
pub mod interfaces;
@@ -32,9 +33,6 @@ pub use syrette_macros::{declare_interface, injectable, named};
pub mod castable_factory;
#[doc(hidden)]
-pub mod dependency_trace;
-
-#[doc(hidden)]
pub mod libs;
// Private
diff --git a/src/provider/async.rs b/src/provider/async.rs
index 8d482cd..557617b 100644
--- a/src/provider/async.rs
+++ b/src/provider/async.rs
@@ -3,18 +3,24 @@ use std::sync::Arc;
use async_trait::async_trait;
+use crate::dependency_history::IDependencyHistory;
use crate::di_container::asynchronous::IAsyncDIContainer;
use crate::errors::injectable::InjectableError;
use crate::interfaces::async_injectable::AsyncInjectable;
use crate::ptr::{ThreadsafeSingletonPtr, TransientPtr};
#[derive(strum_macros::Display, Debug)]
-pub enum AsyncProvidable<DIContainerType>
+pub enum AsyncProvidable<DIContainerType, DependencyHistoryType>
where
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
- Transient(TransientPtr<dyn AsyncInjectable<DIContainerType>>),
- Singleton(ThreadsafeSingletonPtr<dyn AsyncInjectable<DIContainerType>>),
+ Transient(TransientPtr<dyn AsyncInjectable<DIContainerType, DependencyHistoryType>>),
+ Singleton(
+ ThreadsafeSingletonPtr<
+ dyn AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ >,
+ ),
#[cfg(feature = "factory")]
Factory(
crate::ptr::ThreadsafeFactoryPtr<
@@ -37,22 +43,26 @@ where
#[async_trait]
#[cfg_attr(test, mockall::automock, allow(dead_code))]
-pub trait IAsyncProvider<DIContainerType>: Send + Sync
+pub trait IAsyncProvider<DIContainerType, DependencyHistoryType>: Send + Sync
where
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
async fn provide(
&self,
di_container: &Arc<DIContainerType>,
- dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<DIContainerType>, InjectableError>;
+ dependency_history: DependencyHistoryType,
+ ) -> Result<AsyncProvidable<DIContainerType, DependencyHistoryType>, InjectableError>;
- fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerType>>;
+ fn do_clone(&self)
+ -> Box<dyn IAsyncProvider<DIContainerType, DependencyHistoryType>>;
}
-impl<DIContainerType> Clone for Box<dyn IAsyncProvider<DIContainerType>>
+impl<DIContainerType, DependencyHistoryType> Clone
+ for Box<dyn IAsyncProvider<DIContainerType, DependencyHistoryType>>
where
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
fn clone(&self) -> Self
{
@@ -60,127 +70,148 @@ where
}
}
-pub struct AsyncTransientTypeProvider<InjectableType, DIContainerType>
-where
- InjectableType: AsyncInjectable<DIContainerType>,
- DIContainerType: IAsyncDIContainer,
+pub struct AsyncTransientTypeProvider<
+ InjectableType,
+ DIContainerType,
+ DependencyHistoryType,
+> where
+ InjectableType: AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
injectable_phantom: PhantomData<InjectableType>,
di_container_phantom: PhantomData<DIContainerType>,
+ dependency_history_phantom: PhantomData<DependencyHistoryType>,
}
-impl<InjectableType, DIContainerType>
- AsyncTransientTypeProvider<InjectableType, DIContainerType>
+impl<InjectableType, DIContainerType, DependencyHistoryType>
+ AsyncTransientTypeProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: AsyncInjectable<DIContainerType>,
- DIContainerType: IAsyncDIContainer,
+ InjectableType: AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
pub fn new() -> Self
{
Self {
injectable_phantom: PhantomData,
di_container_phantom: PhantomData,
+ dependency_history_phantom: PhantomData,
}
}
}
#[async_trait]
-impl<InjectableType, DIContainerType> IAsyncProvider<DIContainerType>
- for AsyncTransientTypeProvider<InjectableType, DIContainerType>
+impl<InjectableType, DIContainerType, DependencyHistoryType>
+ IAsyncProvider<DIContainerType, DependencyHistoryType>
+ for AsyncTransientTypeProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: AsyncInjectable<DIContainerType>,
- DIContainerType: IAsyncDIContainer,
+ InjectableType: AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync + 'static,
{
async fn provide(
&self,
di_container: &Arc<DIContainerType>,
- dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<DIContainerType>, InjectableError>
+ dependency_history: DependencyHistoryType,
+ ) -> Result<AsyncProvidable<DIContainerType, DependencyHistoryType>, InjectableError>
{
Ok(AsyncProvidable::Transient(
InjectableType::resolve(di_container, dependency_history).await?,
))
}
- fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerType>>
+ fn do_clone(&self)
+ -> Box<dyn IAsyncProvider<DIContainerType, DependencyHistoryType>>
{
Box::new(self.clone())
}
}
-impl<InjectableType, DIContainerType> Clone
- for AsyncTransientTypeProvider<InjectableType, DIContainerType>
+impl<InjectableType, DIContainerType, DependencyHistoryType> Clone
+ for AsyncTransientTypeProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: AsyncInjectable<DIContainerType>,
- DIContainerType: IAsyncDIContainer,
+ InjectableType: AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
fn clone(&self) -> Self
{
Self {
injectable_phantom: self.injectable_phantom,
- di_container_phantom: self.di_container_phantom,
+ di_container_phantom: PhantomData,
+ dependency_history_phantom: PhantomData,
}
}
}
-pub struct AsyncSingletonProvider<InjectableType, DIContainerType>
+pub struct AsyncSingletonProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: AsyncInjectable<DIContainerType>,
- DIContainerType: IAsyncDIContainer,
+ InjectableType: AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
singleton: ThreadsafeSingletonPtr<InjectableType>,
di_container_phantom: PhantomData<DIContainerType>,
+ dependency_history_phantom: PhantomData<DependencyHistoryType>,
}
-impl<InjectableType, DIContainerType>
- AsyncSingletonProvider<InjectableType, DIContainerType>
+impl<InjectableType, DIContainerType, DependencyHistoryType>
+ AsyncSingletonProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: AsyncInjectable<DIContainerType>,
- DIContainerType: IAsyncDIContainer,
+ InjectableType: AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
pub fn new(singleton: ThreadsafeSingletonPtr<InjectableType>) -> Self
{
Self {
singleton,
di_container_phantom: PhantomData,
+ dependency_history_phantom: PhantomData,
}
}
}
#[async_trait]
-impl<InjectableType, DIContainerType> IAsyncProvider<DIContainerType>
- for AsyncSingletonProvider<InjectableType, DIContainerType>
+impl<InjectableType, DIContainerType, DependencyHistoryType>
+ IAsyncProvider<DIContainerType, DependencyHistoryType>
+ for AsyncSingletonProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: AsyncInjectable<DIContainerType>,
- DIContainerType: IAsyncDIContainer,
+ InjectableType: AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync + 'static,
{
async fn provide(
&self,
_di_container: &Arc<DIContainerType>,
- _dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<DIContainerType>, InjectableError>
+ _dependency_history: DependencyHistoryType,
+ ) -> Result<AsyncProvidable<DIContainerType, DependencyHistoryType>, InjectableError>
{
Ok(AsyncProvidable::Singleton(self.singleton.clone()))
}
- fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerType>>
+ fn do_clone(&self)
+ -> Box<dyn IAsyncProvider<DIContainerType, DependencyHistoryType>>
{
Box::new(self.clone())
}
}
-impl<InjectableType, DIContainerType> Clone
- for AsyncSingletonProvider<InjectableType, DIContainerType>
+impl<InjectableType, DIContainerType, DependencyHistoryType> Clone
+ for AsyncSingletonProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: AsyncInjectable<DIContainerType>,
- DIContainerType: IAsyncDIContainer,
+ InjectableType: AsyncInjectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync,
{
fn clone(&self) -> Self
{
Self {
singleton: self.singleton.clone(),
di_container_phantom: PhantomData,
+ dependency_history_phantom: PhantomData,
}
}
}
@@ -218,15 +249,17 @@ impl AsyncFactoryProvider
#[cfg(feature = "factory")]
#[async_trait]
-impl<DIContainerType> IAsyncProvider<DIContainerType> for AsyncFactoryProvider
+impl<DIContainerType, DependencyHistoryType>
+ IAsyncProvider<DIContainerType, DependencyHistoryType> for AsyncFactoryProvider
where
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync + 'static,
{
async fn provide(
&self,
_di_container: &Arc<DIContainerType>,
- _dependency_history: Vec<&'static str>,
- ) -> Result<AsyncProvidable<DIContainerType>, InjectableError>
+ _dependency_history: DependencyHistoryType,
+ ) -> Result<AsyncProvidable<DIContainerType, DependencyHistoryType>, InjectableError>
{
Ok(match self.variant {
AsyncFactoryVariant::Normal => AsyncProvidable::Factory(self.factory.clone()),
@@ -239,7 +272,8 @@ where
})
}
- fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerType>>
+ fn do_clone(&self)
+ -> Box<dyn IAsyncProvider<DIContainerType, DependencyHistoryType>>
{
Box::new(self.clone())
}
@@ -263,6 +297,7 @@ mod tests
use std::error::Error;
use super::*;
+ use crate::test_utils::mocks::MockDependencyHistory;
use crate::test_utils::{mocks, subjects_async};
#[tokio::test]
@@ -270,7 +305,8 @@ mod tests
{
let transient_type_provider = AsyncTransientTypeProvider::<
subjects_async::UserManager,
- mocks::async_di_container::MockAsyncDIContainer,
+ mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
>::new();
let di_container = mocks::async_di_container::MockAsyncDIContainer::new();
@@ -278,7 +314,7 @@ mod tests
assert!(
matches!(
transient_type_provider
- .provide(&Arc::new(di_container), vec![])
+ .provide(&Arc::new(di_container), MockDependencyHistory::new())
.await?,
AsyncProvidable::Transient(_)
),
@@ -293,7 +329,8 @@ mod tests
{
let singleton_provider = AsyncSingletonProvider::<
subjects_async::UserManager,
- mocks::async_di_container::MockAsyncDIContainer,
+ mocks::async_di_container::MockAsyncDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
>::new(ThreadsafeSingletonPtr::new(
subjects_async::UserManager {},
));
@@ -303,7 +340,7 @@ mod tests
assert!(
matches!(
singleton_provider
- .provide(&Arc::new(di_container), vec![])
+ .provide(&Arc::new(di_container), MockDependencyHistory::new())
.await?,
AsyncProvidable::Singleton(_)
),
@@ -345,7 +382,9 @@ mod tests
assert!(
matches!(
- factory_provider.provide(&di_container, vec![]).await?,
+ factory_provider
+ .provide(&di_container, mocks::MockDependencyHistory::new())
+ .await?,
AsyncProvidable::Factory(_)
),
"The provided type is not a factory"
@@ -354,7 +393,7 @@ mod tests
assert!(
matches!(
default_factory_provider
- .provide(&di_container, vec![])
+ .provide(&di_container, MockDependencyHistory::new())
.await?,
AsyncProvidable::DefaultFactory(_)
),
@@ -364,7 +403,7 @@ mod tests
assert!(
matches!(
async_default_factory_provider
- .provide(&di_container, vec![])
+ .provide(&di_container, MockDependencyHistory::new())
.await?,
AsyncProvidable::AsyncDefaultFactory(_)
),
diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs
index e1e2aad..ebe0c37 100644
--- a/src/provider/blocking.rs
+++ b/src/provider/blocking.rs
@@ -1,18 +1,20 @@
use std::marker::PhantomData;
use std::rc::Rc;
+use crate::dependency_history::IDependencyHistory;
use crate::di_container::blocking::IDIContainer;
use crate::errors::injectable::InjectableError;
use crate::interfaces::injectable::Injectable;
use crate::ptr::{SingletonPtr, TransientPtr};
#[derive(strum_macros::Display, Debug)]
-pub enum Providable<DIContainerType>
+pub enum Providable<DIContainerType, DependencyHistoryType>
where
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
- Transient(TransientPtr<dyn Injectable<DIContainerType>>),
- Singleton(SingletonPtr<dyn Injectable<DIContainerType>>),
+ Transient(TransientPtr<dyn Injectable<DIContainerType, DependencyHistoryType>>),
+ Singleton(SingletonPtr<dyn Injectable<DIContainerType, DependencyHistoryType>>),
#[cfg(feature = "factory")]
Factory(crate::ptr::FactoryPtr<dyn crate::interfaces::any_factory::AnyFactory>),
#[cfg(feature = "factory")]
@@ -22,52 +24,59 @@ where
}
#[cfg_attr(test, mockall::automock, allow(dead_code))]
-pub trait IProvider<DIContainerType>
+pub trait IProvider<DIContainerType, DependencyHistoryType>
where
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
fn provide(
&self,
di_container: &Rc<DIContainerType>,
- dependency_history: Vec<&'static str>,
- ) -> Result<Providable<DIContainerType>, InjectableError>;
+ dependency_history: DependencyHistoryType,
+ ) -> Result<Providable<DIContainerType, DependencyHistoryType>, InjectableError>;
}
-pub struct TransientTypeProvider<InjectableType, DIContainerType>
+pub struct TransientTypeProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: Injectable<DIContainerType>,
- DIContainerType: IDIContainer,
+ InjectableType: Injectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
injectable_phantom: PhantomData<InjectableType>,
di_container_phantom: PhantomData<DIContainerType>,
+ dependency_history_phantom: PhantomData<DependencyHistoryType>,
}
-impl<InjectableType, DIContainerType>
- TransientTypeProvider<InjectableType, DIContainerType>
+impl<InjectableType, DIContainerType, DependencyHistoryType>
+ TransientTypeProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: Injectable<DIContainerType>,
- DIContainerType: IDIContainer,
+ InjectableType: Injectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
pub fn new() -> Self
{
Self {
injectable_phantom: PhantomData,
di_container_phantom: PhantomData,
+ dependency_history_phantom: PhantomData,
}
}
}
-impl<InjectableType, DIContainerType> IProvider<DIContainerType>
- for TransientTypeProvider<InjectableType, DIContainerType>
+impl<InjectableType, DIContainerType, DependencyHistoryType>
+ IProvider<DIContainerType, DependencyHistoryType>
+ for TransientTypeProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: Injectable<DIContainerType>,
- DIContainerType: IDIContainer,
+ InjectableType: Injectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
fn provide(
&self,
di_container: &Rc<DIContainerType>,
- dependency_history: Vec<&'static str>,
- ) -> Result<Providable<DIContainerType>, InjectableError>
+ dependency_history: DependencyHistoryType,
+ ) -> Result<Providable<DIContainerType, DependencyHistoryType>, InjectableError>
{
Ok(Providable::Transient(InjectableType::resolve(
di_container,
@@ -76,40 +85,48 @@ where
}
}
-pub struct SingletonProvider<InjectableType, DIContainerType>
+pub struct SingletonProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: Injectable<DIContainerType>,
- DIContainerType: IDIContainer,
+ InjectableType: Injectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
singleton: SingletonPtr<InjectableType>,
+
di_container_phantom: PhantomData<DIContainerType>,
+ dependency_history_phantom: PhantomData<DependencyHistoryType>,
}
-impl<InjectableType, DIContainerType> SingletonProvider<InjectableType, DIContainerType>
+impl<InjectableType, DIContainerType, DependencyHistoryType>
+ SingletonProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: Injectable<DIContainerType>,
- DIContainerType: IDIContainer,
+ InjectableType: Injectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
pub fn new(singleton: SingletonPtr<InjectableType>) -> Self
{
Self {
singleton,
di_container_phantom: PhantomData,
+ dependency_history_phantom: PhantomData,
}
}
}
-impl<InjectableType, DIContainerType> IProvider<DIContainerType>
- for SingletonProvider<InjectableType, DIContainerType>
+impl<InjectableType, DIContainerType, DependencyHistoryType>
+ IProvider<DIContainerType, DependencyHistoryType>
+ for SingletonProvider<InjectableType, DIContainerType, DependencyHistoryType>
where
- InjectableType: Injectable<DIContainerType>,
- DIContainerType: IDIContainer,
+ InjectableType: Injectable<DIContainerType, DependencyHistoryType>,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
fn provide(
&self,
_di_container: &Rc<DIContainerType>,
- _dependency_history: Vec<&'static str>,
- ) -> Result<Providable<DIContainerType>, InjectableError>
+ _dependency_history: DependencyHistoryType,
+ ) -> Result<Providable<DIContainerType, DependencyHistoryType>, InjectableError>
{
Ok(Providable::Singleton(self.singleton.clone()))
}
@@ -138,15 +155,17 @@ impl FactoryProvider
}
#[cfg(feature = "factory")]
-impl<DIContainerType> IProvider<DIContainerType> for FactoryProvider
+impl<DIContainerType, DependencyHistoryType>
+ IProvider<DIContainerType, DependencyHistoryType> for FactoryProvider
where
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
fn provide(
&self,
_di_container: &Rc<DIContainerType>,
- _dependency_history: Vec<&'static str>,
- ) -> Result<Providable<DIContainerType>, InjectableError>
+ _dependency_history: DependencyHistoryType,
+ ) -> Result<Providable<DIContainerType, DependencyHistoryType>, InjectableError>
{
Ok(if self.is_default_factory {
Providable::DefaultFactory(self.factory.clone())
@@ -169,14 +188,18 @@ mod tests
{
let transient_type_provider = TransientTypeProvider::<
subjects::UserManager,
- mocks::blocking_di_container::MockDIContainer,
+ mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
>::new();
let di_container = mocks::blocking_di_container::MockDIContainer::new();
+ let dependency_history_mock = mocks::MockDependencyHistory::new();
+
assert!(
matches!(
- transient_type_provider.provide(&Rc::new(di_container), vec![])?,
+ transient_type_provider
+ .provide(&Rc::new(di_container), dependency_history_mock)?,
Providable::Transient(_)
),
"The provided type is not transient"
@@ -188,17 +211,22 @@ mod tests
#[test]
fn singleton_provider_works() -> Result<(), Box<dyn Error>>
{
- let singleton_provider =
- SingletonProvider::<
- subjects::UserManager,
- mocks::blocking_di_container::MockDIContainer,
- >::new(SingletonPtr::new(subjects::UserManager {}));
+ let singleton_provider = SingletonProvider::<
+ subjects::UserManager,
+ mocks::blocking_di_container::MockDIContainer<mocks::MockDependencyHistory>,
+ mocks::MockDependencyHistory,
+ >::new(SingletonPtr::new(
+ subjects::UserManager {},
+ ));
let di_container = mocks::blocking_di_container::MockDIContainer::new();
assert!(
matches!(
- singleton_provider.provide(&Rc::new(di_container), vec![])?,
+ singleton_provider.provide(
+ &Rc::new(di_container),
+ mocks::MockDependencyHistory::new()
+ )?,
Providable::Singleton(_)
),
"The provided type is not a singleton"
@@ -227,7 +255,8 @@ mod tests
assert!(
matches!(
- factory_provider.provide(&di_container, vec![])?,
+ factory_provider
+ .provide(&di_container, mocks::MockDependencyHistory::new())?,
Providable::Factory(_)
),
"The provided type is not a factory"
@@ -235,7 +264,8 @@ mod tests
assert!(
matches!(
- default_factory_provider.provide(&di_container, vec![])?,
+ default_factory_provider
+ .provide(&di_container, mocks::MockDependencyHistory::new())?,
Providable::DefaultFactory(_)
),
"The provided type is not a default factory"
diff --git a/src/test_utils.rs b/src/test_utils.rs
index 650e338..b4ec951 100644
--- a/src/test_utils.rs
+++ b/src/test_utils.rs
@@ -7,6 +7,7 @@ pub mod subjects
use syrette_macros::declare_interface;
+ use crate::dependency_history::IDependencyHistory;
use crate::di_container::blocking::IDIContainer;
use crate::interfaces::injectable::Injectable;
use crate::ptr::TransientPtr;
@@ -45,13 +46,15 @@ pub mod subjects
declare_interface!(UserManager -> IUserManager);
- impl<DIContainerType> Injectable<DIContainerType> for UserManager
+ impl<DIContainerType, DependencyHistoryType>
+ Injectable<DIContainerType, DependencyHistoryType> for UserManager
where
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
fn resolve(
_di_container: &Rc<DIContainerType>,
- _dependency_history: Vec<&'static str>,
+ _dependency_history: DependencyHistoryType,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
Self: Sized,
@@ -111,13 +114,15 @@ pub mod subjects
declare_interface!(Number -> INumber);
- impl<DIContainerType> Injectable<DIContainerType> for Number
+ impl<DIContainerType, DependencyHistoryType>
+ Injectable<DIContainerType, DependencyHistoryType> for Number
where
- DIContainerType: IDIContainer,
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
{
fn resolve(
_di_container: &Rc<DIContainerType>,
- _dependency_history: Vec<&'static str>,
+ _dependency_history: DependencyHistoryType,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
Self: Sized,
@@ -138,6 +143,7 @@ pub mod subjects_async
use async_trait::async_trait;
use syrette_macros::declare_interface;
+ use crate::dependency_history::IDependencyHistory;
use crate::di_container::asynchronous::IAsyncDIContainer;
use crate::interfaces::async_injectable::AsyncInjectable;
use crate::ptr::TransientPtr;
@@ -177,13 +183,15 @@ pub mod subjects_async
declare_interface!(UserManager -> IUserManager);
#[async_trait]
- impl<DIContainerType> AsyncInjectable<DIContainerType> for UserManager
+ impl<DIContainerType, DependencyHistoryType>
+ AsyncInjectable<DIContainerType, DependencyHistoryType> for UserManager
where
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync + 'static,
{
async fn resolve(
_: &Arc<DIContainerType>,
- _dependency_history: Vec<&'static str>,
+ _dependency_history: DependencyHistoryType,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
Self: Sized,
@@ -244,13 +252,15 @@ pub mod subjects_async
declare_interface!(Number -> INumber, async = true);
#[async_trait]
- impl<DIContainerType> AsyncInjectable<DIContainerType> for Number
+ impl<DIContainerType, DependencyHistoryType>
+ AsyncInjectable<DIContainerType, DependencyHistoryType> for Number
where
- DIContainerType: IAsyncDIContainer,
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync + 'static,
{
async fn resolve(
_: &Arc<DIContainerType>,
- _dependency_history: Vec<&'static str>,
+ _dependency_history: DependencyHistoryType,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
Self: Sized,
@@ -272,6 +282,7 @@ pub mod mocks
use std::rc::Rc;
use super::*;
+ use crate::dependency_history::IDependencyHistory;
use crate::di_container::blocking::binding::builder::BindingBuilder;
use crate::di_container::blocking::details::DIContainerInternals;
use crate::di_container::blocking::IDIContainer;
@@ -280,10 +291,18 @@ pub mod mocks
use crate::ptr::SomePtr;
mock! {
- pub DIContainer {}
-
- impl IDIContainer for DIContainer {
- fn bind<Interface>(self: &mut Rc<Self>) -> BindingBuilder<Interface, Self>
+ pub DIContainer<DependencyHistoryType>
+ where
+ DependencyHistoryType: IDependencyHistory + 'static
+ {}
+
+ impl<DependencyHistoryType> IDIContainer<DependencyHistoryType> for
+ DIContainer<DependencyHistoryType>
+ where
+ DependencyHistoryType: IDependencyHistory + 'static
+ {
+ fn bind<Interface>(self: &mut Rc<Self>) ->
+ BindingBuilder<Interface, Self, DependencyHistoryType>
where
Interface: 'static + ?Sized;
@@ -301,14 +320,18 @@ pub mod mocks
#[doc(hidden)]
fn get_bound<Interface>(
self: &Rc<Self>,
- dependency_history: Vec<&'static str>,
+ dependency_history: DependencyHistoryType,
name: Option<&'static str>,
) -> Result<SomePtr<Interface>, DIContainerError>
where
Interface: 'static + ?Sized;
}
- impl DIContainerInternals for DIContainer
+ impl<DependencyHistoryType> DIContainerInternals<
+ DependencyHistoryType
+ > for DIContainer<DependencyHistoryType>
+ where
+ DependencyHistoryType: IDependencyHistory
{
fn has_binding<Interface>(self: &Rc<Self>, name: Option<&'static str>) -> bool
where
@@ -318,14 +341,14 @@ pub mod mocks
fn set_binding<Interface>(
self: &Rc<Self>,
name: Option<&'static str>,
- provider: Box<dyn IProvider<Self>>,
+ provider: Box<dyn IProvider<Self, DependencyHistoryType>>,
) where
Interface: 'static + ?Sized;
fn remove_binding<Interface>(
self: &Rc<Self>,
name: Option<&'static str>,
- ) -> Option<Box<dyn IProvider<Self>>>
+ ) -> Option<Box<dyn IProvider<Self, DependencyHistoryType>>>
where
Interface: 'static + ?Sized;
}
@@ -338,6 +361,7 @@ pub mod mocks
use std::sync::Arc;
use super::*;
+ use crate::dependency_history::IDependencyHistory;
use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder;
use crate::di_container::asynchronous::details::DIContainerInternals;
use crate::di_container::asynchronous::IAsyncDIContainer;
@@ -346,11 +370,20 @@ pub mod mocks
use crate::ptr::SomeThreadsafePtr;
mock! {
- pub AsyncDIContainer {}
+ pub AsyncDIContainer<DependencyHistoryType>
+ where
+ DependencyHistoryType: IDependencyHistory + 'static + Send + Sync
+ {}
#[async_trait::async_trait]
- impl IAsyncDIContainer for AsyncDIContainer {
- fn bind<Interface>(self: &mut Arc<Self>) -> AsyncBindingBuilder<Interface, Self>
+ impl<DependencyHistoryType> IAsyncDIContainer<
+ DependencyHistoryType
+ > for AsyncDIContainer<DependencyHistoryType>
+ where
+ DependencyHistoryType: IDependencyHistory + 'static + Send + Sync
+ {
+ fn bind<Interface>(self: &mut Arc<Self>) ->
+ AsyncBindingBuilder<Interface, Self, DependencyHistoryType>
where
Interface: 'static + ?Sized + Send + Sync;
@@ -370,7 +403,7 @@ pub mod mocks
#[doc(hidden)]
async fn get_bound<Interface>(
self: &Arc<Self>,
- dependency_history: Vec<&'static str>,
+ dependency_history: DependencyHistoryType,
name: Option<&'static str>,
) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>
where
@@ -378,7 +411,12 @@ pub mod mocks
}
#[async_trait::async_trait]
- impl DIContainerInternals for AsyncDIContainer {
+ impl<DependencyHistoryType> DIContainerInternals<
+ DependencyHistoryType
+ > for AsyncDIContainer<DependencyHistoryType>
+ where
+ DependencyHistoryType: IDependencyHistory + 'static + Send + Sync
+ {
async fn has_binding<Interface>(
self: &Arc<Self>,
name: Option<&'static str>,
@@ -389,17 +427,104 @@ pub mod mocks
async fn set_binding<Interface>(
self: &Arc<Self>,
name: Option<&'static str>,
- provider: Box<dyn IAsyncProvider<Self>>,
+ provider: Box<dyn IAsyncProvider<Self, DependencyHistoryType>>,
) where
Interface: 'static + ?Sized;
async fn remove_binding<Interface>(
self: &Arc<Self>,
name: Option<&'static str>,
- ) -> Option<Box<dyn IAsyncProvider<Self>>>
+ ) -> Option<Box<dyn IAsyncProvider<Self, DependencyHistoryType>>>
where
Interface: 'static + ?Sized;
}
}
}
+
+ #[cfg(feature = "async")]
+ pub mod async_provider
+ {
+ use std::sync::Arc;
+
+ use async_trait::async_trait;
+
+ use super::*;
+ use crate::dependency_history::IDependencyHistory;
+ use crate::di_container::asynchronous::IAsyncDIContainer;
+ use crate::errors::injectable::InjectableError;
+ use crate::provider::r#async::{AsyncProvidable, IAsyncProvider};
+
+ mock! {
+ pub AsyncProvider<
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync
+ > {}
+
+ #[async_trait]
+ impl<
+ DIContainerType: IAsyncDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory + Send + Sync
+ > IAsyncProvider<DIContainerType, DependencyHistoryType> for AsyncProvider<
+ DIContainerType,
+ DependencyHistoryType
+ >
+ {
+ async fn provide(
+ &self,
+ di_container: &Arc<DIContainerType>,
+ dependency_history: DependencyHistoryType
+ ) -> Result<AsyncProvidable<DIContainerType, DependencyHistoryType>, InjectableError>;
+
+ fn do_clone(&self) ->
+ Box<dyn IAsyncProvider<DIContainerType, DependencyHistoryType>>;
+ }
+ }
+ }
+
+ pub mod blocking_provider
+ {
+ use std::rc::Rc;
+
+ use super::*;
+ use crate::dependency_history::IDependencyHistory;
+ use crate::di_container::blocking::IDIContainer;
+ use crate::errors::injectable::InjectableError;
+ use crate::provider::blocking::{IProvider, Providable};
+
+ mock! {
+ pub Provider<DIContainerType, DependencyHistoryType>
+ where
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory,
+ {}
+
+ impl<DIContainerType, DependencyHistoryType> IProvider<
+ DIContainerType,
+ DependencyHistoryType
+ > for Provider<DIContainerType, DependencyHistoryType>
+ where
+ DIContainerType: IDIContainer<DependencyHistoryType>,
+ DependencyHistoryType: IDependencyHistory
+ {
+ fn provide(
+ &self,
+ di_container: &Rc<DIContainerType>,
+ dependency_history: DependencyHistoryType,
+ ) -> Result<Providable<DIContainerType, DependencyHistoryType>, InjectableError>;
+ }
+ }
+ }
+
+ mock! {
+ pub DependencyHistory {}
+
+ impl crate::dependency_history::IDependencyHistory for DependencyHistory
+ {
+ fn push<Dependency: 'static + ?Sized>(&mut self);
+
+ fn contains<Dependency: 'static + ?Sized>(&self) -> bool;
+ }
+
+ impl crate::dependency_history::private::Sealed for DependencyHistory {}
+ }
}