2018-08-31 02:46:11 +02:00
|
|
|
use futures::{Future, IntoFuture};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
|
|
|
mod and_then;
|
2018-08-30 18:17:17 +02:00
|
|
|
mod apply;
|
2018-08-25 18:02:14 +02:00
|
|
|
mod fn_service;
|
|
|
|
mod fn_state_service;
|
2018-08-28 19:39:27 +02:00
|
|
|
mod map;
|
2018-08-25 18:02:14 +02:00
|
|
|
mod map_err;
|
|
|
|
mod map_init_err;
|
|
|
|
|
|
|
|
pub use self::and_then::{AndThen, AndThenNewService};
|
2018-08-31 02:54:59 +02:00
|
|
|
pub use self::apply::{Apply, ApplyNewService};
|
2018-08-29 21:20:35 +02:00
|
|
|
pub use self::fn_service::{FnNewService, FnService};
|
|
|
|
pub use self::fn_state_service::{FnStateNewService, FnStateService};
|
2018-08-28 19:39:27 +02:00
|
|
|
pub use self::map::{Map, MapNewService};
|
2018-08-25 18:02:14 +02:00
|
|
|
pub use self::map_err::{MapErr, MapErrNewService};
|
|
|
|
pub use self::map_init_err::MapInitErr;
|
2018-08-30 18:17:17 +02:00
|
|
|
use {NewService, Service};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2018-08-29 21:20:35 +02:00
|
|
|
pub trait ServiceExt: Service {
|
2018-08-31 21:51:26 +02:00
|
|
|
fn apply<F, R, Req>(self, f: F) -> Apply<Self, F, R, Req>
|
2018-08-31 02:46:11 +02:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2018-08-31 21:51:26 +02:00
|
|
|
Self::Error: Into<<R::Future as Future>::Error>,
|
2018-08-31 02:46:11 +02:00
|
|
|
F: Fn(Req, &mut Self) -> R,
|
2018-08-31 21:51:26 +02:00
|
|
|
R: IntoFuture,
|
2018-08-31 02:46:11 +02:00
|
|
|
{
|
2018-08-31 02:54:59 +02:00
|
|
|
Apply::new(f, self)
|
2018-08-31 02:46:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
|
2018-08-29 21:20:35 +02:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2018-09-04 18:30:52 +02:00
|
|
|
F: Into<B>,
|
2018-08-29 21:20:35 +02:00
|
|
|
B: Service<Request = Self::Response, Error = Self::Error>,
|
|
|
|
{
|
2018-09-04 18:30:52 +02:00
|
|
|
AndThen::new(self, service.into())
|
2018-08-29 21:20:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn map<F, R>(self, f: F) -> Map<Self, F, R>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: Fn(Self::Response) -> R,
|
|
|
|
{
|
|
|
|
Map::new(self, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_err<F, E>(self, f: F) -> MapErr<Self, F, E>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: Fn(Self::Error) -> E,
|
|
|
|
{
|
|
|
|
MapErr::new(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 18:02:14 +02:00
|
|
|
pub trait NewServiceExt: NewService {
|
2018-08-31 21:51:26 +02:00
|
|
|
fn apply<F, R, Req>(self, f: F) -> ApplyNewService<Self, F, R, Req>
|
2018-08-31 02:46:11 +02:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2018-08-31 21:51:26 +02:00
|
|
|
Self::Error: Into<<R::Future as Future>::Error>,
|
2018-08-31 02:46:11 +02:00
|
|
|
F: Fn(Req, &mut Self::Service) -> R + Clone,
|
2018-08-31 21:51:26 +02:00
|
|
|
R: IntoFuture,
|
2018-08-31 02:46:11 +02:00
|
|
|
{
|
2018-08-31 02:54:59 +02:00
|
|
|
ApplyNewService::new(f, self)
|
2018-08-31 02:46:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 18:02:14 +02:00
|
|
|
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
2018-09-04 18:30:52 +02:00
|
|
|
F: Into<B>,
|
2018-08-25 18:02:14 +02:00
|
|
|
B: NewService<
|
|
|
|
Request = Self::Response,
|
|
|
|
Error = Self::Error,
|
|
|
|
InitError = Self::InitError,
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
AndThenNewService::new(self, new_service)
|
|
|
|
}
|
|
|
|
|
2018-08-28 19:39:27 +02:00
|
|
|
fn map<F, R>(self, f: F) -> MapNewService<Self, F, R>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: Fn(Self::Response) -> R,
|
|
|
|
{
|
|
|
|
MapNewService::new(self, f)
|
|
|
|
}
|
|
|
|
|
2018-08-25 18:02:14 +02:00
|
|
|
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: Fn(Self::Error) -> E,
|
|
|
|
{
|
|
|
|
MapErrNewService::new(self, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, F, E>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: Fn(Self::InitError) -> E,
|
|
|
|
{
|
|
|
|
MapInitErr::new(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:30:52 +02:00
|
|
|
impl<T: ?Sized> ServiceExt for T where T: Service {}
|
|
|
|
impl<T: ?Sized> NewServiceExt for T where T: NewService {}
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2018-09-04 18:30:52 +02:00
|
|
|
impl<F, Req, Resp, Err, Fut> From<F> for FnService<F, Req, Resp, Err, Fut>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut + 'static,
|
|
|
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
|
|
|
{
|
2018-09-04 18:30:52 +02:00
|
|
|
fn from(f: F) -> FnService<F, Req, Resp, Err, Fut> {
|
|
|
|
FnService::new(f)
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|