diff options
author | HampusM <hampus@hampusmat.com> | 2022-10-23 18:12:23 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-10-23 18:12:23 +0200 |
commit | 9e01cdf341a7866180b3a63d745f3b2d7578d28a (patch) | |
tree | 0c036b7b4a68e44b6eb2221bf7beb3c34fe9c1c8 /src/di_container/blocking/binding/when_configurator.rs | |
parent | 740ef47d49e02ae2f2184f4c347d8eba8aee38fd (diff) |
refactor!: reduce DI container coupling
BREAKING CHANGE: You now have to import the DI containers's interfaces to use the DI containers's methods
Diffstat (limited to 'src/di_container/blocking/binding/when_configurator.rs')
-rw-r--r-- | src/di_container/blocking/binding/when_configurator.rs | 46 |
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(()) } |