aboutsummaryrefslogtreecommitdiff
path: root/src/di_container/blocking/binding/when_configurator.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/di_container/blocking/binding/when_configurator.rs')
-rw-r--r--src/di_container/blocking/binding/when_configurator.rs46
1 files changed, 27 insertions, 19 deletions
diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs
index 9cd9bb6..5b9a8c0 100644
--- a/src/di_container/blocking/binding/when_configurator.rs
+++ b/src/di_container/blocking/binding/when_configurator.rs
@@ -1,25 +1,31 @@
-//! When configurator for a binding for types inside of a [`DIContainer`].
+//! When configurator for a binding for types inside of a [`IDIContainer`].
+//!
+//! [`IDIContainer`]: crate::di_container::blocking::IDIContainer
use std::any::type_name;
use std::marker::PhantomData;
use std::rc::Rc;
-use crate::di_container::blocking::DIContainer;
+use crate::di_container::blocking::IDIContainer;
use crate::errors::di_container::BindingWhenConfiguratorError;
-/// When configurator for a binding for type 'Interface' inside a [`DIContainer`].
-pub struct BindingWhenConfigurator<Interface>
+/// When configurator for a binding for type 'Interface' inside a [`IDIContainer`].
+///
+/// [`IDIContainer`]: crate::di_container::blocking::IDIContainer
+pub struct BindingWhenConfigurator<Interface, DIContainerType>
where
Interface: 'static + ?Sized,
+ DIContainerType: IDIContainer,
{
- di_container: Rc<DIContainer>,
+ di_container: Rc<DIContainerType>,
interface_phantom: PhantomData<Interface>,
}
-impl<Interface> BindingWhenConfigurator<Interface>
+impl<Interface, DIContainerType> BindingWhenConfigurator<Interface, DIContainerType>
where
Interface: 'static + ?Sized,
+ DIContainerType: IDIContainer,
{
- pub(crate) fn new(di_container: Rc<DIContainer>) -> Self
+ pub(crate) fn new(di_container: Rc<DIContainerType>) -> Self
{
Self {
di_container,
@@ -36,19 +42,21 @@ where
name: &'static str,
) -> Result<(), BindingWhenConfiguratorError>
{
- let mut bindings_mut = self.di_container.bindings.borrow_mut();
+ let binding = self
+ .di_container
+ .remove_binding::<Interface>(None)
+ .map_or_else(
+ || {
+ Err(BindingWhenConfiguratorError::BindingNotFound(type_name::<
+ Interface,
+ >(
+ )))
+ },
+ Ok,
+ )?;
- let binding = bindings_mut.remove::<Interface>(None).map_or_else(
- || {
- Err(BindingWhenConfiguratorError::BindingNotFound(type_name::<
- Interface,
- >(
- )))
- },
- Ok,
- )?;
-
- bindings_mut.set::<Interface>(Some(name), binding);
+ self.di_container
+ .set_binding::<Interface>(Some(name), binding);
Ok(())
}