mirror of
https://github.com/actix/actix-web.git
synced 2024-12-22 08:07:18 +00:00
simplify Apply combinator
This commit is contained in:
parent
c2d73873cc
commit
27af05de9a
2 changed files with 42 additions and 63 deletions
|
@ -1,24 +1,21 @@
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use futures::{Async, Future, Poll};
|
use futures::{Async, Future, IntoFuture, Poll};
|
||||||
use {IntoNewService, NewService, Service};
|
use {IntoNewService, NewService, Service};
|
||||||
|
|
||||||
/// `ApplyService` service combinator
|
/// `Apply` service combinator
|
||||||
pub struct Apply<T, F, R, Req, Resp, Err> {
|
pub struct Apply<T, F, R, Req> {
|
||||||
service: T,
|
service: T,
|
||||||
f: F,
|
f: F,
|
||||||
r: PhantomData<R>,
|
r: PhantomData<Fn(Req) -> R>,
|
||||||
r1: PhantomData<Req>,
|
|
||||||
r2: PhantomData<Resp>,
|
|
||||||
e: PhantomData<Err>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, R, Req, Resp, Err> Apply<T, F, R, Req, Resp, Err>
|
impl<T, F, R, Req> Apply<T, F, R, Req>
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Service,
|
||||||
T::Error: Into<Err>,
|
T::Error: Into<<R::Future as Future>::Error>,
|
||||||
F: Fn(Req, &mut T) -> R,
|
F: Fn(Req, &mut T) -> R,
|
||||||
R: Future<Item = Resp, Error = Err>,
|
R: IntoFuture,
|
||||||
{
|
{
|
||||||
/// Create new `Apply` combinator
|
/// Create new `Apply` combinator
|
||||||
pub fn new(f: F, service: T) -> Self {
|
pub fn new(f: F, service: T) -> Self {
|
||||||
|
@ -26,49 +23,43 @@ where
|
||||||
service,
|
service,
|
||||||
f,
|
f,
|
||||||
r: PhantomData,
|
r: PhantomData,
|
||||||
r1: PhantomData,
|
|
||||||
r2: PhantomData,
|
|
||||||
e: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, R, Req, Resp, Err> Service for Apply<T, F, R, Req, Resp, Err>
|
impl<T, F, R, Req> Service for Apply<T, F, R, Req>
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Service,
|
||||||
T::Error: Into<Err>,
|
T::Error: Into<<R::Future as Future>::Error>,
|
||||||
F: Fn(Req, &mut T) -> R,
|
F: Fn(Req, &mut T) -> R,
|
||||||
R: Future<Item = Resp, Error = Err>,
|
R: IntoFuture,
|
||||||
{
|
{
|
||||||
type Request = Req;
|
type Request = Req;
|
||||||
type Response = Resp;
|
type Response = <R::Future as Future>::Item;
|
||||||
type Error = Err;
|
type Error = <R::Future as Future>::Error;
|
||||||
type Future = R;
|
type Future = R::Future;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
self.service.poll_ready().map_err(|e| e.into())
|
self.service.poll_ready().map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||||
(self.f)(req, &mut self.service)
|
(self.f)(req, &mut self.service).into_future()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `ApplyNewService` new service combinator
|
/// `ApplyNewService` new service combinator
|
||||||
pub struct ApplyNewService<T, F, R, Req, Resp, Err> {
|
pub struct ApplyNewService<T, F, R, Req> {
|
||||||
service: T,
|
service: T,
|
||||||
f: F,
|
f: F,
|
||||||
r: PhantomData<R>,
|
r: PhantomData<Fn(Req) -> R>,
|
||||||
r1: PhantomData<Req>,
|
|
||||||
r2: PhantomData<Resp>,
|
|
||||||
e: PhantomData<Err>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, R, Req, Resp, Err> ApplyNewService<T, F, R, Req, Resp, Err>
|
impl<T, F, R, Req> ApplyNewService<T, F, R, Req>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: NewService,
|
||||||
F: Fn(Req, &mut T::Service) -> R,
|
F: Fn(Req, &mut T::Service) -> R,
|
||||||
R: Future<Item = Resp, Error = Err>,
|
R: IntoFuture,
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new `ApplyNewService` new service instance
|
||||||
pub fn new<F1: IntoNewService<T>>(f: F, service: F1) -> Self {
|
pub fn new<F1: IntoNewService<T>>(f: F, service: F1) -> Self {
|
||||||
|
@ -76,91 +67,79 @@ where
|
||||||
f,
|
f,
|
||||||
service: service.into_new_service(),
|
service: service.into_new_service(),
|
||||||
r: PhantomData,
|
r: PhantomData,
|
||||||
r1: PhantomData,
|
|
||||||
r2: PhantomData,
|
|
||||||
e: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, R, Req, Resp, Err> Clone for ApplyNewService<T, F, R, Req, Resp, Err>
|
impl<T, F, R, Req> Clone for ApplyNewService<T, F, R, Req>
|
||||||
where
|
where
|
||||||
T: NewService + Clone,
|
T: NewService + Clone,
|
||||||
F: Fn(Req, &mut T::Service) -> R + Clone,
|
F: Fn(Req, &mut T::Service) -> R + Clone,
|
||||||
R: Future<Item = Resp, Error = Err>,
|
R: IntoFuture,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
service: self.service.clone(),
|
service: self.service.clone(),
|
||||||
f: self.f.clone(),
|
f: self.f.clone(),
|
||||||
r: PhantomData,
|
r: PhantomData,
|
||||||
r1: PhantomData,
|
|
||||||
r2: PhantomData,
|
|
||||||
e: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, R, Req, Resp, Err> NewService for ApplyNewService<T, F, R, Req, Resp, Err>
|
impl<T, F, R, Req> NewService for ApplyNewService<T, F, R, Req>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: NewService,
|
||||||
T::Error: Into<Err>,
|
T::Error: Into<<R::Future as Future>::Error>,
|
||||||
F: Fn(Req, &mut T::Service) -> R + Clone,
|
F: Fn(Req, &mut T::Service) -> R + Clone,
|
||||||
R: Future<Item = Resp, Error = Err>,
|
R: IntoFuture,
|
||||||
{
|
{
|
||||||
type Request = Req;
|
type Request = Req;
|
||||||
type Response = Resp;
|
type Response = <R::Future as Future>::Item;
|
||||||
type Error = Err;
|
type Error = <R::Future as Future>::Error;
|
||||||
type Service = Apply<T::Service, F, R, Req, Resp, Err>;
|
type Service = Apply<T::Service, F, R, Req>;
|
||||||
|
|
||||||
type InitError = T::InitError;
|
type InitError = T::InitError;
|
||||||
type Future = ApplyNewServiceFuture<T, F, R, Req, Resp, Err>;
|
type Future = ApplyNewServiceFuture<T, F, R, Req>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone())
|
ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ApplyNewServiceFuture<T, F, R, Req, Resp, Err>
|
pub struct ApplyNewServiceFuture<T, F, R, Req>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: NewService,
|
||||||
F: Fn(Req, &mut T::Service) -> R,
|
F: Fn(Req, &mut T::Service) -> R,
|
||||||
R: Future<Item = Resp, Error = Err>,
|
R: IntoFuture,
|
||||||
{
|
{
|
||||||
fut: T::Future,
|
fut: T::Future,
|
||||||
f: Option<F>,
|
f: Option<F>,
|
||||||
r: PhantomData<R>,
|
r: PhantomData<Fn(Req) -> R>,
|
||||||
r1: PhantomData<Req>,
|
|
||||||
r2: PhantomData<Resp>,
|
|
||||||
e: PhantomData<Err>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, R, Req, Resp, Err> ApplyNewServiceFuture<T, F, R, Req, Resp, Err>
|
impl<T, F, R, Req> ApplyNewServiceFuture<T, F, R, Req>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: NewService,
|
||||||
F: Fn(Req, &mut T::Service) -> R,
|
F: Fn(Req, &mut T::Service) -> R,
|
||||||
R: Future<Item = Resp, Error = Err>,
|
R: IntoFuture,
|
||||||
{
|
{
|
||||||
fn new(fut: T::Future, f: F) -> Self {
|
fn new(fut: T::Future, f: F) -> Self {
|
||||||
ApplyNewServiceFuture {
|
ApplyNewServiceFuture {
|
||||||
f: Some(f),
|
f: Some(f),
|
||||||
fut,
|
fut,
|
||||||
r: PhantomData,
|
r: PhantomData,
|
||||||
r1: PhantomData,
|
|
||||||
r2: PhantomData,
|
|
||||||
e: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, R, Req, Resp, Err> Future for ApplyNewServiceFuture<T, F, R, Req, Resp, Err>
|
impl<T, F, R, Req> Future for ApplyNewServiceFuture<T, F, R, Req>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: NewService,
|
||||||
T::Error: Into<Err>,
|
T::Error: Into<<R::Future as Future>::Error>,
|
||||||
F: Fn(Req, &mut T::Service) -> R,
|
F: Fn(Req, &mut T::Service) -> R,
|
||||||
R: Future<Item = Resp, Error = Err>,
|
R: IntoFuture,
|
||||||
{
|
{
|
||||||
type Item = Apply<T::Service, F, R, Req, Resp, Err>;
|
type Item = Apply<T::Service, F, R, Req>;
|
||||||
type Error = T::InitError;
|
type Error = T::InitError;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
|
|
@ -20,12 +20,12 @@ pub use self::map_request::{MapReq, MapReqNewService};
|
||||||
use {NewService, Service};
|
use {NewService, Service};
|
||||||
|
|
||||||
pub trait ServiceExt: Service {
|
pub trait ServiceExt: Service {
|
||||||
fn apply<F, R, Req, Resp, Err>(self, f: F) -> Apply<Self, F, R, Req, Resp, Err>
|
fn apply<F, R, Req>(self, f: F) -> Apply<Self, F, R, Req>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
Self::Error: Into<Err>,
|
Self::Error: Into<<R::Future as Future>::Error>,
|
||||||
F: Fn(Req, &mut Self) -> R,
|
F: Fn(Req, &mut Self) -> R,
|
||||||
R: Future<Item = Resp, Error = Err>,
|
R: IntoFuture,
|
||||||
{
|
{
|
||||||
Apply::new(f, self)
|
Apply::new(f, self)
|
||||||
}
|
}
|
||||||
|
@ -57,12 +57,12 @@ pub trait ServiceExt: Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait NewServiceExt: NewService {
|
pub trait NewServiceExt: NewService {
|
||||||
fn apply<F, R, Req, Resp, Err>(self, f: F) -> ApplyNewService<Self, F, R, Req, Resp, Err>
|
fn apply<F, R, Req>(self, f: F) -> ApplyNewService<Self, F, R, Req>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
Self::Error: Into<Err>,
|
Self::Error: Into<<R::Future as Future>::Error>,
|
||||||
F: Fn(Req, &mut Self::Service) -> R + Clone,
|
F: Fn(Req, &mut Self::Service) -> R + Clone,
|
||||||
R: Future<Item = Resp, Error = Err>,
|
R: IntoFuture,
|
||||||
{
|
{
|
||||||
ApplyNewService::new(f, self)
|
ApplyNewService::new(f, self)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue