From 613bf4a8a094df6f048cc8b1fcf2fc425abddd99 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 3 Oct 2023 19:38:22 +0200 Subject: refactor: remove inner mutability of blocking DI container --- src/di_container/blocking/binding/builder.rs | 12 +-- .../blocking/binding/scope_configurator.rs | 16 ++-- .../blocking/binding/when_configurator.rs | 6 +- src/di_container/blocking/mod.rs | 87 ++++++++-------------- 4 files changed, 50 insertions(+), 71 deletions(-) diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs index cd547d4..f322809 100644 --- a/src/di_container/blocking/binding/builder.rs +++ b/src/di_container/blocking/binding/builder.rs @@ -19,7 +19,7 @@ pub struct BindingBuilder<'di_container, Interface> where Interface: 'static + ?Sized, { - di_container: &'di_container DIContainer, + di_container: &'di_container mut DIContainer, dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, @@ -30,7 +30,7 @@ where Interface: 'static + ?Sized, { pub(crate) fn new( - di_container: &'di_container DIContainer, + di_container: &'di_container mut DIContainer, dependency_history_factory: fn() -> DependencyHistory, ) -> Self { @@ -98,7 +98,7 @@ where >())); } - let binding_scope_configurator = BindingScopeConfigurator::new( + let mut binding_scope_configurator = BindingScopeConfigurator::new( self.di_container, self.dependency_history_factory, ); @@ -324,7 +324,7 @@ mod tests .once(); let binding_builder = BindingBuilder::::new( - &mock_di_container, + &mut mock_di_container, MockDependencyHistory::new, ); @@ -358,7 +358,7 @@ mod tests .once(); let binding_builder = BindingBuilder::::new( - &mock_di_container, + &mut mock_di_container, MockDependencyHistory::new, ); @@ -400,7 +400,7 @@ mod tests .once(); let binding_builder = BindingBuilder::::new( - &mock_di_container, + &mut mock_di_container, MockDependencyHistory::new, ); diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs index ee935a5..ef7578b 100644 --- a/src/di_container/blocking/binding/scope_configurator.rs +++ b/src/di_container/blocking/binding/scope_configurator.rs @@ -18,7 +18,7 @@ where Interface: 'static + ?Sized, Implementation: Injectable, { - di_container: &'di_container DIContainer, + di_container: &'di_container mut DIContainer, dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, @@ -32,7 +32,7 @@ where Implementation: Injectable, { pub(crate) fn new( - di_container: &'di_container DIContainer, + di_container: &'di_container mut DIContainer, dependency_history_factory: fn() -> DependencyHistory, ) -> Self { @@ -75,7 +75,9 @@ where /// # } /// ``` #[allow(clippy::must_use_candidate)] - pub fn in_transient_scope(self) -> BindingWhenConfigurator<'di_container, Interface> + pub fn in_transient_scope( + mut self, + ) -> BindingWhenConfigurator<'di_container, Interface> { self.set_in_transient_scope(); @@ -161,7 +163,7 @@ where Ok(BindingWhenConfigurator::new(self.di_container)) } - pub(crate) fn set_in_transient_scope(&self) + pub(crate) fn set_in_transient_scope(&mut self) { self.di_container.set_binding::( BindingOptions::new(), @@ -193,7 +195,8 @@ mod tests dyn subjects::IUserManager, subjects::UserManager, >::new( - &di_container_mock, MockDependencyHistory::new + &mut di_container_mock, + MockDependencyHistory::new, ); binding_scope_configurator.in_transient_scope(); @@ -214,7 +217,8 @@ mod tests dyn subjects::IUserManager, subjects::UserManager, >::new( - &di_container_mock, MockDependencyHistory::new + &mut di_container_mock, + MockDependencyHistory::new, ); assert!(binding_scope_configurator.in_singleton_scope().is_ok()); diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs index d23d213..e40f7b9 100644 --- a/src/di_container/blocking/binding/when_configurator.rs +++ b/src/di_container/blocking/binding/when_configurator.rs @@ -13,7 +13,7 @@ pub struct BindingWhenConfigurator<'di_container, Interface> where Interface: 'static + ?Sized, { - di_container: &'di_container DIContainer, + di_container: &'di_container mut DIContainer, interface_phantom: PhantomData, } @@ -22,7 +22,7 @@ impl<'di_container, Interface> BindingWhenConfigurator<'di_container, Interface> where Interface: 'static + ?Sized, { - pub(crate) fn new(di_container: &'di_container DIContainer) -> Self + pub(crate) fn new(di_container: &'di_container mut DIContainer) -> Self { Self { di_container, @@ -115,7 +115,7 @@ mod tests .once(); let binding_when_configurator = - BindingWhenConfigurator::::new(&di_container_mock); + BindingWhenConfigurator::::new(&mut di_container_mock); assert!(binding_when_configurator.when_named("cool").is_ok()); } diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index ce3edca..65e697e 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -50,7 +50,6 @@ //! } //! ``` use std::any::type_name; -use std::cell::RefCell; use crate::di_container::binding_storage::DIContainerBindingStorage; use crate::di_container::blocking::binding::builder::BindingBuilder; @@ -76,7 +75,7 @@ pub(crate) type BindingOptionsWithLt = BindingOptions<'static>; #[derive(Default)] pub struct DIContainer { - binding_storage: RefCell>>, + binding_storage: DIContainerBindingStorage>, } impl DIContainer @@ -86,7 +85,7 @@ impl DIContainer pub fn new() -> Self { Self { - binding_storage: RefCell::new(DIContainerBindingStorage::new()), + binding_storage: DIContainerBindingStorage::new(), } } } @@ -323,33 +322,28 @@ impl DIContainer where Interface: ?Sized + 'static, { - self.binding_storage - .borrow() - .has::(binding_options) + self.binding_storage.has::(binding_options) } fn set_binding( - &self, + &mut self, binding_options: BindingOptions<'static>, provider: Box>, ) where Interface: 'static + ?Sized, { self.binding_storage - .borrow_mut() .set::(binding_options, provider); } fn remove_binding( - &self, + &mut self, binding_options: BindingOptions<'static>, ) -> Option>> where Interface: 'static + ?Sized, { - self.binding_storage - .borrow_mut() - .remove::(binding_options) + self.binding_storage.remove::(binding_options) } } @@ -366,7 +360,6 @@ impl DIContainer let name = binding_options.name; self.binding_storage - .borrow() .get::(binding_options) .map_or_else( || { @@ -396,7 +389,7 @@ mod tests #[test] fn can_get() { - let di_container = DIContainer::new(); + let mut di_container = DIContainer::new(); let mut mock_provider = MockIProvider::new(); @@ -408,7 +401,6 @@ mod tests di_container .binding_storage - .borrow_mut() .set::( BindingOptions::new(), Box::new(mock_provider), @@ -424,7 +416,7 @@ mod tests #[test] fn can_get_named() { - let di_container = DIContainer::new(); + let mut di_container = DIContainer::new(); let mut mock_provider = MockIProvider::new(); @@ -436,7 +428,6 @@ mod tests di_container .binding_storage - .borrow_mut() .set::( BindingOptions::new().name("special"), Box::new(mock_provider), @@ -452,7 +443,7 @@ mod tests #[test] fn can_get_singleton() { - let di_container = DIContainer::new(); + let mut di_container = DIContainer::new(); let mut mock_provider = MockIProvider::new(); @@ -466,7 +457,6 @@ mod tests di_container .binding_storage - .borrow_mut() .set::(BindingOptions::new(), Box::new(mock_provider)); let first_number_rc = di_container @@ -489,7 +479,7 @@ mod tests #[test] fn can_get_singleton_named() { - let di_container = DIContainer::new(); + let mut di_container = DIContainer::new(); let mut mock_provider = MockIProvider::new(); @@ -501,13 +491,10 @@ mod tests .expect_provide() .returning_st(move |_, _| Ok(Providable::Singleton(singleton.clone()))); - di_container - .binding_storage - .borrow_mut() - .set::( - BindingOptions::new().name("cool"), - Box::new(mock_provider), - ); + di_container.binding_storage.set::( + BindingOptions::new().name("cool"), + Box::new(mock_provider), + ); let first_number_rc = di_container .get_named::("cool") @@ -574,7 +561,7 @@ mod tests #[crate::factory] type IUserManagerFactory = dyn Fn(Vec) -> TransientPtr; - let di_container = DIContainer::new(); + let mut di_container = DIContainer::new(); let factory_func: &dyn Fn(&DIContainer) -> Box = &|_| { Box::new(move |users| { @@ -595,7 +582,6 @@ mod tests di_container .binding_storage - .borrow_mut() .set::(BindingOptions::new(), Box::new(mock_provider)); di_container @@ -653,7 +639,7 @@ mod tests #[crate::factory] type IUserManagerFactory = dyn Fn(Vec) -> TransientPtr; - let di_container = DIContainer::new(); + let mut di_container = DIContainer::new(); let factory_func: &dyn Fn(&DIContainer) -> Box = &|_| { Box::new(move |users| { @@ -672,13 +658,10 @@ mod tests )))) }); - di_container - .binding_storage - .borrow_mut() - .set::( - BindingOptions::new().name("special"), - Box::new(mock_provider), - ); + di_container.binding_storage.set::( + BindingOptions::new().name("special"), + Box::new(mock_provider), + ); di_container .get_named::("special") @@ -690,18 +673,15 @@ mod tests #[test] fn has_binding_works() { - let di_container = DIContainer::new(); + let mut di_container = DIContainer::new(); // No binding is present yet assert!(!di_container.has_binding::(BindingOptions::new())); - di_container - .binding_storage - .borrow_mut() - .set::( - BindingOptions::new(), - Box::new(MockIProvider::new()), - ); + di_container.binding_storage.set::( + BindingOptions::new(), + Box::new(MockIProvider::new()), + ); assert!(di_container.has_binding::(BindingOptions::new())); } @@ -709,7 +689,7 @@ mod tests #[test] fn set_binding_works() { - let di_container = DIContainer::new(); + let mut di_container = DIContainer::new(); di_container.set_binding::( BindingOptions::new(), @@ -718,22 +698,18 @@ mod tests assert!(di_container .binding_storage - .borrow_mut() .has::(BindingOptions::new())); } #[test] fn remove_binding_works() { - let di_container = DIContainer::new(); + let mut di_container = DIContainer::new(); - di_container - .binding_storage - .borrow_mut() - .set::( - BindingOptions::new(), - Box::new(MockIProvider::new()), - ); + di_container.binding_storage.set::( + BindingOptions::new(), + Box::new(MockIProvider::new()), + ); assert!( // Formatting is weird without this comment @@ -746,7 +722,6 @@ mod tests // Formatting is weird without this comment !di_container .binding_storage - .borrow_mut() .has::(BindingOptions::new()) ); } -- cgit v1.2.3-18-g5258