aboutsummaryrefslogtreecommitdiff
path: root/src/provider
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-09-16 00:26:40 +0200
committerHampusM <hampus@hampusmat.com>2024-09-16 00:26:40 +0200
commite2b38115ec695a6620cdf244fd7bad922262560d (patch)
tree1e104c71d816720b0c4c734b861d6a83467faccb /src/provider
parentd46181de1c19328ff8f3f6a12784cf14c53e9e71 (diff)
feat: make to_*dynamic_value functions usable without nightly Rust
Diffstat (limited to 'src/provider')
-rw-r--r--src/provider/async.rs60
-rw-r--r--src/provider/blocking.rs37
2 files changed, 13 insertions, 84 deletions
diff --git a/src/provider/async.rs b/src/provider/async.rs
index b011d7a..787ef06 100644
--- a/src/provider/async.rs
+++ b/src/provider/async.rs
@@ -1,7 +1,9 @@
use std::marker::PhantomData;
+use std::sync::Arc;
use async_trait::async_trait;
+use crate::castable_function::threadsafe::AnyThreadsafeCastableFunction;
use crate::errors::injectable::InjectableError;
use crate::interfaces::async_injectable::AsyncInjectable;
use crate::ptr::{ThreadsafeSingletonPtr, TransientPtr};
@@ -14,19 +16,16 @@ pub enum AsyncProvidable<DIContainerT>
{
Transient(TransientPtr<dyn AsyncInjectable<DIContainerT>>),
Singleton(ThreadsafeSingletonPtr<dyn AsyncInjectable<DIContainerT>>),
- #[cfg(feature = "factory")]
Function(
- std::sync::Arc<
- dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction,
- >,
+ Arc<dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction>,
ProvidableFunctionKind,
),
}
-#[cfg(feature = "factory")]
#[derive(Debug, Clone, Copy)]
pub enum ProvidableFunctionKind
{
+ #[cfg(feature = "factory")]
UserCalled,
Instant,
AsyncInstant,
@@ -174,22 +173,16 @@ where
}
}
-#[cfg(feature = "factory")]
pub struct AsyncFunctionProvider
{
- function: std::sync::Arc<
- dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction,
- >,
+ function: Arc<dyn AnyThreadsafeCastableFunction>,
providable_func_kind: ProvidableFunctionKind,
}
-#[cfg(feature = "factory")]
impl AsyncFunctionProvider
{
pub fn new(
- function: std::sync::Arc<
- dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction,
- >,
+ function: Arc<dyn AnyThreadsafeCastableFunction>,
providable_func_kind: ProvidableFunctionKind,
) -> Self
{
@@ -200,7 +193,6 @@ impl AsyncFunctionProvider
}
}
-#[cfg(feature = "factory")]
#[async_trait]
impl<DIContainerT> IAsyncProvider<DIContainerT> for AsyncFunctionProvider
where
@@ -224,7 +216,6 @@ where
}
}
-#[cfg(feature = "factory")]
impl Clone for AsyncFunctionProvider
{
fn clone(&self) -> Self
@@ -291,7 +282,6 @@ mod tests
}
#[tokio::test]
- #[cfg(feature = "factory")]
async fn function_provider_works()
{
use std::any::Any;
@@ -313,39 +303,15 @@ mod tests
impl AnyThreadsafeCastableFunction for FooFactory {}
- let user_called_func_provider = AsyncFunctionProvider::new(
- Arc::new(FooFactory),
- ProvidableFunctionKind::UserCalled,
- );
-
let instant_func_provider = AsyncFunctionProvider::new(
Arc::new(FooFactory),
ProvidableFunctionKind::Instant,
);
- let async_instant_func_provider = AsyncFunctionProvider::new(
- Arc::new(FooFactory),
- ProvidableFunctionKind::AsyncInstant,
- );
-
let di_container = MockAsyncDIContainer::new();
assert!(
matches!(
- user_called_func_provider
- .provide(&di_container, MockDependencyHistory::new())
- .await
- .unwrap(),
- AsyncProvidable::Function(_, ProvidableFunctionKind::UserCalled)
- ),
- concat!(
- "The provided type is not a AsyncProvidable::Function of kind ",
- "ProvidableFunctionKind::UserCalled"
- )
- );
-
- assert!(
- matches!(
instant_func_provider
.provide(&di_container, MockDependencyHistory::new())
.await
@@ -357,19 +323,5 @@ mod tests
"ProvidableFunctionKind::Instant"
)
);
-
- assert!(
- matches!(
- async_instant_func_provider
- .provide(&di_container, MockDependencyHistory::new())
- .await
- .unwrap(),
- AsyncProvidable::Function(_, ProvidableFunctionKind::AsyncInstant)
- ),
- 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 e7f113b..6b22ad0 100644
--- a/src/provider/blocking.rs
+++ b/src/provider/blocking.rs
@@ -1,5 +1,7 @@
use std::marker::PhantomData;
+use std::rc::Rc;
+use crate::castable_function::AnyCastableFunction;
use crate::errors::injectable::InjectableError;
use crate::interfaces::injectable::Injectable;
use crate::ptr::{SingletonPtr, TransientPtr};
@@ -12,19 +14,15 @@ pub enum Providable<DIContainerType>
{
Transient(TransientPtr<dyn Injectable<DIContainerType>>),
Singleton(SingletonPtr<dyn Injectable<DIContainerType>>),
- #[cfg(feature = "factory")]
- Function(
- std::rc::Rc<dyn crate::castable_function::AnyCastableFunction>,
- ProvidableFunctionKind,
- ),
+ Function(Rc<dyn AnyCastableFunction>, ProvidableFunctionKind),
}
-#[cfg(feature = "factory")]
#[derive(Debug, Clone, Copy)]
pub enum ProvidableFunctionKind
{
- Instant,
+ #[cfg(feature = "factory")]
UserCalled,
+ Instant,
}
#[cfg_attr(test, mockall::automock)]
@@ -114,18 +112,16 @@ where
}
}
-#[cfg(feature = "factory")]
pub struct FunctionProvider
{
- function: std::rc::Rc<dyn crate::castable_function::AnyCastableFunction>,
+ function: Rc<dyn AnyCastableFunction>,
providable_func_kind: ProvidableFunctionKind,
}
-#[cfg(feature = "factory")]
impl FunctionProvider
{
pub fn new(
- function: std::rc::Rc<dyn crate::castable_function::AnyCastableFunction>,
+ function: Rc<dyn AnyCastableFunction>,
providable_func_kind: ProvidableFunctionKind,
) -> Self
{
@@ -136,7 +132,6 @@ impl FunctionProvider
}
}
-#[cfg(feature = "factory")]
impl<DIContainerType> IProvider<DIContainerType> for FunctionProvider
{
fn provide(
@@ -201,7 +196,6 @@ mod tests
}
#[test]
- #[cfg(feature = "factory")]
fn function_provider_works()
{
use std::any::Any;
@@ -220,11 +214,6 @@ mod tests
}
}
- let user_called_func_provider = FunctionProvider::new(
- Rc::new(FooFactory),
- ProvidableFunctionKind::UserCalled,
- );
-
let instant_func_provider =
FunctionProvider::new(Rc::new(FooFactory), ProvidableFunctionKind::Instant);
@@ -232,18 +221,6 @@ mod tests
assert!(
matches!(
- user_called_func_provider
- .provide(&di_container, MockDependencyHistory::new()),
- Ok(Providable::Function(_, ProvidableFunctionKind::UserCalled))
- ),
- concat!(
- "The provided type is not a Providable::Function of kind ",
- "ProvidableFunctionKind::UserCalled"
- )
- );
-
- assert!(
- matches!(
instant_func_provider
.provide(&di_container, MockDependencyHistory::new()),
Ok(Providable::Function(_, ProvidableFunctionKind::Instant))