From aa548ded39c7ba1927019c748c359523b21d59e8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 29 Oct 2022 14:38:51 +0200 Subject: refactor!: add dependency history type BREAKING CHANGE: Binding builders & configurators now take dependency history type arguments, the DetectedCircular variant of InjectableError now contains a dependency history field & the injectable traits take dependency history instead of a Vec --- src/di_container/blocking/mod.rs | 146 ++++++++++----------------------------- 1 file changed, 38 insertions(+), 108 deletions(-) (limited to 'src/di_container/blocking/mod.rs') diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index bf77aba..6338118 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -54,6 +54,7 @@ use std::any::type_name; use std::cell::RefCell; use std::rc::Rc; +use crate::dependency_history::{DependencyHistory, IDependencyHistory}; use crate::di_container::binding_storage::DIContainerBindingStorage; use crate::di_container::blocking::binding::builder::BindingBuilder; use crate::errors::di_container::DIContainerError; @@ -65,10 +66,15 @@ pub mod binding; pub mod prelude; /// Blocking dependency injection container interface. -pub trait IDIContainer: Sized + 'static + details::DIContainerInternals +pub trait IDIContainer: + Sized + 'static + details::DIContainerInternals +where + DependencyHistoryType: IDependencyHistory, { /// Returns a new [`BindingBuilder`] for the given interface. - fn bind(self: &mut Rc) -> BindingBuilder + fn bind( + self: &mut Rc, + ) -> BindingBuilder where Interface: 'static + ?Sized; @@ -100,7 +106,7 @@ pub trait IDIContainer: Sized + 'static + details::DIContainerInternals #[doc(hidden)] fn get_bound( self: &Rc, - dependency_history: Vec<&'static str>, + dependency_history: DependencyHistoryType, name: Option<&'static str>, ) -> Result, DIContainerError> where @@ -110,7 +116,8 @@ pub trait IDIContainer: Sized + 'static + details::DIContainerInternals /// Blocking dependency injection container. pub struct DIContainer { - binding_storage: RefCell>>, + binding_storage: + RefCell>>, } impl DIContainer @@ -125,21 +132,23 @@ impl DIContainer } } -impl IDIContainer for DIContainer +impl IDIContainer for DIContainer { #[must_use] - fn bind(self: &mut Rc) -> BindingBuilder + fn bind( + self: &mut Rc, + ) -> BindingBuilder where Interface: 'static + ?Sized, { - BindingBuilder::::new(self.clone()) + BindingBuilder::new(self.clone(), DependencyHistory::new) } fn get(self: &Rc) -> Result, DIContainerError> where Interface: 'static + ?Sized, { - self.get_bound::(Vec::new(), None) + self.get_bound::(DependencyHistory::new(), None) } fn get_named( @@ -149,13 +158,13 @@ impl IDIContainer for DIContainer where Interface: 'static + ?Sized, { - self.get_bound::(Vec::new(), Some(name)) + self.get_bound::(DependencyHistory::new(), Some(name)) } #[doc(hidden)] fn get_bound( self: &Rc, - dependency_history: Vec<&'static str>, + dependency_history: DependencyHistory, name: Option<&'static str>, ) -> Result, DIContainerError> where @@ -168,7 +177,7 @@ impl IDIContainer for DIContainer } } -impl details::DIContainerInternals for DIContainer +impl details::DIContainerInternals for DIContainer { fn has_binding(self: &Rc, name: Option<&'static str>) -> bool where @@ -180,7 +189,7 @@ impl details::DIContainerInternals for DIContainer fn set_binding( self: &Rc, name: Option<&'static str>, - provider: Box>, + provider: Box>, ) where Interface: 'static + ?Sized, { @@ -192,7 +201,7 @@ impl details::DIContainerInternals for DIContainer fn remove_binding( self: &Rc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> where Interface: 'static + ?Sized, { @@ -204,7 +213,7 @@ impl DIContainer { fn handle_binding_providable( self: &Rc, - binding_providable: Providable, + binding_providable: Providable, ) -> Result, DIContainerError> where Interface: 'static + ?Sized, @@ -262,8 +271,8 @@ impl DIContainer fn get_binding_providable( self: &Rc, name: Option<&'static str>, - dependency_history: Vec<&'static str>, - ) -> Result, DIContainerError> + dependency_history: DependencyHistory, + ) -> Result, DIContainerError> where Interface: 'static + ?Sized, { @@ -291,9 +300,12 @@ pub(crate) mod details { use std::rc::Rc; + use crate::dependency_history::IDependencyHistory; use crate::provider::blocking::IProvider; - pub trait DIContainerInternals + pub trait DIContainerInternals + where + DependencyHistoryType: IDependencyHistory, { fn has_binding(self: &Rc, name: Option<&'static str>) -> bool where @@ -302,14 +314,14 @@ pub(crate) mod details fn set_binding( self: &Rc, name: Option<&'static str>, - provider: Box>, + provider: Box>, ) where Interface: 'static + ?Sized; fn remove_binding( self: &Rc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> where Interface: 'static + ?Sized; } @@ -320,33 +332,16 @@ mod tests { use std::error::Error; - use mockall::mock; - use super::*; - use crate::errors::injectable::InjectableError; - use crate::provider::blocking::IProvider; use crate::ptr::{SingletonPtr, TransientPtr}; - use crate::test_utils::subjects; + use crate::test_utils::{mocks, subjects}; #[test] fn can_get() -> Result<(), Box> { - mock! { - Provider {} - - impl IProvider for Provider - { - fn provide( - &self, - di_container: &Rc, - dependency_history: Vec<&'static str>, - ) -> Result, InjectableError>; - } - } - let di_container = DIContainer::new(); - let mut mock_provider = MockProvider::new(); + let mut mock_provider = mocks::blocking_provider::MockProvider::new(); mock_provider.expect_provide().returning(|_, _| { Ok(Providable::Transient(TransientPtr::new( @@ -369,22 +364,9 @@ mod tests #[test] fn can_get_named() -> Result<(), Box> { - mock! { - Provider {} - - impl IProvider for Provider - { - fn provide( - &self, - di_container: &Rc, - dependency_history: Vec<&'static str>, - ) -> Result, InjectableError>; - } - } - let di_container = DIContainer::new(); - let mut mock_provider = MockProvider::new(); + let mut mock_provider = mocks::blocking_provider::MockProvider::new(); mock_provider.expect_provide().returning(|_, _| { Ok(Providable::Transient(TransientPtr::new( @@ -407,22 +389,9 @@ mod tests #[test] fn can_get_singleton() -> Result<(), Box> { - mock! { - Provider {} - - impl IProvider for Provider - { - fn provide( - &self, - di_container: &Rc, - dependency_history: Vec<&'static str>, - ) -> Result, InjectableError>; - } - } - let di_container = DIContainer::new(); - let mut mock_provider = MockProvider::new(); + let mut mock_provider = mocks::blocking_provider::MockProvider::new(); let mut singleton = SingletonPtr::new(subjects::Number::new()); @@ -452,22 +421,9 @@ mod tests #[test] fn can_get_singleton_named() -> Result<(), Box> { - mock! { - Provider {} - - impl IProvider for Provider - { - fn provide( - &self, - di_container: &Rc, - dependency_history: Vec<&'static str>, - ) -> Result, InjectableError>; - } - } - let di_container = DIContainer::new(); - let mut mock_provider = MockProvider::new(); + let mut mock_provider = mocks::blocking_provider::MockProvider::new(); let mut singleton = SingletonPtr::new(subjects::Number::new()); @@ -552,19 +508,6 @@ mod tests #[crate::factory] type IUserManagerFactory = dyn Fn(Vec) -> dyn IUserManager; - mock! { - Provider {} - - impl IProvider for Provider - { - fn provide( - &self, - di_container: &Rc, - dependency_history: Vec<&'static str>, - ) -> Result, InjectableError>; - } - } - let di_container = DIContainer::new(); let factory_func: &'static FactoryFunc = &|_: Rc| { @@ -576,7 +519,7 @@ mod tests }) }; - let mut mock_provider = MockProvider::new(); + let mut mock_provider = mocks::blocking_provider::MockProvider::new(); mock_provider.expect_provide().returning_st(|_, _| { Ok(Providable::Factory(FactoryPtr::new(CastableFactory::new( @@ -649,19 +592,6 @@ mod tests #[crate::factory] type IUserManagerFactory = dyn Fn(Vec) -> dyn IUserManager; - mock! { - Provider {} - - impl IProvider for Provider - { - fn provide( - &self, - di_container: &Rc, - dependency_history: Vec<&'static str>, - ) -> Result, InjectableError>; - } - } - let di_container = DIContainer::new(); let factory_func: &'static FactoryFunc = &|_: Rc| { @@ -673,7 +603,7 @@ mod tests }) }; - let mut mock_provider = MockProvider::new(); + let mut mock_provider = mocks::blocking_provider::MockProvider::new(); mock_provider.expect_provide().returning_st(|_, _| { Ok(Providable::Factory(FactoryPtr::new(CastableFactory::new( -- cgit v1.2.3-18-g5258