1#![deny(clippy::pedantic)]
3#![allow(
4 clippy::cast_possible_truncation,
5 clippy::missing_fields_in_debug,
6 clippy::missing_errors_doc,
7 clippy::missing_panics_doc,
8 clippy::must_use_candidate,
9 clippy::type_complexity,
10 clippy::unused_async,
11 clippy::unused_async_trait_impl
12)]
13use std::rc::Rc;
14
15mod and_then;
16mod apply;
17pub mod boxed;
18pub mod cfg;
19mod chain;
20mod ctx;
21mod fn_ready;
22mod fn_service;
23mod fn_shutdown;
24mod macros;
25mod map;
26mod map_err;
27mod map_init_err;
28mod map_state;
29mod middleware;
30pub mod state;
31mod then;
32mod util;
33
34pub mod pipeline;
35mod pl_factory;
36mod pl_inner;
37mod pl_state;
38
39pub use crate::apply::{apply_fn, apply_fn_factory};
40pub use crate::chain::{ServiceChain, ServiceChainFactory, factory, service};
41pub use crate::ctx::Ctx;
42pub use crate::fn_service::{fn_factory, fn_service, fn_service_st};
43pub use crate::map_state::{map_state, map_state_factory};
44pub use crate::middleware::{Identity, Middleware, Stack, apply, fn_layer};
45pub use crate::pipeline::Pipeline;
46pub use crate::state::{RequestState, State};
47
48#[allow(unused_variables)]
49pub trait Service<St, Req> {
105 type Res;
107
108 type Error;
110
111 async fn call(&self, req: Req, ctx: Ctx<'_, Self, St>) -> Result<Self::Res, Self::Error>;
118
119 #[inline]
120 async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), Self::Error> {
129 Ok(())
130 }
131
132 #[inline]
133 async fn shutdown(&self, cfg: Ctx<'_, Self, St>) {}
137
138 #[inline]
139 fn map<F, Res>(self, f: F) -> ServiceChain<dev::Map<F, Self, Res>, St, Req>
147 where
148 Self: Sized,
149 F: Fn(Self::Res) -> Res,
150 {
151 service(dev::Map::new(f, self))
152 }
153
154 #[inline]
155 fn map_err<F, E>(self, f: F) -> ServiceChain<dev::MapErr<F, Self, E>, St, Req>
163 where
164 Self: Sized,
165 F: Fn(Self::Error) -> E,
166 {
167 service(dev::MapErr::new(f, self))
168 }
169
170 #[inline]
171 fn and_then<Next, F>(self, f: F) -> ServiceChain<dev::AndThen<Self, Next>, St, Req>
181 where
182 Self: Sized,
183 Next: Service<St, Self::Res, Error = Self::Error>,
184 F: IntoService<Next, St, Self::Res>,
185 {
186 service(dev::AndThen::new(self, f.into_service()))
187 }
188
189 #[inline]
190 fn pipeline(self, st: St) -> Pipeline<Req, Self::Res, Self::Error>
192 where
193 Self: Sized + 'static,
194 St: 'static,
195 Req: 'static,
196 {
197 Pipeline::new(st, self)
198 }
199}
200
201pub trait ServiceFactory<St, Req> {
213 type Res;
215
216 type Error;
218
219 type Service: Service<St, Req, Res = Self::Res, Error = Self::Error>;
221
222 type InitError;
224
225 async fn create(&self, cfg: &St) -> Result<Self::Service, Self::InitError>;
227
228 #[inline]
229 async fn pipeline(
231 &self,
232 st: St,
233 ) -> Result<Pipeline<Req, Self::Res, Self::Error>, Self::InitError>
234 where
235 Self: 'static,
236 St: 'static,
237 Req: 'static,
238 {
239 let svc = self.create(&st).await?;
240 Ok(Pipeline::new(st, svc))
241 }
242
243 #[inline]
244 fn map<F, Res>(self, f: F) -> ServiceChainFactory<dev::MapFactory<F, Self, Res>, St, Req>
246 where
247 Self: Sized,
248 F: Fn(Self::Res) -> Res + Clone,
249 {
250 factory(dev::MapFactory::new(f, self))
251 }
252
253 #[inline]
254 fn map_err<F, E>(self, f: F) -> ServiceChainFactory<dev::MapErrFactory<F, Self, E>, St, Req>
257 where
258 Self: Sized,
259 F: Fn(Self::Error) -> E + Clone,
260 {
261 factory(dev::MapErrFactory::new(f, self))
262 }
263
264 #[inline]
265 fn map_init_err<F, E>(self, f: F) -> ServiceChainFactory<dev::MapInitErr<F, Self, E>, St, Req>
268 where
269 Self: Sized,
270 F: Fn(Self::InitError) -> E + Clone,
271 {
272 factory(dev::MapInitErr::new(f, self))
273 }
274
275 fn and_then<U, F>(self, f: F) -> ServiceChainFactory<dev::AndThenFactory<Self, U>, St, Req>
277 where
278 Self: Sized,
279 U: ServiceFactory<St, Self::Res, Error = Self::Error, InitError = Self::InitError>,
280 F: IntoServiceFactory<U, St, Self::Res>,
281 {
282 factory(dev::AndThenFactory::new(self, f.into_factory()))
283 }
284
285 fn boxed(self) -> boxed::BoxServiceFactory<St, Req, Self::Res, Self::Error, Self::InitError>
287 where
288 St: 'static,
289 Req: 'static,
290 Self: Sized + 'static,
291 {
292 boxed::factory(self)
293 }
294}
295
296impl<S, St, Req> Service<St, Req> for &S
297where
298 S: Service<St, Req>,
299{
300 type Res = S::Res;
301 type Error = S::Error;
302
303 #[inline]
304 async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), S::Error> {
305 ctx.ready(&**self).await
306 }
307
308 #[inline]
309 async fn call(&self, req: Req, ctx: Ctx<'_, Self, St>) -> Result<S::Res, S::Error> {
310 ctx.call_nowait(&**self, req).await
311 }
312
313 #[inline]
314 async fn shutdown(&self, ctx: Ctx<'_, Self, St>) {
315 ctx.shutdown(&**self).await;
316 }
317}
318
319impl<S, St, Req> Service<St, Req> for Box<S>
320where
321 S: Service<St, Req>,
322{
323 type Res = S::Res;
324 type Error = S::Error;
325
326 #[inline]
327 async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), S::Error> {
328 ctx.ready(&**self).await
329 }
330
331 #[inline]
332 async fn call(&self, req: Req, ctx: Ctx<'_, Self, St>) -> Result<S::Res, S::Error> {
333 ctx.call_nowait(&**self, req).await
334 }
335
336 #[inline]
337 async fn shutdown(&self, ctx: Ctx<'_, Self, St>) {
338 ctx.shutdown(&**self).await;
339 }
340}
341
342impl<S, St, Req> Service<St, Req> for Rc<S>
343where
344 S: Service<St, Req>,
345{
346 type Res = S::Res;
347 type Error = S::Error;
348
349 #[inline]
350 async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), S::Error> {
351 ctx.ready(&**self).await
352 }
353
354 #[inline]
355 async fn call(&self, req: Req, ctx: Ctx<'_, Self, St>) -> Result<S::Res, S::Error> {
356 ctx.call_nowait(&**self, req).await
357 }
358
359 #[inline]
360 async fn shutdown(&self, ctx: Ctx<'_, Self, St>) {
361 ctx.shutdown(&**self).await;
362 }
363}
364
365impl<Sf, St, Req> ServiceFactory<St, Req> for Rc<Sf>
366where
367 Sf: ServiceFactory<St, Req>,
368{
369 type Res = Sf::Res;
370 type Error = Sf::Error;
371 type Service = Sf::Service;
372 type InitError = Sf::InitError;
373
374 async fn create(&self, cfg: &St) -> Result<Self::Service, Self::InitError> {
375 self.as_ref().create(cfg).await
376 }
377}
378
379pub trait ServiceCaller<Req, Res, Err> {
381 async fn call_service(&self, req: Req) -> Result<Res, Err>;
383}
384
385pub trait IntoService<S, St, Req>
387where
388 S: Service<St, Req>,
389{
390 fn into_service(self) -> S;
392}
393
394pub trait IntoServiceFactory<Sf, St, Req>
396where
397 Sf: ServiceFactory<St, Req>,
398{
399 fn into_factory(self) -> Sf;
401}
402
403impl<S, St, Req> IntoService<S, St, Req> for S
404where
405 S: Service<St, Req>,
406{
407 #[inline]
408 fn into_service(self) -> S {
409 self
410 }
411}
412
413impl<Sf, St, Req> IntoServiceFactory<Sf, St, Req> for Sf
414where
415 Sf: ServiceFactory<St, Req>,
416{
417 #[inline]
418 fn into_factory(self) -> Sf {
419 self
420 }
421}
422
423pub mod dev {
424 pub use crate::and_then::{AndThen, AndThenFactory};
425 pub use crate::apply::{Apply, ApplyCtx, ApplyFactory};
426 pub use crate::chain::{ServiceChain, ServiceChainFactory};
427 pub use crate::fn_ready::FnReadiness;
428 pub use crate::fn_service::{FnFactory, FnService, FnServiceSt, FnServiceStFactory};
429 pub use crate::fn_shutdown::FnShutdown;
430 pub use crate::map::{Map, MapFactory};
431 pub use crate::map_err::{MapErr, MapErrFactory};
432 pub use crate::map_init_err::MapInitErr;
433 pub use crate::map_state::{MapState, MapStateFactory};
434 pub use crate::middleware::{ApplyMiddleware, FnMiddleware};
435 pub use crate::then::{Then, ThenFactory};
436}