diff options
Diffstat (limited to 'src/di_container/asynchronous/binding/when_configurator.rs')
-rw-r--r-- | src/di_container/asynchronous/binding/when_configurator.rs | 46 |
1 files changed, 28 insertions, 18 deletions
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<Interface> +/// When configurator for a binding for type 'Interface' inside a [`IAsyncDIContainer`]. +/// +/// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer +pub struct AsyncBindingWhenConfigurator<Interface, DIContainerType> where Interface: 'static + ?Sized + Send + Sync, + DIContainerType: IAsyncDIContainer, { - di_container: Arc<AsyncDIContainer>, + di_container: Arc<DIContainerType>, interface_phantom: PhantomData<Interface>, } -impl<Interface> AsyncBindingWhenConfigurator<Interface> +impl<Interface, DIContainerType> AsyncBindingWhenConfigurator<Interface, DIContainerType> where Interface: 'static + ?Sized + Send + Sync, + DIContainerType: IAsyncDIContainer, { - pub(crate) fn new(di_container: Arc<AsyncDIContainer>) -> Self + pub(crate) fn new(di_container: Arc<DIContainerType>) -> 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::<Interface>(None) + .await + .map_or_else( + || { + Err(AsyncBindingWhenConfiguratorError::BindingNotFound( + type_name::<Interface>(), + )) + }, + Ok, + )?; - let binding = bindings_lock.remove::<Interface>(None).map_or_else( - || { - Err(AsyncBindingWhenConfiguratorError::BindingNotFound( - type_name::<Interface>(), - )) - }, - Ok, - )?; - - bindings_lock.set::<Interface>(Some(name), binding); + self.di_container + .set_binding::<Interface>(Some(name), binding) + .await; Ok(()) } |