aboutsummaryrefslogtreecommitdiff
path: root/src/di_container_binding_map.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_binding_map.rs
parent6e31d8f9e46fece348f329763b39b9c6f2741c07 (diff)
feat: implement async functionality
Diffstat (limited to 'src/di_container_binding_map.rs')
-rw-r--r--src/di_container_binding_map.rs38
1 files changed, 14 insertions, 24 deletions
diff --git a/src/di_container_binding_map.rs b/src/di_container_binding_map.rs
index 4df889d..4aa246e 100644
--- a/src/di_container_binding_map.rs
+++ b/src/di_container_binding_map.rs
@@ -1,10 +1,7 @@
-use std::any::{type_name, TypeId};
+use std::any::TypeId;
use ahash::AHashMap;
-use crate::errors::di_container::DIContainerError;
-use crate::provider::IProvider;
-
#[derive(Debug, PartialEq, Eq, Hash)]
struct DIContainerBindingKey
{
@@ -12,12 +9,16 @@ struct DIContainerBindingKey
name: Option<&'static str>,
}
-pub struct DIContainerBindingMap
+pub struct DIContainerBindingMap<Provider>
+where
+ Provider: 'static + ?Sized,
{
- bindings: AHashMap<DIContainerBindingKey, Box<dyn IProvider>>,
+ bindings: AHashMap<DIContainerBindingKey, Box<Provider>>,
}
-impl DIContainerBindingMap
+impl<Provider> DIContainerBindingMap<Provider>
+where
+ Provider: 'static + ?Sized,
{
pub fn new() -> Self
{
@@ -26,33 +27,22 @@ impl DIContainerBindingMap
}
}
- pub fn get<Interface>(
- &self,
- name: Option<&'static str>,
- ) -> Result<&dyn IProvider, DIContainerError>
+ pub fn get<Interface>(&self, name: Option<&'static str>) -> Option<&Provider>
where
Interface: 'static + ?Sized,
{
let interface_typeid = TypeId::of::<Interface>();
- Ok(self
- .bindings
+ self.bindings
.get(&DIContainerBindingKey {
type_id: interface_typeid,
name,
})
- .ok_or_else(|| DIContainerError::BindingNotFound {
- interface: type_name::<Interface>(),
- name,
- })?
- .as_ref())
+ .map(|provider| provider.as_ref())
}
- pub fn set<Interface>(
- &mut self,
- name: Option<&'static str>,
- provider: Box<dyn IProvider>,
- ) where
+ pub fn set<Interface>(&mut self, name: Option<&'static str>, provider: Box<Provider>)
+ where
Interface: 'static + ?Sized,
{
let interface_typeid = TypeId::of::<Interface>();
@@ -69,7 +59,7 @@ impl DIContainerBindingMap
pub fn remove<Interface>(
&mut self,
name: Option<&'static str>,
- ) -> Option<Box<dyn IProvider>>
+ ) -> Option<Box<Provider>>
where
Interface: 'static + ?Sized,
{