aboutsummaryrefslogtreecommitdiff
path: root/src/di_container
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-09-15 23:02:34 +0200
committerHampusM <hampus@hampusmat.com>2024-09-15 23:22:33 +0200
commita34f7c03779aaf90f34b5ff59587daf1db42de8d (patch)
treed5a2f057b70932e44999fd0f2389811fad5a4478 /src/di_container
parentb655d0e439da0da468e02d394e1189c5603fe752 (diff)
refactor: rename factory provider to function provider
Diffstat (limited to 'src/di_container')
-rw-r--r--src/di_container/asynchronous.rs41
-rw-r--r--src/di_container/asynchronous/binding/builder.rs40
-rw-r--r--src/di_container/blocking.rs39
-rw-r--r--src/di_container/blocking/binding/builder.rs18
4 files changed, 89 insertions, 49 deletions
diff --git a/src/di_container/asynchronous.rs b/src/di_container/asynchronous.rs
index c993b8b..c6308e6 100644
--- a/src/di_container/asynchronous.rs
+++ b/src/di_container/asynchronous.rs
@@ -347,10 +347,13 @@ impl AsyncDIContainer
))
}
#[cfg(feature = "factory")]
- AsyncProvidable::Factory(factory_binding) => {
+ AsyncProvidable::Function(
+ func_bound,
+ crate::provider::r#async::ProvidableFunctionKind::UserCalled,
+ ) => {
use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- let factory = factory_binding
+ let factory = func_bound
.as_any()
.downcast_ref::<ThreadsafeCastableFunction<Interface, Self>>()
.ok_or_else(|| AsyncDIContainerError::CastFailed {
@@ -361,7 +364,10 @@ impl AsyncDIContainer
Ok(SomePtr::ThreadsafeFactory(factory.call(self).into()))
}
#[cfg(feature = "factory")]
- AsyncProvidable::DefaultFactory(binding) => {
+ AsyncProvidable::Function(
+ func_bound,
+ crate::provider::r#async::ProvidableFunctionKind::Instant,
+ ) => {
use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
use crate::ptr::TransientPtr;
@@ -370,7 +376,7 @@ impl AsyncDIContainer
AsyncDIContainer,
>;
- let default_factory = binding
+ let default_factory = func_bound
.as_any()
.downcast_ref::<DefaultFactoryFn<Interface>>()
.ok_or_else(|| AsyncDIContainerError::CastFailed {
@@ -381,7 +387,10 @@ impl AsyncDIContainer
Ok(SomePtr::Transient(default_factory.call(self)()))
}
#[cfg(feature = "factory")]
- AsyncProvidable::AsyncDefaultFactory(binding) => {
+ AsyncProvidable::Function(
+ func_bound,
+ crate::provider::r#async::ProvidableFunctionKind::AsyncInstant,
+ ) => {
use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
use crate::future::BoxFuture;
use crate::ptr::TransientPtr;
@@ -393,7 +402,7 @@ impl AsyncDIContainer
AsyncDIContainer,
>;
- let async_default_factory = binding
+ let async_default_factory = func_bound
.as_any()
.downcast_ref::<AsyncDefaultFactoryFn<Interface>>()
.ok_or_else(|| AsyncDIContainerError::CastFailed {
@@ -652,7 +661,10 @@ mod tests
}
}
+ use std::sync::Arc;
+
use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
+ use crate::provider::r#async::ProvidableFunctionKind;
type IUserManagerFactory =
dyn Fn(Vec<i128>) -> TransientPtr<dyn IUserManager> + Send + Sync;
@@ -672,10 +684,9 @@ mod tests
};
inner_mock_provider.expect_provide().returning(|_, _| {
- Ok(AsyncProvidable::Factory(
- crate::ptr::ThreadsafeFactoryPtr::new(
- ThreadsafeCastableFunction::new(factory_func),
- ),
+ Ok(AsyncProvidable::Function(
+ Arc::new(ThreadsafeCastableFunction::new(factory_func)),
+ ProvidableFunctionKind::UserCalled,
))
});
@@ -734,7 +745,10 @@ mod tests
}
}
+ use std::sync::Arc;
+
use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
+ use crate::provider::r#async::ProvidableFunctionKind;
type IUserManagerFactory =
dyn Fn(Vec<i128>) -> TransientPtr<dyn IUserManager> + Send + Sync;
@@ -754,10 +768,9 @@ mod tests
};
inner_mock_provider.expect_provide().returning(|_, _| {
- Ok(AsyncProvidable::Factory(
- crate::ptr::ThreadsafeFactoryPtr::new(
- ThreadsafeCastableFunction::new(factory_func),
- ),
+ Ok(AsyncProvidable::Function(
+ Arc::new(ThreadsafeCastableFunction::new(factory_func)),
+ ProvidableFunctionKind::UserCalled,
))
});
diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs
index 8465c9a..833517b 100644
--- a/src/di_container/asynchronous/binding/builder.rs
+++ b/src/di_container/asynchronous/binding/builder.rs
@@ -173,8 +173,10 @@ where
Interface: Fn<Args, Output = Return> + Send + Sync,
FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn<Args, Return> + Send + Sync,
{
+ use std::sync::Arc;
+
use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::AsyncFactoryVariant;
+ use crate::provider::r#async::ProvidableFunctionKind;
if self
.di_container
@@ -190,9 +192,9 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::r#async::AsyncFactoryProvider::new(
- crate::ptr::ThreadsafeFactoryPtr::new(factory_impl),
- AsyncFactoryVariant::Normal,
+ Box::new(crate::provider::r#async::AsyncFunctionProvider::new(
+ Arc::new(factory_impl),
+ ProvidableFunctionKind::UserCalled,
)),
);
@@ -270,8 +272,10 @@ where
+ Send
+ Sync,
{
+ use std::sync::Arc;
+
use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::AsyncFactoryVariant;
+ use crate::provider::r#async::ProvidableFunctionKind;
if self
.di_container
@@ -287,9 +291,9 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::r#async::AsyncFactoryProvider::new(
- crate::ptr::ThreadsafeFactoryPtr::new(factory_impl),
- AsyncFactoryVariant::Normal,
+ Box::new(crate::provider::r#async::AsyncFunctionProvider::new(
+ Arc::new(factory_impl),
+ ProvidableFunctionKind::UserCalled,
)),
);
@@ -354,8 +358,10 @@ where
+ Send
+ Sync,
{
+ use std::sync::Arc;
+
use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::AsyncFactoryVariant;
+ use crate::provider::r#async::ProvidableFunctionKind;
if self
.di_container
@@ -371,9 +377,9 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::r#async::AsyncFactoryProvider::new(
- crate::ptr::ThreadsafeFactoryPtr::new(factory_impl),
- AsyncFactoryVariant::Default,
+ Box::new(crate::provider::r#async::AsyncFunctionProvider::new(
+ Arc::new(factory_impl),
+ ProvidableFunctionKind::Instant,
)),
);
@@ -445,8 +451,10 @@ where
+ Send
+ Sync,
{
+ use std::sync::Arc;
+
use crate::castable_function::threadsafe::ThreadsafeCastableFunction;
- use crate::provider::r#async::AsyncFactoryVariant;
+ use crate::provider::r#async::ProvidableFunctionKind;
if self
.di_container
@@ -462,9 +470,9 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::r#async::AsyncFactoryProvider::new(
- crate::ptr::ThreadsafeFactoryPtr::new(factory_impl),
- AsyncFactoryVariant::AsyncDefault,
+ Box::new(crate::provider::r#async::AsyncFunctionProvider::new(
+ Arc::new(factory_impl),
+ ProvidableFunctionKind::AsyncInstant,
)),
);
diff --git a/src/di_container/blocking.rs b/src/di_container/blocking.rs
index d8b0d59..fa3523b 100644
--- a/src/di_container/blocking.rs
+++ b/src/di_container/blocking.rs
@@ -284,10 +284,13 @@ impl DIContainer
})?,
)),
#[cfg(feature = "factory")]
- Providable::Factory(factory_binding) => {
+ Providable::Function(
+ func_bound,
+ crate::provider::blocking::ProvidableFunctionKind::UserCalled,
+ ) => {
use crate::castable_function::CastableFunction;
- let factory = factory_binding
+ let factory = func_bound
.as_any()
.downcast_ref::<CastableFunction<Interface, Self>>()
.ok_or_else(|| DIContainerError::CastFailed {
@@ -298,16 +301,19 @@ impl DIContainer
Ok(SomePtr::Factory(factory.call(self).into()))
}
#[cfg(feature = "factory")]
- Providable::DefaultFactory(factory_binding) => {
+ Providable::Function(
+ func_bound,
+ crate::provider::blocking::ProvidableFunctionKind::Instant,
+ ) => {
use crate::castable_function::CastableFunction;
use crate::ptr::TransientPtr;
- type DefaultFactoryFn<Interface> =
+ type Func<Interface> =
CastableFunction<dyn Fn() -> TransientPtr<Interface>, DIContainer>;
- let default_factory = factory_binding
+ let default_factory = func_bound
.as_any()
- .downcast_ref::<DefaultFactoryFn<Interface>>()
+ .downcast_ref::<Func<Interface>>()
.ok_or_else(|| DIContainerError::CastFailed {
interface: type_name::<Interface>(),
binding_kind: "default factory",
@@ -517,7 +523,10 @@ mod tests
#[cfg(feature = "factory")]
fn can_get_factory()
{
+ use std::rc::Rc;
+
use crate::castable_function::CastableFunction;
+ use crate::provider::blocking::ProvidableFunctionKind;
use crate::ptr::FactoryPtr;
trait IUserManager
@@ -572,9 +581,10 @@ mod tests
let mut mock_provider = MockIProvider::new();
mock_provider.expect_provide().returning_st(|_, _| {
- Ok(Providable::Factory(FactoryPtr::new(CastableFunction::new(
- factory_func,
- ))))
+ Ok(Providable::Function(
+ Rc::new(CastableFunction::new(factory_func)),
+ ProvidableFunctionKind::UserCalled,
+ ))
});
di_container
@@ -592,8 +602,10 @@ mod tests
#[cfg(feature = "factory")]
fn can_get_factory_named()
{
+ use std::rc::Rc;
+
use crate::castable_function::CastableFunction;
- use crate::ptr::FactoryPtr;
+ use crate::provider::blocking::ProvidableFunctionKind;
trait IUserManager
{
@@ -647,9 +659,10 @@ mod tests
let mut mock_provider = MockIProvider::new();
mock_provider.expect_provide().returning_st(|_, _| {
- Ok(Providable::Factory(FactoryPtr::new(CastableFunction::new(
- factory_func,
- ))))
+ Ok(Providable::Function(
+ Rc::new(CastableFunction::new(factory_func)),
+ ProvidableFunctionKind::UserCalled,
+ ))
});
di_container.binding_storage.set::<IUserManagerFactory>(
diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs
index ead1a54..345fb02 100644
--- a/src/di_container/blocking/binding/builder.rs
+++ b/src/di_container/blocking/binding/builder.rs
@@ -181,7 +181,10 @@ where
Interface: Fn<Args, Output = crate::ptr::TransientPtr<Return>>,
Func: Fn(&DIContainer) -> Box<Interface>,
{
+ use std::rc::Rc;
+
use crate::castable_function::CastableFunction;
+ use crate::provider::blocking::ProvidableFunctionKind;
if self
.di_container
@@ -196,9 +199,9 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::blocking::FactoryProvider::new(
- crate::ptr::FactoryPtr::new(factory_impl),
- false,
+ Box::new(crate::provider::blocking::FunctionProvider::new(
+ Rc::new(factory_impl),
+ ProvidableFunctionKind::UserCalled,
)),
);
@@ -269,7 +272,10 @@ where
dyn Fn<(), Output = crate::ptr::TransientPtr<Return>>,
>,
{
+ use std::rc::Rc;
+
use crate::castable_function::CastableFunction;
+ use crate::provider::blocking::ProvidableFunctionKind;
if self
.di_container
@@ -284,9 +290,9 @@ where
self.di_container.set_binding::<Interface>(
BindingOptions::new(),
- Box::new(crate::provider::blocking::FactoryProvider::new(
- crate::ptr::FactoryPtr::new(factory_impl),
- true,
+ Box::new(crate::provider::blocking::FunctionProvider::new(
+ Rc::new(factory_impl),
+ ProvidableFunctionKind::Instant,
)),
);