use std::any::{type_name, TypeId}; use ahash::AHashMap; use error_stack::report; 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) -> error_stack::Result<&dyn IProvider, DIContainerError> where Interface: 'static + ?Sized, { let interface_typeid = TypeId::of::(); Ok(self .bindings .get(&interface_typeid) .ok_or_else(|| { report!(DIContainerError).attach_printable(format!( "No binding exists for {}", 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); } /// Only used by tests in the ``di_container`` module. #[cfg(test)] pub fn count(&self) -> usize { self.bindings.len() } }