#![deny(rust_2018_idioms, warnings)]
#![allow(clippy::type_complexity)]
use std::future::Future;
use std::rc::Rc;
use std::task::{self, Context, Poll};
mod and_then;
mod apply;
pub mod boxed;
mod fn_service;
mod fn_shutdown;
mod macros;
mod map;
mod map_config;
mod map_err;
mod map_init_err;
mod middleware;
mod pipeline;
mod then;
pub use self::apply::{apply_fn, apply_fn_factory};
pub use self::fn_service::{fn_factory, fn_factory_with_config, fn_service};
pub use self::fn_shutdown::fn_shutdown;
pub use self::map_config::{map_config, unit_config};
pub use self::middleware::{apply, Identity, Middleware, Stack};
pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory};
#[allow(unused_variables)]
pub trait Service<Req> {
type Response;
type Error;
type Future<'f>: Future<Output = Result<Self::Response, Self::Error>>
where
Req: 'f,
Self: 'f;
fn call(&self, req: Req) -> Self::Future<'_>;
#[inline]
fn poll_ready(&self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
#[inline]
fn poll_shutdown(&self, cx: &mut task::Context<'_>) -> Poll<()> {
Poll::Ready(())
}
#[inline]
fn map<F, Res>(self, f: F) -> crate::dev::Map<Self, F, Req, Res>
where
Self: Sized,
F: Fn(Self::Response) -> Res,
{
crate::dev::Map::new(self, f)
}
#[inline]
fn map_err<F, E>(self, f: F) -> crate::dev::MapErr<Self, Req, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E,
{
crate::dev::MapErr::new(self, f)
}
}
pub trait ServiceFactory<Req, Cfg = ()> {
type Response;
type Error;
type Service: Service<Req, Response = Self::Response, Error = Self::Error>;
type InitError;
type Future<'f>: Future<Output = Result<Self::Service, Self::InitError>>
where
Cfg: 'f,
Self: 'f;
fn create(&self, cfg: Cfg) -> Self::Future<'_>;
#[inline]
fn map<F, Res>(self, f: F) -> crate::map::MapFactory<Self, F, Req, Res, Cfg>
where
Self: Sized,
F: Fn(Self::Response) -> Res + Clone,
{
crate::map::MapFactory::new(self, f)
}
#[inline]
fn map_err<F, E>(self, f: F) -> crate::map_err::MapErrFactory<Self, Req, Cfg, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E + Clone,
{
crate::map_err::MapErrFactory::new(self, f)
}
#[inline]
fn map_init_err<F, E>(
self,
f: F,
) -> crate::map_init_err::MapInitErr<Self, Req, Cfg, F, E>
where
Self: Sized,
F: Fn(Self::InitError) -> E + Clone,
{
crate::map_init_err::MapInitErr::new(self, f)
}
}
impl<'a, S, Req> Service<Req> for &'a S
where
S: Service<Req> + ?Sized,
{
type Response = S::Response;
type Error = S::Error;
type Future<'f> = S::Future<'f> where 'a: 'f, Req: 'f;
#[inline]
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
(**self).poll_ready(cx)
}
#[inline]
fn poll_shutdown(&self, cx: &mut Context<'_>) -> Poll<()> {
(**self).poll_shutdown(cx)
}
#[inline]
fn call(&self, request: Req) -> S::Future<'_> {
(**self).call(request)
}
}
impl<S, Req> Service<Req> for Box<S>
where
S: Service<Req> + ?Sized,
{
type Response = S::Response;
type Error = S::Error;
type Future<'f> = S::Future<'f> where S: 'f, Req: 'f;
#[inline]
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
(**self).poll_ready(cx)
}
#[inline]
fn poll_shutdown(&self, cx: &mut Context<'_>) -> Poll<()> {
(**self).poll_shutdown(cx)
}
#[inline]
fn call(&self, request: Req) -> S::Future<'_> {
(**self).call(request)
}
}
impl<S, Req> Service<Req> for Rc<S>
where
S: Service<Req> + ?Sized,
{
type Response = S::Response;
type Error = S::Error;
type Future<'f> = S::Future<'f> where S: 'f, Req: 'f;
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
(**self).poll_ready(cx)
}
#[inline]
fn poll_shutdown(&self, cx: &mut Context<'_>) -> Poll<()> {
(**self).poll_shutdown(cx)
}
fn call(&self, request: Req) -> S::Future<'_> {
(**self).call(request)
}
}
impl<S, Req, Cfg> ServiceFactory<Req, Cfg> for Rc<S>
where
S: ServiceFactory<Req, Cfg>,
{
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;
type InitError = S::InitError;
type Future<'f> = S::Future<'f> where S: 'f, Cfg: 'f;
fn create(&self, cfg: Cfg) -> S::Future<'_> {
self.as_ref().create(cfg)
}
}
pub trait IntoService<Svc, Req>
where
Svc: Service<Req>,
{
fn into_service(self) -> Svc;
}
pub trait IntoServiceFactory<T, Req, Cfg = ()>
where
T: ServiceFactory<Req, Cfg>,
{
fn into_factory(self) -> T;
}
impl<Svc, Req> IntoService<Svc, Req> for Svc
where
Svc: Service<Req>,
{
fn into_service(self) -> Svc {
self
}
}
impl<T, Req, Cfg> IntoServiceFactory<T, Req, Cfg> for T
where
T: ServiceFactory<Req, Cfg>,
{
fn into_factory(self) -> T {
self
}
}
pub fn into_service<Svc, Req, F>(tp: F) -> Svc
where
Svc: Service<Req>,
F: IntoService<Svc, Req>,
{
tp.into_service()
}
pub mod dev {
pub use crate::and_then::{AndThen, AndThenFactory};
pub use crate::apply::{Apply, ApplyFactory};
pub use crate::fn_service::{
FnService, FnServiceConfig, FnServiceFactory, FnServiceNoConfig,
};
pub use crate::fn_shutdown::FnShutdown;
pub use crate::map::{Map, MapFactory};
pub use crate::map_config::{MapConfig, UnitConfig};
pub use crate::map_err::{MapErr, MapErrFactory};
pub use crate::map_init_err::MapInitErr;
pub use crate::middleware::ApplyMiddleware;
pub use crate::then::{Then, ThenFactory};
}