From 494d905bcbc50b536de892ecb3fa285dc6b8a727 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 24 Oct 2022 21:53:14 +0200 Subject: refactor: rename DI container binding map to DI container storage --- src/di_container/asynchronous/mod.rs | 29 ++-- src/di_container/binding_map.rs | 285 ---------------------------------- src/di_container/binding_storage.rs | 293 +++++++++++++++++++++++++++++++++++ src/di_container/blocking/mod.rs | 28 ++-- src/di_container/mod.rs | 3 +- 5 files changed, 326 insertions(+), 312 deletions(-) delete mode 100644 src/di_container/binding_map.rs create mode 100644 src/di_container/binding_storage.rs (limited to 'src') diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index 14e6abe..128fbbe 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -58,7 +58,7 @@ use async_trait::async_trait; use tokio::sync::Mutex; use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder; -use crate::di_container::binding_map::DIContainerBindingMap; +use crate::di_container::binding_storage::DIContainerBindingStorage; use crate::errors::async_di_container::AsyncDIContainerError; use crate::future::BoxFuture; use crate::libs::intertrait::cast::error::CastError; @@ -124,7 +124,7 @@ pub trait IAsyncDIContainer: /// Dependency injection container. pub struct AsyncDIContainer { - bindings: Mutex>>, + binding_storage: Mutex>>, } impl AsyncDIContainer @@ -134,7 +134,7 @@ impl AsyncDIContainer pub fn new() -> Arc { Arc::new(Self { - bindings: Mutex::new(DIContainerBindingMap::new()), + binding_storage: Mutex::new(DIContainerBindingStorage::new()), }) } } @@ -196,7 +196,7 @@ impl details::DIContainerInternals for AsyncDIContainer where Interface: ?Sized + 'static, { - self.bindings.lock().await.has::(name) + self.binding_storage.lock().await.has::(name) } async fn set_binding( @@ -206,7 +206,10 @@ impl details::DIContainerInternals for AsyncDIContainer ) where Interface: 'static + ?Sized, { - self.bindings.lock().await.set::(name, provider); + self.binding_storage + .lock() + .await + .set::(name, provider); } async fn remove_binding( @@ -216,7 +219,7 @@ impl details::DIContainerInternals for AsyncDIContainer where Interface: 'static + ?Sized, { - self.bindings.lock().await.remove::(name) + self.binding_storage.lock().await.remove::(name) } } @@ -349,7 +352,7 @@ impl AsyncDIContainer let provider; { - let bindings_lock = self.bindings.lock().await; + let bindings_lock = self.binding_storage.lock().await; provider = bindings_lock .get::(name) @@ -459,7 +462,7 @@ mod tests { di_container - .bindings + .binding_storage .lock() .await .set::(None, Box::new(mock_provider)); @@ -510,7 +513,7 @@ mod tests { di_container - .bindings + .binding_storage .lock() .await .set::( @@ -568,7 +571,7 @@ mod tests { di_container - .bindings + .binding_storage .lock() .await .set::(None, Box::new(mock_provider)); @@ -632,7 +635,7 @@ mod tests { di_container - .bindings + .binding_storage .lock() .await .set::( @@ -755,7 +758,7 @@ mod tests { di_container - .bindings + .binding_storage .lock() .await .set::(None, Box::new(mock_provider)); @@ -866,7 +869,7 @@ mod tests { di_container - .bindings + .binding_storage .lock() .await .set::(Some("special"), Box::new(mock_provider)); diff --git a/src/di_container/binding_map.rs b/src/di_container/binding_map.rs deleted file mode 100644 index 3a73f7a..0000000 --- a/src/di_container/binding_map.rs +++ /dev/null @@ -1,285 +0,0 @@ -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(), - } - } - - #[allow(clippy::borrowed_box)] - pub fn get(&self, name: Option<&'static str>) -> Option<&Box> - where - Interface: 'static + ?Sized, - { - let interface_typeid = TypeId::of::(); - - self.bindings.get(&DIContainerBindingKey { - type_id: interface_typeid, - name, - }) - } - - 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, - }) - } -} - -#[cfg(test)] -mod tests -{ - use super::*; - - mod subjects - { - pub trait SomeProvider - { - fn get_id(&self) -> u8; - } - - pub struct SomeProviderImpl - { - pub id: u8, - } - - impl SomeProvider for SomeProviderImpl - { - fn get_id(&self) -> u8 - { - self.id - } - } - } - - #[test] - fn can_get() - { - type Interface = (); - - let mut binding_map = DIContainerBindingMap::::new(); - - binding_map.bindings.insert( - DIContainerBindingKey { - type_id: TypeId::of::(), - name: None, - }, - Box::new(subjects::SomeProviderImpl { id: 20 }), - ); - - assert!(binding_map - .get::(None) - .map_or_else(|| false, |provider| provider.get_id() == 20)); - } - - #[test] - fn can_get_with_name() - { - type Interface = (); - - let mut binding_map = DIContainerBindingMap::::new(); - - binding_map.bindings.insert( - DIContainerBindingKey { - type_id: TypeId::of::(), - name: Some("hello"), - }, - Box::new(subjects::SomeProviderImpl { id: 11 }), - ); - - assert!(binding_map - .get::(Some("hello")) - .map_or_else(|| false, |provider| provider.get_id() == 11)); - - assert!(binding_map.get::(None).is_none()); - } - - #[test] - fn can_set() - { - type Interface = (); - - let mut binding_map = DIContainerBindingMap::::new(); - - binding_map - .set::(None, Box::new(subjects::SomeProviderImpl { id: 65 })); - - let expected_key = &DIContainerBindingKey { - type_id: TypeId::of::(), - name: None, - }; - - assert!(binding_map.bindings.contains_key(expected_key)); - - assert_eq!(binding_map.bindings[expected_key].get_id(), 65); - } - - #[test] - fn can_set_with_name() - { - type Interface = (); - - let mut binding_map = DIContainerBindingMap::::new(); - - binding_map.set::( - Some("special"), - Box::new(subjects::SomeProviderImpl { id: 3 }), - ); - - let expected_key = &DIContainerBindingKey { - type_id: TypeId::of::(), - name: Some("special"), - }; - - assert!(binding_map.bindings.contains_key(expected_key)); - - assert_eq!(binding_map.bindings[expected_key].get_id(), 3); - } - - #[test] - fn can_remove() - { - type Interface = (); - - let mut binding_map = DIContainerBindingMap::::new(); - - binding_map.bindings.insert( - DIContainerBindingKey { - type_id: TypeId::of::(), - name: None, - }, - Box::new(subjects::SomeProviderImpl { id: 103 }), - ); - - binding_map.remove::(None); - - let expected_key = &DIContainerBindingKey { - type_id: TypeId::of::(), - name: None, - }; - - assert!(!binding_map.bindings.contains_key(expected_key)); - } - - #[test] - fn can_remove_with_name() - { - type Interface = (); - - let mut binding_map = DIContainerBindingMap::::new(); - - binding_map.bindings.insert( - DIContainerBindingKey { - type_id: TypeId::of::(), - name: Some("cool"), - }, - Box::new(subjects::SomeProviderImpl { id: 42 }), - ); - - binding_map.remove::(Some("cool")); - - let expected_key = &DIContainerBindingKey { - type_id: TypeId::of::(), - name: Some("cool"), - }; - - assert!(!binding_map.bindings.contains_key(expected_key)); - } - - #[test] - fn can_get_has() - { - type Interface = (); - - let mut binding_map = DIContainerBindingMap::::new(); - - assert!(!binding_map.has::(None)); - - binding_map.bindings.insert( - DIContainerBindingKey { - type_id: TypeId::of::(), - name: None, - }, - Box::new(subjects::SomeProviderImpl { id: 103 }), - ); - - assert!(binding_map.has::(None)); - } - - #[test] - fn can_get_has_with_name() - { - type Interface = (); - - let mut binding_map = DIContainerBindingMap::::new(); - - assert!(!binding_map.has::(Some("awesome"))); - - binding_map.bindings.insert( - DIContainerBindingKey { - type_id: TypeId::of::(), - name: Some("awesome"), - }, - Box::new(subjects::SomeProviderImpl { id: 101 }), - ); - - assert!(binding_map.has::(Some("awesome"))); - } -} diff --git a/src/di_container/binding_storage.rs b/src/di_container/binding_storage.rs new file mode 100644 index 0000000..2bc208f --- /dev/null +++ b/src/di_container/binding_storage.rs @@ -0,0 +1,293 @@ +use std::any::TypeId; + +use ahash::AHashMap; + +pub struct DIContainerBindingStorage +where + Provider: 'static + ?Sized, +{ + inner: AHashMap>, +} + +impl DIContainerBindingStorage +where + Provider: 'static + ?Sized, +{ + pub fn new() -> Self + { + Self { + inner: AHashMap::new(), + } + } + + #[allow(clippy::borrowed_box)] + pub fn get(&self, name: Option<&'static str>) -> Option<&Box> + where + Interface: 'static + ?Sized, + { + let interface_typeid = TypeId::of::(); + + self.inner.get(&BindingIdentification { + type_id: interface_typeid, + name, + }) + } + + pub fn set(&mut self, name: Option<&'static str>, provider: Box) + where + Interface: 'static + ?Sized, + { + let interface_typeid = TypeId::of::(); + + self.inner.insert( + BindingIdentification { + 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.inner.remove(&BindingIdentification { + type_id: interface_typeid, + name, + }) + } + + pub fn has(&self, name: Option<&'static str>) -> bool + where + Interface: 'static + ?Sized, + { + let interface_typeid = TypeId::of::(); + + self.inner.contains_key(&BindingIdentification { + type_id: interface_typeid, + name, + }) + } +} + +#[derive(Debug, PartialEq, Eq, Hash)] +struct BindingIdentification +{ + type_id: TypeId, + name: Option<&'static str>, +} + +#[cfg(test)] +mod tests +{ + use super::*; + + mod subjects + { + pub trait SomeProvider + { + fn get_id(&self) -> u8; + } + + pub struct SomeProviderImpl + { + pub id: u8, + } + + impl SomeProvider for SomeProviderImpl + { + fn get_id(&self) -> u8 + { + self.id + } + } + } + + #[test] + fn can_get() + { + type Interface = (); + + let mut binding_map = + DIContainerBindingStorage::::new(); + + binding_map.inner.insert( + BindingIdentification { + type_id: TypeId::of::(), + name: None, + }, + Box::new(subjects::SomeProviderImpl { id: 20 }), + ); + + assert!(binding_map + .get::(None) + .map_or_else(|| false, |provider| provider.get_id() == 20)); + } + + #[test] + fn can_get_with_name() + { + type Interface = (); + + let mut binding_map = + DIContainerBindingStorage::::new(); + + binding_map.inner.insert( + BindingIdentification { + type_id: TypeId::of::(), + name: Some("hello"), + }, + Box::new(subjects::SomeProviderImpl { id: 11 }), + ); + + assert!(binding_map + .get::(Some("hello")) + .map_or_else(|| false, |provider| provider.get_id() == 11)); + + assert!(binding_map.get::(None).is_none()); + } + + #[test] + fn can_set() + { + type Interface = (); + + let mut binding_map = + DIContainerBindingStorage::::new(); + + binding_map + .set::(None, Box::new(subjects::SomeProviderImpl { id: 65 })); + + let expected_key = &BindingIdentification { + type_id: TypeId::of::(), + name: None, + }; + + assert!(binding_map.inner.contains_key(expected_key)); + + assert_eq!(binding_map.inner[expected_key].get_id(), 65); + } + + #[test] + fn can_set_with_name() + { + type Interface = (); + + let mut binding_map = + DIContainerBindingStorage::::new(); + + binding_map.set::( + Some("special"), + Box::new(subjects::SomeProviderImpl { id: 3 }), + ); + + let expected_key = &BindingIdentification { + type_id: TypeId::of::(), + name: Some("special"), + }; + + assert!(binding_map.inner.contains_key(expected_key)); + + assert_eq!(binding_map.inner[expected_key].get_id(), 3); + } + + #[test] + fn can_remove() + { + type Interface = (); + + let mut binding_map = + DIContainerBindingStorage::::new(); + + binding_map.inner.insert( + BindingIdentification { + type_id: TypeId::of::(), + name: None, + }, + Box::new(subjects::SomeProviderImpl { id: 103 }), + ); + + binding_map.remove::(None); + + let expected_key = &BindingIdentification { + type_id: TypeId::of::(), + name: None, + }; + + assert!(!binding_map.inner.contains_key(expected_key)); + } + + #[test] + fn can_remove_with_name() + { + type Interface = (); + + let mut binding_map = + DIContainerBindingStorage::::new(); + + binding_map.inner.insert( + BindingIdentification { + type_id: TypeId::of::(), + name: Some("cool"), + }, + Box::new(subjects::SomeProviderImpl { id: 42 }), + ); + + binding_map.remove::(Some("cool")); + + let expected_key = &BindingIdentification { + type_id: TypeId::of::(), + name: Some("cool"), + }; + + assert!(!binding_map.inner.contains_key(expected_key)); + } + + #[test] + fn can_get_has() + { + type Interface = (); + + let mut binding_map = + DIContainerBindingStorage::::new(); + + assert!(!binding_map.has::(None)); + + binding_map.inner.insert( + BindingIdentification { + type_id: TypeId::of::(), + name: None, + }, + Box::new(subjects::SomeProviderImpl { id: 103 }), + ); + + assert!(binding_map.has::(None)); + } + + #[test] + fn can_get_has_with_name() + { + type Interface = (); + + let mut binding_map = + DIContainerBindingStorage::::new(); + + assert!(!binding_map.has::(Some("awesome"))); + + binding_map.inner.insert( + BindingIdentification { + type_id: TypeId::of::(), + name: Some("awesome"), + }, + Box::new(subjects::SomeProviderImpl { id: 101 }), + ); + + assert!(binding_map.has::(Some("awesome"))); + } +} diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index 3b9c16e..bf77aba 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -54,7 +54,7 @@ use std::any::type_name; use std::cell::RefCell; use std::rc::Rc; -use crate::di_container::binding_map::DIContainerBindingMap; +use crate::di_container::binding_storage::DIContainerBindingStorage; use crate::di_container::blocking::binding::builder::BindingBuilder; use crate::errors::di_container::DIContainerError; use crate::libs::intertrait::cast::{CastBox, CastRc}; @@ -110,7 +110,7 @@ pub trait IDIContainer: Sized + 'static + details::DIContainerInternals /// Blocking dependency injection container. pub struct DIContainer { - bindings: RefCell>>, + binding_storage: RefCell>>, } impl DIContainer @@ -120,7 +120,7 @@ impl DIContainer pub fn new() -> Rc { Rc::new(Self { - bindings: RefCell::new(DIContainerBindingMap::new()), + binding_storage: RefCell::new(DIContainerBindingStorage::new()), }) } } @@ -174,7 +174,7 @@ impl details::DIContainerInternals for DIContainer where Interface: ?Sized + 'static, { - self.bindings.borrow().has::(name) + self.binding_storage.borrow().has::(name) } fn set_binding( @@ -184,7 +184,9 @@ impl details::DIContainerInternals for DIContainer ) where Interface: 'static + ?Sized, { - self.bindings.borrow_mut().set::(name, provider); + self.binding_storage + .borrow_mut() + .set::(name, provider); } fn remove_binding( @@ -194,7 +196,7 @@ impl details::DIContainerInternals for DIContainer where Interface: 'static + ?Sized, { - self.bindings.borrow_mut().remove::(name) + self.binding_storage.borrow_mut().remove::(name) } } @@ -265,7 +267,7 @@ impl DIContainer where Interface: 'static + ?Sized, { - self.bindings + self.binding_storage .borrow() .get::(name) .map_or_else( @@ -353,7 +355,7 @@ mod tests }); di_container - .bindings + .binding_storage .borrow_mut() .set::(None, Box::new(mock_provider)); @@ -391,7 +393,7 @@ mod tests }); di_container - .bindings + .binding_storage .borrow_mut() .set::(Some("special"), Box::new(mock_provider)); @@ -431,7 +433,7 @@ mod tests .returning_st(move |_, _| Ok(Providable::Singleton(singleton.clone()))); di_container - .bindings + .binding_storage .borrow_mut() .set::(None, Box::new(mock_provider)); @@ -476,7 +478,7 @@ mod tests .returning_st(move |_, _| Ok(Providable::Singleton(singleton.clone()))); di_container - .bindings + .binding_storage .borrow_mut() .set::(Some("cool"), Box::new(mock_provider)); @@ -583,7 +585,7 @@ mod tests }); di_container - .bindings + .binding_storage .borrow_mut() .set::(None, Box::new(mock_provider)); @@ -680,7 +682,7 @@ mod tests }); di_container - .bindings + .binding_storage .borrow_mut() .set::(Some("special"), Box::new(mock_provider)); diff --git a/src/di_container/mod.rs b/src/di_container/mod.rs index bf2c7a0..5820bc8 100644 --- a/src/di_container/mod.rs +++ b/src/di_container/mod.rs @@ -6,4 +6,5 @@ pub mod asynchronous; pub mod blocking; -pub(crate) mod binding_map; +// Private. +pub(crate) mod binding_storage; -- cgit v1.2.3-18-g5258