From e2b38115ec695a6620cdf244fd7bad922262560d Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 16 Sep 2024 00:26:40 +0200 Subject: feat: make to_*dynamic_value functions usable without nightly Rust --- src/provider/async.rs | 60 ++++++--------------------------------------------- 1 file changed, 6 insertions(+), 54 deletions(-) (limited to 'src/provider/async.rs') 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 { Transient(TransientPtr>), Singleton(ThreadsafeSingletonPtr>), - #[cfg(feature = "factory")] Function( - std::sync::Arc< - dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, - >, + Arc, 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, providable_func_kind: ProvidableFunctionKind, } -#[cfg(feature = "factory")] impl AsyncFunctionProvider { pub fn new( - function: std::sync::Arc< - dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, - >, + function: Arc, providable_func_kind: ProvidableFunctionKind, ) -> Self { @@ -200,7 +193,6 @@ impl AsyncFunctionProvider } } -#[cfg(feature = "factory")] #[async_trait] impl IAsyncProvider 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,37 +303,13 @@ 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 @@ -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" - ) - ); } } -- cgit v1.2.3-18-g5258