diff options
author | HampusM <hampus@hampusmat.com> | 2023-09-17 13:32:06 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-09-17 13:32:06 +0200 |
commit | a1ddd1bc6e1af0d98b87126d3e86da30bc1d3f1c (patch) | |
tree | d4a9cfd9e3970a955e27ac518a593f55e8ad8d9c /src/private | |
parent | 3e3a853615e97ee7c3aef09736eae8170c6fd78e (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
Diffstat (limited to 'src/private')
-rw-r--r-- | src/private/castable_factory/blocking.rs | 23 | ||||
-rw-r--r-- | src/private/factory.rs | 4 |
2 files changed, 12 insertions, 15 deletions
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. |