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
263
264
265
266
267
268
269
270
271
272
273
274
275
use ;
/// A trait implemented by service factories to create instances of services that implement the [`Service`](crate::Service) trait.
///
/// `MakeService` enables flexible service chain construction with state migration between instances.
/// It's particularly useful for managing stateful resources (e.g., connection pools) when updating
/// service configurations.
///
/// # Key Features
///
/// - State preservation and resource reuse between service instances
/// - Optional creation of services from scratch
/// - Efficient management of stateful resources across service updates
///
/// # Example Implementation
///
/// ```rust
/// use std::convert::Infallible;
/// use service_async::{MakeService, Service};
///
/// struct MyService {
/// connection_pool: ConnectionPool,
/// config: Config,
/// }
///
/// struct MyServiceFactory {
/// config: Config,
/// }
///
/// impl MakeService for MyServiceFactory {
/// type Service = MyService;
/// type Error = Infallible;
///
/// fn make_via_ref(&self, old: Option<&Self::Service>) -> Result<Self::Service, Self::Error> {
/// Ok(match old {
/// Some(existing) => MyService {
/// connection_pool: existing.connection_pool.clone(),
/// config: self.config.clone(),
/// },
/// None => MyService {
/// connection_pool: ConnectionPool::new(),
/// config: self.config.clone(),
/// },
/// })
/// }
/// }
/// ```
///
/// In this example, `MyServiceFactory` implements `MakeService` to create `MyService` instances,
/// demonstrating how to reuse a connection pool when an existing service is provided.
/// A boxed trait object of `MakeService` that enables type erasure for service factories.
///
/// `BoxedMakeService<S, E>` allows different implementations of `MakeService` to be
/// treated uniformly, as long as they produce the same `Service` type `S` and `Error` type `E`.
//
/// This type is particularly useful when designing systems with pluggable or configurable
/// service factories, where the exact implementation of the factory may vary or be determined at runtime.
pub type BoxedMakeService<S, E> =
;
/// `ArcMakeService<S, E>` is similar to `BoxedMakeService<S, E>`, but uses `Arc` instead of `Box`.
/// This allows for multiple owners of the same `MakeService` implementation, enabling efficient
/// cloning and sharing across multiple components or threads.
///
/// # Key Features
///
/// - Shared Ownership: Allows multiple components to share the same `MakeService` instance.
/// - Type Erasure: Enables storing different `MakeService` implementations uniformly.
/// - Efficient Cloning: `Arc` allows for cheap cloning of the service factory reference.
///
/// This type is particularly useful in scenarios where a service factory needs to be
/// shared across multiple parts of an application, such as in worker pools.
pub type ArcMakeService<S, E> =
;
pub type BoxedMakeBoxedService<Req, Resp, SE, ME> =
;
pub type ArcMakeBoxedService<Req, Resp, SE, ME> =
;
/// A trait implemented by asynchronous service factories to create instances of services
/// that implement the [`Service`](crate::Service) trait.
///
/// `AsyncMakeService` is the asynchronous counterpart to [`MakeService`]. It enables flexible
/// service chain construction with state migration between instances, allowing for asynchronous
/// initialization or resource acquisition.
///
/// # Key Features
///
/// - Asynchronous service creation, suitable for I/O-bound initialization
/// - State preservation and resource reuse between service instances
/// - Optional creation of services from scratch
/// - Efficient management of stateful resources across service updates
///
/// # Example Implementation
///
/// ```rust
/// use std::convert::Infallible;
/// use your_crate::{AsyncMakeService, Service};
///
/// struct MyAsyncService {
/// connection_pool: AsyncConnectionPool,
/// config: Config,
/// }
///
/// struct MyAsyncServiceFactory {
/// config: Config,
/// }
///
/// impl AsyncMakeService for MyAsyncServiceFactory {
/// type Service = MyAsyncService;
/// type Error = Infallible;
///
/// async fn make_via_ref(&self, old: Option<&Self::Service>) -> Result<Self::Service, Self::Error> {
/// Ok(match old {
/// Some(existing) => MyAsyncService {
/// connection_pool: existing.connection_pool.clone(),
/// config: self.config.clone(),
/// },
/// None => MyAsyncService {
/// connection_pool: AsyncConnectionPool::new().await,
/// config: self.config.clone(),
/// },
/// })
/// }
/// }
/// ```
///
/// In this example, `MyAsyncServiceFactory` implements `AsyncMakeService` to create `MyAsyncService`
/// instances asynchronously, demonstrating how to reuse a connection pool or create a new one when needed.
/// Impl AsyncMakeService where T: MakeService.
;