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/binding/builder.rs | 49 +++++++++++----------- .../blocking/binding/scope_configurator.rs | 25 +++++------ .../blocking/binding/when_configurator.rs | 13 +++--- 3 files changed, 43 insertions(+), 44 deletions(-) (limited to 'src/di_container/blocking/binding') diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs index 64e787e..8eebcc0 100644 --- a/src/di_container/blocking/binding/builder.rs +++ b/src/di_container/blocking/binding/builder.rs @@ -1,7 +1,6 @@ //! Binding builder for types inside of a [`DIContainer`]. use std::any::type_name; use std::marker::PhantomData; -use std::rc::Rc; use crate::di_container::blocking::binding::scope_configurator::BindingScopeConfigurator; #[cfg(feature = "factory")] @@ -16,22 +15,22 @@ use_double!(crate::di_container::blocking::DIContainer); /// Binding builder for type `Interface` inside a [`DIContainer`]. #[must_use = "No binding will be created if you don't use the binding builder"] -pub struct BindingBuilder +pub struct BindingBuilder<'di_container, Interface> where Interface: 'static + ?Sized, { - di_container: Rc, + di_container: &'di_container DIContainer, dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, } -impl BindingBuilder +impl<'di_container, Interface> BindingBuilder<'di_container, Interface> where Interface: 'static + ?Sized, { pub(crate) fn new( - di_container: Rc, + di_container: &'di_container DIContainer, dependency_history_factory: fn() -> DependencyHistory, ) -> Self { @@ -83,7 +82,10 @@ where /// ``` pub fn to( self, - ) -> Result, BindingBuilderError> + ) -> Result< + BindingScopeConfigurator<'di_container, Interface, Implementation>, + BindingBuilderError, + > where Implementation: Injectable, { @@ -97,7 +99,7 @@ where } let binding_scope_configurator = BindingScopeConfigurator::new( - self.di_container.clone(), + self.di_container, self.dependency_history_factory, ); @@ -151,13 +153,13 @@ where /// di_container /// .bind::() /// .to_factory(&|context| { - /// Box::new(move |name, id| { - /// let customer_id_factory = context - /// .get::() - /// .unwrap() - /// .factory() - /// .unwrap(); + /// let customer_id_factory = context + /// .get::() + /// .unwrap() + /// .factory() + /// .unwrap(); /// + /// Box::new(move |name, id| { /// let customer_id = customer_id_factory(id); /// /// let customer = TransientPtr::new(Customer::new(name, customer_id)); @@ -174,12 +176,12 @@ where pub fn to_factory( self, factory_func: &'static Func, - ) -> Result, BindingBuilderError> + ) -> Result, BindingBuilderError> where Args: std::marker::Tuple + 'static, Return: 'static + ?Sized, Interface: Fn>, - Func: Fn<(std::rc::Rc,), Output = Box>, + Func: Fn(&DIContainer) -> Box, { use crate::private::castable_factory::blocking::CastableFactory; @@ -260,14 +262,13 @@ where pub fn to_default_factory( self, factory_func: &'static FactoryFunc, - ) -> Result, BindingBuilderError> + ) -> Result, BindingBuilderError> where Return: 'static + ?Sized, - FactoryFunc: Fn< - (Rc,), - Output = crate::ptr::TransientPtr< - dyn Fn<(), Output = crate::ptr::TransientPtr>, - >, + FactoryFunc: Fn( + &DIContainer, + ) -> crate::ptr::TransientPtr< + dyn Fn<(), Output = crate::ptr::TransientPtr>, >, { use crate::private::castable_factory::blocking::CastableFactory; @@ -325,7 +326,7 @@ mod tests .once(); let binding_builder = BindingBuilder::::new( - Rc::new(mock_di_container), + &mock_di_container, MockDependencyHistory::new, ); @@ -361,7 +362,7 @@ mod tests .once(); let binding_builder = BindingBuilder::::new( - Rc::new(mock_di_container), + &mock_di_container, MockDependencyHistory::new, ); @@ -403,7 +404,7 @@ mod tests .once(); let binding_builder = BindingBuilder::::new( - Rc::new(mock_di_container), + &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 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 +pub struct BindingScopeConfigurator<'di_container, Interface, Implementation> where Interface: 'static + ?Sized, Implementation: Injectable, { - di_container: Rc, + di_container: &'di_container DIContainer, dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, implementation_phantom: PhantomData, } -impl BindingScopeConfigurator +impl<'di_container, Interface, Implementation> + BindingScopeConfigurator<'di_container, Interface, Implementation> where Interface: 'static + ?Sized, Implementation: Injectable, { pub(crate) fn new( - di_container: Rc, + 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 + 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, BindingScopeConfiguratorError> + ) -> Result< + BindingWhenConfigurator<'di_container, Interface>, + BindingScopeConfiguratorError, + > { let singleton: SingletonPtr = 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()); diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs index 3d267b2..2a1af2c 100644 --- a/src/di_container/blocking/binding/when_configurator.rs +++ b/src/di_container/blocking/binding/when_configurator.rs @@ -1,7 +1,6 @@ //! When configurator for a binding for types inside of a [`DIContainer`]. use std::any::type_name; use std::marker::PhantomData; -use std::rc::Rc; use crate::di_container::BindingOptions; use crate::errors::di_container::BindingWhenConfiguratorError; @@ -10,20 +9,20 @@ use crate::util::use_double; use_double!(crate::di_container::blocking::DIContainer); /// When configurator for a binding for type `Interface` inside a [`DIContainer`]. -pub struct BindingWhenConfigurator +pub struct BindingWhenConfigurator<'di_container, Interface> where Interface: 'static + ?Sized, { - di_container: Rc, + di_container: &'di_container DIContainer, interface_phantom: PhantomData, } -impl BindingWhenConfigurator +impl<'di_container, Interface> BindingWhenConfigurator<'di_container, Interface> where Interface: 'static + ?Sized, { - pub(crate) fn new(di_container: Rc) -> Self + pub(crate) fn new(di_container: &'di_container DIContainer) -> Self { Self { di_container, @@ -88,9 +87,7 @@ mod tests .once(); let binding_when_configurator = - BindingWhenConfigurator::::new(Rc::new( - di_container_mock, - )); + BindingWhenConfigurator::::new(&di_container_mock); assert!(binding_when_configurator.when_named("cool").is_ok()); } -- cgit v1.2.3-18-g5258