mydi/
lib.rs

1pub mod component_meta;
2pub mod expander;
3pub mod injection_binder;
4pub mod injector;
5pub mod tags;
6mod tuples;
7
8pub use mydi_macros::Component;
9pub use mydi_macros::ComponentExpander;
10
11pub type Injector = injector::Injector;
12pub type InjectionBinder<T> = injection_binder::InjectionBinder<T>;
13
14pub type Lazy<T> = std::sync::Arc<once_cell::sync::Lazy<T, Box<dyn FnOnce() -> T>>>;
15
16impl<T: Clone + 'static> crate::component_meta::ComponentMeta for Lazy<T> {
17    fn inject(injector: &crate::injector::Injector) -> anyhow::Result<Self> {
18        use std::sync::Arc;
19        let injector = injector.clone();
20        let func: Box<dyn FnOnce() -> T + 'static> = Box::new(move || -> T {
21            match injector.get::<T>() {
22                Ok(x) => x,
23                _ => unreachable!(),
24            }
25        });
26        let result = once_cell::sync::Lazy::new(func);
27        Ok(Arc::new(result))
28    }
29
30    fn debug_line() -> Option<String> {
31        None
32    }
33
34    fn dependencies_names() -> Vec<(std::any::TypeId, &'static str)> {
35        vec![(std::any::TypeId::of::<T>(), std::any::type_name::<T>())]
36    }
37
38    fn lazy() -> bool {
39        true
40    }
41}
42
43#[macro_export]
44macro_rules! erase {
45    ( $pointer:ident < $dyn_type:ty > ) => {{
46        |x| -> $pointer<$dyn_type> { $pointer::new(x) }
47    }};
48}