2018-08-31 02:46:11 +02:00
|
|
|
use futures::{Future, IntoFuture};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2018-09-11 18:30:22 +02:00
|
|
|
/// re-export for convinience
|
|
|
|
pub use tower_service::{NewService, Service};
|
|
|
|
|
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-09-04 21:07:13 +02:00
|
|
|
mod from_err;
|
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-09-04 21:07:13 +02:00
|
|
|
pub use self::from_err::FromErr;
|
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-09-12 22:34:53 +02:00
|
|
|
/// An extension trait for `Service`s that provides a variety of convenient
|
|
|
|
/// adapters
|
2018-08-29 21:20:35 +02:00
|
|
|
pub trait ServiceExt: Service {
|
2018-09-18 00:53:41 +02:00
|
|
|
/// Apply function to specified service and use it as a next service in chain.
|
|
|
|
fn apply<S, F, R>(self, service: S, f: F) -> AndThen<Self, Apply<S, F, R, Self::Response>>
|
2018-08-31 02:46:11 +02:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2018-09-18 00:53:41 +02:00
|
|
|
S: Service,
|
|
|
|
S::Error: Into<<R::Future as Future>::Error>,
|
|
|
|
F: Fn(Self::Response, &mut S) -> R,
|
|
|
|
R: IntoFuture<Error = Self::Error>,
|
2018-08-31 02:46:11 +02:00
|
|
|
{
|
2018-09-18 00:53:41 +02:00
|
|
|
self.and_then(Apply::new(service, f))
|
2018-08-31 02:46:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 22:34:53 +02:00
|
|
|
/// Call another service after call to this one has resolved successfully.
|
|
|
|
///
|
|
|
|
/// This function can be used to chain two services together and ensure that
|
|
|
|
/// the second service isn't called until call to the fist service have
|
|
|
|
/// finished. Result of the call to the first service is used as an
|
|
|
|
/// input parameter for the second service's call.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving service and returns a
|
|
|
|
/// wrapped version of it.
|
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:49:21 +02:00
|
|
|
F: IntoService<B>,
|
2018-08-29 21:20:35 +02:00
|
|
|
B: Service<Request = Self::Response, Error = Self::Error>,
|
|
|
|
{
|
2018-09-04 18:49:21 +02:00
|
|
|
AndThen::new(self, service.into_service())
|
2018-08-29 21:20:35 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 22:34:53 +02:00
|
|
|
/// Map this service's error to any error implementing `From` for
|
|
|
|
/// this service`s `Error`.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving service and returns a
|
|
|
|
/// wrapped version of it.
|
2018-09-04 21:07:13 +02:00
|
|
|
fn from_err<E>(self) -> FromErr<Self, E>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
E: From<Self::Error>,
|
|
|
|
{
|
|
|
|
FromErr::new(self)
|
|
|
|
}
|
|
|
|
|
2018-09-12 22:34:53 +02:00
|
|
|
/// Map this service's output to a different type, returning a new service
|
|
|
|
/// of the resulting type.
|
|
|
|
///
|
|
|
|
/// This function is similar to the `Option::map` or `Iterator::map` where
|
|
|
|
/// it will change the type of the underlying service.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving service and returns a
|
|
|
|
/// wrapped version of it, similar to the existing `map` methods in the
|
|
|
|
/// standard library.
|
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)
|
|
|
|
}
|
|
|
|
|
2018-09-12 22:34:53 +02:00
|
|
|
/// Map this service's error to a different error, returning a new service.
|
|
|
|
///
|
|
|
|
/// This function is similar to the `Result::map_err` where it will change
|
|
|
|
/// the error type of the underlying service. This is useful for example to
|
|
|
|
/// ensure that services have the same error type.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving service and returns a
|
|
|
|
/// wrapped version of it.
|
2018-08-29 21:20:35 +02:00
|
|
|
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-09-18 00:53:41 +02:00
|
|
|
fn apply<S, F, R>(
|
|
|
|
self, service: S, f: F,
|
|
|
|
) -> AndThenNewService<Self, ApplyNewService<S, F, R, Self::Response>>
|
2018-08-31 02:46:11 +02:00
|
|
|
where
|
|
|
|
Self: Sized,
|
2018-09-18 00:53:41 +02:00
|
|
|
S: NewService<InitError = Self::InitError>,
|
|
|
|
S::Error: Into<<R::Future as Future>::Error>,
|
|
|
|
F: Fn(Self::Response, &mut S::Service) -> R + Clone,
|
|
|
|
R: IntoFuture<Error = Self::Error>,
|
2018-08-31 02:46:11 +02:00
|
|
|
{
|
2018-09-18 00:53:41 +02:00
|
|
|
self.and_then(ApplyNewService::new(service, f))
|
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:49:21 +02:00
|
|
|
F: IntoNewService<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:57:47 +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:57:47 +02:00
|
|
|
/// Trait for types that can be converted to a `Service`
|
2018-09-04 18:49:21 +02:00
|
|
|
pub trait IntoService<T>
|
|
|
|
where
|
|
|
|
T: Service,
|
|
|
|
{
|
2018-09-04 18:57:47 +02:00
|
|
|
/// Convert to a `Service`
|
2018-09-04 18:49:21 +02:00
|
|
|
fn into_service(self) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait for types that can be converted to a Service
|
|
|
|
pub trait IntoNewService<T>
|
|
|
|
where
|
|
|
|
T: NewService,
|
|
|
|
{
|
2018-09-04 18:57:47 +02:00
|
|
|
/// Convert to an `NewService`
|
2018-09-04 18:49:21 +02:00
|
|
|
fn into_new_service(self) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoService<T> for T
|
|
|
|
where
|
|
|
|
T: Service,
|
|
|
|
{
|
|
|
|
fn into_service(self) -> T {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoNewService<T> for T
|
|
|
|
where
|
|
|
|
T: NewService,
|
|
|
|
{
|
|
|
|
fn into_new_service(self) -> T {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|