diff options
-rw-r--r-- | syrette/src/di_container.rs | 43 | ||||
-rw-r--r-- | syrette/src/provider.rs | 46 |
2 files changed, 48 insertions, 41 deletions
diff --git a/syrette/src/di_container.rs b/syrette/src/di_container.rs index ce0d829..7aa8225 100644 --- a/syrette/src/di_container.rs +++ b/syrette/src/di_container.rs @@ -7,48 +7,9 @@ use std::rc::Rc; use error_stack::{Context, Report, ResultExt}; -use crate::injectable::{Injectable, ResolveError}; +use crate::injectable::Injectable; use crate::libs::intertrait::cast_box::CastBox; - -trait IInjectableTypeProvider -{ - fn provide( - &self, - di_container: &DIContainer, - ) -> error_stack::Result<Box<dyn Injectable>, ResolveError>; -} - -struct InjectableTypeProvider<InjectableType> -where - InjectableType: Injectable, -{ - _phantom_data: PhantomData<InjectableType>, -} - -impl<InjectableType> InjectableTypeProvider<InjectableType> -where - InjectableType: Injectable, -{ - fn new() -> Self - { - Self { - _phantom_data: PhantomData, - } - } -} - -impl<InjectableType> IInjectableTypeProvider for InjectableTypeProvider<InjectableType> -where - InjectableType: Injectable, -{ - fn provide( - &self, - di_container: &DIContainer, - ) -> error_stack::Result<Box<dyn Injectable>, ResolveError> - { - Ok(InjectableType::resolve(di_container)?) - } -} +use crate::provider::{IInjectableTypeProvider, InjectableTypeProvider}; pub struct BindingBuilder<'a, InterfaceTrait> where diff --git a/syrette/src/provider.rs b/syrette/src/provider.rs new file mode 100644 index 0000000..090aaac --- /dev/null +++ b/syrette/src/provider.rs @@ -0,0 +1,46 @@ +use std::marker::PhantomData; + +extern crate error_stack; + +use crate::injectable::{Injectable, ResolveError}; +use crate::DIContainer; + +pub trait IInjectableTypeProvider +{ + fn provide( + &self, + di_container: &DIContainer, + ) -> error_stack::Result<Box<dyn Injectable>, ResolveError>; +} + +pub struct InjectableTypeProvider<InjectableType> +where + InjectableType: Injectable, +{ + _phantom_data: PhantomData<InjectableType>, +} + +impl<InjectableType> InjectableTypeProvider<InjectableType> +where + InjectableType: Injectable, +{ + pub fn new() -> Self + { + Self { + _phantom_data: PhantomData, + } + } +} + +impl<InjectableType> IInjectableTypeProvider for InjectableTypeProvider<InjectableType> +where + InjectableType: Injectable, +{ + fn provide( + &self, + di_container: &DIContainer, + ) -> error_stack::Result<Box<dyn Injectable>, ResolveError> + { + Ok(InjectableType::resolve(di_container)?) + } +} |