1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-19 17:52:38 +01:00
actix-net/src/service/mod.rs

156 lines
3.5 KiB
Rust
Raw Normal View History

use futures::{Future, IntoFuture};
2018-08-25 18:02:14 +02:00
mod and_then;
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;
2018-08-30 05:20:13 +02:00
mod map_request;
2018-08-25 18:02:14 +02:00
pub use self::and_then::{AndThen, AndThenNewService};
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 05:20:13 +02:00
pub use self::map_request::{MapReq, MapReqNewService};
use {NewService, Service};
2018-08-25 18:02:14 +02:00
2018-08-29 21:20:35 +02:00
pub trait ServiceExt: Service {
fn and_then<F, B>(self, new_service: F) -> AndThen<Self, B>
where
Self: Sized,
F: IntoService<B>,
B: Service<Request = Self::Response, Error = Self::Error>,
{
AndThen::new(self, new_service.into_service())
}
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 {
fn apply<T, F, R, Req, Resp, Err, F2>(
f: F, service: F2,
) -> ApplyNewService<T, F, R, Req, Resp, Err>
where
T: NewService,
F: Fn(Req, &mut T::Service) -> R + Clone,
R: Future<Item = Resp, Error = Err>,
F2: IntoNewService<T>,
{
ApplyNewService::new(f, service.into_new_service())
}
2018-08-25 18:02:14 +02:00
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
where
Self: Sized,
F: IntoNewService<B>,
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)
}
2018-08-30 05:20:13 +02:00
fn map_request<F, R>(self, f: F) -> MapReqNewService<Self, F, R>
where
Self: Sized,
F: Fn(R) -> Self::Request,
{
MapReqNewService::new(self, f)
}
2018-08-25 18:02:14 +02:00
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-08-29 21:20:35 +02:00
impl<T: Service> ServiceExt for T {}
2018-08-25 18:02:14 +02:00
impl<T: NewService> NewServiceExt for T {}
/// Trait for types that can be converted to a Service
pub trait IntoService<T>
where
T: Service,
{
/// Create service
2018-08-29 21:20:35 +02:00
fn into_service(self) -> T;
2018-08-25 18:02:14 +02:00
}
/// Trait for types that can be converted to a Service
pub trait IntoNewService<T>
where
T: NewService,
{
/// Create service
fn into_new_service(self) -> T;
}
impl<T> IntoService<T> for T
where
T: Service,
{
2018-08-29 21:20:35 +02:00
fn into_service(self) -> T {
2018-08-25 18:02:14 +02:00
self
}
}
impl<T> IntoNewService<T> for T
where
T: NewService,
{
fn into_new_service(self) -> T {
self
}
}
impl<F, Req, Resp, Err, Fut> IntoService<FnService<F, Req, Resp, Err, Fut>> for F
where
F: Fn(Req) -> Fut + 'static,
Fut: IntoFuture<Item = Resp, Error = Err>,
{
2018-08-29 21:20:35 +02:00
fn into_service(self) -> FnService<F, Req, Resp, Err, Fut> {
2018-08-25 18:02:14 +02:00
FnService::new(self)
}
}