1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::{InjectResult, Injector, Request, Service, Svc};
pub trait ServiceFactory<D, R>: 'static
where
R: Service,
{
fn invoke(&mut self, injector: &mut Injector) -> InjectResult<Svc<R>>;
}
macro_rules! impl_provider_function {
() => {
impl_provider_function!(@impl ());
};
($first:ident $(, $rest:ident)*) => {
impl_provider_function!(@impl ($first $(, $rest)*));
impl_provider_function!($($rest),*);
};
(@impl ($($type_name:ident),*)) => {
impl <F, R $(, $type_name)*> ServiceFactory<($($type_name,)*), R> for F
where
F: 'static + FnMut($($type_name),*) -> R,
R: Service,
$($type_name: Request,)*
{
#[allow(unused_variables, unused_mut, unused_assignments, non_snake_case)]
fn invoke(&mut self, injector: &mut Injector) -> InjectResult<Svc<R>> {
let result = self($(
match injector.get::<$type_name>() {
Ok(dependency) => dependency,
Err($crate::InjectError::MissingProvider { service_info }) => {
return Err($crate::InjectError::MissingDependency {
dependency_info: service_info,
service_info: $crate::ServiceInfo::of::<R>(),
})
},
Err(error) => return Err(error),
}
),*);
Ok(Svc::new(result))
}
}
};
}
impl_provider_function!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);