From 7bed48c852a741df5a14359916faf21d90d39814 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 31 Aug 2023 19:19:06 +0200 Subject: refactor: pass around BindingOptions instead of name --- src/di_container/asynchronous/mod.rs | 63 +++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 22 deletions(-) (limited to 'src/di_container/asynchronous/mod.rs') diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index e5f7f5d..8d67b99 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -235,10 +235,7 @@ impl IAsyncDIContainer for AsyncDIContainer { Box::pin(async move { let binding_providable = self - .get_binding_providable::( - binding_options.name, - dependency_history, - ) + .get_binding_providable::(binding_options, dependency_history) .await?; self.handle_binding_providable(binding_providable).await @@ -249,16 +246,22 @@ impl IAsyncDIContainer for AsyncDIContainer #[async_trait] impl details::DIContainerInternals for AsyncDIContainer { - async fn has_binding(self: &Arc, name: Option<&'static str>) -> bool + async fn has_binding( + self: &Arc, + binding_options: BindingOptions<'static>, + ) -> bool where Interface: ?Sized + 'static, { - self.binding_storage.lock().await.has::(name) + self.binding_storage + .lock() + .await + .has::(binding_options) } async fn set_binding( self: &Arc, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, provider: Box>, ) where Interface: 'static + ?Sized, @@ -266,17 +269,20 @@ impl details::DIContainerInternals for AsyncDIContainer self.binding_storage .lock() .await - .set::(name, provider); + .set::(binding_options, provider); } async fn remove_binding( self: &Arc, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, ) -> Option>> where Interface: 'static + ?Sized, { - self.binding_storage.lock().await.remove::(name) + self.binding_storage + .lock() + .await + .remove::(binding_options) } } @@ -411,7 +417,7 @@ impl AsyncDIContainer async fn get_binding_providable( self: &Arc, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, dependency_history: DependencyHistory, ) -> Result, AsyncDIContainerError> where @@ -423,12 +429,12 @@ impl AsyncDIContainer let bindings_lock = self.binding_storage.lock().await; provider = bindings_lock - .get::(name) + .get::(binding_options.clone()) .map_or_else( || { Err(AsyncDIContainerError::BindingNotFound { interface: type_name::(), - name, + name: binding_options.name, }) }, Ok, @@ -452,6 +458,7 @@ pub(crate) mod details use async_trait::async_trait; + use crate::di_container::BindingOptions; use crate::provider::r#async::IAsyncProvider; #[async_trait] @@ -459,21 +466,21 @@ pub(crate) mod details { async fn has_binding( self: &Arc, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, ) -> bool where Interface: ?Sized + 'static; async fn set_binding( self: &Arc, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, provider: Box>, ) where Interface: 'static + ?Sized; async fn remove_binding( self: &Arc, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, ) -> Option>> where Interface: 'static + ?Sized; @@ -514,7 +521,10 @@ mod tests .binding_storage .lock() .await - .set::(None, Box::new(mock_provider)); + .set::( + BindingOptions::new(), + Box::new(mock_provider), + ); } di_container @@ -550,7 +560,7 @@ mod tests .lock() .await .set::( - Some("special"), + BindingOptions::new().name("special"), Box::new(mock_provider), ); } @@ -591,7 +601,10 @@ mod tests .binding_storage .lock() .await - .set::(None, Box::new(mock_provider)); + .set::( + BindingOptions::new(), + Box::new(mock_provider), + ); } let first_number_rc = di_container @@ -640,7 +653,7 @@ mod tests .lock() .await .set::( - Some("cool"), + BindingOptions::new().name("cool"), Box::new(mock_provider), ); } @@ -747,7 +760,10 @@ mod tests .binding_storage .lock() .await - .set::(None, Box::new(mock_provider)); + .set::( + BindingOptions::new(), + Box::new(mock_provider), + ); } di_container @@ -843,7 +859,10 @@ mod tests .binding_storage .lock() .await - .set::(Some("special"), Box::new(mock_provider)); + .set::( + BindingOptions::new().name("special"), + Box::new(mock_provider), + ); } di_container -- cgit v1.2.3-18-g5258