From b54dee1fb52f259de8b485d050d75c6956750b7f Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 3 Aug 2022 14:13:55 +0200 Subject: feat!: prevent binding the same interface more than once BREAKING CHANGE: The 'to' and 'to_factory' methods of BindingBuilder now return 'Result' --- src/di_container_binding_map.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/di_container_binding_map.rs') diff --git a/src/di_container_binding_map.rs b/src/di_container_binding_map.rs index b505321..fee33b0 100644 --- a/src/di_container_binding_map.rs +++ b/src/di_container_binding_map.rs @@ -30,23 +30,29 @@ impl DIContainerBindingMap .get(&interface_typeid) .ok_or_else(|| { report!(DIContainerError).attach_printable(format!( - "No binding exists for {}", + "No binding exists for interface '{}'", type_name::() )) })? .as_ref()) } - pub fn set(&mut self, provider: Box) + pub fn set(&mut self, provider: Box) -> Option<()> where Interface: 'static + ?Sized, { let interface_typeid = TypeId::of::(); + if self.bindings.contains_key(&interface_typeid) { + return None; + } + self.bindings.insert(interface_typeid, provider); + + Some(()) } - /// Only used by tests in the ``di_container`` module. + /// Only used by tests in the `di_container` module. #[cfg(test)] pub fn count(&self) -> usize { -- cgit v1.2.3-18-g5258