aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-03 19:38:22 +0200
committerHampusM <hampus@hampusmat.com>2023-10-03 19:38:22 +0200
commit613bf4a8a094df6f048cc8b1fcf2fc425abddd99 (patch)
tree43981a48e4e09ad4c0b96b679184ce6ecb6f4e9f
parente0d399133b0c06011c9b775fd545ce649be37ff4 (diff)
refactor: remove inner mutability of blocking DI container
-rw-r--r--src/di_container/blocking/binding/builder.rs12
-rw-r--r--src/di_container/blocking/binding/scope_configurator.rs16
-rw-r--r--src/di_container/blocking/binding/when_configurator.rs6
-rw-r--r--src/di_container/blocking/mod.rs87
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<Interface>,
@@ -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::<dyn subjects::INumber>::new(
- &mock_di_container,
+ &mut mock_di_container,
MockDependencyHistory::new,
);
@@ -358,7 +358,7 @@ mod tests
.once();
let binding_builder = BindingBuilder::<IUserManagerFactory>::new(
- &mock_di_container,
+ &mut mock_di_container,
MockDependencyHistory::new,
);
@@ -400,7 +400,7 @@ mod tests
.once();
let binding_builder = BindingBuilder::<dyn subjects::IUserManager>::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<DIContainer>,
{
- di_container: &'di_container DIContainer,
+ di_container: &'di_container mut DIContainer,
dependency_history_factory: fn() -> DependencyHistory,
interface_phantom: PhantomData<Interface>,
@@ -32,7 +32,7 @@ where
Implementation: Injectable<DIContainer>,
{
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::<Interface>(
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<Interface>,
}
@@ -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::<dyn subjects::INumber>::new(&di_container_mock);
+ BindingWhenConfigurator::<dyn subjects::INumber>::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<DIContainerBindingStorage<dyn IProvider<Self>>>,
+ binding_storage: DIContainerBindingStorage<dyn IProvider<Self>>,
}
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::<Interface>(binding_options)
+ self.binding_storage.has::<Interface>(binding_options)
}
fn set_binding<Interface>(
- &self,
+ &mut self,
binding_options: BindingOptions<'static>,
provider: Box<dyn IProvider<Self>>,
) where
Interface: 'static + ?Sized,
{
self.binding_storage
- .borrow_mut()
.set::<Interface>(binding_options, provider);
}
fn remove_binding<Interface>(
- &self,
+ &mut self,
binding_options: BindingOptions<'static>,
) -> Option<Box<dyn IProvider<Self>>>
where
Interface: 'static + ?Sized,
{
- self.binding_storage
- .borrow_mut()
- .remove::<Interface>(binding_options)
+ self.binding_storage.remove::<Interface>(binding_options)
}
}
@@ -366,7 +360,6 @@ impl DIContainer
let name = binding_options.name;
self.binding_storage
- .borrow()
.get::<Interface>(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::<dyn subjects::IUserManager>(
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::<dyn subjects::IUserManager>(
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::<dyn subjects::INumber>(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::<dyn subjects::INumber>(
- BindingOptions::new().name("cool"),
- Box::new(mock_provider),
- );
+ di_container.binding_storage.set::<dyn subjects::INumber>(
+ BindingOptions::new().name("cool"),
+ Box::new(mock_provider),
+ );
let first_number_rc = di_container
.get_named::<dyn subjects::INumber>("cool")
@@ -574,7 +561,7 @@ mod tests
#[crate::factory]
type IUserManagerFactory = dyn Fn(Vec<i128>) -> TransientPtr<dyn IUserManager>;
- let di_container = DIContainer::new();
+ let mut di_container = DIContainer::new();
let factory_func: &dyn Fn(&DIContainer) -> Box<IUserManagerFactory> = &|_| {
Box::new(move |users| {
@@ -595,7 +582,6 @@ mod tests
di_container
.binding_storage
- .borrow_mut()
.set::<IUserManagerFactory>(BindingOptions::new(), Box::new(mock_provider));
di_container
@@ -653,7 +639,7 @@ mod tests
#[crate::factory]
type IUserManagerFactory = dyn Fn(Vec<i128>) -> TransientPtr<dyn IUserManager>;
- let di_container = DIContainer::new();
+ let mut di_container = DIContainer::new();
let factory_func: &dyn Fn(&DIContainer) -> Box<IUserManagerFactory> = &|_| {
Box::new(move |users| {
@@ -672,13 +658,10 @@ mod tests
))))
});
- di_container
- .binding_storage
- .borrow_mut()
- .set::<IUserManagerFactory>(
- BindingOptions::new().name("special"),
- Box::new(mock_provider),
- );
+ di_container.binding_storage.set::<IUserManagerFactory>(
+ BindingOptions::new().name("special"),
+ Box::new(mock_provider),
+ );
di_container
.get_named::<IUserManagerFactory>("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::<subjects::Ninja>(BindingOptions::new()));
- di_container
- .binding_storage
- .borrow_mut()
- .set::<subjects::Ninja>(
- BindingOptions::new(),
- Box::new(MockIProvider::new()),
- );
+ di_container.binding_storage.set::<subjects::Ninja>(
+ BindingOptions::new(),
+ Box::new(MockIProvider::new()),
+ );
assert!(di_container.has_binding::<subjects::Ninja>(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::<subjects::Ninja>(
BindingOptions::new(),
@@ -718,22 +698,18 @@ mod tests
assert!(di_container
.binding_storage
- .borrow_mut()
.has::<subjects::Ninja>(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::<subjects::Ninja>(
- BindingOptions::new(),
- Box::new(MockIProvider::new()),
- );
+ di_container.binding_storage.set::<subjects::Ninja>(
+ 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::<subjects::Ninja>(BindingOptions::new())
);
}