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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! Fluent wiring methods shared by [`ModrunBuilder`](crate::ModrunBuilder) and [`Module`](crate::Module).
/// Implements `.provide` / `.supply` / `.invoke` (and private variants on [`Module`](crate::Module)).
///
/// The type must provide `fn push_option(&mut self, Box<dyn ModOption>)`.
macro_rules! impl_wiring_methods {
($ty:ty) => {
impl $ty {
/// Register a constructor. Dependencies are injected by cloning singletons
/// (or by cloning an `Arc<T>` if the parameter is `Arc<T>`).
///
/// Constructors that return `Result<T, E>` must use
/// [`provide_result`](Self::provide_result) instead; passing one here
/// is a compile error.
///
/// ```compile_fail
/// # use modrun::Modrun;
/// #[derive(Clone)]
/// struct Config;
///
/// fn new_config() -> Result<Config, std::io::Error> {
/// Ok(Config)
/// }
///
/// Modrun::builder().provide(new_config);
/// ```
#[must_use]
pub fn provide<M, F>(mut self, ctor: F) -> Self
where
F: $crate::provide::ProviderFn<M> + 'static,
{
self.push_option($crate::provide::provide(ctor));
self
}
/// Register a fallible constructor (`Result<T, E>`).
#[must_use]
pub fn provide_result<M, F>(mut self, ctor: F) -> Self
where
F: $crate::provide::FallibleProviderFn<M> + 'static,
{
self.push_option($crate::provide::provide_result(ctor));
self
}
/// Register an `async` constructor, awaited while the graph is built.
#[must_use]
pub fn provide_async<M, F>(mut self, ctor: F) -> Self
where
F: $crate::provide::AsyncProviderFn<M> + 'static,
{
self.push_option($crate::provide::provide_async(ctor));
self
}
/// Register a fallible `async` constructor (`Result<T, E>`).
#[must_use]
pub fn provide_result_async<M, F>(mut self, ctor: F) -> Self
where
F: $crate::provide::FallibleAsyncProviderFn<M> + 'static,
{
self.push_option($crate::provide::provide_result_async(ctor));
self
}
/// Register an already-erased constructor, from
/// [`ProviderFn::into_provider`](crate::__wiring::ProviderFn::into_provider).
#[doc(hidden)]
#[must_use]
pub fn provide_dyn(mut self, provider: $crate::__wiring::DynProvider) -> Self {
self.push_option($crate::provide::provide_dyn(provider));
self
}
/// Supply a pre-built value. Inject by value requires `T: Clone`; inject
/// `Arc<T>` does not.
#[must_use]
pub fn supply<T: Send + Sync + 'static>(mut self, value: T) -> Self {
self.push_option($crate::supply::supply(value));
self
}
/// Register an invoker that pulls the dependency graph.
#[must_use]
pub fn invoke<M, F>(mut self, func: F) -> Self
where
F: $crate::invoke::InvokeFn<M> + 'static,
{
self.push_option($crate::invoke::invoke(func));
self
}
/// Register an `async` invoker that pulls the dependency graph.
#[must_use]
pub fn invoke_async<M, F>(mut self, func: F) -> Self
where
F: $crate::invoke::AsyncInvokeFn<M> + 'static,
{
self.push_option($crate::invoke::invoke_async(func));
self
}
/// Register an already-erased invoker, from
/// [`InvokeFn::into_invoke`](crate::__wiring::InvokeFn::into_invoke).
#[doc(hidden)]
#[must_use]
pub fn invoke_dyn(mut self, invoker: $crate::__wiring::DynInvoker) -> Self {
self.push_option($crate::invoke::invoke_dyn(invoker));
self
}
}
};
}
pub(crate) use impl_wiring_methods;
/// Private provide/supply variants — only on [`Module`](crate::Module), because a
/// private binding on the root builder sits on every ancestor chain.
macro_rules! impl_private_wiring_methods {
($ty:ty) => {
impl $ty {
/// Like [`provide`](Self::provide), but only visible inside this module.
#[must_use]
pub fn provide_private<M, F>(mut self, ctor: F) -> Self
where
F: $crate::provide::ProviderFn<M> + 'static,
{
self.push_option($crate::provide::provide_priv(ctor));
self
}
/// Like [`provide_result`](Self::provide_result), scoped privately to the module.
#[must_use]
pub fn provide_result_private<M, F>(mut self, ctor: F) -> Self
where
F: $crate::provide::FallibleProviderFn<M> + 'static,
{
self.push_option($crate::provide::provide_result_priv(ctor));
self
}
/// Like [`provide_async`](Self::provide_async), scoped privately to the module.
#[must_use]
pub fn provide_async_private<M, F>(mut self, ctor: F) -> Self
where
F: $crate::provide::AsyncProviderFn<M> + 'static,
{
self.push_option($crate::provide::provide_async_priv(ctor));
self
}
/// Like [`provide_result_async`](Self::provide_result_async), scoped privately.
#[must_use]
pub fn provide_result_async_private<M, F>(mut self, ctor: F) -> Self
where
F: $crate::provide::FallibleAsyncProviderFn<M> + 'static,
{
self.push_option($crate::provide::provide_result_async_priv(ctor));
self
}
/// Like [`provide_dyn`](Self::provide_dyn), scoped privately to the module.
#[doc(hidden)]
#[must_use]
pub fn provide_dyn_private(mut self, provider: $crate::__wiring::DynProvider) -> Self {
self.push_option($crate::provide::provide_dyn_priv(provider));
self
}
/// Supply a value private to this module.
#[must_use]
pub fn supply_private<T: Send + Sync + 'static>(mut self, value: T) -> Self {
self.push_option($crate::supply::supply_priv(value));
self
}
}
};
}
pub(crate) use impl_private_wiring_methods;
/// Group provide/supply variants shared by [`ModrunBuilder`](crate::ModrunBuilder) and [`Module`](crate::Module).
macro_rules! impl_group_wiring_methods {
($ty:ty) => {
impl $ty {
/// Register a group member constructor (sync / infallible).
///
/// `T` is inferred from the constructor's return type; there is no
/// `provide_group::<T>(...)` turbofish. For a trait-object group,
/// return `Arc<dyn Trait>` from the constructor.
#[must_use]
pub fn provide_group<M, F>(mut self, ctor: F) -> Self
where
M: $crate::provide::ProviderMarker,
<M as $crate::provide::ProviderMarker>::Output: Clone + Send + Sync + 'static,
F: $crate::provide::ProviderFn<M> + 'static,
{
self.push_option($crate::provide_group::provide_group::<M, F>(ctor));
self
}
/// Register a fallible group member constructor (`Result<T, E>`).
#[must_use]
pub fn provide_group_result<M, F>(mut self, ctor: F) -> Self
where
M: $crate::provide::ProviderMarker,
<M as $crate::provide::ProviderMarker>::Output: Clone + Send + Sync + 'static,
F: $crate::provide::FallibleProviderFn<M> + 'static,
{
self.push_option($crate::provide_group::provide_group_result::<M, F>(ctor));
self
}
/// Register an async group member constructor.
#[must_use]
pub fn provide_group_async<M, F>(mut self, ctor: F) -> Self
where
M: $crate::provide::ProviderMarker,
<M as $crate::provide::ProviderMarker>::Output: Clone + Send + Sync + 'static,
F: $crate::provide::AsyncProviderFn<M> + 'static,
{
self.push_option($crate::provide_group::provide_group_async::<M, F>(ctor));
self
}
/// Register a fallible async group member constructor (`Result<T, E>`).
#[must_use]
pub fn provide_group_result_async<M, F>(mut self, ctor: F) -> Self
where
M: $crate::provide::ProviderMarker,
<M as $crate::provide::ProviderMarker>::Output: Clone + Send + Sync + 'static,
F: $crate::provide::FallibleAsyncProviderFn<M> + 'static,
{
self.push_option($crate::provide_group::provide_group_result_async::<M, F>(
ctor,
));
self
}
/// Supply a pre-built group member.
#[must_use]
pub fn supply_group<T: Clone + Send + Sync + 'static>(mut self, value: T) -> Self {
self.push_option($crate::supply_group::supply_group(value));
self
}
/// Register an already-erased group member, from
/// [`ProviderFn::into_provider`](crate::__wiring::ProviderFn::into_provider).
#[doc(hidden)]
#[must_use]
pub fn provide_group_dyn<T: Clone + Send + Sync + 'static>(
mut self,
provider: $crate::__wiring::DynProvider,
) -> Self {
self.push_option($crate::provide_group::provide_group_dyn::<T>(provider));
self
}
}
};
}
pub(crate) use impl_group_wiring_methods;