From 9e01cdf341a7866180b3a63d745f3b2d7578d28a Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 23 Oct 2022 18:12:23 +0200 Subject: refactor!: reduce DI container coupling BREAKING CHANGE: You now have to import the DI containers's interfaces to use the DI containers's methods --- src/provider/blocking.rs | 75 +++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 27 deletions(-) (limited to 'src/provider/blocking.rs') diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs index 16e8847..b768b55 100644 --- a/src/provider/blocking.rs +++ b/src/provider/blocking.rs @@ -1,16 +1,18 @@ use std::marker::PhantomData; use std::rc::Rc; +use crate::di_container::blocking::IDIContainer; use crate::errors::injectable::InjectableError; use crate::interfaces::injectable::Injectable; use crate::ptr::{SingletonPtr, TransientPtr}; -use crate::DIContainer; #[derive(strum_macros::Display, Debug)] -pub enum Providable +pub enum Providable +where + DIContainerType: IDIContainer, { - Transient(TransientPtr), - Singleton(SingletonPtr), + Transient(TransientPtr>), + Singleton(SingletonPtr>), #[cfg(feature = "factory")] Factory(crate::ptr::FactoryPtr), #[cfg(feature = "factory")] @@ -19,43 +21,52 @@ pub enum Providable ), } -pub trait IProvider +pub trait IProvider +where + DIContainerType: IDIContainer, { fn provide( &self, - di_container: &Rc, + di_container: &Rc, dependency_history: Vec<&'static str>, - ) -> Result; + ) -> Result, InjectableError>; } -pub struct TransientTypeProvider +pub struct TransientTypeProvider where - InjectableType: Injectable, + InjectableType: Injectable, + DIContainerType: IDIContainer, { injectable_phantom: PhantomData, + di_container_phantom: PhantomData, } -impl TransientTypeProvider +impl + TransientTypeProvider where - InjectableType: Injectable, + InjectableType: Injectable, + DIContainerType: IDIContainer, { pub fn new() -> Self { Self { injectable_phantom: PhantomData, + di_container_phantom: PhantomData, } } } -impl IProvider for TransientTypeProvider +impl IProvider + for TransientTypeProvider where - InjectableType: Injectable, + InjectableType: Injectable, + DIContainerType: IDIContainer, { fn provide( &self, - di_container: &Rc, + di_container: &Rc, dependency_history: Vec<&'static str>, - ) -> Result + ) -> Result, InjectableError> { Ok(Providable::Transient(InjectableType::resolve( di_container, @@ -64,32 +75,40 @@ where } } -pub struct SingletonProvider +pub struct SingletonProvider where - InjectableType: Injectable, + InjectableType: Injectable, + DIContainerType: IDIContainer, { singleton: SingletonPtr, + di_container_phantom: PhantomData, } -impl SingletonProvider +impl SingletonProvider where - InjectableType: Injectable, + InjectableType: Injectable, + DIContainerType: IDIContainer, { pub fn new(singleton: SingletonPtr) -> Self { - Self { singleton } + Self { + singleton, + di_container_phantom: PhantomData, + } } } -impl IProvider for SingletonProvider +impl IProvider + for SingletonProvider where - InjectableType: Injectable, + InjectableType: Injectable, + DIContainerType: IDIContainer, { fn provide( &self, - _di_container: &Rc, + _di_container: &Rc, _dependency_history: Vec<&'static str>, - ) -> Result + ) -> Result, InjectableError> { Ok(Providable::Singleton(self.singleton.clone())) } @@ -118,13 +137,15 @@ impl FactoryProvider } #[cfg(feature = "factory")] -impl IProvider for FactoryProvider +impl IProvider for FactoryProvider +where + DIContainerType: IDIContainer, { fn provide( &self, - _di_container: &Rc, + _di_container: &Rc, _dependency_history: Vec<&'static str>, - ) -> Result + ) -> Result, InjectableError> { Ok(if self.is_default_factory { Providable::DefaultFactory(self.factory.clone()) -- cgit v1.2.3-18-g5258