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: &DependencyMap) -> bool
pub fn same_types(&self, other: &DependencyMap) -> 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() -> DependencyMap
Sourcepub fn insert<T>(&mut self, item: T) -> Option<Arc<T>>
pub fn insert<T>(&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: DependencyMap)
pub fn insert_container(&mut self, container: DependencyMap)
Inserts all dependencies from another container into itself.
Sourcepub fn remove<T>(&mut self) -> Option<Arc<T>>
pub fn remove<T>(&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.
Sourcepub fn try_get<V>(&self) -> Option<Arc<V>>
pub fn try_get<V>(&self) -> Option<Arc<V>>
Tries to get a value from the container; does not panic.
Examples found in repository?
20async fn report_error(error: teloxide::RequestError, deps: DependencyMap) {
21 log::error!("Could not handle update: {error}");
22 let (Some(bot), Some(update)) = (deps.try_get::<Bot>(), deps.try_get::<Update>()) else {
23 return;
24 };
25 if let Some(chat) = update.chat() {
26 bot.send_message("Something went wrong. Please try again later.")
27 .chat_id(chat.id)
28 .await
29 .log_on_error()
30 .await;
31 }
32}Trait Implementations§
Source§impl Clone for DependencyMap
impl Clone for DependencyMap
Source§fn clone(&self) -> DependencyMap
fn clone(&self) -> DependencyMap
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for DependencyMap
impl Debug for DependencyMap
Source§impl Default for DependencyMap
impl Default for DependencyMap
Source§fn default() -> DependencyMap
fn default() -> DependencyMap
Auto Trait Implementations§
impl !RefUnwindSafe for DependencyMap
impl !UnwindSafe for DependencyMap
impl Forget for DependencyMap
impl Freeze for DependencyMapwhere
*const LeafNode<TypeId, Dependency> is !null: Freeze,
PhantomData<(Owned, LeafOrInternal)>: Freeze,
PhantomData<Box<(TypeId, Dependency)>>: Freeze,
impl Move for DependencyMap
impl Send for DependencyMap
impl Sync for DependencyMap
impl Unpin for DependencyMap
impl UnsafeUnpin for DependencyMapwhere
*const LeafNode<TypeId, Dependency> is !null: UnsafeUnpin,
PhantomData<(Owned, LeafOrInternal)>: UnsafeUnpin,
PhantomData<Box<(TypeId, Dependency)>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Erasable for T
impl<T> Erasable for T
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more