Struct dptree::di::DependencyMap
source · [−]pub struct DependencyMap { /* private fields */ }
Expand description
A DI container with multiple dependencies.
This DI container stores types by their corresponding type identifiers. It cannot prove at compile-time that a type of a requested value exists within the container, so if you do not provide necessary types but they were requested, it will panic.
Examples
use dptree::di::{DependencyMap, DependencySupplier};
let mut container = DependencyMap::new();
container.insert(5_i32);
container.insert("abc");
assert_eq!(container.get(), Arc::new(5_i32));
assert_eq!(container.get(), Arc::new("abc"));
// If a type of a value already exists within the container, it will be replaced.
let old_value = container.insert(10_i32).unwrap();
assert_eq!(old_value, Arc::new(5_i32));
assert_eq!(container.get(), Arc::new(10_i32));
When a value is not found within the container, it will panic:
use dptree::di::{DependencyMap, DependencySupplier};
let mut container = DependencyMap::new();
container.insert(10i32);
container.insert(true);
container.insert("static str");
// thread 'main' panicked at 'alloc::string::String was requested, but not provided. Available types:
// &str
// bool
// i32
// ', /media/hirrolot/772CF8924BEBB279/Documents/Rust/dptree/src/di.rs:150:17
// note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
let string: Arc<String> = container.get();
Implementations
sourceimpl DependencyMap
impl DependencyMap
pub fn new() -> Self
sourcepub fn insert<T: Send + Sync + 'static>(&mut self, item: T) -> Option<Arc<T>>
pub fn insert<T: Send + Sync + 'static>(&mut self, item: T) -> Option<Arc<T>>
Inserts a value into the container.
If the container do not has this type present, None
is returned.
Otherwise, the value is updated, and the old value is returned.
sourcepub fn insert_container(&mut self, container: Self)
pub fn insert_container(&mut self, container: Self)
Inserts all dependencies from another container into itself.
Trait Implementations
sourceimpl Clone for DependencyMap
impl Clone for DependencyMap
sourcefn clone(&self) -> DependencyMap
fn clone(&self) -> DependencyMap
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for DependencyMap
impl Debug for DependencyMap
sourceimpl Default for DependencyMap
impl Default for DependencyMap
sourcefn default() -> DependencyMap
fn default() -> DependencyMap
Returns the “default value” for a type. Read more
sourceimpl<V> DependencySupplier<V> for DependencyMap where
V: Send + Sync + 'static,
impl<V> DependencySupplier<V> for DependencyMap where
V: Send + Sync + 'static,
sourceimpl<T: Send + Sync + 'static> Insert<T> for DependencyMap
impl<T: Send + Sync + 'static> Insert<T> for DependencyMap
Auto Trait Implementations
impl !RefUnwindSafe for DependencyMap
impl Send for DependencyMap
impl Sync for DependencyMap
impl Unpin for DependencyMap
impl !UnwindSafe for DependencyMap
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more