aboutsummaryrefslogtreecommitdiff
path: root/src/di_container.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-29 20:52:56 +0200
committerHampusM <hampus@hampusmat.com>2022-08-29 21:01:32 +0200
commit080cc42bb1da09059dbc35049a7ded0649961e0c (patch)
tree307ee564124373616022c1ba2b4d5af80845cd92 /src/di_container.rs
parent6e31d8f9e46fece348f329763b39b9c6f2741c07 (diff)
feat: implement async functionality
Diffstat (limited to 'src/di_container.rs')
-rw-r--r--src/di_container.rs30
1 files changed, 22 insertions, 8 deletions
diff --git a/src/di_container.rs b/src/di_container.rs
index e42175b..b0e5af1 100644
--- a/src/di_container.rs
+++ b/src/di_container.rs
@@ -1,4 +1,4 @@
-//! Dependency injection container and other related utilities.
+//! Dependency injection container.
//!
//! # Examples
//! ```
@@ -53,7 +53,7 @@ use std::any::type_name;
use std::marker::PhantomData;
#[cfg(feature = "factory")]
-use crate::castable_factory::CastableFactory;
+use crate::castable_factory::blocking::CastableFactory;
use crate::di_container_binding_map::DIContainerBindingMap;
use crate::errors::di_container::{
BindingBuilderError,
@@ -63,7 +63,12 @@ use crate::errors::di_container::{
};
use crate::interfaces::injectable::Injectable;
use crate::libs::intertrait::cast::{CastBox, CastRc};
-use crate::provider::{Providable, SingletonProvider, TransientTypeProvider};
+use crate::provider::blocking::{
+ IProvider,
+ Providable,
+ SingletonProvider,
+ TransientTypeProvider,
+};
use crate::ptr::{SingletonPtr, SomePtr};
/// When configurator for a binding for type 'Interface' inside a [`DIContainer`].
@@ -256,7 +261,7 @@ where
self.di_container.bindings.set::<Interface>(
None,
- Box::new(crate::provider::FactoryProvider::new(
+ Box::new(crate::provider::blocking::FactoryProvider::new(
crate::ptr::FactoryPtr::new(factory_impl),
)),
);
@@ -290,7 +295,7 @@ where
self.di_container.bindings.set::<Interface>(
None,
- Box::new(crate::provider::FactoryProvider::new(
+ Box::new(crate::provider::blocking::FactoryProvider::new(
crate::ptr::FactoryPtr::new(factory_impl),
)),
);
@@ -302,7 +307,7 @@ where
/// Dependency injection container.
pub struct DIContainer
{
- bindings: DIContainerBindingMap,
+ bindings: DIContainerBindingMap<dyn IProvider>,
}
impl DIContainer
@@ -416,7 +421,16 @@ impl DIContainer
Interface: 'static + ?Sized,
{
self.bindings
- .get::<Interface>(name)?
+ .get::<Interface>(name)
+ .map_or_else(
+ || {
+ Err(DIContainerError::BindingNotFound {
+ interface: type_name::<Interface>(),
+ name,
+ })
+ },
+ Ok,
+ )?
.provide(self, dependency_history)
.map_err(|err| DIContainerError::BindingResolveFailed {
reason: err,
@@ -442,7 +456,7 @@ mod tests
use super::*;
use crate::errors::injectable::InjectableError;
- use crate::provider::IProvider;
+ use crate::provider::blocking::IProvider;
use crate::ptr::TransientPtr;
mod subjects