From 9e01cdf341a7866180b3a63d745f3b2d7578d28a Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 23 Oct 2022 18:12:23 +0200 Subject: refactor!: reduce DI container coupling BREAKING CHANGE: You now have to import the DI containers's interfaces to use the DI containers's methods --- .../asynchronous/binding/when_configurator.rs | 46 +++++++++++++--------- 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'src/di_container/asynchronous/binding/when_configurator.rs') diff --git a/src/di_container/asynchronous/binding/when_configurator.rs b/src/di_container/asynchronous/binding/when_configurator.rs index b245ad8..3175420 100644 --- a/src/di_container/asynchronous/binding/when_configurator.rs +++ b/src/di_container/asynchronous/binding/when_configurator.rs @@ -1,25 +1,31 @@ -//! When configurator for a binding for types inside of a [`AsyncDIContainer`]. +//! When configurator for a binding for types inside of a [`IAsyncDIContainer`]. +//! +//! [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer use std::any::type_name; use std::marker::PhantomData; use std::sync::Arc; +use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::async_di_container::AsyncBindingWhenConfiguratorError; -use crate::AsyncDIContainer; -/// When configurator for a binding for type 'Interface' inside a [`AsyncDIContainer`]. -pub struct AsyncBindingWhenConfigurator +/// When configurator for a binding for type 'Interface' inside a [`IAsyncDIContainer`]. +/// +/// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer +pub struct AsyncBindingWhenConfigurator where Interface: 'static + ?Sized + Send + Sync, + DIContainerType: IAsyncDIContainer, { - di_container: Arc, + di_container: Arc, interface_phantom: PhantomData, } -impl AsyncBindingWhenConfigurator +impl AsyncBindingWhenConfigurator where Interface: 'static + ?Sized + Send + Sync, + DIContainerType: IAsyncDIContainer, { - pub(crate) fn new(di_container: Arc) -> Self + pub(crate) fn new(di_container: Arc) -> Self { Self { di_container, @@ -36,18 +42,22 @@ where name: &'static str, ) -> Result<(), AsyncBindingWhenConfiguratorError> { - let mut bindings_lock = self.di_container.bindings.lock().await; + let binding = self + .di_container + .remove_binding::(None) + .await + .map_or_else( + || { + Err(AsyncBindingWhenConfiguratorError::BindingNotFound( + type_name::(), + )) + }, + Ok, + )?; - let binding = bindings_lock.remove::(None).map_or_else( - || { - Err(AsyncBindingWhenConfiguratorError::BindingNotFound( - type_name::(), - )) - }, - Ok, - )?; - - bindings_lock.set::(Some(name), binding); + self.di_container + .set_binding::(Some(name), binding) + .await; Ok(()) } -- cgit v1.2.3-18-g5258