aboutsummaryrefslogtreecommitdiff
path: root/src/provider.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-07-27 18:05:34 +0200
committerHampusM <hampus@hampusmat.com>2022-07-31 12:17:51 +0200
commit3388f857b32cf1893d7b54582c8fd16e4965550b (patch)
tree2d78d6ca563b071cc2d6e4b3e80c05d93e575737 /src/provider.rs
parent3fbf26181f1b4b9e594debb103fd347bd93240ea (diff)
feat: implement binding singletons
Diffstat (limited to 'src/provider.rs')
-rw-r--r--src/provider.rs43
1 files changed, 37 insertions, 6 deletions
diff --git a/src/provider.rs b/src/provider.rs
index eb5b5d8..2e832f8 100644
--- a/src/provider.rs
+++ b/src/provider.rs
@@ -4,14 +4,15 @@ use std::marker::PhantomData;
use crate::errors::injectable::ResolveError;
use crate::interfaces::any_factory::AnyFactory;
use crate::interfaces::injectable::Injectable;
-use crate::ptr::{FactoryPtr, TransientPtr};
+use crate::ptr::{FactoryPtr, SingletonPtr, TransientPtr};
use crate::DIContainer;
extern crate error_stack;
pub enum Providable
{
- Injectable(TransientPtr<dyn Injectable>),
+ Transient(TransientPtr<dyn Injectable>),
+ Singleton(SingletonPtr<dyn Injectable>),
#[allow(dead_code)]
Factory(FactoryPtr<dyn AnyFactory>),
}
@@ -24,14 +25,14 @@ pub trait IProvider
) -> error_stack::Result<Providable, ResolveError>;
}
-pub struct InjectableTypeProvider<InjectableType>
+pub struct TransientTypeProvider<InjectableType>
where
InjectableType: Injectable,
{
injectable_phantom: PhantomData<InjectableType>,
}
-impl<InjectableType> InjectableTypeProvider<InjectableType>
+impl<InjectableType> TransientTypeProvider<InjectableType>
where
InjectableType: Injectable,
{
@@ -43,7 +44,7 @@ where
}
}
-impl<InjectableType> IProvider for InjectableTypeProvider<InjectableType>
+impl<InjectableType> IProvider for TransientTypeProvider<InjectableType>
where
InjectableType: Injectable,
{
@@ -52,12 +53,42 @@ where
di_container: &DIContainer,
) -> error_stack::Result<Providable, ResolveError>
{
- Ok(Providable::Injectable(InjectableType::resolve(
+ Ok(Providable::Transient(InjectableType::resolve(
di_container,
)?))
}
}
+pub struct SingletonProvider<InjectableType>
+where
+ InjectableType: Injectable,
+{
+ singleton: SingletonPtr<InjectableType>,
+}
+
+impl<InjectableType> SingletonProvider<InjectableType>
+where
+ InjectableType: Injectable,
+{
+ pub fn new(singleton: SingletonPtr<InjectableType>) -> Self
+ {
+ Self { singleton }
+ }
+}
+
+impl<InjectableType> IProvider for SingletonProvider<InjectableType>
+where
+ InjectableType: Injectable,
+{
+ fn provide(
+ &self,
+ _di_container: &DIContainer,
+ ) -> error_stack::Result<Providable, ResolveError>
+ {
+ Ok(Providable::Singleton(self.singleton.clone()))
+ }
+}
+
#[cfg(feature = "factory")]
pub struct FactoryProvider
{