diff options
author | HampusM <hampus@hampusmat.com> | 2023-09-18 22:35:58 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-09-18 22:35:58 +0200 |
commit | 4fd0d6b4951b08a20d5378bca75561109dc6d036 (patch) | |
tree | 8496d679e7ee3debcd415ed563911e2166594212 /src/di_container/asynchronous/binding/builder.rs | |
parent | de2e1349f459f7f69226b2decd366be690426ea7 (diff) |
refactor!: make the async DI container not inside a Arc
BREAKING CHANGE: The async DI container is no longer inside of a Arc. This affects AsyncBindingBuilder, AsyncBindingScopeConfigurator, AsyncBindingWhenConfigurator & AsyncInjectable
Diffstat (limited to 'src/di_container/asynchronous/binding/builder.rs')
-rw-r--r-- | src/di_container/asynchronous/binding/builder.rs | 74 |
1 files changed, 40 insertions, 34 deletions
diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index c253929..acb1c69 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -1,7 +1,6 @@ //! Binding builder for types inside of a [`AsyncDIContainer`]. use std::any::type_name; use std::marker::PhantomData; -use std::sync::Arc; use crate::di_container::asynchronous::binding::scope_configurator::AsyncBindingScopeConfigurator; #[cfg(feature = "factory")] @@ -21,22 +20,22 @@ pub type BoxFn<Args, Return> = Box<(dyn Fn<Args, Output = Return> + Send + Sync) /// Binding builder for type `Interface` inside a [`AsyncDIContainer`]. #[must_use = "No binding will be created if you don't use the binding builder"] -pub struct AsyncBindingBuilder<Interface> +pub struct AsyncBindingBuilder<'di_container, Interface> where Interface: 'static + ?Sized + Send + Sync, { - di_container: Arc<AsyncDIContainer>, + di_container: &'di_container AsyncDIContainer, dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData<Interface>, } -impl<Interface> AsyncBindingBuilder<Interface> +impl<'di_container, Interface> AsyncBindingBuilder<'di_container, Interface> where Interface: 'static + ?Sized + Send + Sync, { pub(crate) fn new( - di_container: Arc<AsyncDIContainer>, + di_container: &'di_container AsyncDIContainer, dependency_history_factory: fn() -> DependencyHistory, ) -> Self { @@ -91,7 +90,7 @@ where pub async fn to<Implementation>( self, ) -> Result< - AsyncBindingScopeConfigurator<Interface, Implementation>, + AsyncBindingScopeConfigurator<'di_container, Interface, Implementation>, AsyncBindingBuilderError, > where @@ -109,7 +108,7 @@ where } let binding_scope_configurator = AsyncBindingScopeConfigurator::new( - self.di_container.clone(), + self.di_container, self.dependency_history_factory, ); @@ -170,13 +169,15 @@ where pub async fn to_factory<Args, Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, - ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingBuilderError> + ) -> Result< + AsyncBindingWhenConfigurator<'di_container, Interface>, + AsyncBindingBuilderError, + > where Args: std::marker::Tuple + 'static, Return: 'static + ?Sized, Interface: Fn<Args, Output = Return> + Send + Sync, - FactoryFunc: - Fn<(Arc<AsyncDIContainer>,), Output = BoxFn<Args, Return>> + Send + Sync, + FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn<Args, Return> + Send + Sync, { use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::provider::r#async::AsyncFactoryVariant; @@ -204,7 +205,7 @@ where ) .await; - Ok(AsyncBindingWhenConfigurator::new(self.di_container.clone())) + Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } /// Creates a binding of factory type `Interface` to a async factory inside of the @@ -268,16 +269,19 @@ where pub async fn to_async_factory<Args, Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, - ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingBuilderError> + ) -> Result< + AsyncBindingWhenConfigurator<'di_container, Interface>, + AsyncBindingBuilderError, + > where Args: std::marker::Tuple + 'static, Return: 'static + ?Sized, Interface: Fn<Args, Output = crate::future::BoxFuture<'static, Return>> + Send + Sync, - FactoryFunc: Fn< - (Arc<AsyncDIContainer>,), - Output = BoxFn<Args, crate::future::BoxFuture<'static, Return>>, - > + Send + FactoryFunc: Fn( + &AsyncDIContainer, + ) -> BoxFn<Args, crate::future::BoxFuture<'static, Return>> + + Send + Sync, { use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; @@ -306,7 +310,7 @@ where ) .await; - Ok(AsyncBindingWhenConfigurator::new(self.di_container.clone())) + Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } /// Creates a binding of type `Interface` to a factory that takes no arguments @@ -360,13 +364,14 @@ where pub async fn to_default_factory<Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, - ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingBuilderError> + ) -> Result< + AsyncBindingWhenConfigurator<'di_container, Interface>, + AsyncBindingBuilderError, + > where Return: 'static + ?Sized, - FactoryFunc: Fn< - (Arc<AsyncDIContainer>,), - Output = BoxFn<(), crate::ptr::TransientPtr<Return>>, - > + Send + FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn<(), crate::ptr::TransientPtr<Return>> + + Send + Sync, { use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; @@ -395,7 +400,7 @@ where ) .await; - Ok(AsyncBindingWhenConfigurator::new(self.di_container.clone())) + Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } /// Creates a binding of factory type `Interface` to a async factory inside of the @@ -454,13 +459,14 @@ where pub async fn to_async_default_factory<Return, FactoryFunc>( self, factory_func: &'static FactoryFunc, - ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingBuilderError> + ) -> Result< + AsyncBindingWhenConfigurator<'di_container, Interface>, + AsyncBindingBuilderError, + > where Return: 'static + ?Sized, - FactoryFunc: Fn< - (Arc<AsyncDIContainer>,), - Output = BoxFn<(), crate::future::BoxFuture<'static, Return>>, - > + Send + FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn<(), crate::future::BoxFuture<'static, Return>> + + Send + Sync, { use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; @@ -489,7 +495,7 @@ where ) .await; - Ok(AsyncBindingWhenConfigurator::new(self.di_container.clone())) + Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } } @@ -524,7 +530,7 @@ mod tests let binding_builder = AsyncBindingBuilder::<dyn subjects_async::IUserManager>::new( - Arc::new(di_container_mock), + &di_container_mock, MockDependencyHistory::new, ); @@ -565,7 +571,7 @@ mod tests .once(); let binding_builder = AsyncBindingBuilder::<IUserManagerFactory>::new( - Arc::new(di_container_mock), + &di_container_mock, MockDependencyHistory::new, ); @@ -614,7 +620,7 @@ mod tests .once(); let binding_builder = AsyncBindingBuilder::<IUserManagerFactory>::new( - Arc::new(di_container_mock), + &di_container_mock, MockDependencyHistory::new, ); @@ -659,7 +665,7 @@ mod tests let binding_builder = AsyncBindingBuilder::<dyn subjects_async::IUserManager>::new( - Arc::new(di_container_mock), + &di_container_mock, MockDependencyHistory::new, ); @@ -705,7 +711,7 @@ mod tests let binding_builder = AsyncBindingBuilder::<dyn subjects_async::IUserManager>::new( - Arc::new(di_container_mock), + &di_container_mock, MockDependencyHistory::new, ); |