aboutsummaryrefslogtreecommitdiff
path: root/src/di_container
diff options
context:
space:
mode:
Diffstat (limited to 'src/di_container')
-rw-r--r--src/di_container/asynchronous/mod.rs29
-rw-r--r--src/di_container/binding_storage.rs (renamed from src/di_container/binding_map.rs)100
-rw-r--r--src/di_container/blocking/mod.rs28
-rw-r--r--src/di_container/mod.rs3
4 files changed, 87 insertions, 73 deletions
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<DIContainerBindingMap<dyn IAsyncProvider<Self>>>,
+ binding_storage: Mutex<DIContainerBindingStorage<dyn IAsyncProvider<Self>>>,
}
impl AsyncDIContainer
@@ -134,7 +134,7 @@ impl AsyncDIContainer
pub fn new() -> Arc<Self>
{
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::<Interface>(name)
+ self.binding_storage.lock().await.has::<Interface>(name)
}
async fn set_binding<Interface>(
@@ -206,7 +206,10 @@ impl details::DIContainerInternals for AsyncDIContainer
) where
Interface: 'static + ?Sized,
{
- self.bindings.lock().await.set::<Interface>(name, provider);
+ self.binding_storage
+ .lock()
+ .await
+ .set::<Interface>(name, provider);
}
async fn remove_binding<Interface>(
@@ -216,7 +219,7 @@ impl details::DIContainerInternals for AsyncDIContainer
where
Interface: 'static + ?Sized,
{
- self.bindings.lock().await.remove::<Interface>(name)
+ self.binding_storage.lock().await.remove::<Interface>(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::<Interface>(name)
@@ -459,7 +462,7 @@ mod tests
{
di_container
- .bindings
+ .binding_storage
.lock()
.await
.set::<dyn subjects_async::IUserManager>(None, Box::new(mock_provider));
@@ -510,7 +513,7 @@ mod tests
{
di_container
- .bindings
+ .binding_storage
.lock()
.await
.set::<dyn subjects_async::IUserManager>(
@@ -568,7 +571,7 @@ mod tests
{
di_container
- .bindings
+ .binding_storage
.lock()
.await
.set::<dyn subjects_async::INumber>(None, Box::new(mock_provider));
@@ -632,7 +635,7 @@ mod tests
{
di_container
- .bindings
+ .binding_storage
.lock()
.await
.set::<dyn subjects_async::INumber>(
@@ -755,7 +758,7 @@ mod tests
{
di_container
- .bindings
+ .binding_storage
.lock()
.await
.set::<IUserManagerFactory>(None, Box::new(mock_provider));
@@ -866,7 +869,7 @@ mod tests
{
di_container
- .bindings
+ .binding_storage
.lock()
.await
.set::<IUserManagerFactory>(Some("special"), Box::new(mock_provider));
diff --git a/src/di_container/binding_map.rs b/src/di_container/binding_storage.rs
index 3a73f7a..2bc208f 100644
--- a/src/di_container/binding_map.rs
+++ b/src/di_container/binding_storage.rs
@@ -2,28 +2,21 @@ use std::any::TypeId;
use ahash::AHashMap;
-#[derive(Debug, PartialEq, Eq, Hash)]
-struct DIContainerBindingKey
-{
- type_id: TypeId,
- name: Option<&'static str>,
-}
-
-pub struct DIContainerBindingMap<Provider>
+pub struct DIContainerBindingStorage<Provider>
where
Provider: 'static + ?Sized,
{
- bindings: AHashMap<DIContainerBindingKey, Box<Provider>>,
+ inner: AHashMap<BindingIdentification, Box<Provider>>,
}
-impl<Provider> DIContainerBindingMap<Provider>
+impl<Provider> DIContainerBindingStorage<Provider>
where
Provider: 'static + ?Sized,
{
pub fn new() -> Self
{
Self {
- bindings: AHashMap::new(),
+ inner: AHashMap::new(),
}
}
@@ -34,7 +27,7 @@ where
{
let interface_typeid = TypeId::of::<Interface>();
- self.bindings.get(&DIContainerBindingKey {
+ self.inner.get(&BindingIdentification {
type_id: interface_typeid,
name,
})
@@ -46,8 +39,8 @@ where
{
let interface_typeid = TypeId::of::<Interface>();
- self.bindings.insert(
- DIContainerBindingKey {
+ self.inner.insert(
+ BindingIdentification {
type_id: interface_typeid,
name,
},
@@ -64,7 +57,7 @@ where
{
let interface_typeid = TypeId::of::<Interface>();
- self.bindings.remove(&DIContainerBindingKey {
+ self.inner.remove(&BindingIdentification {
type_id: interface_typeid,
name,
})
@@ -76,13 +69,20 @@ where
{
let interface_typeid = TypeId::of::<Interface>();
- self.bindings.contains_key(&DIContainerBindingKey {
+ 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
{
@@ -114,10 +114,11 @@ mod tests
{
type Interface = ();
- let mut binding_map = DIContainerBindingMap::<dyn subjects::SomeProvider>::new();
+ let mut binding_map =
+ DIContainerBindingStorage::<dyn subjects::SomeProvider>::new();
- binding_map.bindings.insert(
- DIContainerBindingKey {
+ binding_map.inner.insert(
+ BindingIdentification {
type_id: TypeId::of::<Interface>(),
name: None,
},
@@ -134,10 +135,11 @@ mod tests
{
type Interface = ();
- let mut binding_map = DIContainerBindingMap::<dyn subjects::SomeProvider>::new();
+ let mut binding_map =
+ DIContainerBindingStorage::<dyn subjects::SomeProvider>::new();
- binding_map.bindings.insert(
- DIContainerBindingKey {
+ binding_map.inner.insert(
+ BindingIdentification {
type_id: TypeId::of::<Interface>(),
name: Some("hello"),
},
@@ -156,19 +158,20 @@ mod tests
{
type Interface = ();
- let mut binding_map = DIContainerBindingMap::<dyn subjects::SomeProvider>::new();
+ let mut binding_map =
+ DIContainerBindingStorage::<dyn subjects::SomeProvider>::new();
binding_map
.set::<Interface>(None, Box::new(subjects::SomeProviderImpl { id: 65 }));
- let expected_key = &DIContainerBindingKey {
+ let expected_key = &BindingIdentification {
type_id: TypeId::of::<Interface>(),
name: None,
};
- assert!(binding_map.bindings.contains_key(expected_key));
+ assert!(binding_map.inner.contains_key(expected_key));
- assert_eq!(binding_map.bindings[expected_key].get_id(), 65);
+ assert_eq!(binding_map.inner[expected_key].get_id(), 65);
}
#[test]
@@ -176,21 +179,22 @@ mod tests
{
type Interface = ();
- let mut binding_map = DIContainerBindingMap::<dyn subjects::SomeProvider>::new();
+ let mut binding_map =
+ DIContainerBindingStorage::<dyn subjects::SomeProvider>::new();
binding_map.set::<Interface>(
Some("special"),
Box::new(subjects::SomeProviderImpl { id: 3 }),
);
- let expected_key = &DIContainerBindingKey {
+ let expected_key = &BindingIdentification {
type_id: TypeId::of::<Interface>(),
name: Some("special"),
};
- assert!(binding_map.bindings.contains_key(expected_key));
+ assert!(binding_map.inner.contains_key(expected_key));
- assert_eq!(binding_map.bindings[expected_key].get_id(), 3);
+ assert_eq!(binding_map.inner[expected_key].get_id(), 3);
}
#[test]
@@ -198,10 +202,11 @@ mod tests
{
type Interface = ();
- let mut binding_map = DIContainerBindingMap::<dyn subjects::SomeProvider>::new();
+ let mut binding_map =
+ DIContainerBindingStorage::<dyn subjects::SomeProvider>::new();
- binding_map.bindings.insert(
- DIContainerBindingKey {
+ binding_map.inner.insert(
+ BindingIdentification {
type_id: TypeId::of::<Interface>(),
name: None,
},
@@ -210,12 +215,12 @@ mod tests
binding_map.remove::<Interface>(None);
- let expected_key = &DIContainerBindingKey {
+ let expected_key = &BindingIdentification {
type_id: TypeId::of::<Interface>(),
name: None,
};
- assert!(!binding_map.bindings.contains_key(expected_key));
+ assert!(!binding_map.inner.contains_key(expected_key));
}
#[test]
@@ -223,10 +228,11 @@ mod tests
{
type Interface = ();
- let mut binding_map = DIContainerBindingMap::<dyn subjects::SomeProvider>::new();
+ let mut binding_map =
+ DIContainerBindingStorage::<dyn subjects::SomeProvider>::new();
- binding_map.bindings.insert(
- DIContainerBindingKey {
+ binding_map.inner.insert(
+ BindingIdentification {
type_id: TypeId::of::<Interface>(),
name: Some("cool"),
},
@@ -235,12 +241,12 @@ mod tests
binding_map.remove::<Interface>(Some("cool"));
- let expected_key = &DIContainerBindingKey {
+ let expected_key = &BindingIdentification {
type_id: TypeId::of::<Interface>(),
name: Some("cool"),
};
- assert!(!binding_map.bindings.contains_key(expected_key));
+ assert!(!binding_map.inner.contains_key(expected_key));
}
#[test]
@@ -248,12 +254,13 @@ mod tests
{
type Interface = ();
- let mut binding_map = DIContainerBindingMap::<dyn subjects::SomeProvider>::new();
+ let mut binding_map =
+ DIContainerBindingStorage::<dyn subjects::SomeProvider>::new();
assert!(!binding_map.has::<Interface>(None));
- binding_map.bindings.insert(
- DIContainerBindingKey {
+ binding_map.inner.insert(
+ BindingIdentification {
type_id: TypeId::of::<Interface>(),
name: None,
},
@@ -268,12 +275,13 @@ mod tests
{
type Interface = ();
- let mut binding_map = DIContainerBindingMap::<dyn subjects::SomeProvider>::new();
+ let mut binding_map =
+ DIContainerBindingStorage::<dyn subjects::SomeProvider>::new();
assert!(!binding_map.has::<Interface>(Some("awesome")));
- binding_map.bindings.insert(
- DIContainerBindingKey {
+ binding_map.inner.insert(
+ BindingIdentification {
type_id: TypeId::of::<Interface>(),
name: 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<DIContainerBindingMap<dyn IProvider<Self>>>,
+ binding_storage: RefCell<DIContainerBindingStorage<dyn IProvider<Self>>>,
}
impl DIContainer
@@ -120,7 +120,7 @@ impl DIContainer
pub fn new() -> Rc<Self>
{
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::<Interface>(name)
+ self.binding_storage.borrow().has::<Interface>(name)
}
fn set_binding<Interface>(
@@ -184,7 +184,9 @@ impl details::DIContainerInternals for DIContainer
) where
Interface: 'static + ?Sized,
{
- self.bindings.borrow_mut().set::<Interface>(name, provider);
+ self.binding_storage
+ .borrow_mut()
+ .set::<Interface>(name, provider);
}
fn remove_binding<Interface>(
@@ -194,7 +196,7 @@ impl details::DIContainerInternals for DIContainer
where
Interface: 'static + ?Sized,
{
- self.bindings.borrow_mut().remove::<Interface>(name)
+ self.binding_storage.borrow_mut().remove::<Interface>(name)
}
}
@@ -265,7 +267,7 @@ impl DIContainer
where
Interface: 'static + ?Sized,
{
- self.bindings
+ self.binding_storage
.borrow()
.get::<Interface>(name)
.map_or_else(
@@ -353,7 +355,7 @@ mod tests
});
di_container
- .bindings
+ .binding_storage
.borrow_mut()
.set::<dyn subjects::IUserManager>(None, Box::new(mock_provider));
@@ -391,7 +393,7 @@ mod tests
});
di_container
- .bindings
+ .binding_storage
.borrow_mut()
.set::<dyn subjects::IUserManager>(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::<dyn subjects::INumber>(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::<dyn subjects::INumber>(Some("cool"), Box::new(mock_provider));
@@ -583,7 +585,7 @@ mod tests
});
di_container
- .bindings
+ .binding_storage
.borrow_mut()
.set::<IUserManagerFactory>(None, Box::new(mock_provider));
@@ -680,7 +682,7 @@ mod tests
});
di_container
- .bindings
+ .binding_storage
.borrow_mut()
.set::<IUserManagerFactory>(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;