From 89c238f9c82ade2d7656e2bee76838a391609a88 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 5 Aug 2023 23:14:06 +0200 Subject: refactor!: remove IDependencyHistory BREAKING CHANGE: IDependencyHistory has been removed as part of an effort to simplify the API. This affects IDIContainer, DIContainer, IAsyncDIContainer, AsyncDIContainer, Injectable, AsyncInjectable, BindingBuilder, AsyncBindingBuilder, BindingScopeConfigurator, BindingWhenConfigurator, AsyncBindingScopeConfigurator, AsyncBindingWhenConfigurator and DependencyHistory --- src/di_container/blocking/binding/builder.rs | 78 ++++++++------------- .../blocking/binding/scope_configurator.rs | 79 ++++++++-------------- .../blocking/binding/when_configurator.rs | 17 ++--- src/di_container/blocking/mod.rs | 43 +++++------- 4 files changed, 81 insertions(+), 136 deletions(-) (limited to 'src/di_container/blocking') diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs index 27151d7..991961c 100644 --- a/src/di_container/blocking/binding/builder.rs +++ b/src/di_container/blocking/binding/builder.rs @@ -5,40 +5,39 @@ use std::any::type_name; use std::marker::PhantomData; use std::rc::Rc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::binding::scope_configurator::BindingScopeConfigurator; #[cfg(feature = "factory")] use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator; use crate::di_container::blocking::IDIContainer; use crate::errors::di_container::BindingBuilderError; use crate::interfaces::injectable::Injectable; +use crate::util::use_dependency_history; + +use_dependency_history!(); /// Binding builder for type `Interface` inside a [`IDIContainer`]. /// /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer #[must_use = "No binding will be created if you don't use the binding builder"] -pub struct BindingBuilder +pub struct BindingBuilder where Interface: 'static + ?Sized, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { di_container: Rc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, } -impl - BindingBuilder +impl BindingBuilder where Interface: 'static + ?Sized, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory + 'static, + DIContainerType: IDIContainer, { pub(crate) fn new( di_container: Rc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, ) -> Self { Self { @@ -93,16 +92,11 @@ where pub fn to( self, ) -> Result< - BindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, - >, + BindingScopeConfigurator, BindingBuilderError, > where - Implementation: Injectable, + Implementation: Injectable, { { if self.di_container.has_binding::(None) { @@ -194,10 +188,7 @@ where pub fn to_factory( self, factory_func: &'static Func, - ) -> Result< - BindingWhenConfigurator, - BindingBuilderError, - > + ) -> Result, BindingBuilderError> where Args: std::marker::Tuple + 'static, Return: 'static + ?Sized, @@ -283,10 +274,7 @@ where pub fn to_default_factory( self, factory_func: &'static FactoryFunc, - ) -> Result< - BindingWhenConfigurator, - BindingBuilderError, - > + ) -> Result, BindingBuilderError> where Return: 'static + ?Sized, FactoryFunc: Fn< @@ -326,6 +314,7 @@ mod tests use mockall::predicate::eq; use super::*; + use crate::dependency_history::MockDependencyHistory; use crate::test_utils::{mocks, subjects}; #[test] @@ -345,14 +334,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = BindingBuilder::< - dyn subjects::INumber, - mocks::blocking_di_container::MockDIContainer, - mocks::MockDependencyHistory, - >::new( - Rc::new(mock_di_container), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + BindingBuilder::< + dyn subjects::INumber, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(mock_di_container), MockDependencyHistory::new); binding_builder.to::()?; @@ -384,14 +370,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = BindingBuilder::< - IUserManagerFactory, - mocks::blocking_di_container::MockDIContainer, - mocks::MockDependencyHistory, - >::new( - Rc::new(mock_di_container), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + BindingBuilder::< + IUserManagerFactory, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(mock_di_container), MockDependencyHistory::new); binding_builder.to_factory(&|_| { Box::new(move |_num, _text| { @@ -430,14 +413,11 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_builder = BindingBuilder::< - dyn subjects::IUserManager, - mocks::blocking_di_container::MockDIContainer, - mocks::MockDependencyHistory, - >::new( - Rc::new(mock_di_container), - mocks::MockDependencyHistory::new, - ); + let binding_builder = + BindingBuilder::< + dyn subjects::IUserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(mock_di_container), MockDependencyHistory::new); binding_builder.to_default_factory(&|_| { Box::new(move || { diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs index f318dd6..0e2437f 100644 --- a/src/di_container/blocking/binding/scope_configurator.rs +++ b/src/di_container/blocking/binding/scope_configurator.rs @@ -4,51 +4,42 @@ use std::marker::PhantomData; use std::rc::Rc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::binding::when_configurator::BindingWhenConfigurator; use crate::di_container::blocking::IDIContainer; use crate::errors::di_container::BindingScopeConfiguratorError; use crate::interfaces::injectable::Injectable; use crate::provider::blocking::{SingletonProvider, TransientTypeProvider}; use crate::ptr::SingletonPtr; +use crate::util::use_dependency_history; + +use_dependency_history!(); /// Scope configurator for a binding for type `Interface` inside a [`IDIContainer`]. /// /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer -pub struct BindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, -> where +pub struct BindingScopeConfigurator +where Interface: 'static + ?Sized, - Implementation: Injectable, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + Implementation: Injectable, + DIContainerType: IDIContainer, { di_container: Rc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, interface_phantom: PhantomData, implementation_phantom: PhantomData, } -impl - BindingScopeConfigurator< - Interface, - Implementation, - DIContainerType, - DependencyHistoryType, - > +impl + BindingScopeConfigurator where Interface: 'static + ?Sized, - Implementation: Injectable, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory + 'static, + Implementation: Injectable, + DIContainerType: IDIContainer, { pub(crate) fn new( di_container: Rc, - dependency_history_factory: fn() -> DependencyHistoryType, + dependency_history_factory: fn() -> DependencyHistory, ) -> Self { Self { @@ -63,9 +54,8 @@ where /// /// This is the default. #[allow(clippy::must_use_candidate)] - pub fn in_transient_scope( - self, - ) -> BindingWhenConfigurator + pub fn in_transient_scope(self) + -> BindingWhenConfigurator { self.set_in_transient_scope(); @@ -79,7 +69,7 @@ where pub fn in_singleton_scope( self, ) -> Result< - BindingWhenConfigurator, + BindingWhenConfigurator, BindingScopeConfiguratorError, > { @@ -101,11 +91,7 @@ where { self.di_container.set_binding::( None, - Box::new(TransientTypeProvider::< - Implementation, - DIContainerType, - DependencyHistoryType, - >::new()), + Box::new(TransientTypeProvider::::new()), ); } } @@ -114,6 +100,7 @@ where mod tests { use super::*; + use crate::dependency_history::MockDependencyHistory; use crate::test_utils::{mocks, subjects}; #[test] @@ -127,15 +114,12 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_scope_configurator = BindingScopeConfigurator::< - dyn subjects::IUserManager, - subjects::UserManager, - mocks::blocking_di_container::MockDIContainer, - mocks::MockDependencyHistory, - >::new( - Rc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_scope_configurator = + BindingScopeConfigurator::< + dyn subjects::IUserManager, + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(di_container_mock), MockDependencyHistory::new); binding_scope_configurator.in_transient_scope(); } @@ -151,15 +135,12 @@ mod tests .return_once(|_name, _provider| ()) .once(); - let binding_scope_configurator = BindingScopeConfigurator::< - dyn subjects::IUserManager, - subjects::UserManager, - mocks::blocking_di_container::MockDIContainer, - mocks::MockDependencyHistory, - >::new( - Rc::new(di_container_mock), - mocks::MockDependencyHistory::new, - ); + let binding_scope_configurator = + BindingScopeConfigurator::< + dyn subjects::IUserManager, + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::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 31b1b48..fcef377 100644 --- a/src/di_container/blocking/binding/when_configurator.rs +++ b/src/di_container/blocking/binding/when_configurator.rs @@ -5,38 +5,32 @@ use std::any::type_name; use std::marker::PhantomData; use std::rc::Rc; -use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::IDIContainer; use crate::errors::di_container::BindingWhenConfiguratorError; /// When configurator for a binding for type `Interface` inside a [`IDIContainer`]. /// /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer -pub struct BindingWhenConfigurator +pub struct BindingWhenConfigurator where Interface: 'static + ?Sized, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { di_container: Rc, interface_phantom: PhantomData, - dependency_history_phantom: PhantomData, } -impl - BindingWhenConfigurator +impl BindingWhenConfigurator where Interface: 'static + ?Sized, - DIContainerType: IDIContainer, - DependencyHistoryType: IDependencyHistory, + DIContainerType: IDIContainer, { pub(crate) fn new(di_container: Rc) -> Self { Self { di_container, interface_phantom: PhantomData, - dependency_history_phantom: PhantomData, } } @@ -97,8 +91,7 @@ mod tests let binding_when_configurator = BindingWhenConfigurator::< dyn subjects::INumber, - mocks::blocking_di_container::MockDIContainer, - mocks::MockDependencyHistory, + mocks::blocking_di_container::MockDIContainer, >::new(Rc::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 cd7065c..5781583 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -54,7 +54,6 @@ 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; @@ -62,6 +61,9 @@ use crate::private::cast::boxed::CastBox; use crate::private::cast::rc::CastRc; use crate::provider::blocking::{IProvider, Providable}; use crate::ptr::SomePtr; +use crate::util::use_dependency_history; + +use_dependency_history!(); pub mod binding; pub mod prelude; @@ -69,15 +71,10 @@ pub mod prelude; /// Blocking dependency injection container interface. /// /// **This trait is sealed and cannot be implemented for types outside this crate.** -pub trait IDIContainer: - Sized + 'static + details::DIContainerInternals -where - DependencyHistoryType: IDependencyHistory, +pub trait IDIContainer: Sized + 'static + details::DIContainerInternals { /// Returns a new [`BindingBuilder`] for the given interface. - fn bind( - self: &mut Rc, - ) -> BindingBuilder + fn bind(self: &mut Rc) -> BindingBuilder where Interface: 'static + ?Sized; @@ -109,7 +106,7 @@ where #[doc(hidden)] fn get_bound( self: &Rc, - dependency_history: DependencyHistoryType, + dependency_history: DependencyHistory, name: Option<&'static str>, ) -> Result, DIContainerError> where @@ -119,8 +116,7 @@ where /// Blocking dependency injection container. pub struct DIContainer { - binding_storage: - RefCell>>, + binding_storage: RefCell>>, } impl DIContainer @@ -135,11 +131,9 @@ impl DIContainer } } -impl IDIContainer for DIContainer +impl IDIContainer for DIContainer { - fn bind( - self: &mut Rc, - ) -> BindingBuilder + fn bind(self: &mut Rc) -> BindingBuilder where Interface: 'static + ?Sized, { @@ -183,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 @@ -195,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, { @@ -207,7 +201,7 @@ impl details::DIContainerInternals for DIContainer fn remove_binding( self: &Rc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> where Interface: 'static + ?Sized, { @@ -219,7 +213,7 @@ impl DIContainer { fn handle_binding_providable( #[cfg(feature = "factory")] self: &Rc, - binding_providable: Providable, + binding_providable: Providable, ) -> Result, DIContainerError> where Interface: 'static + ?Sized, @@ -278,7 +272,7 @@ impl DIContainer self: &Rc, name: Option<&'static str>, dependency_history: DependencyHistory, - ) -> Result, DIContainerError> + ) -> Result, DIContainerError> where Interface: 'static + ?Sized, { @@ -306,12 +300,9 @@ pub(crate) mod details { use std::rc::Rc; - use crate::dependency_history::IDependencyHistory; use crate::provider::blocking::IProvider; - pub trait DIContainerInternals - where - DependencyHistoryType: IDependencyHistory, + pub trait DIContainerInternals { fn has_binding(self: &Rc, name: Option<&'static str>) -> bool where @@ -320,14 +311,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; } -- cgit v1.2.3-18-g5258