aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-09-17 13:32:06 +0200
committerHampusM <hampus@hampusmat.com>2023-09-17 13:32:06 +0200
commita1ddd1bc6e1af0d98b87126d3e86da30bc1d3f1c (patch)
treed4a9cfd9e3970a955e27ac518a593f55e8ad8d9c
parent3e3a853615e97ee7c3aef09736eae8170c6fd78e (diff)
refactor!: make the blocking DI container not inside a Rc
BREAKING CHANGE: The blocking DI container is no longer inside of a Rc. This affects BindingBuilder, BindingScopeConfigurator, BindingWhenConfigurator & Injectable
-rw-r--r--examples/basic/bootstrap.rs3
-rw-r--r--examples/factory/bootstrap.rs3
-rw-r--r--examples/generics/bootstrap.rs4
-rw-r--r--examples/named/bootstrap.rs4
-rw-r--r--examples/with-3rd-party/bootstrap.rs3
-rw-r--r--macros/src/injectable/implementation.rs4
-rw-r--r--src/di_container/binding_storage.rs10
-rw-r--r--src/di_container/blocking/binding/builder.rs49
-rw-r--r--src/di_container/blocking/binding/scope_configurator.rs25
-rw-r--r--src/di_container/blocking/binding/when_configurator.rs13
-rw-r--r--src/di_container/blocking/mod.rs63
-rw-r--r--src/interfaces/injectable.rs3
-rw-r--r--src/private/castable_factory/blocking.rs23
-rw-r--r--src/private/factory.rs4
-rw-r--r--src/provider/blocking.rs15
-rw-r--r--src/test_utils.rs5
16 files changed, 110 insertions, 121 deletions
diff --git a/examples/basic/bootstrap.rs b/examples/basic/bootstrap.rs
index 2c45676..95632f2 100644
--- a/examples/basic/bootstrap.rs
+++ b/examples/basic/bootstrap.rs
@@ -1,5 +1,4 @@
use std::error::Error;
-use std::rc::Rc;
use syrette::DIContainer;
@@ -10,7 +9,7 @@ use crate::interfaces::cat::ICat;
use crate::interfaces::dog::IDog;
use crate::interfaces::human::IHuman;
-pub fn bootstrap() -> Result<Rc<DIContainer>, Box<dyn Error>>
+pub fn bootstrap() -> Result<DIContainer, Box<dyn Error>>
{
let mut di_container = DIContainer::new();
diff --git a/examples/factory/bootstrap.rs b/examples/factory/bootstrap.rs
index f8bef6e..5543ee5 100644
--- a/examples/factory/bootstrap.rs
+++ b/examples/factory/bootstrap.rs
@@ -1,5 +1,4 @@
use std::error::Error;
-use std::rc::Rc;
use syrette::ptr::TransientPtr;
use syrette::DIContainer;
@@ -9,7 +8,7 @@ use crate::interfaces::user_manager::IUserManager;
use crate::user::User;
use crate::user_manager::UserManager;
-pub fn bootstrap() -> Result<Rc<DIContainer>, Box<dyn Error>>
+pub fn bootstrap() -> Result<DIContainer, Box<dyn Error>>
{
let mut di_container = DIContainer::new();
diff --git a/examples/generics/bootstrap.rs b/examples/generics/bootstrap.rs
index 98d03db..233cd59 100644
--- a/examples/generics/bootstrap.rs
+++ b/examples/generics/bootstrap.rs
@@ -1,11 +1,9 @@
-use std::rc::Rc;
-
use syrette::{di_container_bind, DIContainer};
use crate::interfaces::printer::IPrinter;
use crate::printer::Printer;
-pub fn bootstrap() -> Rc<DIContainer>
+pub fn bootstrap() -> DIContainer
{
let mut di_container = DIContainer::new();
diff --git a/examples/named/bootstrap.rs b/examples/named/bootstrap.rs
index 5f63b47..f7b66ad 100644
--- a/examples/named/bootstrap.rs
+++ b/examples/named/bootstrap.rs
@@ -1,5 +1,3 @@
-use std::rc::Rc;
-
use anyhow::Result;
use syrette::DIContainer;
@@ -9,7 +7,7 @@ use crate::katana::Katana;
use crate::ninja::Ninja;
use crate::shuriken::Shuriken;
-pub fn bootstrap() -> Result<Rc<DIContainer>>
+pub fn bootstrap() -> Result<DIContainer>
{
let mut di_container = DIContainer::new();
diff --git a/examples/with-3rd-party/bootstrap.rs b/examples/with-3rd-party/bootstrap.rs
index 4fea754..5cd0f85 100644
--- a/examples/with-3rd-party/bootstrap.rs
+++ b/examples/with-3rd-party/bootstrap.rs
@@ -1,5 +1,4 @@
use std::error::Error;
-use std::rc::Rc;
use syrette::ptr::TransientPtr;
use syrette::{declare_default_factory, DIContainer};
@@ -10,7 +9,7 @@ use crate::ninja::Ninja;
declare_default_factory!(Shuriken);
-pub fn bootstrap() -> Result<Rc<DIContainer>, Box<dyn Error>>
+pub fn bootstrap() -> Result<DIContainer, Box<dyn Error>>
{
let mut di_container = DIContainer::new();
diff --git a/macros/src/injectable/implementation.rs b/macros/src/injectable/implementation.rs
index 935a8ae..3e22f91 100644
--- a/macros/src/injectable/implementation.rs
+++ b/macros/src/injectable/implementation.rs
@@ -292,9 +292,7 @@ impl<Dep: IDependency> InjectableImpl<Dep>
> for #self_type
{
fn resolve(
- #di_container_var: &std::rc::Rc<
- syrette::di_container::blocking::DIContainer
- >,
+ #di_container_var: &syrette::di_container::blocking::DIContainer,
mut #dependency_history_var: syrette::dependency_history::DependencyHistory
) -> Result<
syrette::ptr::TransientPtr<Self>,
diff --git a/src/di_container/binding_storage.rs b/src/di_container/binding_storage.rs
index 2451791..b3fea6f 100644
--- a/src/di_container/binding_storage.rs
+++ b/src/di_container/binding_storage.rs
@@ -63,6 +63,16 @@ where
}
}
+impl<Provider> Default for DIContainerBindingStorage<Provider>
+where
+ Provider: 'static + ?Sized,
+{
+ fn default() -> Self
+ {
+ Self::new()
+ }
+}
+
#[derive(Debug, PartialEq, Eq, Hash)]
struct BindingId<'opts>
{
diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs
index 64e787e..8eebcc0 100644
--- a/src/di_container/blocking/binding/builder.rs
+++ b/src/di_container/blocking/binding/builder.rs
@@ -1,7 +1,6 @@
//! Binding builder for types inside of a [`DIContainer`].
use std::any::type_name;
use std::marker::PhantomData;
-use std::rc::Rc;
use crate::di_container::blocking::binding::scope_configurator::BindingScopeConfigurator;
#[cfg(feature = "factory")]
@@ -16,22 +15,22 @@ use_double!(crate::di_container::blocking::DIContainer);
/// Binding builder for type `Interface` inside a [`DIContainer`].
#[must_use = "No binding will be created if you don't use the binding builder"]
-pub struct BindingBuilder<Interface>
+pub struct BindingBuilder<'di_container, Interface>
where
Interface: 'static + ?Sized,
{
- di_container: Rc<DIContainer>,
+ di_container: &'di_container DIContainer,
dependency_history_factory: fn() -> DependencyHistory,
interface_phantom: PhantomData<Interface>,
}
-impl<Interface> BindingBuilder<Interface>
+impl<'di_container, Interface> BindingBuilder<'di_container, Interface>
where
Interface: 'static + ?Sized,
{
pub(crate) fn new(
- di_container: Rc<DIContainer>,
+ di_container: &'di_container DIContainer,
dependency_history_factory: fn() -> DependencyHistory,
) -> Self
{
@@ -83,7 +82,10 @@ where
/// ```
pub fn to<Implementation>(
self,
- ) -> Result<BindingScopeConfigurator<Interface, Implementation>, BindingBuilderError>
+ ) -> Result<
+ BindingScopeConfigurator<'di_container, Interface, Implementation>,
+ BindingBuilderError,
+ >
where
Implementation: Injectable<DIContainer>,
{
@@ -97,7 +99,7 @@ where
}
let binding_scope_configurator = BindingScopeConfigurator::new(
- self.di_container.clone(),
+ self.di_container,
self.dependency_history_factory,
);
@@ -151,13 +153,13 @@ where
/// di_container
/// .bind::<ICustomerFactory>()
/// .to_factory(&|context| {
- /// Box::new(move |name, id| {
- /// let customer_id_factory = context
- /// .get::<ICustomerIDFactory>()
- /// .unwrap()
- /// .factory()
- /// .unwrap();
+ /// let customer_id_factory = context
+ /// .get::<ICustomerIDFactory>()
+ /// .unwrap()
+ /// .factory()
+ /// .unwrap();
///
+ /// Box::new(move |name, id| {
/// let customer_id = customer_id_factory(id);
///
/// let customer = TransientPtr::new(Customer::new(name, customer_id));
@@ -174,12 +176,12 @@ where
pub fn to_factory<Args, Return, Func>(
self,
factory_func: &'static Func,
- ) -> Result<BindingWhenConfigurator<Interface>, BindingBuilderError>
+ ) -> Result<BindingWhenConfigurator<'di_container, Interface>, BindingBuilderError>
where
Args: std::marker::Tuple + 'static,
Return: 'static + ?Sized,
Interface: Fn<Args, Output = crate::ptr::TransientPtr<Return>>,
- Func: Fn<(std::rc::Rc<DIContainer>,), Output = Box<Interface>>,
+ Func: Fn(&DIContainer) -> Box<Interface>,
{
use crate::private::castable_factory::blocking::CastableFactory;
@@ -260,14 +262,13 @@ where
pub fn to_default_factory<Return, FactoryFunc>(
self,
factory_func: &'static FactoryFunc,
- ) -> Result<BindingWhenConfigurator<Interface>, BindingBuilderError>
+ ) -> Result<BindingWhenConfigurator<'di_container, Interface>, BindingBuilderError>
where
Return: 'static + ?Sized,
- FactoryFunc: Fn<
- (Rc<DIContainer>,),
- Output = crate::ptr::TransientPtr<
- dyn Fn<(), Output = crate::ptr::TransientPtr<Return>>,
- >,
+ FactoryFunc: Fn(
+ &DIContainer,
+ ) -> crate::ptr::TransientPtr<
+ dyn Fn<(), Output = crate::ptr::TransientPtr<Return>>,
>,
{
use crate::private::castable_factory::blocking::CastableFactory;
@@ -325,7 +326,7 @@ mod tests
.once();
let binding_builder = BindingBuilder::<dyn subjects::INumber>::new(
- Rc::new(mock_di_container),
+ &mock_di_container,
MockDependencyHistory::new,
);
@@ -361,7 +362,7 @@ mod tests
.once();
let binding_builder = BindingBuilder::<IUserManagerFactory>::new(
- Rc::new(mock_di_container),
+ &mock_di_container,
MockDependencyHistory::new,
);
@@ -403,7 +404,7 @@ mod tests
.once();
let binding_builder = BindingBuilder::<dyn subjects::IUserManager>::new(
- Rc::new(mock_di_container),
+ &mock_di_container,
MockDependencyHistory::new,
);
diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs
index be469ba..3d939ba 100644
--- a/src/di_container/blocking/binding/scope_configurator.rs
+++ b/src/di_container/blocking/binding/scope_configurator.rs
@@ -1,6 +1,5 @@
//! Scope configurator for a binding for types inside of a [`DIContainer`].
use std::marker::PhantomData;
-use std::rc::Rc;
use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator;
use crate::di_container::BindingOptions;
@@ -14,25 +13,26 @@ use_double!(crate::dependency_history::DependencyHistory);
use_double!(crate::di_container::blocking::DIContainer);
/// Scope configurator for a binding for type `Interface` inside a [`DIContainer`].
-pub struct BindingScopeConfigurator<Interface, Implementation>
+pub struct BindingScopeConfigurator<'di_container, Interface, Implementation>
where
Interface: 'static + ?Sized,
Implementation: Injectable<DIContainer>,
{
- di_container: Rc<DIContainer>,
+ di_container: &'di_container DIContainer,
dependency_history_factory: fn() -> DependencyHistory,
interface_phantom: PhantomData<Interface>,
implementation_phantom: PhantomData<Implementation>,
}
-impl<Interface, Implementation> BindingScopeConfigurator<Interface, Implementation>
+impl<'di_container, Interface, Implementation>
+ BindingScopeConfigurator<'di_container, Interface, Implementation>
where
Interface: 'static + ?Sized,
Implementation: Injectable<DIContainer>,
{
pub(crate) fn new(
- di_container: Rc<DIContainer>,
+ di_container: &'di_container DIContainer,
dependency_history_factory: fn() -> DependencyHistory,
) -> Self
{
@@ -48,7 +48,7 @@ where
///
/// This is the default.
#[allow(clippy::must_use_candidate)]
- pub fn in_transient_scope(self) -> BindingWhenConfigurator<Interface>
+ pub fn in_transient_scope(self) -> BindingWhenConfigurator<'di_container, Interface>
{
self.set_in_transient_scope();
@@ -61,11 +61,14 @@ where
/// Will return Err if resolving the implementation fails.
pub fn in_singleton_scope(
self,
- ) -> Result<BindingWhenConfigurator<Interface>, BindingScopeConfiguratorError>
+ ) -> Result<
+ BindingWhenConfigurator<'di_container, Interface>,
+ BindingScopeConfiguratorError,
+ >
{
let singleton: SingletonPtr<Implementation> = SingletonPtr::from(
Implementation::resolve(
- &self.di_container,
+ self.di_container,
(self.dependency_history_factory)(),
)
.map_err(BindingScopeConfiguratorError::SingletonResolveFailed)?,
@@ -111,8 +114,7 @@ mod tests
dyn subjects::IUserManager,
subjects::UserManager,
>::new(
- Rc::new(di_container_mock),
- MockDependencyHistory::new,
+ &di_container_mock, MockDependencyHistory::new
);
binding_scope_configurator.in_transient_scope();
@@ -133,8 +135,7 @@ mod tests
dyn subjects::IUserManager,
subjects::UserManager,
>::new(
- Rc::new(di_container_mock),
- MockDependencyHistory::new,
+ &di_container_mock, MockDependencyHistory::new
);
assert!(binding_scope_configurator.in_singleton_scope().is_ok());
diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs
index 3d267b2..2a1af2c 100644
--- a/src/di_container/blocking/binding/when_configurator.rs
+++ b/src/di_container/blocking/binding/when_configurator.rs
@@ -1,7 +1,6 @@
//! When configurator for a binding for types inside of a [`DIContainer`].
use std::any::type_name;
use std::marker::PhantomData;
-use std::rc::Rc;
use crate::di_container::BindingOptions;
use crate::errors::di_container::BindingWhenConfiguratorError;
@@ -10,20 +9,20 @@ use crate::util::use_double;
use_double!(crate::di_container::blocking::DIContainer);
/// When configurator for a binding for type `Interface` inside a [`DIContainer`].
-pub struct BindingWhenConfigurator<Interface>
+pub struct BindingWhenConfigurator<'di_container, Interface>
where
Interface: 'static + ?Sized,
{
- di_container: Rc<DIContainer>,
+ di_container: &'di_container DIContainer,
interface_phantom: PhantomData<Interface>,
}
-impl<Interface> BindingWhenConfigurator<Interface>
+impl<'di_container, Interface> BindingWhenConfigurator<'di_container, Interface>
where
Interface: 'static + ?Sized,
{
- pub(crate) fn new(di_container: Rc<DIContainer>) -> Self
+ pub(crate) fn new(di_container: &'di_container DIContainer) -> Self
{
Self {
di_container,
@@ -88,9 +87,7 @@ mod tests
.once();
let binding_when_configurator =
- BindingWhenConfigurator::<dyn subjects::INumber>::new(Rc::new(
- di_container_mock,
- ));
+ BindingWhenConfigurator::<dyn subjects::INumber>::new(&di_container_mock);
assert!(binding_when_configurator.when_named("cool").is_ok());
}
diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs
index 09e7db1..a38b5da 100644
--- a/src/di_container/blocking/mod.rs
+++ b/src/di_container/blocking/mod.rs
@@ -51,7 +51,6 @@
//! ```
use std::any::type_name;
use std::cell::RefCell;
-use std::rc::Rc;
use crate::di_container::binding_storage::DIContainerBindingStorage;
use crate::di_container::blocking::binding::builder::BindingBuilder;
@@ -74,6 +73,7 @@ pub(crate) type BindingOptionsWithLt<'a> = BindingOptions<'a>;
pub(crate) type BindingOptionsWithLt = BindingOptions<'static>;
/// Blocking dependency injection container.
+#[derive(Default)]
pub struct DIContainer
{
binding_storage: RefCell<DIContainerBindingStorage<dyn IProvider<Self>>>,
@@ -83,11 +83,11 @@ impl DIContainer
{
/// Returns a new `DIContainer`.
#[must_use]
- pub fn new() -> Rc<Self>
+ pub fn new() -> Self
{
- Rc::new(Self {
+ Self {
binding_storage: RefCell::new(DIContainerBindingStorage::new()),
- })
+ }
}
}
@@ -96,7 +96,7 @@ impl DIContainer
{
/// Returns a new [`BindingBuilder`] for the given interface.
#[allow(clippy::missing_panics_doc)]
- pub fn bind<Interface>(self: &mut Rc<Self>) -> BindingBuilder<Interface>
+ pub fn bind<Interface>(&mut self) -> BindingBuilder<'_, Interface>
where
Interface: 'static + ?Sized,
{
@@ -104,7 +104,7 @@ impl DIContainer
panic!("Nope");
#[cfg(not(test))]
- BindingBuilder::new(self.clone(), DependencyHistory::new)
+ BindingBuilder::new(self, DependencyHistory::new)
}
/// Returns the type bound with `Interface`.
@@ -114,7 +114,7 @@ impl DIContainer
/// - No binding for `Interface` exists
/// - Resolving the binding for `Interface` fails
/// - Casting the binding for `Interface` fails
- pub fn get<Interface>(self: &Rc<Self>) -> Result<SomePtr<Interface>, DIContainerError>
+ pub fn get<Interface>(&self) -> Result<SomePtr<Interface>, DIContainerError>
where
Interface: 'static + ?Sized,
{
@@ -129,7 +129,7 @@ impl DIContainer
/// - Resolving the binding for `Interface` fails
/// - Casting the binding for `Interface` fails
pub fn get_named<Interface>(
- self: &Rc<Self>,
+ &self,
name: &'static str,
) -> Result<SomePtr<Interface>, DIContainerError>
where
@@ -177,7 +177,7 @@ impl DIContainer
/// # }
/// ```
pub fn get_bound<Interface>(
- self: &Rc<Self>,
+ &self,
dependency_history: DependencyHistory,
binding_options: BindingOptionsWithLt,
) -> Result<SomePtr<Interface>, DIContainerError>
@@ -215,7 +215,7 @@ impl DIContainer
binding_kind: "factory",
})?;
- Ok(SomePtr::Factory(factory.call(self.clone()).into()))
+ Ok(SomePtr::Factory(factory.call(self).into()))
}
#[cfg(feature = "factory")]
Providable::DefaultFactory(factory_binding) => {
@@ -234,15 +234,12 @@ impl DIContainer
binding_kind: "default factory",
})?;
- Ok(SomePtr::Transient(default_factory.call(self.clone())()))
+ Ok(SomePtr::Transient(default_factory.call(self)()))
}
}
}
- fn has_binding<Interface>(
- self: &Rc<Self>,
- binding_options: BindingOptionsWithLt,
- ) -> bool
+ fn has_binding<Interface>(&self, binding_options: BindingOptionsWithLt) -> bool
where
Interface: ?Sized + 'static,
{
@@ -252,7 +249,7 @@ impl DIContainer
}
fn set_binding<Interface>(
- self: &Rc<Self>,
+ &self,
binding_options: BindingOptions<'static>,
provider: Box<dyn IProvider<Self>>,
) where
@@ -264,7 +261,7 @@ impl DIContainer
}
fn remove_binding<Interface>(
- self: &Rc<Self>,
+ &self,
binding_options: BindingOptions<'static>,
) -> Option<Box<dyn IProvider<Self>>>
where
@@ -279,7 +276,7 @@ impl DIContainer
impl DIContainer
{
fn get_binding_providable<Interface>(
- self: &Rc<Self>,
+ &self,
binding_options: BindingOptionsWithLt,
dependency_history: DependencyHistory,
) -> Result<Providable<Self>, DIContainerError>
@@ -494,15 +491,14 @@ mod tests
let di_container = DIContainer::new();
- let factory_func: &dyn Fn(Rc<DIContainer>) -> Box<IUserManagerFactory> =
- &|_: Rc<DIContainer>| {
- Box::new(move |users| {
- let user_manager: TransientPtr<dyn IUserManager> =
- TransientPtr::new(UserManager::new(users));
+ let factory_func: &dyn Fn(&DIContainer) -> Box<IUserManagerFactory> = &|_| {
+ Box::new(move |users| {
+ let user_manager: TransientPtr<dyn IUserManager> =
+ TransientPtr::new(UserManager::new(users));
- user_manager
- })
- };
+ user_manager
+ })
+ };
let mut mock_provider = MockIProvider::new();
@@ -572,15 +568,14 @@ mod tests
let di_container = DIContainer::new();
- let factory_func: &dyn Fn(Rc<DIContainer>) -> Box<IUserManagerFactory> =
- &|_: Rc<DIContainer>| {
- Box::new(move |users| {
- let user_manager: TransientPtr<dyn IUserManager> =
- TransientPtr::new(UserManager::new(users));
+ let factory_func: &dyn Fn(&DIContainer) -> Box<IUserManagerFactory> = &|_| {
+ Box::new(move |users| {
+ let user_manager: TransientPtr<dyn IUserManager> =
+ TransientPtr::new(UserManager::new(users));
- user_manager
- })
- };
+ user_manager
+ })
+ };
let mut mock_provider = MockIProvider::new();
diff --git a/src/interfaces/injectable.rs b/src/interfaces/injectable.rs
index 458b167..d82f042 100644
--- a/src/interfaces/injectable.rs
+++ b/src/interfaces/injectable.rs
@@ -1,6 +1,5 @@
//! Interface for structs that can be injected into or be injected to.
use std::fmt::Debug;
-use std::rc::Rc;
use crate::errors::injectable::InjectableError;
use crate::private::cast::CastFrom;
@@ -17,7 +16,7 @@ pub trait Injectable<DIContainerT>: CastFrom
/// # Errors
/// Will return `Err` if resolving the dependencies fails.
fn resolve(
- di_container: &Rc<DIContainerT>,
+ di_container: &DIContainerT,
dependency_history: DependencyHistory,
) -> Result<TransientPtr<Self>, InjectableError>
where
diff --git a/src/private/castable_factory/blocking.rs b/src/private/castable_factory/blocking.rs
index 0761b7e..a28ce71 100644
--- a/src/private/castable_factory/blocking.rs
+++ b/src/private/castable_factory/blocking.rs
@@ -1,6 +1,5 @@
use std::any::type_name;
use std::fmt::Debug;
-use std::rc::Rc;
use crate::private::any_factory::AnyFactory;
use crate::private::factory::IFactory;
@@ -11,7 +10,7 @@ where
ReturnInterface: 'static + ?Sized,
DIContainerT: 'static,
{
- func: &'static dyn Fn<(Rc<DIContainerT>,), Output = TransientPtr<ReturnInterface>>,
+ func: &'static dyn Fn(&DIContainerT) -> TransientPtr<ReturnInterface>,
}
impl<ReturnInterface, DIContainerT> CastableFactory<ReturnInterface, DIContainerT>
@@ -19,10 +18,7 @@ where
ReturnInterface: 'static + ?Sized,
{
pub fn new(
- func: &'static dyn Fn<
- (Rc<DIContainerT>,),
- Output = TransientPtr<ReturnInterface>,
- >,
+ func: &'static dyn Fn(&DIContainerT) -> TransientPtr<ReturnInterface>,
) -> Self
{
Self { func }
@@ -34,7 +30,7 @@ impl<ReturnInterface, DIContainerT> IFactory<ReturnInterface, DIContainerT>
where
ReturnInterface: 'static + ?Sized,
{
- fn call(&self, di_container: Rc<DIContainerT>) -> TransientPtr<ReturnInterface>
+ fn call(&self, di_container: &DIContainerT) -> TransientPtr<ReturnInterface>
{
(self.func)(di_container)
}
@@ -58,7 +54,9 @@ where
{
let ret = type_name::<TransientPtr<ReturnInterface>>();
- formatter.write_fmt(format_args!("CastableFactory (Rc<DIContainer>) -> {ret}"))
+ formatter.write_fmt(format_args!(
+ "CastableFactory (&DIContainer) -> {ret} {{ ... }}"
+ ))
}
}
@@ -77,12 +75,13 @@ mod tests
#[test]
fn can_call()
{
- let castable_factory =
- CastableFactory::new(&|_| TransientPtr::new(Bacon { heal_amount: 27 }));
+ let castable_factory = CastableFactory::new(&|_: &MockDIContainer| {
+ TransientPtr::new(Bacon { heal_amount: 27 })
+ });
- let mock_di_container = Rc::new(MockDIContainer::new());
+ let mock_di_container = MockDIContainer::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 23b4e8f..94e1023 100644
--- a/src/private/factory.rs
+++ b/src/private/factory.rs
@@ -1,5 +1,3 @@
-use std::rc::Rc;
-
use crate::private::cast::CastFrom;
use crate::ptr::TransientPtr;
@@ -8,7 +6,7 @@ pub trait IFactory<ReturnInterface, DIContainerT>: CastFrom
where
ReturnInterface: 'static + ?Sized,
{
- fn call(&self, di_container: Rc<DIContainerT>) -> TransientPtr<ReturnInterface>;
+ fn call(&self, di_container: &DIContainerT) -> TransientPtr<ReturnInterface>;
}
/// Interface for a threadsafe factory.
diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs
index c719f59..1533f68 100644
--- a/src/provider/blocking.rs
+++ b/src/provider/blocking.rs
@@ -1,5 +1,4 @@
use std::marker::PhantomData;
-use std::rc::Rc;
use crate::errors::injectable::InjectableError;
use crate::interfaces::injectable::Injectable;
@@ -24,7 +23,7 @@ pub trait IProvider<DIContainerType>
{
fn provide(
&self,
- di_container: &Rc<DIContainerType>,
+ di_container: &DIContainerType,
dependency_history: DependencyHistory,
) -> Result<Providable<DIContainerType>, InjectableError>;
}
@@ -58,7 +57,7 @@ where
{
fn provide(
&self,
- di_container: &Rc<DIContainerType>,
+ di_container: &DIContainerType,
dependency_history: DependencyHistory,
) -> Result<Providable<DIContainerType>, InjectableError>
{
@@ -98,7 +97,7 @@ where
{
fn provide(
&self,
- _di_container: &Rc<DIContainerType>,
+ _di_container: &DIContainerType,
_dependency_history: DependencyHistory,
) -> Result<Providable<DIContainerType>, InjectableError>
{
@@ -133,7 +132,7 @@ impl<DIContainerType> IProvider<DIContainerType> for FactoryProvider
{
fn provide(
&self,
- _di_container: &Rc<DIContainerType>,
+ _di_container: &DIContainerType,
_dependency_history: DependencyHistory,
) -> Result<Providable<DIContainerType>, InjectableError>
{
@@ -168,7 +167,7 @@ mod tests
assert!(
matches!(
transient_type_provider
- .provide(&Rc::new(di_container), dependency_history_mock)?,
+ .provide(&di_container, dependency_history_mock)?,
Providable::Transient(_)
),
"The provided type is not transient"
@@ -190,7 +189,7 @@ mod tests
assert!(
matches!(
singleton_provider
- .provide(&Rc::new(di_container), MockDependencyHistory::new())?,
+ .provide(&di_container, MockDependencyHistory::new())?,
Providable::Singleton(_)
),
"The provided type is not a singleton"
@@ -215,7 +214,7 @@ mod tests
let default_factory_provider =
FactoryProvider::new(FactoryPtr::new(FooFactory), true);
- let di_container = Rc::new(MockDIContainer::new());
+ let di_container = MockDIContainer::new();
assert!(
matches!(
diff --git a/src/test_utils.rs b/src/test_utils.rs
index 41e3753..6071cfb 100644
--- a/src/test_utils.rs
+++ b/src/test_utils.rs
@@ -3,7 +3,6 @@ pub mod subjects
//! Test subjects.
use std::fmt::Debug;
- use std::rc::Rc;
use syrette_macros::declare_interface;
@@ -51,7 +50,7 @@ pub mod subjects
impl<DIContainerT> Injectable<DIContainerT> for UserManager
{
fn resolve(
- _di_container: &Rc<DIContainerT>,
+ _di_container: &DIContainerT,
_dependency_history: DependencyHistory,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
@@ -115,7 +114,7 @@ pub mod subjects
impl<DIContainerT> Injectable<DIContainerT> for Number
{
fn resolve(
- _di_container: &Rc<DIContainerT>,
+ _di_container: &DIContainerT,
_dependency_history: DependencyHistory,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where