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. However, you can use crate::type_check to make
sure that all types are provided before executing a particular handler.
Lookup uses the exact type inserted. If your handler takes
Arc<dyn MyTrait + Send + Sync>, coerce the pointer to that type before
inserting it. Inserting an Arc<Concrete> instead registers Arc<Concrete>,
even if Concrete implements MyTrait.
use dptree::di::DependencyMap;
use std::sync::Arc;
trait MyTrait {}
impl MyTrait for u16 {}
let data: Arc<dyn MyTrait + Send + Sync> = Arc::new(42_u16);
let mut container = DependencyMap::new();
container.insert(data);
assert!(container.try_get::<Arc<dyn MyTrait + Send + Sync>>().is_some());
assert!(container.try_get::<Arc<u16>>().is_none());§Examples
use dptree::di::DependencyMap;
use std::sync::Arc;
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));Dependency values must implement Send + Sync + 'static on all targets,
including wasm32. A handler can capture an Rc on wasm32, but cannot
store it in the dependency map or inject it as a handler argument.
use dptree::di::DependencyMap;
use std::rc::Rc;
DependencyMap::new().insert(Rc::new(42));Maps do not implement PartialEq: the erased dependency values cannot
generally be compared. Use Self::same_types to compare their type sets.
use dptree::deps;
assert_eq!(deps![false], deps![true]);When a value is not found within the container, it will panic:
use dptree::di::DependencyMap;
use std::sync::Arc;
let mut container = DependencyMap::new();
container.insert(10i32);
container.insert(true);
container.insert("static str");
// `alloc::string::String` was requested, but not provided. Available types:
// `i32`
// `&str`
// `bool`
let string: Arc<String> = container.get();Implementations§
Source§impl DependencyMap
impl DependencyMap
Sourcepub fn same_types(&self, other: &Self) -> bool
pub fn same_types(&self, other: &Self) -> bool
Returns whether both maps contain exactly the same dependency types.
Values are not compared. Use Self::get or Self::try_get to
compare individual values using their own equality implementations.
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.
Sourcepub fn remove<T: Send + Sync + 'static>(&mut self) -> Option<Arc<T>>
pub fn remove<T: Send + Sync + 'static>(&mut self) -> Option<Arc<T>>
Removes a value from the container.
If the container do not has this type present, None is returned.
Otherwise, the value is removed and returned.