From a1ddd1bc6e1af0d98b87126d3e86da30bc1d3f1c Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 17 Sep 2023 13:32:06 +0200 Subject: refactor!: make the blocking DI container not inside a Rc BREAKING CHANGE: The blocking DI container is no longer inside of a Rc. This affects BindingBuilder, BindingScopeConfigurator, BindingWhenConfigurator & Injectable --- src/di_container/blocking/mod.rs | 63 ++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 34 deletions(-) (limited to 'src/di_container/blocking/mod.rs') diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index 09e7db1..a38b5da 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -51,7 +51,6 @@ //! ``` use std::any::type_name; use std::cell::RefCell; -use std::rc::Rc; use crate::di_container::binding_storage::DIContainerBindingStorage; use crate::di_container::blocking::binding::builder::BindingBuilder; @@ -74,6 +73,7 @@ pub(crate) type BindingOptionsWithLt<'a> = BindingOptions<'a>; pub(crate) type BindingOptionsWithLt = BindingOptions<'static>; /// Blocking dependency injection container. +#[derive(Default)] pub struct DIContainer { binding_storage: RefCell>>, @@ -83,11 +83,11 @@ impl DIContainer { /// Returns a new `DIContainer`. #[must_use] - pub fn new() -> Rc + pub fn new() -> Self { - Rc::new(Self { + Self { binding_storage: RefCell::new(DIContainerBindingStorage::new()), - }) + } } } @@ -96,7 +96,7 @@ impl DIContainer { /// Returns a new [`BindingBuilder`] for the given interface. #[allow(clippy::missing_panics_doc)] - pub fn bind(self: &mut Rc) -> BindingBuilder + pub fn bind(&mut self) -> BindingBuilder<'_, Interface> where Interface: 'static + ?Sized, { @@ -104,7 +104,7 @@ impl DIContainer panic!("Nope"); #[cfg(not(test))] - BindingBuilder::new(self.clone(), DependencyHistory::new) + BindingBuilder::new(self, DependencyHistory::new) } /// Returns the type bound with `Interface`. @@ -114,7 +114,7 @@ impl DIContainer /// - No binding for `Interface` exists /// - Resolving the binding for `Interface` fails /// - Casting the binding for `Interface` fails - pub fn get(self: &Rc) -> Result, DIContainerError> + pub fn get(&self) -> Result, DIContainerError> where Interface: 'static + ?Sized, { @@ -129,7 +129,7 @@ impl DIContainer /// - Resolving the binding for `Interface` fails /// - Casting the binding for `Interface` fails pub fn get_named( - self: &Rc, + &self, name: &'static str, ) -> Result, DIContainerError> where @@ -177,7 +177,7 @@ impl DIContainer /// # } /// ``` pub fn get_bound( - self: &Rc, + &self, dependency_history: DependencyHistory, binding_options: BindingOptionsWithLt, ) -> Result, DIContainerError> @@ -215,7 +215,7 @@ impl DIContainer binding_kind: "factory", })?; - Ok(SomePtr::Factory(factory.call(self.clone()).into())) + Ok(SomePtr::Factory(factory.call(self).into())) } #[cfg(feature = "factory")] Providable::DefaultFactory(factory_binding) => { @@ -234,15 +234,12 @@ impl DIContainer binding_kind: "default factory", })?; - Ok(SomePtr::Transient(default_factory.call(self.clone())())) + Ok(SomePtr::Transient(default_factory.call(self)())) } } } - fn has_binding( - self: &Rc, - binding_options: BindingOptionsWithLt, - ) -> bool + fn has_binding(&self, binding_options: BindingOptionsWithLt) -> bool where Interface: ?Sized + 'static, { @@ -252,7 +249,7 @@ impl DIContainer } fn set_binding( - self: &Rc, + &self, binding_options: BindingOptions<'static>, provider: Box>, ) where @@ -264,7 +261,7 @@ impl DIContainer } fn remove_binding( - self: &Rc, + &self, binding_options: BindingOptions<'static>, ) -> Option>> where @@ -279,7 +276,7 @@ impl DIContainer impl DIContainer { fn get_binding_providable( - self: &Rc, + &self, binding_options: BindingOptionsWithLt, dependency_history: DependencyHistory, ) -> Result, DIContainerError> @@ -494,15 +491,14 @@ mod tests let di_container = DIContainer::new(); - let factory_func: &dyn Fn(Rc) -> Box = - &|_: Rc| { - Box::new(move |users| { - let user_manager: TransientPtr = - TransientPtr::new(UserManager::new(users)); + let factory_func: &dyn Fn(&DIContainer) -> Box = &|_| { + Box::new(move |users| { + let user_manager: TransientPtr = + TransientPtr::new(UserManager::new(users)); - user_manager - }) - }; + user_manager + }) + }; let mut mock_provider = MockIProvider::new(); @@ -572,15 +568,14 @@ mod tests let di_container = DIContainer::new(); - let factory_func: &dyn Fn(Rc) -> Box = - &|_: Rc| { - Box::new(move |users| { - let user_manager: TransientPtr = - TransientPtr::new(UserManager::new(users)); + let factory_func: &dyn Fn(&DIContainer) -> Box = &|_| { + Box::new(move |users| { + let user_manager: TransientPtr = + TransientPtr::new(UserManager::new(users)); - user_manager - }) - }; + user_manager + }) + }; let mut mock_provider = MockIProvider::new(); -- cgit v1.2.3-18-g5258