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/blocking/binding/builder.rs | 44 +++++++++------ .../blocking/binding/scope_configurator.rs | 13 +++-- .../blocking/binding/when_configurator.rs | 9 ++-- src/di_container/blocking/mod.rs | 63 ++++++++++++++-------- 4 files changed, 82 insertions(+), 47 deletions(-) (limited to 'src/di_container/blocking') diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs index 0c323ec..577f034 100644 --- a/src/di_container/blocking/binding/builder.rs +++ b/src/di_container/blocking/binding/builder.rs @@ -9,6 +9,7 @@ use crate::di_container::blocking::binding::scope_configurator::BindingScopeConf #[cfg(feature = "factory")] use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator; use crate::di_container::blocking::IDIContainer; +use crate::di_container::BindingOptions; use crate::errors::di_container::BindingBuilderError; use crate::interfaces::injectable::Injectable; use crate::util::use_double; @@ -99,7 +100,10 @@ where Implementation: Injectable, { { - if self.di_container.has_binding::(None) { + if self + .di_container + .has_binding::(BindingOptions::new()) + { return Err(BindingBuilderError::BindingAlreadyExists(type_name::< Interface, >( @@ -197,7 +201,10 @@ where { use crate::private::castable_factory::blocking::CastableFactory; - if self.di_container.has_binding::(None) { + if self + .di_container + .has_binding::(BindingOptions::new()) + { return Err(BindingBuilderError::BindingAlreadyExists(type_name::< Interface, >())); @@ -206,7 +213,7 @@ where let factory_impl = CastableFactory::new(factory_func); self.di_container.set_binding::( - None, + BindingOptions::new(), Box::new(crate::provider::blocking::FactoryProvider::new( crate::ptr::FactoryPtr::new(factory_impl), false, @@ -286,7 +293,10 @@ where { use crate::private::castable_factory::blocking::CastableFactory; - if self.di_container.has_binding::(None) { + if self + .di_container + .has_binding::(BindingOptions::new()) + { return Err(BindingBuilderError::BindingAlreadyExists(type_name::< Interface, >())); @@ -295,7 +305,7 @@ where let factory_impl = CastableFactory::new(factory_func); self.di_container.set_binding::( - None, + BindingOptions::new(), Box::new(crate::provider::blocking::FactoryProvider::new( crate::ptr::FactoryPtr::new(factory_impl), true, @@ -324,14 +334,14 @@ mod tests mock_di_container .expect_has_binding::() - .with(eq(None)) - .return_once(|_name| false) + .with(eq(BindingOptions::new())) + .return_once(|_options| false) .once(); mock_di_container .expect_set_binding::() - .withf(|name, _provider| name.is_none()) - .return_once(|_name, _provider| ()) + .withf(|options, _provider| options.name.is_none()) + .return_once(|_options, _provider| ()) .once(); let binding_builder = @@ -361,14 +371,14 @@ mod tests mock_di_container .expect_has_binding::() - .with(eq(None)) - .return_once(|_name| false) + .with(eq(BindingOptions::new())) + .return_once(|_| false) .once(); mock_di_container .expect_set_binding::() - .withf(|name, _provider| name.is_none()) - .return_once(|_name, _provider| ()) + .withf(|options, _provider| options.name.is_none()) + .return_once(|_, _provider| ()) .once(); let binding_builder = @@ -404,14 +414,14 @@ mod tests mock_di_container .expect_has_binding::() - .with(eq(None)) - .return_once(|_name| false) + .with(eq(BindingOptions::new())) + .return_once(|_| false) .once(); mock_di_container .expect_set_binding::() - .withf(|name, _provider| name.is_none()) - .return_once(|_name, _provider| ()) + .withf(|options, _provider| options.name.is_none()) + .return_once(|_, _provider| ()) .once(); let binding_builder = diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs index 0aefa93..0fcdfdf 100644 --- a/src/di_container/blocking/binding/scope_configurator.rs +++ b/src/di_container/blocking/binding/scope_configurator.rs @@ -6,6 +6,7 @@ use std::rc::Rc; use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator; use crate::di_container::blocking::IDIContainer; +use crate::di_container::BindingOptions; use crate::errors::di_container::BindingScopeConfiguratorError; use crate::interfaces::injectable::Injectable; use crate::provider::blocking::{SingletonProvider, TransientTypeProvider}; @@ -81,8 +82,10 @@ where .map_err(BindingScopeConfiguratorError::SingletonResolveFailed)?, ); - self.di_container - .set_binding::(None, Box::new(SingletonProvider::new(singleton))); + self.di_container.set_binding::( + BindingOptions::new(), + Box::new(SingletonProvider::new(singleton)), + ); Ok(BindingWhenConfigurator::new(self.di_container)) } @@ -90,7 +93,7 @@ where pub(crate) fn set_in_transient_scope(&self) { self.di_container.set_binding::( - None, + BindingOptions::new(), Box::new(TransientTypeProvider::::new()), ); } @@ -110,7 +113,7 @@ mod tests di_container_mock .expect_set_binding::() - .withf(|name, _provider| name.is_none()) + .withf(|options, _provider| options.name.is_none()) .return_once(|_name, _provider| ()) .once(); @@ -131,7 +134,7 @@ mod tests di_container_mock .expect_set_binding::() - .withf(|name, _provider| name.is_none()) + .withf(|options, _provider| options.name.is_none()) .return_once(|_name, _provider| ()) .once(); diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs index fcef377..52b23ff 100644 --- a/src/di_container/blocking/binding/when_configurator.rs +++ b/src/di_container/blocking/binding/when_configurator.rs @@ -6,6 +6,7 @@ use std::marker::PhantomData; use std::rc::Rc; use crate::di_container::blocking::IDIContainer; +use crate::di_container::BindingOptions; use crate::errors::di_container::BindingWhenConfiguratorError; /// When configurator for a binding for type `Interface` inside a [`IDIContainer`]. @@ -45,7 +46,7 @@ where { let binding = self .di_container - .remove_binding::(None) + .remove_binding::(BindingOptions::new()) .map_or_else( || { Err(BindingWhenConfiguratorError::BindingNotFound(type_name::< @@ -57,7 +58,7 @@ where )?; self.di_container - .set_binding::(Some(name), binding); + .set_binding::(BindingOptions::new().name(name), binding); Ok(()) } @@ -79,13 +80,13 @@ mod tests di_container_mock .expect_remove_binding::() - .with(eq(None)) + .with(eq(BindingOptions::new())) .return_once(|_name| Some(Box::new(MockIProvider::new()))) .once(); di_container_mock .expect_set_binding::() - .withf(|name, _provider| name == &Some("cool")) + .withf(|options, _provider| options.name == Some("cool")) .return_once(|_name, _provider| ()) .once(); diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index 5b0acc8..27ea0fb 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -210,10 +210,8 @@ impl IDIContainer for DIContainer where Interface: 'static + ?Sized, { - let binding_providable = self.get_binding_providable::( - binding_options.name, - dependency_history, - )?; + let binding_providable = self + .get_binding_providable::(binding_options, dependency_history)?; #[cfg(feature = "factory")] return self.handle_binding_providable(binding_providable); @@ -225,33 +223,37 @@ impl IDIContainer for DIContainer impl details::DIContainerInternals for DIContainer { - fn has_binding(self: &Rc, name: Option<&'static str>) -> bool + fn has_binding(self: &Rc, binding_options: BindingOptions) -> bool where Interface: ?Sized + 'static, { - self.binding_storage.borrow().has::(name) + self.binding_storage + .borrow() + .has::(binding_options) } fn set_binding( self: &Rc, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, provider: Box>, ) where Interface: 'static + ?Sized, { self.binding_storage .borrow_mut() - .set::(name, provider); + .set::(binding_options, provider); } fn remove_binding( self: &Rc, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, ) -> Option>> where Interface: 'static + ?Sized, { - self.binding_storage.borrow_mut().remove::(name) + self.binding_storage + .borrow_mut() + .remove::(binding_options) } } @@ -316,15 +318,17 @@ impl DIContainer fn get_binding_providable( self: &Rc, - name: Option<&str>, + binding_options: BindingOptions, dependency_history: DependencyHistory, ) -> Result, DIContainerError> where Interface: 'static + ?Sized, { + let name = binding_options.name; + self.binding_storage .borrow() - .get::(name) + .get::(binding_options) .map_or_else( || { Err(DIContainerError::BindingNotFound { @@ -346,24 +350,29 @@ pub(crate) mod details { use std::rc::Rc; + use crate::di_container::blocking::BindingOptionsWithLt; + use crate::di_container::BindingOptions; use crate::provider::blocking::IProvider; pub trait DIContainerInternals { - fn has_binding(self: &Rc, name: Option<&'static str>) -> bool + fn has_binding( + self: &Rc, + binding_options: BindingOptionsWithLt, + ) -> bool where Interface: ?Sized + 'static; fn set_binding( self: &Rc, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, provider: Box>, ) where Interface: 'static + ?Sized; fn remove_binding( self: &Rc, - name: Option<&'static str>, + binding_options: BindingOptions<'static>, ) -> Option>> where Interface: 'static + ?Sized; @@ -395,7 +404,10 @@ mod tests di_container .binding_storage .borrow_mut() - .set::(None, Box::new(mock_provider)); + .set::( + BindingOptions::new(), + Box::new(mock_provider), + ); di_container .get::()? @@ -420,7 +432,10 @@ mod tests di_container .binding_storage .borrow_mut() - .set::(Some("special"), Box::new(mock_provider)); + .set::( + BindingOptions::new().name("special"), + Box::new(mock_provider), + ); di_container .get_named::("special")? @@ -447,7 +462,7 @@ mod tests di_container .binding_storage .borrow_mut() - .set::(None, Box::new(mock_provider)); + .set::(BindingOptions::new(), Box::new(mock_provider)); let first_number_rc = di_container.get::()?.singleton()?; @@ -479,7 +494,10 @@ mod tests di_container .binding_storage .borrow_mut() - .set::(Some("cool"), Box::new(mock_provider)); + .set::( + BindingOptions::new().name("cool"), + Box::new(mock_provider), + ); let first_number_rc = di_container .get_named::("cool")? @@ -567,7 +585,7 @@ mod tests di_container .binding_storage .borrow_mut() - .set::(None, Box::new(mock_provider)); + .set::(BindingOptions::new(), Box::new(mock_provider)); di_container.get::()?.factory()?; @@ -645,7 +663,10 @@ mod tests di_container .binding_storage .borrow_mut() - .set::(Some("special"), Box::new(mock_provider)); + .set::( + BindingOptions::new().name("special"), + Box::new(mock_provider), + ); di_container .get_named::("special")? -- cgit v1.2.3-18-g5258