use futures::{Future, IntoFuture}; mod and_then; mod apply; mod fn_service; mod fn_state_service; mod map; mod map_err; mod map_init_err; pub use self::and_then::{AndThen, AndThenNewService}; pub use self::apply::{Apply, ApplyNewService}; pub use self::fn_service::{FnNewService, FnService}; pub use self::fn_state_service::{FnStateNewService, FnStateService}; pub use self::map::{Map, MapNewService}; pub use self::map_err::{MapErr, MapErrNewService}; pub use self::map_init_err::MapInitErr; use {NewService, Service}; pub trait ServiceExt: Service { fn apply(self, f: F) -> Apply where Self: Sized, Self::Error: Into<::Error>, F: Fn(Req, &mut Self) -> R, R: IntoFuture, { Apply::new(f, self) } fn and_then(self, service: F) -> AndThen where Self: Sized, F: IntoService, B: Service, { AndThen::new(self, service.into_service()) } fn map(self, f: F) -> Map where Self: Sized, F: Fn(Self::Response) -> R, { Map::new(self, f) } fn map_err(self, f: F) -> MapErr where Self: Sized, F: Fn(Self::Error) -> E, { MapErr::new(self, f) } } pub trait NewServiceExt: NewService { fn apply(self, f: F) -> ApplyNewService where Self: Sized, Self::Error: Into<::Error>, F: Fn(Req, &mut Self::Service) -> R + Clone, R: IntoFuture, { ApplyNewService::new(f, self) } fn and_then(self, new_service: F) -> AndThenNewService where Self: Sized, F: IntoNewService, B: NewService< Request = Self::Response, Error = Self::Error, InitError = Self::InitError, >, { AndThenNewService::new(self, new_service) } fn map(self, f: F) -> MapNewService where Self: Sized, F: Fn(Self::Response) -> R, { MapNewService::new(self, f) } fn map_err(self, f: F) -> MapErrNewService where Self: Sized, F: Fn(Self::Error) -> E, { MapErrNewService::new(self, f) } fn map_init_err(self, f: F) -> MapInitErr where Self: Sized, F: Fn(Self::InitError) -> E, { MapInitErr::new(self, f) } } impl ServiceExt for T where T: Service {} impl NewServiceExt for T where T: NewService {} /// Trait for types that can be converted to a `Service` pub trait IntoService where T: Service, { /// Convert to a `Service` fn into_service(self) -> T; } /// Trait for types that can be converted to a Service pub trait IntoNewService where T: NewService, { /// Convert to an `NewService` fn into_new_service(self) -> T; } impl IntoService for T where T: Service, { fn into_service(self) -> T { self } } impl IntoNewService for T where T: NewService, { fn into_new_service(self) -> T { self } }