1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-22 08:07:18 +00:00

add apply() combinator

This commit is contained in:
Nikolay Kim 2018-08-30 17:46:11 -07:00
parent fbd17e43f3
commit 7ff24863ab
2 changed files with 24 additions and 4 deletions

View file

@ -1,7 +1,7 @@
use std::marker::PhantomData;
use futures::{Async, Future, Poll};
use {NewService, Service, IntoNewService};
use {IntoNewService, NewService, Service};
/// `ApplyService` service combinator
pub struct ApplyService<T, F, R, Req, Resp, Err> {

View file

@ -1,4 +1,4 @@
use futures::IntoFuture;
use futures::{Future, IntoFuture};
mod and_then;
mod apply;
@ -20,13 +20,23 @@ pub use self::map_request::{MapReq, MapReqNewService};
use {NewService, Service};
pub trait ServiceExt: Service {
fn and_then<F, B>(self, new_service: F) -> AndThen<Self, B>
fn apply<F, R, Req, Resp, Err>(self, f: F) -> ApplyService<Self, F, R, Req, Resp, Err>
where
Self: Sized,
Self::Error: Into<Err>,
F: Fn(Req, &mut Self) -> R,
R: Future<Item = Resp, Error = Err>,
{
ApplyService::new(f, self)
}
fn and_then<F, B>(self, 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())
AndThen::new(self, service.into_service())
}
fn map<F, R>(self, f: F) -> Map<Self, F, R>
@ -47,6 +57,16 @@ pub trait ServiceExt: Service {
}
pub trait NewServiceExt: NewService {
fn apply<F, R, Req, Resp, Err>(self, f: F) -> Apply<Self, F, R, Req, Resp, Err>
where
Self: Sized,
Self::Error: Into<Err>,
F: Fn(Req, &mut Self::Service) -> R + Clone,
R: Future<Item = Resp, Error = Err>,
{
Apply::new(f, self)
}
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
where
Self: Sized,