From 7bed48c852a741df5a14359916faf21d90d39814 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 31 Aug 2023 19:19:06 +0200 Subject: refactor: pass around BindingOptions instead of name --- src/di_container/binding_storage.rs | 170 +++++++++++++++--------------------- 1 file changed, 72 insertions(+), 98 deletions(-) (limited to 'src/di_container/binding_storage.rs') diff --git a/src/di_container/binding_storage.rs b/src/di_container/binding_storage.rs index 3c3c565..2451791 100644 --- a/src/di_container/binding_storage.rs +++ b/src/di_container/binding_storage.rs @@ -2,11 +2,13 @@ use std::any::TypeId; use ahash::AHashMap; +use crate::di_container::BindingOptions; + pub struct DIContainerBindingStorage where Provider: 'static + ?Sized, { - inner: AHashMap, Box>, + inner: AHashMap, Box>, } impl DIContainerBindingStorage @@ -21,69 +23,64 @@ where } #[allow(clippy::borrowed_box)] - pub fn get<'me, Interface>( - &'me self, - name: Option<&'me str>, - ) -> Option<&'me Box> + pub fn get<'this, Interface>( + &'this self, + options: BindingOptions<'this>, + ) -> Option<&'this Box> where Interface: 'static + ?Sized, { - let interface_typeid = TypeId::of::(); - - self.inner.get(&BindingIdentification { - type_id: interface_typeid, - name, - }) + self.inner.get(&BindingId::new::(options)) } - pub fn set(&mut self, name: Option<&'static str>, provider: Box) - where + pub fn set( + &mut self, + options: BindingOptions<'static>, + provider: Box, + ) where Interface: 'static + ?Sized, { - let interface_typeid = TypeId::of::(); - - self.inner.insert( - BindingIdentification { - type_id: interface_typeid, - name, - }, - provider, - ); + self.inner + .insert(BindingId::new::(options), provider); } pub fn remove( &mut self, - name: Option<&'static str>, + options: BindingOptions<'static>, ) -> Option> where Interface: 'static + ?Sized, { - let interface_typeid = TypeId::of::(); - - self.inner.remove(&BindingIdentification { - type_id: interface_typeid, - name, - }) + self.inner.remove(&BindingId::new::(options)) } - pub fn has(&self, name: Option<&'static str>) -> bool + pub fn has(&self, options: BindingOptions) -> bool where Interface: 'static + ?Sized, { - let interface_typeid = TypeId::of::(); - - self.inner.contains_key(&BindingIdentification { - type_id: interface_typeid, - name, - }) + self.inner + .contains_key(&BindingId::new::(options)) } } #[derive(Debug, PartialEq, Eq, Hash)] -struct BindingIdentification<'a> +struct BindingId<'opts> { type_id: TypeId, - name: Option<&'a str>, + options: BindingOptions<'opts>, +} + +impl<'opts> BindingId<'opts> +{ + fn new(options: BindingOptions<'opts>) -> Self + where + Interface: ?Sized + 'static, + { + Self { + type_id: TypeId::of::(), + options, + } + } } #[cfg(test)] @@ -121,15 +118,12 @@ mod tests DIContainerBindingStorage::::new(); binding_map.inner.insert( - BindingIdentification { - type_id: TypeId::of::(), - name: None, - }, + BindingId::new::(BindingOptions::new()), Box::new(subjects::SomeProviderImpl { id: 20 }), ); assert!(binding_map - .get::(None) + .get::(BindingOptions::new()) .map_or_else(|| false, |provider| provider.get_id() == 20)); } @@ -142,18 +136,17 @@ mod tests DIContainerBindingStorage::::new(); binding_map.inner.insert( - BindingIdentification { - type_id: TypeId::of::(), - name: Some("hello"), - }, + BindingId::new::(BindingOptions::new().name("hello")), Box::new(subjects::SomeProviderImpl { id: 11 }), ); assert!(binding_map - .get::(Some("hello")) + .get::(BindingOptions::new().name("hello")) .map_or_else(|| false, |provider| provider.get_id() == 11)); - assert!(binding_map.get::(None).is_none()); + assert!(binding_map + .get::(BindingOptions::new()) + .is_none()); } #[test] @@ -164,17 +157,16 @@ mod tests let mut binding_map = DIContainerBindingStorage::::new(); - binding_map - .set::(None, Box::new(subjects::SomeProviderImpl { id: 65 })); + binding_map.set::( + BindingOptions::new(), + Box::new(subjects::SomeProviderImpl { id: 65 }), + ); - let expected_key = &BindingIdentification { - type_id: TypeId::of::(), - name: None, - }; + let expected_key = BindingId::new::(BindingOptions::new()); - assert!(binding_map.inner.contains_key(expected_key)); + assert!(binding_map.inner.contains_key(&expected_key)); - assert_eq!(binding_map.inner[expected_key].get_id(), 65); + assert_eq!(binding_map.inner[&expected_key].get_id(), 65); } #[test] @@ -186,18 +178,16 @@ mod tests DIContainerBindingStorage::::new(); binding_map.set::( - Some("special"), + BindingOptions::new().name("special"), Box::new(subjects::SomeProviderImpl { id: 3 }), ); - let expected_key = &BindingIdentification { - type_id: TypeId::of::(), - name: Some("special"), - }; + let expected_key = + BindingId::new::(BindingOptions::new().name("special")); - assert!(binding_map.inner.contains_key(expected_key)); + assert!(binding_map.inner.contains_key(&expected_key)); - assert_eq!(binding_map.inner[expected_key].get_id(), 3); + assert_eq!(binding_map.inner[&expected_key].get_id(), 3); } #[test] @@ -209,21 +199,15 @@ mod tests DIContainerBindingStorage::::new(); binding_map.inner.insert( - BindingIdentification { - type_id: TypeId::of::(), - name: None, - }, + BindingId::new::(BindingOptions::new()), Box::new(subjects::SomeProviderImpl { id: 103 }), ); - binding_map.remove::(None); - - let expected_key = &BindingIdentification { - type_id: TypeId::of::(), - name: None, - }; + binding_map.remove::(BindingOptions::new()); - assert!(!binding_map.inner.contains_key(expected_key)); + assert!(!binding_map + .inner + .contains_key(&BindingId::new::(BindingOptions::new()))); } #[test] @@ -235,21 +219,17 @@ mod tests DIContainerBindingStorage::::new(); binding_map.inner.insert( - BindingIdentification { - type_id: TypeId::of::(), - name: Some("cool"), - }, + BindingId::new::(BindingOptions::new().name("cool")), Box::new(subjects::SomeProviderImpl { id: 42 }), ); - binding_map.remove::(Some("cool")); + binding_map.remove::(BindingOptions::new().name("cool")); - let expected_key = &BindingIdentification { - type_id: TypeId::of::(), - name: Some("cool"), - }; - - assert!(!binding_map.inner.contains_key(expected_key)); + assert!( + !binding_map.inner.contains_key(&BindingId::new::( + BindingOptions::new().name("cool") + )) + ); } #[test] @@ -260,17 +240,14 @@ mod tests let mut binding_map = DIContainerBindingStorage::::new(); - assert!(!binding_map.has::(None)); + assert!(!binding_map.has::(BindingOptions::new())); binding_map.inner.insert( - BindingIdentification { - type_id: TypeId::of::(), - name: None, - }, + BindingId::new::(BindingOptions::new()), Box::new(subjects::SomeProviderImpl { id: 103 }), ); - assert!(binding_map.has::(None)); + assert!(binding_map.has::(BindingOptions::new())); } #[test] @@ -281,16 +258,13 @@ mod tests let mut binding_map = DIContainerBindingStorage::::new(); - assert!(!binding_map.has::(Some("awesome"))); + assert!(!binding_map.has::(BindingOptions::new().name("awesome"))); binding_map.inner.insert( - BindingIdentification { - type_id: TypeId::of::(), - name: Some("awesome"), - }, + BindingId::new::(BindingOptions::new().name("awesome")), Box::new(subjects::SomeProviderImpl { id: 101 }), ); - assert!(binding_map.has::(Some("awesome"))); + assert!(binding_map.has::(BindingOptions::new().name("awesome"))); } } -- cgit v1.2.3-18-g5258