use std::any::TypeId; use ahash::AHashMap; #[derive(Debug, PartialEq, Eq, Hash)] struct DIContainerBindingKey { type_id: TypeId, name: Option<&'static str>, } pub struct DIContainerBindingMap where Provider: 'static + ?Sized, { bindings: AHashMap>, } impl DIContainerBindingMap where Provider: 'static + ?Sized, { pub fn new() -> Self { Self { bindings: AHashMap::new(), } } pub fn get(&self, name: Option<&'static str>) -> Option<&Provider> where Interface: 'static + ?Sized, { let interface_typeid = TypeId::of::(); self.bindings .get(&DIContainerBindingKey { type_id: interface_typeid, name, }) .map(|provider| provider.as_ref()) } pub fn set(&mut self, name: Option<&'static str>, provider: Box) where Interface: 'static + ?Sized, { let interface_typeid = TypeId::of::(); self.bindings.insert( DIContainerBindingKey { type_id: interface_typeid, name, }, provider, ); } pub fn remove( &mut self, name: Option<&'static str>, ) -> Option> where Interface: 'static + ?Sized, { let interface_typeid = TypeId::of::(); self.bindings.remove(&DIContainerBindingKey { type_id: interface_typeid, name, }) } pub fn has(&self, name: Option<&'static str>) -> bool where Interface: 'static + ?Sized, { let interface_typeid = TypeId::of::(); self.bindings.contains_key(&DIContainerBindingKey { type_id: interface_typeid, name, }) } /// Only used by tests in the `di_container` module. #[cfg(test)] pub fn count(&self) -> usize { self.bindings.len() } }