1use std::any::TypeId;
7use std::future::Future;
8use std::sync::Arc;
9
10use crate::error::AppError;
11use crate::scope::Scope;
12use crate::store::Store;
13
14pub type BoxFuture<T> = std::pin::Pin<Box<dyn Future<Output = T> + Send>>;
16
17pub trait Component: Send + Sync + 'static {
36 type Deps: DepsTuple;
42
43 fn build(deps: Self::Deps) -> Self;
47
48 const SCOPE: Scope = Scope::Singleton;
50
51 #[allow(unused_variables)]
57 fn inner_init(&mut self, store: &Store) -> crate::RIE<()> {
58 Ok(())
59 }
60
61 #[allow(unused_variables)]
65 fn init(app: &Arc<crate::App>) -> crate::RIE<()> {
66 Ok(())
67 }
68
69 #[allow(unused_variables)]
71 fn async_init(app: &Arc<crate::App>) -> BoxFuture<crate::RIE<()>> {
72 Box::pin(async { Ok(()) })
73 }
74
75 #[allow(unused_variables)]
77 fn async_run(app: &Arc<crate::App>, token: crate::CancellationToken) -> BoxFuture<crate::RIE<()>> {
78 Box::pin(async { Ok(()) })
79 }
80
81 #[allow(unused_variables)]
83 fn shutdown(&self) {}
84
85 fn init_sort() -> i32 {
87 10000
88 }
89
90 fn trait_impls() -> &'static [fn() -> TypeId] {
95 &[]
96 }
97}
98
99pub trait DepsTuple: Sized {
103 fn resolve(store: &Store) -> Result<Self, AppError>;
105
106 fn dep_type_ids() -> Vec<TypeId>;
108}
109
110impl DepsTuple for () {
113 fn resolve(_store: &Store) -> crate::RIE<Self> {
114 Ok(())
115 }
116
117 fn dep_type_ids() -> Vec<TypeId> {
118 Vec::new()
119 }
120}
121
122macro_rules! impl_deps_tuple {
123 ($($T:ident),+) => {
124 impl<$($T: Component),+> DepsTuple for ($(Arc<$T>,)+) {
125 fn resolve(store: &Store) -> Result<Self, AppError> {
126 Ok(($(
127 store.inject::<$T>()?
128 ,)+))
129 }
130
131 fn dep_type_ids() -> Vec<TypeId> {
132 vec![$(TypeId::of::<$T>()),+]
133 }
134 }
135 };
136}
137
138impl_deps_tuple!(A);
139impl_deps_tuple!(A, B);
140impl_deps_tuple!(A, B, C);
141impl_deps_tuple!(A, B, C, D);
142impl_deps_tuple!(A, B, C, D, E);
143impl_deps_tuple!(A, B, C, D, E, F);
144impl_deps_tuple!(A, B, C, D, E, F, G);
145impl_deps_tuple!(A, B, C, D, E, F, G, H);
146impl_deps_tuple!(A, B, C, D, E, F, G, H, I);
147impl_deps_tuple!(A, B, C, D, E, F, G, H, I, J);
148impl_deps_tuple!(A, B, C, D, E, F, G, H, I, J, K);
149impl_deps_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);
150impl_deps_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M);
151impl_deps_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
152impl_deps_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
153impl_deps_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);