diff options
author | HampusM <hampus@hampusmat.com> | 2023-09-17 13:32:06 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-09-17 13:32:06 +0200 |
commit | a1ddd1bc6e1af0d98b87126d3e86da30bc1d3f1c (patch) | |
tree | d4a9cfd9e3970a955e27ac518a593f55e8ad8d9c /src/di_container/blocking/binding/scope_configurator.rs | |
parent | 3e3a853615e97ee7c3aef09736eae8170c6fd78e (diff) |
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
Diffstat (limited to 'src/di_container/blocking/binding/scope_configurator.rs')
-rw-r--r-- | src/di_container/blocking/binding/scope_configurator.rs | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs index be469ba..3d939ba 100644 --- a/src/di_container/blocking/binding/scope_configurator.rs +++ b/src/di_container/blocking/binding/scope_configurator.rs @@ -1,6 +1,5 @@ //! Scope configurator for a binding for types inside of a [`DIContainer`]. use std::marker::PhantomData; -use std::rc::Rc; use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator; use crate::di_container::BindingOptions; @@ -14,25 +13,26 @@ use_double!(crate::dependency_history::DependencyHistory); use_double!(crate::di_container::blocking::DIContainer); /// Scope configurator for a binding for type `Interface` inside a [`DIContainer`]. -pub struct BindingScopeConfigurator<Interface, Implementation> +pub struct BindingScopeConfigurator<'di_container, Interface, Implementation> where Interface: 'static + ?Sized, Implementation: Injectable<DIContainer>, { - di_container: Rc<DIContainer>, + di_container: &'di_container DIContainer, dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData<Interface>, implementation_phantom: PhantomData<Implementation>, } -impl<Interface, Implementation> BindingScopeConfigurator<Interface, Implementation> +impl<'di_container, Interface, Implementation> + BindingScopeConfigurator<'di_container, Interface, Implementation> where Interface: 'static + ?Sized, Implementation: Injectable<DIContainer>, { pub(crate) fn new( - di_container: Rc<DIContainer>, + di_container: &'di_container DIContainer, dependency_history_factory: fn() -> DependencyHistory, ) -> Self { @@ -48,7 +48,7 @@ where /// /// This is the default. #[allow(clippy::must_use_candidate)] - pub fn in_transient_scope(self) -> BindingWhenConfigurator<Interface> + pub fn in_transient_scope(self) -> BindingWhenConfigurator<'di_container, Interface> { self.set_in_transient_scope(); @@ -61,11 +61,14 @@ where /// Will return Err if resolving the implementation fails. pub fn in_singleton_scope( self, - ) -> Result<BindingWhenConfigurator<Interface>, BindingScopeConfiguratorError> + ) -> Result< + BindingWhenConfigurator<'di_container, Interface>, + BindingScopeConfiguratorError, + > { let singleton: SingletonPtr<Implementation> = SingletonPtr::from( Implementation::resolve( - &self.di_container, + self.di_container, (self.dependency_history_factory)(), ) .map_err(BindingScopeConfiguratorError::SingletonResolveFailed)?, @@ -111,8 +114,7 @@ mod tests dyn subjects::IUserManager, subjects::UserManager, >::new( - Rc::new(di_container_mock), - MockDependencyHistory::new, + &di_container_mock, MockDependencyHistory::new ); binding_scope_configurator.in_transient_scope(); @@ -133,8 +135,7 @@ mod tests dyn subjects::IUserManager, subjects::UserManager, >::new( - Rc::new(di_container_mock), - MockDependencyHistory::new, + &di_container_mock, MockDependencyHistory::new ); assert!(binding_scope_configurator.in_singleton_scope().is_ok()); |