aboutsummaryrefslogtreecommitdiff
path: root/src/di_container/blocking/binding
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/di_container/blocking/binding
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/di_container/blocking/binding')
-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
3 files changed, 116 insertions, 40 deletions
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!(