mod and_then;
mod either;
mod map_err;
mod map_request;
mod map_response;
mod map_result;
mod service_fn;
mod then;
pub mod backoff;
pub mod rng;
pub use self::{
and_then::{AndThen, AndThenLayer},
either::Either,
map_err::{MapErr, MapErrLayer},
map_request::{MapRequest, MapRequestLayer},
map_response::{MapResponse, MapResponseLayer},
map_result::{MapResult, MapResultLayer},
service_fn::{service_fn, ServiceFn},
then::{Then, ThenLayer},
};
use std::future::Future;
use crate::layer::util::Identity;
pub trait ServiceExt<Request>: tower_async_service::Service<Request> {
fn oneshot(
self,
req: Request,
) -> impl std::future::Future<Output = Result<Self::Response, Self::Error>>
where
Self: Sized,
{
async move { self.call(req).await }
}
fn and_then<F>(self, f: F) -> AndThen<Self, F>
where
Self: Sized,
F: Clone,
{
AndThen::new(self, f)
}
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
where
Self: Sized,
F: Fn(Self::Response) -> Response,
{
MapResponse::new(self, f)
}
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
where
Self: Sized,
F: Fn(Self::Error) -> Error,
{
MapErr::new(self, f)
}
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
where
Self: Sized,
Error: From<Self::Error>,
F: Fn(Result<Self::Response, Self::Error>) -> Result<Response, Error>,
{
MapResult::new(self, f)
}
fn map_request<F, NewRequest>(self, f: F) -> MapRequest<Self, F>
where
Self: Sized,
F: Fn(NewRequest) -> Request,
{
MapRequest::new(self, f)
}
#[cfg(feature = "filter")]
fn filter<F, NewRequest>(self, filter: F) -> crate::filter::Filter<Self, F>
where
Self: Sized,
F: crate::filter::Predicate<NewRequest>,
{
crate::filter::Filter::new(self, filter)
}
#[cfg(feature = "filter")]
fn filter_async<F, NewRequest>(self, filter: F) -> crate::filter::AsyncFilter<Self, F>
where
Self: Sized,
F: crate::filter::AsyncPredicate<NewRequest>,
{
crate::filter::AsyncFilter::new(self, filter)
}
fn then<F, Response, Error, Fut>(self, f: F) -> Then<Self, F>
where
Self: Sized,
Error: From<Self::Error>,
F: Fn(Result<Self::Response, Self::Error>) -> Fut,
Fut: Future<Output = Result<Response, Error>>,
{
Then::new(self, f)
}
}
impl<T: ?Sized, Request> ServiceExt<Request> for T where T: tower_async_service::Service<Request> {}
pub fn option_layer<L>(layer: Option<L>) -> Either<L, Identity> {
if let Some(layer) = layer {
Either::Left(layer)
} else {
Either::Right(Identity::new())
}
}