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;
44pub use crate::middleware::{Identity, Middleware, Stack, apply, fn_layer};
45pub use crate::pipeline::Pipeline;
46pub use crate::state::{RequestState, State};
47
48#[deprecated]
49pub use crate::chain::svc;
50
51#[allow(unused_variables)]
52pub trait Service<St, Req> {
108 type Res;
110
111 type Error;
113
114 async fn call(&self, req: Req, ctx: Ctx<'_, Self, St>) -> Result<Self::Res, Self::Error>;
121
122 #[inline]
123 async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), Self::Error> {
132 Ok(())
133 }
134
135 #[inline]
136 async fn shutdown(&self, cfg: Ctx<'_, Self, St>) {}
140
141 #[inline]
142 fn map<F, Res>(self, f: F) -> ServiceChain<dev::Map<F, Self, Res>, St, Req>
150 where
151 Self: Sized,
152 F: Fn(Self::Res) -> Res,
153 {
154 service(dev::Map::new(f, self))
155 }
156
157 #[inline]
158 fn map_err<F, E>(self, f: F) -> ServiceChain<dev::MapErr<F, Self, E>, St, Req>
166 where
167 Self: Sized,
168 F: Fn(Self::Error) -> E,
169 {
170 service(dev::MapErr::new(f, self))
171 }
172
173 #[inline]
174 fn and_then<Next, F>(self, f: F) -> ServiceChain<dev::AndThen<Self, Next>, St, Req>
184 where
185 Self: Sized,
186 Next: Service<St, Self::Res, Error = Self::Error>,
187 F: IntoService<Next, St, Self::Res>,
188 {
189 service(dev::AndThen::new(self, f.into_service()))
190 }
191
192 #[inline]
193 fn pipeline(self, st: St) -> Pipeline<Req, Self::Res, Self::Error>
195 where
196 Self: Sized + 'static,
197 St: 'static,
198 Req: 'static,
199 {
200 Pipeline::new(st, self)
201 }
202}
203
204pub trait ServiceFactory<St, Req> {
216 type Res;
218
219 type Error;
221
222 type Service: Service<St, Req, Res = Self::Res, Error = Self::Error>;
224
225 type InitError;
227
228 async fn create(&self, cfg: &St) -> Result<Self::Service, Self::InitError>;
230
231 #[inline]
232 async fn pipeline(
234 &self,
235 st: St,
236 ) -> Result<Pipeline<Req, Self::Res, Self::Error>, Self::InitError>
237 where
238 Self: 'static,
239 St: 'static,
240 Req: 'static,
241 {
242 let svc = self.create(&st).await?;
243 Ok(Pipeline::new(st, svc))
244 }
245
246 #[inline]
247 fn map<F, Res>(self, f: F) -> ServiceChainFactory<dev::MapFactory<F, Self, Res>, St, Req>
249 where
250 Self: Sized,
251 F: Fn(Self::Res) -> Res + Clone,
252 {
253 factory(dev::MapFactory::new(f, self))
254 }
255
256 #[inline]
257 fn map_err<F, E>(self, f: F) -> ServiceChainFactory<dev::MapErrFactory<F, Self, E>, St, Req>
260 where
261 Self: Sized,
262 F: Fn(Self::Error) -> E + Clone,
263 {
264 factory(dev::MapErrFactory::new(f, self))
265 }
266
267 #[inline]
268 fn map_init_err<F, E>(self, f: F) -> ServiceChainFactory<dev::MapInitErr<F, Self, E>, St, Req>
271 where
272 Self: Sized,
273 F: Fn(Self::InitError) -> E + Clone,
274 {
275 factory(dev::MapInitErr::new(f, self))
276 }
277
278 fn and_then<U, F>(self, f: F) -> ServiceChainFactory<dev::AndThenFactory<Self, U>, St, Req>
280 where
281 Self: Sized,
282 U: ServiceFactory<St, Self::Res, Error = Self::Error, InitError = Self::InitError>,
283 F: IntoServiceFactory<U, St, Self::Res>,
284 {
285 factory(dev::AndThenFactory::new(self, f.into_factory()))
286 }
287
288 fn boxed(self) -> boxed::BoxServiceFactory<St, Req, Self::Res, Self::Error, Self::InitError>
290 where
291 St: 'static,
292 Req: 'static,
293 Self: Sized + 'static,
294 {
295 boxed::factory(self)
296 }
297}
298
299impl<S, St, Req> Service<St, Req> for &S
300where
301 S: Service<St, Req>,
302{
303 type Res = S::Res;
304 type Error = S::Error;
305
306 #[inline]
307 async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), S::Error> {
308 ctx.ready(&**self).await
309 }
310
311 #[inline]
312 async fn call(&self, req: Req, ctx: Ctx<'_, Self, St>) -> Result<S::Res, S::Error> {
313 ctx.call_nowait(&**self, req).await
314 }
315
316 #[inline]
317 async fn shutdown(&self, ctx: Ctx<'_, Self, St>) {
318 ctx.shutdown(&**self).await;
319 }
320}
321
322impl<S, St, Req> Service<St, Req> for Box<S>
323where
324 S: Service<St, Req>,
325{
326 type Res = S::Res;
327 type Error = S::Error;
328
329 #[inline]
330 async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), S::Error> {
331 ctx.ready(&**self).await
332 }
333
334 #[inline]
335 async fn call(&self, req: Req, ctx: Ctx<'_, Self, St>) -> Result<S::Res, S::Error> {
336 ctx.call_nowait(&**self, req).await
337 }
338
339 #[inline]
340 async fn shutdown(&self, ctx: Ctx<'_, Self, St>) {
341 ctx.shutdown(&**self).await;
342 }
343}
344
345impl<S, St, Req> Service<St, Req> for Rc<S>
346where
347 S: Service<St, Req>,
348{
349 type Res = S::Res;
350 type Error = S::Error;
351
352 #[inline]
353 async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), S::Error> {
354 ctx.ready(&**self).await
355 }
356
357 #[inline]
358 async fn call(&self, req: Req, ctx: Ctx<'_, Self, St>) -> Result<S::Res, S::Error> {
359 ctx.call_nowait(&**self, req).await
360 }
361
362 #[inline]
363 async fn shutdown(&self, ctx: Ctx<'_, Self, St>) {
364 ctx.shutdown(&**self).await;
365 }
366}
367
368impl<Sf, St, Req> ServiceFactory<St, Req> for Rc<Sf>
369where
370 Sf: ServiceFactory<St, Req>,
371{
372 type Res = Sf::Res;
373 type Error = Sf::Error;
374 type Service = Sf::Service;
375 type InitError = Sf::InitError;
376
377 async fn create(&self, cfg: &St) -> Result<Self::Service, Self::InitError> {
378 self.as_ref().create(cfg).await
379 }
380}
381
382pub trait IntoService<S, St, Req>
384where
385 S: Service<St, Req>,
386{
387 fn into_service(self) -> S;
389}
390
391pub trait IntoServiceFactory<Sf, St, Req>
393where
394 Sf: ServiceFactory<St, Req>,
395{
396 fn into_factory(self) -> Sf;
398}
399
400impl<S, St, Req> IntoService<S, St, Req> for S
401where
402 S: Service<St, Req>,
403{
404 #[inline]
405 fn into_service(self) -> S {
406 self
407 }
408}
409
410impl<Sf, St, Req> IntoServiceFactory<Sf, St, Req> for Sf
411where
412 Sf: ServiceFactory<St, Req>,
413{
414 #[inline]
415 fn into_factory(self) -> Sf {
416 self
417 }
418}
419
420#[inline(always)]
422#[allow(clippy::inline_always)]
423pub fn __assert_svc<St, Req, Res, Err>(
424 s: impl Service<St, Req, Res = Res, Error = Err>,
425) -> impl Service<St, Req, Res = Res, Error = Err> {
426 s
427}
428
429#[inline(always)]
431#[allow(clippy::inline_always)]
432pub fn __assert_factory<Sf, St, Req, Res, Err, InitErr>(f: Sf) -> Sf
433where
434 Sf: ServiceFactory<St, Req, Res = Res, Error = Err, InitError = InitErr>,
435{
436 f
437}
438
439pub mod dev {
440 pub use crate::and_then::{AndThen, AndThenFactory};
441 pub use crate::apply::{Apply, ApplyCtx, ApplyFactory};
442 pub use crate::chain::{ServiceChain, ServiceChainFactory};
443 pub use crate::fn_ready::FnReadiness;
444 pub use crate::fn_service::{FnFactory, FnService, FnServiceSt, FnServiceStFactory};
445 pub use crate::fn_shutdown::FnShutdown;
446 pub use crate::map::{Map, MapFactory};
447 pub use crate::map_err::{MapErr, MapErrFactory};
448 pub use crate::map_init_err::MapInitErr;
449 pub use crate::map_state::MapState;
450 pub use crate::middleware::{ApplyMiddleware, FnMiddleware};
451 pub use crate::then::{Then, ThenFactory};
452}