use std::any::{type_name, TypeId}; use ahash::AHashMap; use crate::{errors::di_container::DIContainerError, provider::IProvider}; pub struct DIContainerBindingMap { bindings: AHashMap>, } impl DIContainerBindingMap { pub fn new() -> Self { Self { bindings: AHashMap::new(), } } pub fn get(&self) -> Result<&dyn IProvider, DIContainerError> where Interface: 'static + ?Sized, { let interface_typeid = TypeId::of::(); Ok(self .bindings .get(&interface_typeid) .ok_or_else(|| DIContainerError::BindingNotFound(type_name::()))? .as_ref()) } pub fn set(&mut self, provider: Box) where Interface: 'static + ?Sized, { let interface_typeid = TypeId::of::(); self.bindings.insert(interface_typeid, provider); } pub fn has(&self) -> bool where Interface: 'static + ?Sized, { let interface_typeid = TypeId::of::(); self.bindings.contains_key(&interface_typeid) } /// Only used by tests in the `di_container` module. #[cfg(test)] pub fn count(&self) -> usize { self.bindings.len() } }