aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-09-18 22:35:58 +0200
committerHampusM <hampus@hampusmat.com>2023-09-18 22:35:58 +0200
commit4fd0d6b4951b08a20d5378bca75561109dc6d036 (patch)
tree8496d679e7ee3debcd415ed563911e2166594212 /src
parentde2e1349f459f7f69226b2decd366be690426ea7 (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')
-rw-r--r--src/di_container/asynchronous/binding/builder.rs74
-rw-r--r--src/di_container/asynchronous/binding/scope_configurator.rs29
-rw-r--r--src/di_container/asynchronous/binding/when_configurator.rs11
-rw-r--r--src/di_container/asynchronous/mod.rs84
-rw-r--r--src/interfaces/async_injectable.rs7
-rw-r--r--src/private/castable_factory/threadsafe.rs22
-rw-r--r--src/private/factory.rs5
-rw-r--r--src/provider/async.rs15
-rw-r--r--src/test_utils.rs9
9 files changed, 116 insertions, 140 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,
);
diff --git a/src/di_container/asynchronous/binding/scope_configurator.rs b/src/di_container/asynchronous/binding/scope_configurator.rs
index 3557202..f079234 100644
--- a/src/di_container/asynchronous/binding/scope_configurator.rs
+++ b/src/di_container/asynchronous/binding/scope_configurator.rs
@@ -1,6 +1,5 @@
//! Scope configurator for a binding for types inside of a [`AsyncDIContainer`].
use std::marker::PhantomData;
-use std::sync::Arc;
use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingWhenConfigurator;
use crate::di_container::BindingOptions;
@@ -14,25 +13,26 @@ use_double!(crate::dependency_history::DependencyHistory);
use_double!(crate::di_container::asynchronous::AsyncDIContainer);
/// Scope configurator for a binding for type `Interface` inside a [`AsyncDIContainer`].
-pub struct AsyncBindingScopeConfigurator<Interface, Implementation>
+pub struct AsyncBindingScopeConfigurator<'di_container, Interface, Implementation>
where
Interface: 'static + ?Sized + Send + Sync,
Implementation: AsyncInjectable<AsyncDIContainer>,
{
- di_container: Arc<AsyncDIContainer>,
+ di_container: &'di_container AsyncDIContainer,
dependency_history_factory: fn() -> DependencyHistory,
interface_phantom: PhantomData<Interface>,
implementation_phantom: PhantomData<Implementation>,
}
-impl<Interface, Implementation> AsyncBindingScopeConfigurator<Interface, Implementation>
+impl<'di_container, Interface, Implementation>
+ AsyncBindingScopeConfigurator<'di_container, Interface, Implementation>
where
Interface: 'static + ?Sized + Send + Sync,
Implementation: AsyncInjectable<AsyncDIContainer>,
{
pub(crate) fn new(
- di_container: Arc<AsyncDIContainer>,
+ di_container: &'di_container AsyncDIContainer,
dependency_history_factory: fn() -> DependencyHistory,
) -> Self
{
@@ -47,11 +47,13 @@ where
/// Configures the binding to be in a transient scope.
///
/// This is the default.
- pub async fn in_transient_scope(self) -> AsyncBindingWhenConfigurator<Interface>
+ pub async fn in_transient_scope(
+ self,
+ ) -> AsyncBindingWhenConfigurator<'di_container, Interface>
{
self.set_in_transient_scope().await;
- AsyncBindingWhenConfigurator::new(self.di_container.clone())
+ AsyncBindingWhenConfigurator::new(self.di_container)
}
/// Configures the binding to be in a singleton scope.
@@ -60,12 +62,15 @@ where
/// Will return Err if resolving the implementation fails.
pub async fn in_singleton_scope(
self,
- ) -> Result<AsyncBindingWhenConfigurator<Interface>, AsyncBindingScopeConfiguratorError>
+ ) -> Result<
+ AsyncBindingWhenConfigurator<'di_container, Interface>,
+ AsyncBindingScopeConfiguratorError,
+ >
{
let singleton: ThreadsafeSingletonPtr<Implementation> =
ThreadsafeSingletonPtr::from(
Implementation::resolve(
- &self.di_container,
+ self.di_container,
(self.dependency_history_factory)(),
)
.await
@@ -79,7 +84,7 @@ where
)
.await;
- Ok(AsyncBindingWhenConfigurator::new(self.di_container.clone()))
+ Ok(AsyncBindingWhenConfigurator::new(self.di_container))
}
pub(crate) async fn set_in_transient_scope(&self)
@@ -118,7 +123,7 @@ mod tests
AsyncBindingScopeConfigurator::<
dyn subjects_async::IUserManager,
subjects_async::UserManager,
- >::new(Arc::new(di_container_mock), MockDependencyHistory::new);
+ >::new(&di_container_mock, MockDependencyHistory::new);
binding_scope_configurator.in_transient_scope().await;
}
@@ -138,7 +143,7 @@ mod tests
AsyncBindingScopeConfigurator::<
dyn subjects_async::IUserManager,
subjects_async::UserManager,
- >::new(Arc::new(di_container_mock), MockDependencyHistory::new);
+ >::new(&di_container_mock, MockDependencyHistory::new);
assert!(binding_scope_configurator
.in_singleton_scope()
diff --git a/src/di_container/asynchronous/binding/when_configurator.rs b/src/di_container/asynchronous/binding/when_configurator.rs
index 3c1de7c..b7c2767 100644
--- a/src/di_container/asynchronous/binding/when_configurator.rs
+++ b/src/di_container/asynchronous/binding/when_configurator.rs
@@ -1,7 +1,6 @@
//! When configurator for a binding for types inside of a [`AsyncDIContainer`].
use std::any::type_name;
use std::marker::PhantomData;
-use std::sync::Arc;
use crate::di_container::BindingOptions;
use crate::errors::async_di_container::AsyncBindingWhenConfiguratorError;
@@ -10,20 +9,20 @@ use crate::util::use_double;
use_double!(crate::di_container::asynchronous::AsyncDIContainer);
/// When configurator for a binding for type `Interface` inside a [`AsyncDIContainer`].
-pub struct AsyncBindingWhenConfigurator<Interface>
+pub struct AsyncBindingWhenConfigurator<'di_container, Interface>
where
Interface: 'static + ?Sized + Send + Sync,
{
- di_container: Arc<AsyncDIContainer>,
+ di_container: &'di_container AsyncDIContainer,
interface_phantom: PhantomData<Interface>,
}
-impl<Interface> AsyncBindingWhenConfigurator<Interface>
+impl<'di_container, Interface> AsyncBindingWhenConfigurator<'di_container, Interface>
where
Interface: 'static + ?Sized + Send + Sync,
{
- pub(crate) fn new(di_container: Arc<AsyncDIContainer>) -> Self
+ pub(crate) fn new(di_container: &'di_container AsyncDIContainer) -> Self
{
Self {
di_container,
@@ -90,7 +89,7 @@ mod tests
let binding_when_configurator = AsyncBindingWhenConfigurator::<
dyn subjects_async::INumber,
- >::new(Arc::new(di_container_mock));
+ >::new(&di_container_mock);
assert!(binding_when_configurator
.when_named("awesome")
diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs
index 827364d..4be232d 100644
--- a/src/di_container/asynchronous/mod.rs
+++ b/src/di_container/asynchronous/mod.rs
@@ -71,6 +71,7 @@ use_double!(crate::dependency_history::DependencyHistory);
pub mod binding;
/// Async dependency injection container.
+#[derive(Default)]
pub struct AsyncDIContainer
{
binding_storage: Mutex<DIContainerBindingStorage<dyn IAsyncProvider<Self>>>,
@@ -80,11 +81,11 @@ impl AsyncDIContainer
{
/// Returns a new `AsyncDIContainer`.
#[must_use]
- pub fn new() -> Arc<Self>
+ pub fn new() -> Self
{
- Arc::new(Self {
+ Self {
binding_storage: Mutex::new(DIContainerBindingStorage::new()),
- })
+ }
}
}
@@ -93,7 +94,7 @@ impl AsyncDIContainer
{
/// Returns a new [`AsyncBindingBuilder`] for the given interface.
#[allow(clippy::missing_panics_doc)]
- pub fn bind<Interface>(self: &mut Arc<Self>) -> AsyncBindingBuilder<Interface>
+ pub fn bind<Interface>(&mut self) -> AsyncBindingBuilder<'_, Interface>
where
Interface: 'static + ?Sized + Send + Sync,
{
@@ -101,7 +102,7 @@ impl AsyncDIContainer
panic!("Bind function is unusable when testing");
#[cfg(not(test))]
- AsyncBindingBuilder::new(self.clone(), DependencyHistory::new)
+ AsyncBindingBuilder::new(self, DependencyHistory::new)
}
/// Returns the type bound with `Interface`.
@@ -112,7 +113,7 @@ impl AsyncDIContainer
/// - Resolving the binding for `Interface` fails
/// - Casting the binding for `Interface` fails
pub async fn get<Interface>(
- self: &Arc<Self>,
+ &self,
) -> Result<SomePtr<Interface>, AsyncDIContainerError>
where
Interface: 'static + ?Sized + Send + Sync,
@@ -129,7 +130,7 @@ impl AsyncDIContainer
/// - Resolving the binding for `Interface` fails
/// - Casting the binding for `Interface` fails
pub async fn get_named<Interface>(
- self: &Arc<Self>,
+ &self,
name: &'static str,
) -> Result<SomePtr<Interface>, AsyncDIContainerError>
where
@@ -177,7 +178,7 @@ impl AsyncDIContainer
/// # });
/// ```
pub async fn get_bound<Interface>(
- self: &Arc<Self>,
+ &self,
dependency_history: DependencyHistory,
binding_options: BindingOptions<'static>,
) -> Result<SomePtr<Interface>, AsyncDIContainerError>
@@ -192,7 +193,7 @@ impl AsyncDIContainer
}
async fn has_binding<Interface>(
- self: &Arc<Self>,
+ &self,
binding_options: BindingOptions<'static>,
) -> bool
where
@@ -205,7 +206,7 @@ impl AsyncDIContainer
}
async fn set_binding<Interface>(
- self: &Arc<Self>,
+ &self,
binding_options: BindingOptions<'static>,
provider: Box<dyn IAsyncProvider<Self>>,
) where
@@ -218,7 +219,7 @@ impl AsyncDIContainer
}
async fn remove_binding<Interface>(
- self: &Arc<Self>,
+ &self,
binding_options: BindingOptions<'static>,
) -> Option<Box<dyn IAsyncProvider<Self>>>
where
@@ -234,7 +235,7 @@ impl AsyncDIContainer
impl AsyncDIContainer
{
async fn handle_binding_providable<Interface>(
- self: &Arc<Self>,
+ &self,
binding_providable: AsyncProvidable<Self>,
) -> Result<SomePtr<Interface>, AsyncDIContainerError>
where
@@ -299,9 +300,7 @@ impl AsyncDIContainer
}
})?;
- Ok(SomePtr::ThreadsafeFactory(
- factory.call(self.clone()).into(),
- ))
+ Ok(SomePtr::ThreadsafeFactory(factory.call(self).into()))
}
#[cfg(feature = "factory")]
AsyncProvidable::DefaultFactory(binding) => {
@@ -317,7 +316,7 @@ impl AsyncDIContainer
DefaultFactoryFn<Interface>,
>(binding, "default factory")?;
- Ok(SomePtr::Transient(default_factory.call(self.clone())()))
+ Ok(SomePtr::Transient(default_factory.call(self)()))
}
#[cfg(feature = "factory")]
AsyncProvidable::AsyncDefaultFactory(binding) => {
@@ -338,9 +337,7 @@ impl AsyncDIContainer
binding, "async default factory"
)?;
- Ok(SomePtr::Transient(
- async_default_factory.call(self.clone())().await,
- ))
+ Ok(SomePtr::Transient(async_default_factory.call(self)().await))
}
}
}
@@ -368,7 +365,7 @@ impl AsyncDIContainer
}
async fn get_binding_providable<Interface>(
- self: &Arc<Self>,
+ &self,
binding_options: BindingOptions<'static>,
dependency_history: DependencyHistory,
) -> Result<AsyncProvidable<Self>, AsyncDIContainerError>
@@ -644,21 +641,13 @@ mod tests
let mut mock_provider = MockAsyncProvider::new();
mock_provider.expect_do_clone().returning(|| {
- type FactoryFunc = Box<
- (dyn Fn<(Vec<i128>,), Output = TransientPtr<dyn IUserManager>> + Send + Sync)
- >;
-
let mut inner_mock_provider = MockAsyncProvider::new();
- let factory_func: &'static (dyn Fn<
- (Arc<AsyncDIContainer>,),
- Output = FactoryFunc> + Send + Sync) = &|_| {
+ let factory_func = &|_: &AsyncDIContainer| {
Box::new(|users| {
- let user_manager: TransientPtr<dyn IUserManager> =
- TransientPtr::new(UserManager::new(users));
-
- user_manager
- })
+ TransientPtr::new(UserManager::new(users))
+ as TransientPtr<dyn IUserManager>
+ }) as Box<IUserManagerFactory>
};
inner_mock_provider.expect_provide().returning(|_, _| {
@@ -672,16 +661,11 @@ mod tests
Box::new(inner_mock_provider)
});
- {
- di_container
- .binding_storage
- .lock()
- .await
- .set::<IUserManagerFactory>(
- BindingOptions::new(),
- Box::new(mock_provider),
- );
- }
+ di_container
+ .binding_storage
+ .lock()
+ .await
+ .set::<IUserManagerFactory>(BindingOptions::new(), Box::new(mock_provider));
di_container
.get::<IUserManagerFactory>()
@@ -743,21 +727,13 @@ mod tests
let mut mock_provider = MockAsyncProvider::new();
mock_provider.expect_do_clone().returning(|| {
- type FactoryFunc = Box<
- (dyn Fn<(Vec<i128>,), Output = TransientPtr<dyn IUserManager>> + Send + Sync)
- >;
-
let mut inner_mock_provider = MockAsyncProvider::new();
- let factory_func: &'static (dyn Fn<
- (Arc<AsyncDIContainer>,),
- Output = FactoryFunc> + Send + Sync) = &|_| {
+ let factory_func = &|_: &AsyncDIContainer| {
Box::new(|users| {
- let user_manager: TransientPtr<dyn IUserManager> =
- TransientPtr::new(UserManager::new(users));
-
- user_manager
- })
+ TransientPtr::new(UserManager::new(users))
+ as TransientPtr<dyn IUserManager>
+ }) as Box<IUserManagerFactory>
};
inner_mock_provider.expect_provide().returning(|_, _| {
diff --git a/src/interfaces/async_injectable.rs b/src/interfaces/async_injectable.rs
index c455970..4c18487 100644
--- a/src/interfaces/async_injectable.rs
+++ b/src/interfaces/async_injectable.rs
@@ -1,6 +1,5 @@
//! Interface for structs that can be injected into or be injected to.
use std::fmt::Debug;
-use std::sync::Arc;
use crate::errors::injectable::InjectableError;
use crate::future::BoxFuture;
@@ -11,14 +10,14 @@ use crate::util::use_double;
use_double!(crate::dependency_history::DependencyHistory);
/// Interface for structs that can be injected into or be injected to.
-pub trait AsyncInjectable<DIContainerType>: CastFromArc
+pub trait AsyncInjectable<DIContainerT>: CastFromArc
{
/// Resolves the dependencies of the injectable.
///
/// # Errors
/// Will return `Err` if resolving the dependencies fails.
fn resolve<'di_container, 'fut>(
- di_container: &'di_container Arc<DIContainerType>,
+ di_container: &'di_container DIContainerT,
dependency_history: DependencyHistory,
) -> BoxFuture<'fut, Result<TransientPtr<Self>, InjectableError>>
where
@@ -26,7 +25,7 @@ pub trait AsyncInjectable<DIContainerType>: CastFromArc
'di_container: 'fut;
}
-impl<DIContainerType> Debug for dyn AsyncInjectable<DIContainerType>
+impl<DIContainerT> Debug for dyn AsyncInjectable<DIContainerT>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
diff --git a/src/private/castable_factory/threadsafe.rs b/src/private/castable_factory/threadsafe.rs
index 6e608b1..6e8da42 100644
--- a/src/private/castable_factory/threadsafe.rs
+++ b/src/private/castable_factory/threadsafe.rs
@@ -1,6 +1,5 @@
use std::any::type_name;
use std::fmt::Debug;
-use std::sync::Arc;
use crate::private::any_factory::{AnyFactory, AnyThreadsafeFactory};
use crate::private::factory::IThreadsafeFactory;
@@ -11,9 +10,7 @@ where
DIContainerT: 'static,
ReturnInterface: 'static + ?Sized,
{
- func: &'static (dyn Fn(Arc<DIContainerT>) -> TransientPtr<ReturnInterface>
- + Send
- + Sync),
+ func: &'static (dyn Fn(&DIContainerT) -> TransientPtr<ReturnInterface> + Send + Sync),
}
impl<ReturnInterface, DIContainerT>
@@ -23,7 +20,7 @@ where
ReturnInterface: 'static + ?Sized,
{
pub fn new(
- func: &'static (dyn Fn<(Arc<DIContainerT>,), Output = TransientPtr<ReturnInterface>>
+ func: &'static (dyn Fn(&DIContainerT) -> TransientPtr<ReturnInterface>
+ Send
+ Sync),
) -> Self
@@ -38,7 +35,7 @@ where
DIContainerT: 'static,
ReturnInterface: 'static + ?Sized,
{
- fn call(&self, di_container: Arc<DIContainerT>) -> TransientPtr<ReturnInterface>
+ fn call(&self, di_container: &DIContainerT) -> TransientPtr<ReturnInterface>
{
(self.func)(di_container)
}
@@ -72,7 +69,7 @@ where
let ret = type_name::<TransientPtr<ReturnInterface>>();
formatter.write_fmt(format_args!(
- "ThreadsafeCastableFactory (Arc<AsyncDIContainer>) -> {ret} {{ ... }}",
+ "ThreadsafeCastableFactory (&AsyncDIContainer) -> {ret} {{ ... }}",
))
}
}
@@ -92,13 +89,14 @@ mod tests
#[test]
fn can_call()
{
- let castable_factory = ThreadsafeCastableFactory::new(&|_| {
- TransientPtr::new(Bacon { heal_amount: 27 })
- });
+ let castable_factory =
+ ThreadsafeCastableFactory::new(&|_: &MockAsyncDIContainer| {
+ TransientPtr::new(Bacon { heal_amount: 27 })
+ });
- let mock_di_container = Arc::new(MockAsyncDIContainer::new());
+ let mock_di_container = MockAsyncDIContainer::new();
- let output = castable_factory.call(mock_di_container);
+ let output = castable_factory.call(&mock_di_container);
assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 }));
}
diff --git a/src/private/factory.rs b/src/private/factory.rs
index c1672e1..7191c2c 100644
--- a/src/private/factory.rs
+++ b/src/private/factory.rs
@@ -1,6 +1,3 @@
-#[cfg(feature = "async")]
-use std::sync::Arc;
-
use crate::private::cast::CastFrom;
use crate::ptr::TransientPtr;
@@ -19,5 +16,5 @@ pub trait IThreadsafeFactory<ReturnInterface, DIContainerT>:
where
ReturnInterface: 'static + ?Sized,
{
- fn call(&self, di_container: Arc<DIContainerT>) -> TransientPtr<ReturnInterface>;
+ fn call(&self, di_container: &DIContainerT) -> TransientPtr<ReturnInterface>;
}
diff --git a/src/provider/async.rs b/src/provider/async.rs
index ae633fd..2bf7be1 100644
--- a/src/provider/async.rs
+++ b/src/provider/async.rs
@@ -1,5 +1,4 @@
use std::marker::PhantomData;
-use std::sync::Arc;
use async_trait::async_trait;
@@ -43,7 +42,7 @@ where
{
async fn provide(
&self,
- di_container: &Arc<DIContainerT>,
+ di_container: &DIContainerT,
dependency_history: DependencyHistory,
) -> Result<AsyncProvidable<DIContainerT>, InjectableError>;
@@ -90,7 +89,7 @@ where
{
async fn provide(
&self,
- di_container: &Arc<DIContainerT>,
+ di_container: &DIContainerT,
dependency_history: DependencyHistory,
) -> Result<AsyncProvidable<DIContainerT>, InjectableError>
{
@@ -150,7 +149,7 @@ where
{
async fn provide(
&self,
- _di_container: &Arc<DIContainerT>,
+ _di_container: &DIContainerT,
_dependency_history: DependencyHistory,
) -> Result<AsyncProvidable<DIContainerT>, InjectableError>
{
@@ -217,7 +216,7 @@ where
{
async fn provide(
&self,
- _di_container: &Arc<DIContainerT>,
+ _di_container: &DIContainerT,
_dependency_history: DependencyHistory,
) -> Result<AsyncProvidable<DIContainerT>, InjectableError>
{
@@ -273,7 +272,7 @@ mod tests
assert!(
matches!(
transient_type_provider
- .provide(&Arc::new(di_container), MockDependencyHistory::new())
+ .provide(&di_container, MockDependencyHistory::new())
.await?,
AsyncProvidable::Transient(_)
),
@@ -298,7 +297,7 @@ mod tests
assert!(
matches!(
singleton_provider
- .provide(&Arc::new(di_container), MockDependencyHistory::new())
+ .provide(&di_container, MockDependencyHistory::new())
.await?,
AsyncProvidable::Singleton(_)
),
@@ -335,7 +334,7 @@ mod tests
AsyncFactoryVariant::AsyncDefault,
);
- let di_container = Arc::new(MockAsyncDIContainer::new());
+ let di_container = MockAsyncDIContainer::new();
assert!(
matches!(
diff --git a/src/test_utils.rs b/src/test_utils.rs
index 6071cfb..491e9b4 100644
--- a/src/test_utils.rs
+++ b/src/test_utils.rs
@@ -138,7 +138,6 @@ pub mod subjects_async
//! Test subjects.
use std::fmt::Debug;
- use std::sync::Arc;
use async_trait::async_trait;
use syrette_macros::declare_interface;
@@ -187,7 +186,7 @@ pub mod subjects_async
impl<DIContainerType> AsyncInjectable<DIContainerType> for UserManager
{
async fn resolve(
- _: &Arc<DIContainerType>,
+ _: &DIContainerType,
_dependency_history: DependencyHistory,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
@@ -252,7 +251,7 @@ pub mod subjects_async
impl<DIContainerType> AsyncInjectable<DIContainerType> for Number
{
async fn resolve(
- _: &Arc<DIContainerType>,
+ _: &DIContainerType,
_dependency_history: DependencyHistory,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
@@ -271,8 +270,6 @@ pub mod mocks
#[cfg(feature = "async")]
pub mod async_provider
{
- use std::sync::Arc;
-
use async_trait::async_trait;
use mockall::mock;
@@ -293,7 +290,7 @@ pub mod mocks
{
async fn provide(
&self,
- di_container: &Arc<DIContainerT>,
+ di_container: &DIContainerT,
dependency_history: DependencyHistory
) -> Result<AsyncProvidable<DIContainerT>, InjectableError>;