diff options
author | HampusM <hampus@hampusmat.com> | 2024-09-15 23:02:34 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-09-15 23:22:33 +0200 |
commit | a34f7c03779aaf90f34b5ff59587daf1db42de8d (patch) | |
tree | d5a2f057b70932e44999fd0f2389811fad5a4478 /src/provider | |
parent | b655d0e439da0da468e02d394e1189c5603fe752 (diff) |
refactor: rename factory provider to function provider
Diffstat (limited to 'src/provider')
-rw-r--r-- | src/provider/async.rs | 122 | ||||
-rw-r--r-- | src/provider/blocking.rs | 77 |
2 files changed, 105 insertions, 94 deletions
diff --git a/src/provider/async.rs b/src/provider/async.rs index 68eed87..b011d7a 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -15,25 +15,23 @@ pub enum AsyncProvidable<DIContainerT> Transient(TransientPtr<dyn AsyncInjectable<DIContainerT>>), Singleton(ThreadsafeSingletonPtr<dyn AsyncInjectable<DIContainerT>>), #[cfg(feature = "factory")] - Factory( - crate::ptr::ThreadsafeFactoryPtr< - dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, - >, - ), - #[cfg(feature = "factory")] - DefaultFactory( - crate::ptr::ThreadsafeFactoryPtr< - dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, - >, - ), - #[cfg(feature = "factory")] - AsyncDefaultFactory( - crate::ptr::ThreadsafeFactoryPtr< + Function( + std::sync::Arc< dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, >, + ProvidableFunctionKind, ), } +#[cfg(feature = "factory")] +#[derive(Debug, Clone, Copy)] +pub enum ProvidableFunctionKind +{ + UserCalled, + Instant, + AsyncInstant, +} + #[async_trait] #[cfg_attr(test, mockall::automock, allow(dead_code))] pub trait IAsyncProvider<DIContainerT>: Send + Sync @@ -177,40 +175,34 @@ where } #[cfg(feature = "factory")] -#[derive(Clone, Copy)] -pub enum AsyncFactoryVariant -{ - Normal, - Default, - AsyncDefault, -} - -#[cfg(feature = "factory")] -pub struct AsyncFactoryProvider +pub struct AsyncFunctionProvider { - factory: crate::ptr::ThreadsafeFactoryPtr< + function: std::sync::Arc< dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, >, - variant: AsyncFactoryVariant, + providable_func_kind: ProvidableFunctionKind, } #[cfg(feature = "factory")] -impl AsyncFactoryProvider +impl AsyncFunctionProvider { pub fn new( - factory: crate::ptr::ThreadsafeFactoryPtr< + function: std::sync::Arc< dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, >, - variant: AsyncFactoryVariant, + providable_func_kind: ProvidableFunctionKind, ) -> Self { - Self { factory, variant } + Self { + function, + providable_func_kind, + } } } #[cfg(feature = "factory")] #[async_trait] -impl<DIContainerT> IAsyncProvider<DIContainerT> for AsyncFactoryProvider +impl<DIContainerT> IAsyncProvider<DIContainerT> for AsyncFunctionProvider where DIContainerT: Send + Sync, { @@ -220,15 +212,10 @@ where _dependency_history: DependencyHistory, ) -> Result<AsyncProvidable<DIContainerT>, InjectableError> { - Ok(match self.variant { - AsyncFactoryVariant::Normal => AsyncProvidable::Factory(self.factory.clone()), - AsyncFactoryVariant::Default => { - AsyncProvidable::DefaultFactory(self.factory.clone()) - } - AsyncFactoryVariant::AsyncDefault => { - AsyncProvidable::AsyncDefaultFactory(self.factory.clone()) - } - }) + Ok(AsyncProvidable::Function( + self.function.clone(), + self.providable_func_kind, + )) } fn do_clone(&self) -> Box<dyn IAsyncProvider<DIContainerT>> @@ -238,13 +225,13 @@ where } #[cfg(feature = "factory")] -impl Clone for AsyncFactoryProvider +impl Clone for AsyncFunctionProvider { fn clone(&self) -> Self { Self { - factory: self.factory.clone(), - variant: self.variant, + function: self.function.clone(), + providable_func_kind: self.providable_func_kind, } } } @@ -305,13 +292,13 @@ mod tests #[tokio::test] #[cfg(feature = "factory")] - async fn async_factory_provider_works() + async fn function_provider_works() { use std::any::Any; + use std::sync::Arc; use crate::castable_function::threadsafe::AnyThreadsafeCastableFunction; use crate::castable_function::AnyCastableFunction; - use crate::ptr::ThreadsafeFactoryPtr; #[derive(Debug)] struct FooFactory; @@ -326,54 +313,63 @@ mod tests impl AnyThreadsafeCastableFunction for FooFactory {} - let factory_provider = AsyncFactoryProvider::new( - ThreadsafeFactoryPtr::new(FooFactory), - AsyncFactoryVariant::Normal, + let user_called_func_provider = AsyncFunctionProvider::new( + Arc::new(FooFactory), + ProvidableFunctionKind::UserCalled, ); - let default_factory_provider = AsyncFactoryProvider::new( - ThreadsafeFactoryPtr::new(FooFactory), - AsyncFactoryVariant::Default, + let instant_func_provider = AsyncFunctionProvider::new( + Arc::new(FooFactory), + ProvidableFunctionKind::Instant, ); - let async_default_factory_provider = AsyncFactoryProvider::new( - ThreadsafeFactoryPtr::new(FooFactory), - AsyncFactoryVariant::AsyncDefault, + let async_instant_func_provider = AsyncFunctionProvider::new( + Arc::new(FooFactory), + ProvidableFunctionKind::AsyncInstant, ); let di_container = MockAsyncDIContainer::new(); assert!( matches!( - factory_provider + user_called_func_provider .provide(&di_container, MockDependencyHistory::new()) .await .unwrap(), - AsyncProvidable::Factory(_) + AsyncProvidable::Function(_, ProvidableFunctionKind::UserCalled) ), - "The provided type is not a factory" + concat!( + "The provided type is not a AsyncProvidable::Function of kind ", + "ProvidableFunctionKind::UserCalled" + ) ); assert!( matches!( - default_factory_provider + instant_func_provider .provide(&di_container, MockDependencyHistory::new()) .await .unwrap(), - AsyncProvidable::DefaultFactory(_) + AsyncProvidable::Function(_, ProvidableFunctionKind::Instant) ), - "The provided type is not a default factory" + concat!( + "The provided type is not a AsyncProvidable::Function of kind ", + "ProvidableFunctionKind::Instant" + ) ); assert!( matches!( - async_default_factory_provider + async_instant_func_provider .provide(&di_container, MockDependencyHistory::new()) .await .unwrap(), - AsyncProvidable::AsyncDefaultFactory(_) + AsyncProvidable::Function(_, ProvidableFunctionKind::AsyncInstant) ), - "The provided type is not a async default factory" + concat!( + "The provided type is not a AsyncProvidable::Function of kind ", + "ProvidableFunctionKind::AsyncInstant" + ) ); } } diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs index 6475dc7..e7f113b 100644 --- a/src/provider/blocking.rs +++ b/src/provider/blocking.rs @@ -13,13 +13,20 @@ pub enum Providable<DIContainerType> Transient(TransientPtr<dyn Injectable<DIContainerType>>), Singleton(SingletonPtr<dyn Injectable<DIContainerType>>), #[cfg(feature = "factory")] - Factory(crate::ptr::FactoryPtr<dyn crate::castable_function::AnyCastableFunction>), - #[cfg(feature = "factory")] - DefaultFactory( - crate::ptr::FactoryPtr<dyn crate::castable_function::AnyCastableFunction>, + Function( + std::rc::Rc<dyn crate::castable_function::AnyCastableFunction>, + ProvidableFunctionKind, ), } +#[cfg(feature = "factory")] +#[derive(Debug, Clone, Copy)] +pub enum ProvidableFunctionKind +{ + Instant, + UserCalled, +} + #[cfg_attr(test, mockall::automock)] pub trait IProvider<DIContainerType> { @@ -108,31 +115,29 @@ where } #[cfg(feature = "factory")] -pub struct FactoryProvider +pub struct FunctionProvider { - factory: crate::ptr::FactoryPtr<dyn crate::castable_function::AnyCastableFunction>, - is_default_factory: bool, + function: std::rc::Rc<dyn crate::castable_function::AnyCastableFunction>, + providable_func_kind: ProvidableFunctionKind, } #[cfg(feature = "factory")] -impl FactoryProvider +impl FunctionProvider { pub fn new( - factory: crate::ptr::FactoryPtr< - dyn crate::castable_function::AnyCastableFunction, - >, - is_default_factory: bool, + function: std::rc::Rc<dyn crate::castable_function::AnyCastableFunction>, + providable_func_kind: ProvidableFunctionKind, ) -> Self { Self { - factory, - is_default_factory, + function, + providable_func_kind, } } } #[cfg(feature = "factory")] -impl<DIContainerType> IProvider<DIContainerType> for FactoryProvider +impl<DIContainerType> IProvider<DIContainerType> for FunctionProvider { fn provide( &self, @@ -140,11 +145,10 @@ impl<DIContainerType> IProvider<DIContainerType> for FactoryProvider _dependency_history: DependencyHistory, ) -> Result<Providable<DIContainerType>, InjectableError> { - Ok(if self.is_default_factory { - Providable::DefaultFactory(self.factory.clone()) - } else { - Providable::Factory(self.factory.clone()) - }) + Ok(Providable::Function( + self.function.clone(), + self.providable_func_kind, + )) } } @@ -198,12 +202,12 @@ mod tests #[test] #[cfg(feature = "factory")] - fn factory_provider_works() + fn function_provider_works() { use std::any::Any; + use std::rc::Rc; use crate::castable_function::AnyCastableFunction; - use crate::ptr::FactoryPtr; #[derive(Debug)] struct FooFactory; @@ -216,27 +220,38 @@ mod tests } } - let factory_provider = FactoryProvider::new(FactoryPtr::new(FooFactory), false); - let default_factory_provider = - FactoryProvider::new(FactoryPtr::new(FooFactory), true); + let user_called_func_provider = FunctionProvider::new( + Rc::new(FooFactory), + ProvidableFunctionKind::UserCalled, + ); + + let instant_func_provider = + FunctionProvider::new(Rc::new(FooFactory), ProvidableFunctionKind::Instant); let di_container = MockDIContainer::new(); assert!( matches!( - factory_provider.provide(&di_container, MockDependencyHistory::new()), - Ok(Providable::Factory(_)) + user_called_func_provider + .provide(&di_container, MockDependencyHistory::new()), + Ok(Providable::Function(_, ProvidableFunctionKind::UserCalled)) ), - "The provided type is not a factory" + concat!( + "The provided type is not a Providable::Function of kind ", + "ProvidableFunctionKind::UserCalled" + ) ); assert!( matches!( - default_factory_provider + instant_func_provider .provide(&di_container, MockDependencyHistory::new()), - Ok(Providable::DefaultFactory(_)) + Ok(Providable::Function(_, ProvidableFunctionKind::Instant)) ), - "The provided type is not a default factory" + concat!( + "The provided type is not a Providable::Function of kind ", + "ProvidableFunctionKind::Instant" + ) ); } } |