aboutsummaryrefslogtreecommitdiff
path: root/src/di_container/blocking/binding
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-09-14 19:41:56 +0200
committerHampusM <hampus@hampusmat.com>2023-09-14 21:17:37 +0200
commit9bed6b4d2772fd020ea9eb6eaaba4ca014d96f94 (patch)
treedff0d7768f4a56c9d75e4a6c89974891373e997b /src/di_container/blocking/binding
parent9a7cf8cfd376da09c982606aa79f253319dfe382 (diff)
refactor!: remove blocking DI container interface
BREAKING CHANGE: IDIContainer have been removed and multiple structs no longer take a DI container generic parameter
Diffstat (limited to 'src/di_container/blocking/binding')
-rw-r--r--src/di_container/blocking/binding/builder.rs92
-rw-r--r--src/di_container/blocking/binding/scope_configurator.rs68
-rw-r--r--src/di_container/blocking/binding/when_configurator.rs35
3 files changed, 82 insertions, 113 deletions
diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs
index bfc9e4e..64e787e 100644
--- a/src/di_container/blocking/binding/builder.rs
+++ b/src/di_container/blocking/binding/builder.rs
@@ -1,6 +1,4 @@
-//! Binding builder for types inside of a [`IDIContainer`].
-//!
-//! [`IDIContainer`]: crate::di_container::blocking::IDIContainer
+//! Binding builder for types inside of a [`DIContainer`].
use std::any::type_name;
use std::marker::PhantomData;
use std::rc::Rc;
@@ -8,36 +6,32 @@ use std::rc::Rc;
use crate::di_container::blocking::binding::scope_configurator::BindingScopeConfigurator;
#[cfg(feature = "factory")]
use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator;
-use crate::di_container::blocking::IDIContainer;
use crate::di_container::BindingOptions;
use crate::errors::di_container::BindingBuilderError;
use crate::interfaces::injectable::Injectable;
use crate::util::use_double;
use_double!(crate::dependency_history::DependencyHistory);
+use_double!(crate::di_container::blocking::DIContainer);
-/// Binding builder for type `Interface` inside a [`IDIContainer`].
-///
-/// [`IDIContainer`]: crate::di_container::blocking::IDIContainer
+/// Binding builder for type `Interface` inside a [`DIContainer`].
#[must_use = "No binding will be created if you don't use the binding builder"]
-pub struct BindingBuilder<Interface, DIContainerType>
+pub struct BindingBuilder<Interface>
where
Interface: 'static + ?Sized,
- DIContainerType: IDIContainer,
{
- di_container: Rc<DIContainerType>,
+ di_container: Rc<DIContainer>,
dependency_history_factory: fn() -> DependencyHistory,
interface_phantom: PhantomData<Interface>,
}
-impl<Interface, DIContainerType> BindingBuilder<Interface, DIContainerType>
+impl<Interface> BindingBuilder<Interface>
where
Interface: 'static + ?Sized,
- DIContainerType: IDIContainer,
{
pub(crate) fn new(
- di_container: Rc<DIContainerType>,
+ di_container: Rc<DIContainer>,
dependency_history_factory: fn() -> DependencyHistory,
) -> Self
{
@@ -49,13 +43,13 @@ where
}
/// Creates a binding of type `Interface` to type `Implementation` inside of the
- /// associated [`IDIContainer`].
+ /// associated [`DIContainer`].
///
/// The scope of the binding is transient. But that can be changed by using the
/// returned [`BindingScopeConfigurator`]
///
/// # Errors
- /// Will return Err if the associated [`IDIContainer`] already have a binding for
+ /// Will return Err if the associated [`DIContainer`] already have a binding for
/// the interface.
///
/// # Examples
@@ -63,7 +57,6 @@ where
/// # use std::error::Error;
/// #
/// # use syrette::{DIContainer, injectable};
- /// # use syrette::di_container::blocking::IDIContainer;
/// #
/// # trait Foo {}
/// #
@@ -88,16 +81,11 @@ where
/// # Ok(())
/// # }
/// ```
- ///
- /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer
pub fn to<Implementation>(
self,
- ) -> Result<
- BindingScopeConfigurator<Interface, Implementation, DIContainerType>,
- BindingBuilderError,
- >
+ ) -> Result<BindingScopeConfigurator<Interface, Implementation>, BindingBuilderError>
where
- Implementation: Injectable<DIContainerType>,
+ Implementation: Injectable<DIContainer>,
{
if self
.di_container
@@ -119,10 +107,10 @@ where
}
/// Creates a binding of factory type `Interface` to a factory inside of the
- /// associated [`IDIContainer`].
+ /// associated [`DIContainer`].
///
/// # Errors
- /// Will return Err if the associated [`IDIContainer`] already have a binding for
+ /// Will return Err if the associated [`DIContainer`] already have a binding for
/// the interface.
///
/// # Examples
@@ -131,7 +119,6 @@ where
/// #
/// # use syrette::{DIContainer, factory};
/// # use syrette::ptr::TransientPtr;
- /// # use syrette::di_container::blocking::IDIContainer;
/// #
/// # trait ICustomerID {}
/// # trait ICustomer {}
@@ -182,19 +169,17 @@ where
/// # Ok(())
/// # }
/// ```
- ///
- /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer
#[cfg(feature = "factory")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
pub fn to_factory<Args, Return, Func>(
self,
factory_func: &'static Func,
- ) -> Result<BindingWhenConfigurator<Interface, DIContainerType>, BindingBuilderError>
+ ) -> Result<BindingWhenConfigurator<Interface>, BindingBuilderError>
where
Args: std::marker::Tuple + 'static,
Return: 'static + ?Sized,
Interface: Fn<Args, Output = crate::ptr::TransientPtr<Return>>,
- Func: Fn<(std::rc::Rc<DIContainerType>,), Output = Box<Interface>>,
+ Func: Fn<(std::rc::Rc<DIContainer>,), Output = Box<Interface>>,
{
use crate::private::castable_factory::blocking::CastableFactory;
@@ -221,10 +206,10 @@ where
}
/// Creates a binding of type `Interface` to a factory that takes no arguments
- /// inside of the associated [`IDIContainer`].
+ /// inside of the associated [`DIContainer`].
///
/// # Errors
- /// Will return Err if the associated [`IDIContainer`] already have a binding for
+ /// Will return Err if the associated [`DIContainer`] already have a binding for
/// the interface.
///
/// # Examples
@@ -233,7 +218,6 @@ where
/// #
/// # use syrette::{DIContainer, factory};
/// # use syrette::ptr::TransientPtr;
- /// # use syrette::di_container::blocking::IDIContainer;
/// #
/// # trait IBuffer {}
/// #
@@ -271,18 +255,16 @@ where
/// # Ok(())
/// # }
/// ```
- ///
- /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer
#[cfg(feature = "factory")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
pub fn to_default_factory<Return, FactoryFunc>(
self,
factory_func: &'static FactoryFunc,
- ) -> Result<BindingWhenConfigurator<Interface, DIContainerType>, BindingBuilderError>
+ ) -> Result<BindingWhenConfigurator<Interface>, BindingBuilderError>
where
Return: 'static + ?Sized,
FactoryFunc: Fn<
- (Rc<DIContainerType>,),
+ (Rc<DIContainer>,),
Output = crate::ptr::TransientPtr<
dyn Fn<(), Output = crate::ptr::TransientPtr<Return>>,
>,
@@ -322,12 +304,13 @@ mod tests
use super::*;
use crate::dependency_history::MockDependencyHistory;
- use crate::test_utils::{mocks, subjects};
+ use crate::di_container::blocking::MockDIContainer;
+ use crate::test_utils::subjects;
#[test]
fn can_bind_to() -> Result<(), Box<dyn Error>>
{
- let mut mock_di_container = mocks::blocking_di_container::MockDIContainer::new();
+ let mut mock_di_container = MockDIContainer::new();
mock_di_container
.expect_has_binding::<dyn subjects::INumber>()
@@ -341,11 +324,10 @@ mod tests
.return_once(|_options, _provider| ())
.once();
- let binding_builder =
- BindingBuilder::<
- dyn subjects::INumber,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(mock_di_container), MockDependencyHistory::new);
+ let binding_builder = BindingBuilder::<dyn subjects::INumber>::new(
+ Rc::new(mock_di_container),
+ MockDependencyHistory::new,
+ );
binding_builder.to::<subjects::Number>()?;
@@ -364,7 +346,7 @@ mod tests
type IUserManagerFactory =
dyn Fn(i32, String) -> TransientPtr<dyn subjects::IUserManager>;
- let mut mock_di_container = mocks::blocking_di_container::MockDIContainer::new();
+ let mut mock_di_container = MockDIContainer::new();
mock_di_container
.expect_has_binding::<IUserManagerFactory>()
@@ -378,11 +360,10 @@ mod tests
.return_once(|_, _provider| ())
.once();
- let binding_builder =
- BindingBuilder::<
- IUserManagerFactory,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(mock_di_container), MockDependencyHistory::new);
+ let binding_builder = BindingBuilder::<IUserManagerFactory>::new(
+ Rc::new(mock_di_container),
+ MockDependencyHistory::new,
+ );
binding_builder.to_factory(&|_| {
Box::new(move |_num, _text| {
@@ -407,7 +388,7 @@ mod tests
declare_default_factory!(dyn subjects::IUserManager);
- let mut mock_di_container = mocks::blocking_di_container::MockDIContainer::new();
+ let mut mock_di_container = MockDIContainer::new();
mock_di_container
.expect_has_binding::<dyn subjects::IUserManager>()
@@ -421,11 +402,10 @@ mod tests
.return_once(|_, _provider| ())
.once();
- let binding_builder =
- BindingBuilder::<
- dyn subjects::IUserManager,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(mock_di_container), MockDependencyHistory::new);
+ let binding_builder = BindingBuilder::<dyn subjects::IUserManager>::new(
+ Rc::new(mock_di_container),
+ 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 0fcdfdf..be469ba 100644
--- a/src/di_container/blocking/binding/scope_configurator.rs
+++ b/src/di_container/blocking/binding/scope_configurator.rs
@@ -1,11 +1,8 @@
-//! Scope configurator for a binding for types inside of a [`IDIContainer`].
-//!
-//! [`IDIContainer`]: crate::di_container::blocking::IDIContainer
+//! Scope configurator for a binding for types inside of a [`DIContainer`].
use std::marker::PhantomData;
use std::rc::Rc;
use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator;
-use crate::di_container::blocking::IDIContainer;
use crate::di_container::BindingOptions;
use crate::errors::di_container::BindingScopeConfiguratorError;
use crate::interfaces::injectable::Injectable;
@@ -14,32 +11,28 @@ use crate::ptr::SingletonPtr;
use crate::util::use_double;
use_double!(crate::dependency_history::DependencyHistory);
+use_double!(crate::di_container::blocking::DIContainer);
-/// Scope configurator for a binding for type `Interface` inside a [`IDIContainer`].
-///
-/// [`IDIContainer`]: crate::di_container::blocking::IDIContainer
-pub struct BindingScopeConfigurator<Interface, Implementation, DIContainerType>
+/// Scope configurator for a binding for type `Interface` inside a [`DIContainer`].
+pub struct BindingScopeConfigurator<Interface, Implementation>
where
Interface: 'static + ?Sized,
- Implementation: Injectable<DIContainerType>,
- DIContainerType: IDIContainer,
+ Implementation: Injectable<DIContainer>,
{
- di_container: Rc<DIContainerType>,
+ di_container: Rc<DIContainer>,
dependency_history_factory: fn() -> DependencyHistory,
interface_phantom: PhantomData<Interface>,
implementation_phantom: PhantomData<Implementation>,
}
-impl<Interface, Implementation, DIContainerType>
- BindingScopeConfigurator<Interface, Implementation, DIContainerType>
+impl<Interface, Implementation> BindingScopeConfigurator<Interface, Implementation>
where
Interface: 'static + ?Sized,
- Implementation: Injectable<DIContainerType>,
- DIContainerType: IDIContainer,
+ Implementation: Injectable<DIContainer>,
{
pub(crate) fn new(
- di_container: Rc<DIContainerType>,
+ di_container: Rc<DIContainer>,
dependency_history_factory: fn() -> DependencyHistory,
) -> Self
{
@@ -55,8 +48,7 @@ where
///
/// This is the default.
#[allow(clippy::must_use_candidate)]
- pub fn in_transient_scope(self)
- -> BindingWhenConfigurator<Interface, DIContainerType>
+ pub fn in_transient_scope(self) -> BindingWhenConfigurator<Interface>
{
self.set_in_transient_scope();
@@ -69,10 +61,7 @@ where
/// Will return Err if resolving the implementation fails.
pub fn in_singleton_scope(
self,
- ) -> Result<
- BindingWhenConfigurator<Interface, DIContainerType>,
- BindingScopeConfiguratorError,
- >
+ ) -> Result<BindingWhenConfigurator<Interface>, BindingScopeConfiguratorError>
{
let singleton: SingletonPtr<Implementation> = SingletonPtr::from(
Implementation::resolve(
@@ -94,7 +83,7 @@ where
{
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(TransientTypeProvider::<Implementation, DIContainerType>::new()),
+ Box::new(TransientTypeProvider::<Implementation, DIContainer>::new()),
);
}
}
@@ -104,12 +93,13 @@ mod tests
{
use super::*;
use crate::dependency_history::MockDependencyHistory;
- use crate::test_utils::{mocks, subjects};
+ use crate::di_container::blocking::MockDIContainer;
+ use crate::test_utils::subjects;
#[test]
fn in_transient_scope_works()
{
- let mut di_container_mock = mocks::blocking_di_container::MockDIContainer::new();
+ let mut di_container_mock = MockDIContainer::new();
di_container_mock
.expect_set_binding::<dyn subjects::IUserManager>()
@@ -117,12 +107,13 @@ mod tests
.return_once(|_name, _provider| ())
.once();
- let binding_scope_configurator =
- BindingScopeConfigurator::<
- dyn subjects::IUserManager,
- subjects::UserManager,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(di_container_mock), MockDependencyHistory::new);
+ let binding_scope_configurator = BindingScopeConfigurator::<
+ dyn subjects::IUserManager,
+ subjects::UserManager,
+ >::new(
+ Rc::new(di_container_mock),
+ MockDependencyHistory::new,
+ );
binding_scope_configurator.in_transient_scope();
}
@@ -130,7 +121,7 @@ mod tests
#[test]
fn in_singleton_scope_works()
{
- let mut di_container_mock = mocks::blocking_di_container::MockDIContainer::new();
+ let mut di_container_mock = MockDIContainer::new();
di_container_mock
.expect_set_binding::<dyn subjects::IUserManager>()
@@ -138,12 +129,13 @@ mod tests
.return_once(|_name, _provider| ())
.once();
- let binding_scope_configurator =
- BindingScopeConfigurator::<
- dyn subjects::IUserManager,
- subjects::UserManager,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(di_container_mock), MockDependencyHistory::new);
+ let binding_scope_configurator = BindingScopeConfigurator::<
+ dyn subjects::IUserManager,
+ subjects::UserManager,
+ >::new(
+ Rc::new(di_container_mock),
+ MockDependencyHistory::new,
+ );
assert!(binding_scope_configurator.in_singleton_scope().is_ok());
}
diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs
index 52b23ff..3d267b2 100644
--- a/src/di_container/blocking/binding/when_configurator.rs
+++ b/src/di_container/blocking/binding/when_configurator.rs
@@ -1,33 +1,29 @@
-//! When configurator for a binding for types inside of a [`IDIContainer`].
-//!
-//! [`IDIContainer`]: crate::di_container::blocking::IDIContainer
+//! When configurator for a binding for types inside of a [`DIContainer`].
use std::any::type_name;
use std::marker::PhantomData;
use std::rc::Rc;
-use crate::di_container::blocking::IDIContainer;
use crate::di_container::BindingOptions;
use crate::errors::di_container::BindingWhenConfiguratorError;
+use crate::util::use_double;
-/// When configurator for a binding for type `Interface` inside a [`IDIContainer`].
-///
-/// [`IDIContainer`]: crate::di_container::blocking::IDIContainer
-pub struct BindingWhenConfigurator<Interface, DIContainerType>
+use_double!(crate::di_container::blocking::DIContainer);
+
+/// When configurator for a binding for type `Interface` inside a [`DIContainer`].
+pub struct BindingWhenConfigurator<Interface>
where
Interface: 'static + ?Sized,
- DIContainerType: IDIContainer,
{
- di_container: Rc<DIContainerType>,
+ di_container: Rc<DIContainer>,
interface_phantom: PhantomData<Interface>,
}
-impl<Interface, DIContainerType> BindingWhenConfigurator<Interface, DIContainerType>
+impl<Interface> BindingWhenConfigurator<Interface>
where
Interface: 'static + ?Sized,
- DIContainerType: IDIContainer,
{
- pub(crate) fn new(di_container: Rc<DIContainerType>) -> Self
+ pub(crate) fn new(di_container: Rc<DIContainer>) -> Self
{
Self {
di_container,
@@ -70,13 +66,14 @@ mod tests
use mockall::predicate::eq;
use super::*;
+ use crate::di_container::blocking::MockDIContainer;
use crate::provider::blocking::MockIProvider;
- use crate::test_utils::{mocks, subjects};
+ use crate::test_utils::subjects;
#[test]
fn when_named_works()
{
- let mut di_container_mock = mocks::blocking_di_container::MockDIContainer::new();
+ let mut di_container_mock = MockDIContainer::new();
di_container_mock
.expect_remove_binding::<dyn subjects::INumber>()
@@ -90,10 +87,10 @@ mod tests
.return_once(|_name, _provider| ())
.once();
- let binding_when_configurator = BindingWhenConfigurator::<
- dyn subjects::INumber,
- mocks::blocking_di_container::MockDIContainer,
- >::new(Rc::new(di_container_mock));
+ let binding_when_configurator =
+ BindingWhenConfigurator::<dyn subjects::INumber>::new(Rc::new(
+ di_container_mock,
+ ));
assert!(binding_when_configurator.when_named("cool").is_ok());
}