aboutsummaryrefslogtreecommitdiff
path: root/src/di_container_binding_map.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-27 23:41:41 +0200
committerHampusM <hampus@hampusmat.com>2022-08-27 23:41:41 +0200
commite0f90a8e384615c79d7d51c66d19294d75e79391 (patch)
treef3df3d1cd92f7d4a978feaa5a9a5f773dd0901ee /src/di_container_binding_map.rs
parentd4078c84a83d121a4e3492955359cedb3b404476 (diff)
feat: implement named bindings
Diffstat (limited to 'src/di_container_binding_map.rs')
-rw-r--r--src/di_container_binding_map.rs59
1 files changed, 51 insertions, 8 deletions
diff --git a/src/di_container_binding_map.rs b/src/di_container_binding_map.rs
index 20d040f..d4b46f2 100644
--- a/src/di_container_binding_map.rs
+++ b/src/di_container_binding_map.rs
@@ -4,9 +4,16 @@ use ahash::AHashMap;
use crate::{errors::di_container::DIContainerError, provider::IProvider};
+#[derive(Debug, PartialEq, Eq, Hash)]
+struct DIContainerBindingKey
+{
+ type_id: TypeId,
+ name: Option<&'static str>,
+}
+
pub struct DIContainerBindingMap
{
- bindings: AHashMap<TypeId, Box<dyn IProvider>>,
+ bindings: AHashMap<DIContainerBindingKey, Box<dyn IProvider>>,
}
impl DIContainerBindingMap
@@ -18,7 +25,10 @@ impl DIContainerBindingMap
}
}
- pub fn get<Interface>(&self) -> Result<&dyn IProvider, DIContainerError>
+ pub fn get<Interface>(
+ &self,
+ name: Option<&'static str>,
+ ) -> Result<&dyn IProvider, DIContainerError>
where
Interface: 'static + ?Sized,
{
@@ -26,27 +36,60 @@ impl DIContainerBindingMap
Ok(self
.bindings
- .get(&interface_typeid)
- .ok_or_else(|| DIContainerError::BindingNotFound(type_name::<Interface>()))?
+ .get(&DIContainerBindingKey {
+ type_id: interface_typeid,
+ name,
+ })
+ .ok_or_else(|| DIContainerError::BindingNotFound {
+ interface: type_name::<Interface>(),
+ name,
+ })?
.as_ref())
}
- pub fn set<Interface>(&mut self, provider: Box<dyn IProvider>)
+ pub fn set<Interface>(
+ &mut self,
+ name: Option<&'static str>,
+ provider: Box<dyn IProvider>,
+ ) where
+ Interface: 'static + ?Sized,
+ {
+ let interface_typeid = TypeId::of::<Interface>();
+
+ self.bindings.insert(
+ DIContainerBindingKey {
+ type_id: interface_typeid,
+ name,
+ },
+ provider,
+ );
+ }
+
+ pub fn remove<Interface>(
+ &mut self,
+ name: Option<&'static str>,
+ ) -> Option<Box<dyn IProvider>>
where
Interface: 'static + ?Sized,
{
let interface_typeid = TypeId::of::<Interface>();
- self.bindings.insert(interface_typeid, provider);
+ self.bindings.remove(&DIContainerBindingKey {
+ type_id: interface_typeid,
+ name,
+ })
}
- pub fn has<Interface>(&self) -> bool
+ pub fn has<Interface>(&self, name: Option<&'static str>) -> bool
where
Interface: 'static + ?Sized,
{
let interface_typeid = TypeId::of::<Interface>();
- self.bindings.contains_key(&interface_typeid)
+ self.bindings.contains_key(&DIContainerBindingKey {
+ type_id: interface_typeid,
+ name,
+ })
}
/// Only used by tests in the `di_container` module.