1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 19:12:56 +01:00

revert generic Request change

This commit is contained in:
Nikolay Kim 2019-03-09 06:36:23 -08:00
parent 2099629fe3
commit 6bbbdba921
16 changed files with 563 additions and 485 deletions

View File

@ -1,3 +1,5 @@
use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll}; use futures::{try_ready, Async, Future, Poll};
use super::{IntoNewService, NewService, Service}; use super::{IntoNewService, NewService, Service};
@ -14,10 +16,10 @@ pub struct AndThen<A, B> {
impl<A, B> AndThen<A, B> { impl<A, B> AndThen<A, B> {
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<R>(a: A, b: B) -> Self pub fn new(a: A, b: B) -> Self
where where
A: Service<R>, A: Service,
B: Service<A::Response, Error = A::Error>, B: Service<Request = A::Response, Error = A::Error>,
{ {
Self { a, b: Cell::new(b) } Self { a, b: Cell::new(b) }
} }
@ -35,39 +37,40 @@ where
} }
} }
impl<A, B, R> Service<R> for AndThen<A, B> impl<A, B> Service for AndThen<A, B>
where where
A: Service<R>, A: Service,
B: Service<A::Response, Error = A::Error>, B: Service<Request = A::Response, Error = A::Error>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Future = AndThenFuture<A, B, R>; type Future = AndThenFuture<A, B>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.poll_ready()); try_ready!(self.a.poll_ready());
self.b.get_mut().poll_ready() self.b.get_mut().poll_ready()
} }
fn call(&mut self, req: R) -> Self::Future { fn call(&mut self, req: A::Request) -> Self::Future {
AndThenFuture::new(self.a.call(req), self.b.clone()) AndThenFuture::new(self.a.call(req), self.b.clone())
} }
} }
pub struct AndThenFuture<A, B, R> pub struct AndThenFuture<A, B>
where where
A: Service<R>, A: Service,
B: Service<A::Response, Error = A::Error>, B: Service<Request = A::Response, Error = A::Error>,
{ {
b: Cell<B>, b: Cell<B>,
fut_b: Option<B::Future>, fut_b: Option<B::Future>,
fut_a: Option<A::Future>, fut_a: Option<A::Future>,
} }
impl<A, B, R> AndThenFuture<A, B, R> impl<A, B> AndThenFuture<A, B>
where where
A: Service<R>, A: Service,
B: Service<A::Response, Error = A::Error>, B: Service<Request = A::Response, Error = A::Error>,
{ {
fn new(a: A::Future, b: Cell<B>) -> Self { fn new(a: A::Future, b: Cell<B>) -> Self {
AndThenFuture { AndThenFuture {
@ -78,10 +81,10 @@ where
} }
} }
impl<A, B, R> Future for AndThenFuture<A, B, R> impl<A, B> Future for AndThenFuture<A, B>
where where
A: Service<R>, A: Service,
B: Service<A::Response, Error = A::Error>, B: Service<Request = A::Response, Error = A::Error>,
{ {
type Item = B::Response; type Item = B::Response;
type Error = A::Error; type Error = A::Error;
@ -104,43 +107,46 @@ where
} }
/// `AndThenNewService` new service combinator /// `AndThenNewService` new service combinator
pub struct AndThenNewService<A, B> { pub struct AndThenNewService<A, B, C> {
a: A, a: A,
b: B, b: B,
_t: PhantomData<C>,
} }
impl<A, B> AndThenNewService<A, B> { impl<A, B, C> AndThenNewService<A, B, C> {
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<R, C, F: IntoNewService<B, A::Response, C>>(a: A, f: F) -> Self pub fn new<F: IntoNewService<B, C>>(a: A, f: F) -> Self
where where
A: NewService<R, C>, A: NewService<C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>, B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{ {
Self { Self {
a, a,
b: f.into_new_service(), b: f.into_new_service(),
_t: PhantomData,
} }
} }
} }
impl<A, B, R, C> NewService<R, C> for AndThenNewService<A, B> impl<A, B, C> NewService<C> for AndThenNewService<A, B, C>
where where
A: NewService<R, C>, A: NewService<C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>, B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Service = AndThen<A::Service, B::Service>; type Service = AndThen<A::Service, B::Service>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = AndThenNewServiceFuture<A, B, R, C>; type Future = AndThenNewServiceFuture<A, B, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg))
} }
} }
impl<A, B> Clone for AndThenNewService<A, B> impl<A, B, C> Clone for AndThenNewService<A, B, C>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
@ -149,14 +155,15 @@ where
Self { Self {
a: self.a.clone(), a: self.a.clone(),
b: self.b.clone(), b: self.b.clone(),
_t: PhantomData,
} }
} }
} }
pub struct AndThenNewServiceFuture<A, B, R, C> pub struct AndThenNewServiceFuture<A, B, C>
where where
A: NewService<R, C>, A: NewService<C>,
B: NewService<A::Response, C>, B: NewService<C, Request = A::Response>,
{ {
fut_b: B::Future, fut_b: B::Future,
fut_a: A::Future, fut_a: A::Future,
@ -164,10 +171,10 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B, R, C> AndThenNewServiceFuture<A, B, R, C> impl<A, B, C> AndThenNewServiceFuture<A, B, C>
where where
A: NewService<R, C>, A: NewService<C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>, B: NewService<C, Request = A::Response>,
{ {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self { fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
AndThenNewServiceFuture { AndThenNewServiceFuture {
@ -179,10 +186,10 @@ where
} }
} }
impl<A, B, R, C> Future for AndThenNewServiceFuture<A, B, R, C> impl<A, B, C> Future for AndThenNewServiceFuture<A, B, C>
where where
A: NewService<R, C>, A: NewService<C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>, B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{ {
type Item = AndThen<A::Service, B::Service>; type Item = AndThen<A::Service, B::Service>;
type Error = A::InitError; type Error = A::InitError;
@ -222,7 +229,8 @@ mod tests {
use crate::{NewService, Service, ServiceExt}; use crate::{NewService, Service, ServiceExt};
struct Srv1(Rc<Cell<usize>>); struct Srv1(Rc<Cell<usize>>);
impl Service<&'static str> for Srv1 { impl Service for Srv1 {
type Request = &'static str;
type Response = &'static str; type Response = &'static str;
type Error = (); type Error = ();
type Future = FutureResult<Self::Response, ()>; type Future = FutureResult<Self::Response, ()>;
@ -240,7 +248,8 @@ mod tests {
#[derive(Clone)] #[derive(Clone)]
struct Srv2(Rc<Cell<usize>>); struct Srv2(Rc<Cell<usize>>);
impl Service<&'static str> for Srv2 { impl Service for Srv2 {
type Request = &'static str;
type Response = (&'static str, &'static str); type Response = (&'static str, &'static str);
type Error = (); type Error = ();
type Future = FutureResult<Self::Response, ()>; type Future = FutureResult<Self::Response, ()>;

View File

@ -1,4 +1,3 @@
use std::marker::PhantomData;
use std::rc::Rc; use std::rc::Rc;
use futures::{Async, Future, Poll}; use futures::{Async, Future, Poll};
@ -8,22 +7,22 @@ use crate::from_err::FromErr;
use crate::{NewService, Transform}; use crate::{NewService, Transform};
/// `Apply` new service combinator /// `Apply` new service combinator
pub struct AndThenTransform<T, A, B, BR> { pub struct AndThenTransform<T, A, B, C> {
a: A, a: A,
b: B, b: B,
t: Rc<T>, t: Rc<T>,
_t: PhantomData<BR>, _t: std::marker::PhantomData<C>,
} }
impl<T, A, B, BR> AndThenTransform<T, A, B, BR> { impl<T, A, B, C> AndThenTransform<T, A, B, C>
where
A: NewService<C>,
B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<AR, C>(t: T, a: A, b: B) -> Self pub fn new(t: T, a: A, b: B) -> Self {
where
A: NewService<AR, C>,
B: NewService<BR, C, InitError = A::InitError>,
T: Transform<B::Service, A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
Self { Self {
a, a,
b, b,
@ -33,7 +32,7 @@ impl<T, A, B, BR> AndThenTransform<T, A, B, BR> {
} }
} }
impl<T, A, B, BR> Clone for AndThenTransform<T, A, B, BR> impl<T, A, B, C> Clone for AndThenTransform<T, A, B, C>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
@ -48,19 +47,20 @@ where
} }
} }
impl<T, A, B, AR, BR, C> NewService<AR, C> for AndThenTransform<T, A, B, BR> impl<T, A, B, C> NewService<C> for AndThenTransform<T, A, B, C>
where where
A: NewService<AR, C>, A: NewService<C>,
B: NewService<BR, C, InitError = A::InitError>, B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, A::Response, InitError = A::InitError>, T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
type Request = A::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type InitError = T::InitError; type InitError = T::InitError;
type Service = AndThen<FromErr<A::Service, T::Error>, T::Transform>; type Service = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
type Future = AndThenTransformFuture<T, A, B, AR, BR, C>; type Future = AndThenTransformFuture<T, A, B, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
AndThenTransformFuture { AndThenTransformFuture {
@ -74,11 +74,11 @@ where
} }
} }
pub struct AndThenTransformFuture<T, A, B, AR, BR, C> pub struct AndThenTransformFuture<T, A, B, C>
where where
A: NewService<AR, C>, A: NewService<C>,
B: NewService<BR, C, InitError = A::InitError>, B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, A::Response, InitError = A::InitError>, T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
fut_a: A::Future, fut_a: A::Future,
@ -89,11 +89,11 @@ where
t_cell: Rc<T>, t_cell: Rc<T>,
} }
impl<T, A, B, AR, BR, C> Future for AndThenTransformFuture<T, A, B, AR, BR, C> impl<T, A, B, C> Future for AndThenTransformFuture<T, A, B, C>
where where
A: NewService<AR, C>, A: NewService<C>,
B: NewService<BR, C, InitError = A::InitError>, B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, A::Response, InitError = A::InitError>, T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
type Item = AndThen<FromErr<A::Service, T::Error>, T::Transform>; type Item = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
@ -138,7 +138,8 @@ mod tests {
#[derive(Clone)] #[derive(Clone)]
struct Srv; struct Srv;
impl Service<()> for Srv { impl Service for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;

View File

@ -6,23 +6,30 @@ use super::{IntoNewService, IntoService, NewService, Service};
use crate::cell::Cell; use crate::cell::Cell;
/// `Apply` service combinator /// `Apply` service combinator
pub struct AndThenApply<A, B, F, Out, AReq, BReq> { pub struct AndThenApply<A, B, F, Out>
where
A: Service,
B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
a: A, a: A,
b: Cell<B>, b: Cell<B>,
f: Cell<F>, f: Cell<F>,
r: PhantomData<(Out, AReq, BReq)>, r: PhantomData<(Out,)>,
} }
impl<A, B, F, Out, AReq, BReq> AndThenApply<A, B, F, Out, AReq, BReq> impl<A, B, F, Out> AndThenApply<A, B, F, Out>
where where
A: Service<AReq>, A: Service,
B: Service<BReq, Error = A::Error>, B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out, F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
/// Create new `Apply` combinator /// Create new `Apply` combinator
pub fn new<A1: IntoService<A, AReq>, B1: IntoService<B, BReq>>(a: A1, b: B1, f: F) -> Self { pub fn new<A1: IntoService<A>, B1: IntoService<B>>(a: A1, b: B1, f: F) -> Self {
Self { Self {
f: Cell::new(f), f: Cell::new(f),
a: a.into_service(), a: a.into_service(),
@ -32,9 +39,13 @@ where
} }
} }
impl<A, B, F, Out, AReq, BReq> Clone for AndThenApply<A, B, F, Out, AReq, BReq> impl<A, B, F, Out> Clone for AndThenApply<A, B, F, Out>
where where
A: Clone, A: Service + Clone,
B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
AndThenApply { AndThenApply {
@ -46,38 +57,38 @@ where
} }
} }
impl<A, B, F, Out, AReq, BReq> Service<AReq> for AndThenApply<A, B, F, Out, AReq, BReq> impl<A, B, F, Out> Service for AndThenApply<A, B, F, Out>
where where
A: Service<AReq>, A: Service,
B: Service<BReq, Error = A::Error>, B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out, F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
type Request = A::Request;
type Response = Out::Item; type Response = Out::Item;
type Error = A::Error; type Error = A::Error;
type Future = AndThenApplyFuture<A, B, F, Out, AReq, BReq>; type Future = AndThenApplyFuture<A, B, F, Out>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.poll_ready()); try_ready!(self.a.poll_ready());
self.b.get_mut().poll_ready() self.b.get_mut().poll_ready()
} }
fn call(&mut self, req: AReq) -> Self::Future { fn call(&mut self, req: A::Request) -> Self::Future {
AndThenApplyFuture { AndThenApplyFuture {
b: self.b.clone(), b: self.b.clone(),
f: self.f.clone(), f: self.f.clone(),
fut_b: None, fut_b: None,
fut_a: Some(self.a.call(req)), fut_a: Some(self.a.call(req)),
_t: PhantomData,
} }
} }
} }
pub struct AndThenApplyFuture<A, B, F, Out, AReq, BReq> pub struct AndThenApplyFuture<A, B, F, Out>
where where
A: Service<AReq>, A: Service,
B: Service<BReq, Error = A::Error>, B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out, F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
@ -86,13 +97,12 @@ where
f: Cell<F>, f: Cell<F>,
fut_a: Option<A::Future>, fut_a: Option<A::Future>,
fut_b: Option<Out::Future>, fut_b: Option<Out::Future>,
_t: PhantomData<(AReq, BReq)>,
} }
impl<A, B, F, Out, AReq, BReq> Future for AndThenApplyFuture<A, B, F, Out, AReq, BReq> impl<A, B, F, Out> Future for AndThenApplyFuture<A, B, F, Out>
where where
A: Service<AReq>, A: Service,
B: Service<BReq, Error = A::Error>, B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out, F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
@ -119,23 +129,23 @@ where
} }
/// `ApplyNewService` new service combinator /// `ApplyNewService` new service combinator
pub struct AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg> { pub struct AndThenApplyNewService<A, B, F, Out, Cfg> {
a: A, a: A,
b: B, b: B,
f: Cell<F>, f: Cell<F>,
r: PhantomData<(Out, AReq, BReq, Cfg)>, r: PhantomData<(Out, Cfg)>,
} }
impl<A, B, F, Out, AReq, BReq, Cfg> AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg> impl<A, B, F, Out, Cfg> AndThenApplyNewService<A, B, F, Out, Cfg>
where where
A: NewService<AReq, Cfg>, A: NewService<Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>, B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<A1: IntoNewService<A, AReq, Cfg>, B1: IntoNewService<B, BReq, Cfg>>( pub fn new<A1: IntoNewService<A, Cfg>, B1: IntoNewService<B, Cfg>>(
a: A1, a: A1,
b: B1, b: B1,
f: F, f: F,
@ -149,8 +159,7 @@ where
} }
} }
impl<A, B, F, Out, AReq, BReq, Cfg> Clone impl<A, B, F, Out, Cfg> Clone for AndThenApplyNewService<A, B, F, Out, Cfg>
for AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
@ -165,21 +174,21 @@ where
} }
} }
impl<A, B, F, Out, AReq, BReq, Cfg> NewService<AReq, Cfg> impl<A, B, F, Out, Cfg> NewService<Cfg> for AndThenApplyNewService<A, B, F, Out, Cfg>
for AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg>
where where
A: NewService<AReq, Cfg>, A: NewService<Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>, B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
type Request = A::Request;
type Response = Out::Item; type Response = Out::Item;
type Error = A::Error; type Error = A::Error;
type Service = AndThenApply<A::Service, B::Service, F, Out, AReq, BReq>; type Service = AndThenApply<A::Service, B::Service, F, Out>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = AndThenApplyNewServiceFuture<A, B, F, Out, AReq, BReq, Cfg>; type Future = AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future { fn new_service(&self, cfg: &Cfg) -> Self::Future {
AndThenApplyNewServiceFuture { AndThenApplyNewServiceFuture {
@ -192,10 +201,10 @@ where
} }
} }
pub struct AndThenApplyNewServiceFuture<A, B, F, Out, AReq, BReq, Cfg> pub struct AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>
where where
A: NewService<AReq, Cfg>, A: NewService<Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>, B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
@ -207,16 +216,15 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B, F, Out, AReq, BReq, Cfg> Future impl<A, B, F, Out, Cfg> Future for AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>
for AndThenApplyNewServiceFuture<A, B, F, Out, AReq, BReq, Cfg>
where where
A: NewService<AReq, Cfg>, A: NewService<Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>, B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
type Item = AndThenApply<A::Service, B::Service, F, Out, AReq, BReq>; type Item = AndThenApply<A::Service, B::Service, F, Out>;
type Error = A::InitError; type Error = A::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -255,7 +263,8 @@ mod tests {
#[derive(Clone)] #[derive(Clone)]
struct Srv; struct Srv;
impl Service<()> for Srv { impl Service for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;

View File

@ -5,23 +5,24 @@ use futures::{Async, Future, IntoFuture, Poll};
use super::{IntoNewService, IntoService, NewService, Service}; use super::{IntoNewService, IntoService, NewService, Service};
/// `Apply` service combinator /// `Apply` service combinator
pub struct Apply<T, R, F, In, Out> { pub struct Apply<T, F, In, Out>
where
T: Service,
{
service: T, service: T,
f: F, f: F,
r: PhantomData<(R, In, Out)>, r: PhantomData<(In, Out)>,
} }
impl<T, R, F, In, Out> Apply<T, R, F, In, Out> impl<T, F, In, Out> Apply<T, F, In, Out>
where where
T: Service,
F: FnMut(In, &mut T) -> Out, F: FnMut(In, &mut T) -> Out,
Out: IntoFuture,
Out::Error: From<T::Error>,
{ {
/// Create new `Apply` combinator /// Create new `Apply` combinator
pub fn new<I: IntoService<T, R>>(service: I, f: F) -> Self pub fn new<I: IntoService<T>>(service: I, f: F) -> Self {
where
T: Service<R>,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
Self { Self {
service: service.into_service(), service: service.into_service(),
f, f,
@ -30,9 +31,9 @@ where
} }
} }
impl<T, R, F, In, Out> Clone for Apply<T, R, F, In, Out> impl<T, F, In, Out> Clone for Apply<T, F, In, Out>
where where
T: Clone, T: Service + Clone,
F: Clone, F: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
@ -44,13 +45,14 @@ where
} }
} }
impl<T, R, F, In, Out> Service<In> for Apply<T, R, F, In, Out> impl<T, F, In, Out> Service for Apply<T, F, In, Out>
where where
T: Service<R>, T: Service,
F: FnMut(In, &mut T) -> Out, F: FnMut(In, &mut T) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: From<T::Error>, Out::Error: From<T::Error>,
{ {
type Request = In;
type Response = Out::Item; type Response = Out::Item;
type Error = Out::Error; type Error = Out::Error;
type Future = Out::Future; type Future = Out::Future;
@ -65,21 +67,24 @@ where
} }
/// `ApplyNewService` new service combinator /// `ApplyNewService` new service combinator
pub struct ApplyNewService<T, F, In, Out, Req> { pub struct ApplyNewService<T, F, In, Out, Cfg>
where
T: NewService<Cfg>,
{
service: T, service: T,
f: F, f: F,
r: PhantomData<(In, Out, Req)>, r: PhantomData<(In, Out, Cfg)>,
} }
impl<T, F, In, Out, Req> ApplyNewService<T, F, In, Out, Req> { impl<T, F, In, Out, Cfg> ApplyNewService<T, F, In, Out, Cfg>
where
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<Cfg, F1: IntoNewService<T, Req, Cfg>>(service: F1, f: F) -> Self pub fn new<F1: IntoNewService<T, Cfg>>(service: F1, f: F) -> Self {
where
T: NewService<Req, Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
Self { Self {
f, f,
service: service.into_new_service(), service: service.into_new_service(),
@ -88,10 +93,11 @@ impl<T, F, In, Out, Req> ApplyNewService<T, F, In, Out, Req> {
} }
} }
impl<T, F, In, Out, Req> Clone for ApplyNewService<T, F, In, Out, Req> impl<T, F, In, Out, Cfg> Clone for ApplyNewService<T, F, In, Out, Cfg>
where where
T: Clone, T: NewService<Cfg> + Clone,
F: Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
@ -102,28 +108,29 @@ where
} }
} }
impl<T, F, In, Out, Req, Cfg> NewService<In, Cfg> for ApplyNewService<T, F, In, Out, Req> impl<T, F, In, Out, Cfg> NewService<Cfg> for ApplyNewService<T, F, In, Out, Cfg>
where where
T: NewService<Req, Cfg>, T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
Out::Error: From<T::Error>, Out::Error: From<T::Error>,
{ {
type Request = In;
type Response = Out::Item; type Response = Out::Item;
type Error = Out::Error; type Error = Out::Error;
type Service = Apply<T::Service, Req, F, In, Out>; type Service = Apply<T::Service, F, In, Out>;
type InitError = T::InitError; type InitError = T::InitError;
type Future = ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>; type Future = ApplyNewServiceFuture<T, F, In, Out, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future { fn new_service(&self, cfg: &Cfg) -> Self::Future {
ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone()) ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone())
} }
} }
pub struct ApplyNewServiceFuture<T, F, In, Out, Req, Cfg> pub struct ApplyNewServiceFuture<T, F, In, Out, Cfg>
where where
T: NewService<Req, Cfg>, T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
@ -132,9 +139,9 @@ where
r: PhantomData<(In, Out)>, r: PhantomData<(In, Out)>,
} }
impl<T, F, In, Out, Req, Cfg> ApplyNewServiceFuture<T, F, In, Out, Req, Cfg> impl<T, F, In, Out, Cfg> ApplyNewServiceFuture<T, F, In, Out, Cfg>
where where
T: NewService<Req, Cfg>, T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
@ -147,14 +154,14 @@ where
} }
} }
impl<T, F, In, Out, Req, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Req, Cfg> impl<T, F, In, Out, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Cfg>
where where
T: NewService<Req, Cfg>, T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
Out::Error: From<T::Error>, Out::Error: From<T::Error>,
{ {
type Item = Apply<T::Service, Req, F, In, Out>; type Item = Apply<T::Service, F, In, Out>;
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> {
@ -176,7 +183,8 @@ mod tests {
#[derive(Clone)] #[derive(Clone)]
struct Srv; struct Srv;
impl Service<()> for Srv { impl Service for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;

View File

@ -30,7 +30,8 @@ impl<R, E> Default for Blank<R, E> {
} }
} }
impl<R, E> Service<R> for Blank<R, E> { impl<R, E> Service for Blank<R, E> {
type Request = R;
type Response = R; type Response = R;
type Error = E; type Error = E;
type Future = FutureResult<R, E>; type Future = FutureResult<R, E>;
@ -67,7 +68,8 @@ impl<R, E1, E2> Default for BlankNewService<R, E1, E2> {
} }
} }
impl<R, E1, E2> NewService<R, ()> for BlankNewService<R, E1, E2> { impl<R, E1, E2> NewService<()> for BlankNewService<R, E1, E2> {
type Request = R;
type Response = R; type Response = R;
type Error = E1; type Error = E1;
type Service = Blank<R, E1>; type Service = Blank<R, E1>;

View File

@ -1,49 +1,48 @@
use std::marker::PhantomData;
use crate::{NewService, Service}; use crate::{NewService, Service};
use futures::{Future, IntoFuture, Poll}; use futures::{Future, IntoFuture, Poll};
pub type BoxedService<Req, Res, Err> = Box< pub type BoxedService<Req, Res, Err> = Box<
Service<Req, Response = Res, Error = Err, Future = Box<Future<Item = Res, Error = Err>>>, Service<
Request = Req,
Response = Res,
Error = Err,
Future = Box<Future<Item = Res, Error = Err>>,
>,
>; >;
/// Create boxed new service /// Create boxed new service
pub fn new_service<T, R, C>( pub fn new_service<T, C>(
service: T, service: T,
) -> BoxedNewService<C, R, T::Response, T::Error, T::InitError> ) -> BoxedNewService<C, T::Request, T::Response, T::Error, T::InitError>
where where
C: 'static, C: 'static,
T: NewService<R, C> + 'static, T: NewService<C> + 'static,
T::Request: 'static,
T::Response: 'static, T::Response: 'static,
T::Service: 'static, T::Service: 'static,
T::Future: 'static, T::Future: 'static,
T::Error: 'static, T::Error: 'static,
T::InitError: 'static, T::InitError: 'static,
R: 'static,
{ {
BoxedNewService(Box::new(NewServiceWrapper { BoxedNewService(Box::new(NewServiceWrapper {
service, service,
_t: PhantomData, _t: std::marker::PhantomData,
})) }))
} }
/// Create boxed service /// Create boxed service
pub fn service<T, R>(service: T) -> BoxedService<R, T::Response, T::Error> pub fn service<T>(service: T) -> BoxedService<T::Request, T::Response, T::Error>
where where
T: Service<R> + 'static, T: Service + 'static,
T::Future: 'static, T::Future: 'static,
R: 'static,
{ {
Box::new(ServiceWrapper { Box::new(ServiceWrapper(service))
service,
_t: PhantomData,
})
} }
type Inner<C, Req, Res, Err, InitErr> = Box< type Inner<C, Req, Res, Err, InitErr> = Box<
NewService< NewService<
Req,
C, C,
Request = Req,
Response = Res, Response = Res,
Error = Err, Error = Err,
InitError = InitErr, InitError = InitErr,
@ -54,14 +53,14 @@ type Inner<C, Req, Res, Err, InitErr> = Box<
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>); pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
impl<C, Req, Res, Err, InitErr> NewService<Req, C> impl<C, Req, Res, Err, InitErr> NewService<C> for BoxedNewService<C, Req, Res, Err, InitErr>
for BoxedNewService<C, Req, Res, Err, InitErr>
where where
Req: 'static, Req: 'static,
Res: 'static, Res: 'static,
Err: 'static, Err: 'static,
InitErr: 'static, InitErr: 'static,
{ {
type Request = Req;
type Response = Res; type Response = Res;
type Error = Err; type Error = Err;
type InitError = InitErr; type InitError = InitErr;
@ -73,22 +72,23 @@ where
} }
} }
struct NewServiceWrapper<T: NewService<R, C>, R, C> { struct NewServiceWrapper<C, T: NewService<C>> {
service: T, service: T,
_t: std::marker::PhantomData<(R, C)>, _t: std::marker::PhantomData<C>,
} }
impl<C, T, Req, Res, Err, InitErr> NewService<Req, C> for NewServiceWrapper<T, Req, C> impl<C, T, Req, Res, Err, InitErr> NewService<C> for NewServiceWrapper<C, T>
where where
Req: 'static, Req: 'static,
Res: 'static, Res: 'static,
Err: 'static, Err: 'static,
InitErr: 'static, InitErr: 'static,
T: NewService<Req, C, Response = Res, Error = Err, InitError = InitErr>, T: NewService<C, Request = Req, Response = Res, Error = Err, InitError = InitErr>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
<T::Service as Service<Req>>::Future: 'static, <T::Service as Service>::Future: 'static,
{ {
type Request = Req;
type Response = Res; type Response = Res;
type Error = Err; type Error = Err;
type InitError = InitErr; type InitError = InitErr;
@ -105,40 +105,33 @@ where
} }
} }
struct ServiceWrapper<T: Service<R>, R> { struct ServiceWrapper<T: Service>(T);
service: T,
_t: PhantomData<R>,
}
impl<T, R> ServiceWrapper<T, R> impl<T> ServiceWrapper<T>
where where
T: Service<R> + 'static, T: Service + 'static,
T::Future: 'static, T::Future: 'static,
R: 'static,
{ {
fn boxed(service: T) -> BoxedService<R, T::Response, T::Error> { fn boxed(service: T) -> BoxedService<T::Request, T::Response, T::Error> {
Box::new(ServiceWrapper { Box::new(ServiceWrapper(service))
service,
_t: PhantomData,
})
} }
} }
impl<T, Req, Res, Err> Service<Req> for ServiceWrapper<T, Req> impl<T, Req, Res, Err> Service for ServiceWrapper<T>
where where
T: Service<Req, Response = Res, Error = Err>, T: Service<Request = Req, Response = Res, Error = Err>,
T::Future: 'static, T::Future: 'static,
Req: 'static,
{ {
type Request = Req;
type Response = Res; type Response = Res;
type Error = Err; type Error = Err;
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>; type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready() self.0.poll_ready()
} }
fn call(&mut self, req: Req) -> Self::Future { fn call(&mut self, req: Self::Request) -> Self::Future {
Box::new(self.service.call(req)) Box::new(self.0.call(req))
} }
} }

View File

@ -15,22 +15,22 @@ where
} }
/// Create `NewService` for function that can produce services /// Create `NewService` for function that can produce services
pub fn fn_factory<F, R, S, E, Req>(f: F) -> FnNewServiceNoConfig<F, R, S, E, Req> pub fn fn_factory<F, R, S, E>(f: F) -> FnNewServiceNoConfig<F, R, S, E>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service<Req>, S: Service,
{ {
FnNewServiceNoConfig::new(f) FnNewServiceNoConfig::new(f)
} }
/// Create `NewService` for function that can produce services with configuration /// Create `NewService` for function that can produce services with configuration
pub fn fn_cfg_factory<F, C, R, S, E, Req>(f: F) -> FnNewServiceConfig<F, C, R, S, E, Req> pub fn fn_cfg_factory<F, C, R, S, E>(f: F) -> FnNewServiceConfig<F, C, R, S, E>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>, R::Item: IntoService<S>,
S: Service<Req>, S: Service,
{ {
FnNewServiceConfig::new(f) FnNewServiceConfig::new(f)
} }
@ -67,11 +67,12 @@ where
} }
} }
impl<F, Req, Out> Service<Req> for FnService<F, Req, Out> impl<F, Req, Out> Service for FnService<F, Req, Out>
where where
F: FnMut(Req) -> Out, F: FnMut(Req) -> Out,
Out: IntoFuture, Out: IntoFuture,
{ {
type Request = Req;
type Response = Out::Item; type Response = Out::Item;
type Error = Out::Error; type Error = Out::Error;
type Future = Out::Future; type Future = Out::Future;
@ -85,7 +86,7 @@ where
} }
} }
impl<F, Req, Out> IntoService<FnService<F, Req, Out>, Req> for F impl<F, Req, Out> IntoService<FnService<F, Req, Out>> for F
where where
F: FnMut(Req) -> Out + 'static, F: FnMut(Req) -> Out + 'static,
Out: IntoFuture, Out: IntoFuture,
@ -114,11 +115,12 @@ where
} }
} }
impl<F, Req, Out, Cfg> NewService<Req, Cfg> for FnNewService<F, Req, Out, Cfg> impl<F, Req, Out, Cfg> NewService<Cfg> for FnNewService<F, Req, Out, Cfg>
where where
F: FnMut(Req) -> Out + Clone, F: FnMut(Req) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
type Request = Req;
type Response = Out::Item; type Response = Out::Item;
type Error = Out::Error; type Error = Out::Error;
type Service = FnService<F, Req, Out>; type Service = FnService<F, Req, Out>;
@ -141,7 +143,7 @@ where
} }
} }
impl<F, Req, Out, Cfg> IntoNewService<FnNewService<F, Req, Out, Cfg>, Req, Cfg> for F impl<F, Req, Out, Cfg> IntoNewService<FnNewService<F, Req, Out, Cfg>, Cfg> for F
where where
F: Fn(Req) -> Out + Clone, F: Fn(Req) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
@ -152,33 +154,33 @@ where
} }
/// Converter for `Fn() -> Future<Service>` fn /// Converter for `Fn() -> Future<Service>` fn
pub struct FnNewServiceNoConfig<F, R, S, E, Req> pub struct FnNewServiceNoConfig<F, R, S, E>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service<Req>, S: Service,
{ {
f: F, f: F,
_t: PhantomData<Req>,
} }
impl<F, R, S, E, Req> FnNewServiceNoConfig<F, R, S, E, Req> impl<F, R, S, E> FnNewServiceNoConfig<F, R, S, E>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service<Req>, S: Service,
{ {
pub fn new(f: F) -> Self { pub fn new(f: F) -> Self {
FnNewServiceNoConfig { f, _t: PhantomData } FnNewServiceNoConfig { f }
} }
} }
impl<F, R, S, E, Req> NewService<Req, ()> for FnNewServiceNoConfig<F, R, S, E, Req> impl<F, R, S, E> NewService<()> for FnNewServiceNoConfig<F, R, S, E>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service<Req>, S: Service,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Service = S; type Service = S;
@ -191,65 +193,66 @@ where
} }
} }
impl<F, R, S, E, Req> Clone for FnNewServiceNoConfig<F, R, S, E, Req> impl<F, R, S, E> Clone for FnNewServiceNoConfig<F, R, S, E>
where where
F: Fn() -> R + Clone, F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service<Req>, S: Service,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self::new(self.f.clone()) Self::new(self.f.clone())
} }
} }
impl<F, R, S, E, Req> IntoNewService<FnNewServiceNoConfig<F, R, S, E, Req>, Req, ()> for F impl<F, R, S, E> IntoNewService<FnNewServiceNoConfig<F, R, S, E>, ()> for F
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service<Req>, S: Service,
{ {
fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E, Req> { fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E> {
FnNewServiceNoConfig::new(self) FnNewServiceNoConfig::new(self)
} }
} }
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService /// Convert `Fn(&Config) -> Future<Service>` fn to NewService
pub struct FnNewServiceConfig<F, C, R, S, E, Req> pub struct FnNewServiceConfig<F, C, R, S, E>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>, R::Item: IntoService<S>,
S: Service<Req>, S: Service,
{ {
f: F, f: F,
_t: PhantomData<(C, R, S, E, Req)>, _t: PhantomData<(C, R, S, E)>,
} }
impl<F, C, R, S, E, Req> FnNewServiceConfig<F, C, R, S, E, Req> impl<F, C, R, S, E> FnNewServiceConfig<F, C, R, S, E>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>, R::Item: IntoService<S>,
S: Service<Req>, S: Service,
{ {
pub fn new(f: F) -> Self { pub fn new(f: F) -> Self {
FnNewServiceConfig { f, _t: PhantomData } FnNewServiceConfig { f, _t: PhantomData }
} }
} }
impl<F, C, R, S, E, Req> NewService<Req, C> for FnNewServiceConfig<F, C, R, S, E, Req> impl<F, C, R, S, E> NewService<C> for FnNewServiceConfig<F, C, R, S, E>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>, R::Item: IntoService<S>,
S: Service<Req>, S: Service,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Service = S; type Service = S;
type InitError = E; type InitError = E;
type Future = FnNewServiceConfigFut<R, S, E, Req>; type Future = FnNewServiceConfigFut<R, S, E>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
FnNewServiceConfigFut { FnNewServiceConfigFut {
@ -259,21 +262,21 @@ where
} }
} }
pub struct FnNewServiceConfigFut<R, S, E, Req> pub struct FnNewServiceConfigFut<R, S, E>
where where
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>, R::Item: IntoService<S>,
S: Service<Req>, S: Service,
{ {
fut: R::Future, fut: R::Future,
_t: PhantomData<(S, Req)>, _t: PhantomData<(S,)>,
} }
impl<R, S, E, Req> Future for FnNewServiceConfigFut<R, S, E, Req> impl<R, S, E> Future for FnNewServiceConfigFut<R, S, E>
where where
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>, R::Item: IntoService<S>,
S: Service<Req>, S: Service,
{ {
type Item = S; type Item = S;
type Error = R::Error; type Error = R::Error;
@ -283,44 +286,26 @@ where
} }
} }
impl<F, C, R, S, E, Req> Clone for FnNewServiceConfig<F, C, R, S, E, Req> impl<F, C, R, S, E> Clone for FnNewServiceConfig<F, C, R, S, E>
where where
F: Fn(&C) -> R + Clone, F: Fn(&C) -> R + Clone,
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>, R::Item: IntoService<S>,
S: Service<Req>, S: Service,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self::new(self.f.clone()) Self::new(self.f.clone())
} }
} }
impl<F, C, R, S, E, Req> impl<F, C, R, S, E> IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E>, C> for F
IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E, Req>, Req, C> for F
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>, R::Item: IntoService<S>,
S: Service<Req>, S: Service,
{ {
fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E, Req> { fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E> {
FnNewServiceConfig::new(self) FnNewServiceConfig::new(self)
} }
} }
#[cfg(test)]
mod tests {
use crate::{IntoService, Service, ServiceExt};
#[test]
fn test_fn_service() {
let mut rt = actix_rt::Runtime::new().unwrap();
let srv = (|_t: &str| -> Result<usize, ()> { Ok(1) }).into_service();
let mut srv = srv.and_then(|test: usize| Ok(test));
let s = "HELLO".to_owned();
let res = rt.block_on(srv.call(&s)).unwrap();
assert_eq!(res, 1);
}
}

View File

@ -5,16 +5,16 @@ use futures::IntoFuture;
use crate::{Apply, IntoTransform, Service, Transform}; use crate::{Apply, IntoTransform, Service, Transform};
pub struct FnTransform<F, S, R, In, Out, Err> pub struct FnTransform<F, S, In, Out, Err>
where where
F: FnMut(In, &mut S) -> Out + Clone, F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
f: F, f: F,
_t: PhantomData<(S, R, In, Out, Err)>, _t: PhantomData<(S, In, Out, Err)>,
} }
impl<F, S, R, In, Out, Err> FnTransform<F, S, R, In, Out, Err> impl<F, S, In, Out, Err> FnTransform<F, S, In, Out, Err>
where where
F: FnMut(In, &mut S) -> Out + Clone, F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
@ -24,16 +24,17 @@ where
} }
} }
impl<F, S, R, In, Out, Err> Transform<S, In> for FnTransform<F, S, R, In, Out, Err> impl<F, S, In, Out, Err> Transform<S> for FnTransform<F, S, In, Out, Err>
where where
S: Service<R>, S: Service,
F: FnMut(In, &mut S) -> Out + Clone, F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
Out::Error: From<S::Error>, Out::Error: From<S::Error>,
{ {
type Request = In;
type Response = Out::Item; type Response = Out::Item;
type Error = Out::Error; type Error = Out::Error;
type Transform = Apply<S, R, F, In, Out>; type Transform = Apply<S, F, In, Out>;
type InitError = Err; type InitError = Err;
type Future = FutureResult<Self::Transform, Self::InitError>; type Future = FutureResult<Self::Transform, Self::InitError>;
@ -42,19 +43,19 @@ where
} }
} }
impl<F, S, R, In, Out, Err> IntoTransform<FnTransform<F, S, R, In, Out, Err>, S, In> for F impl<F, S, In, Out, Err> IntoTransform<FnTransform<F, S, In, Out, Err>, S> for F
where where
S: Service<R>, S: Service,
F: FnMut(In, &mut S) -> Out + Clone, F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
Out::Error: From<S::Error>, Out::Error: From<S::Error>,
{ {
fn into_transform(self) -> FnTransform<F, S, R, In, Out, Err> { fn into_transform(self) -> FnTransform<F, S, In, Out, Err> {
FnTransform::new(self) FnTransform::new(self)
} }
} }
impl<F, S, R, In, Out, Err> Clone for FnTransform<F, S, R, In, Out, Err> impl<F, S, In, Out, Err> Clone for FnTransform<F, S, In, Out, Err>
where where
F: FnMut(In, &mut S) -> Out + Clone, F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,

View File

@ -13,9 +13,9 @@ pub struct FromErr<A, E> {
} }
impl<A, E> FromErr<A, E> { impl<A, E> FromErr<A, E> {
pub(crate) fn new<R>(service: A) -> Self pub(crate) fn new(service: A) -> Self
where where
A: Service<R>, A: Service,
E: From<A::Error>, E: From<A::Error>,
{ {
FromErr { FromErr {
@ -37,20 +37,21 @@ where
} }
} }
impl<A, E, R> Service<R> for FromErr<A, E> impl<A, E> Service for FromErr<A, E>
where where
A: Service<R>, A: Service,
E: From<A::Error>, E: From<A::Error>,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Future = FromErrFuture<A, R, E>; type Future = FromErrFuture<A, E>;
fn poll_ready(&mut self) -> Poll<(), E> { fn poll_ready(&mut self) -> Poll<(), E> {
self.service.poll_ready().map_err(E::from) self.service.poll_ready().map_err(E::from)
} }
fn call(&mut self, req: R) -> Self::Future { fn call(&mut self, req: A::Request) -> Self::Future {
FromErrFuture { FromErrFuture {
fut: self.service.call(req), fut: self.service.call(req),
f: PhantomData, f: PhantomData,
@ -58,14 +59,14 @@ where
} }
} }
pub struct FromErrFuture<A: Service<R>, R, E> { pub struct FromErrFuture<A: Service, E> {
fut: A::Future, fut: A::Future,
f: PhantomData<E>, f: PhantomData<E>,
} }
impl<A, R, E> Future for FromErrFuture<A, R, E> impl<A, E> Future for FromErrFuture<A, E>
where where
A: Service<R>, A: Service,
E: From<A::Error>, E: From<A::Error>,
{ {
type Item = A::Response; type Item = A::Response;
@ -87,9 +88,9 @@ pub struct FromErrNewService<A, E, C> {
impl<A, E, C> FromErrNewService<A, E, C> { impl<A, E, C> FromErrNewService<A, E, C> {
/// Create new `FromErr` new service instance /// Create new `FromErr` new service instance
pub fn new<R>(a: A) -> Self pub fn new(a: A) -> Self
where where
A: NewService<R, C>, A: NewService<C>,
E: From<A::Error>, E: From<A::Error>,
{ {
Self { a, e: PhantomData } Self { a, e: PhantomData }
@ -108,17 +109,18 @@ where
} }
} }
impl<A, E, C, R> NewService<R, C> for FromErrNewService<A, E, C> impl<A, E, C> NewService<C> for FromErrNewService<A, E, C>
where where
A: NewService<R, C>, A: NewService<C>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Service = FromErr<A::Service, E>; type Service = FromErr<A::Service, E>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = FromErrNewServiceFuture<A, E, C, R>; type Future = FromErrNewServiceFuture<A, E, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
FromErrNewServiceFuture { FromErrNewServiceFuture {
@ -128,18 +130,18 @@ where
} }
} }
pub struct FromErrNewServiceFuture<A, E, C, R> pub struct FromErrNewServiceFuture<A, E, C>
where where
A: NewService<R, C>, A: NewService<C>,
E: From<A::Error>, E: From<A::Error>,
{ {
fut: A::Future, fut: A::Future,
e: PhantomData<E>, e: PhantomData<E>,
} }
impl<A, E, C, R> Future for FromErrNewServiceFuture<A, E, C, R> impl<A, E, C> Future for FromErrNewServiceFuture<A, E, C>
where where
A: NewService<R, C>, A: NewService<C>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Item = FromErr<A::Service, E>; type Item = FromErr<A::Service, E>;
@ -162,7 +164,8 @@ mod tests {
use crate::{IntoNewService, NewService, Service, ServiceExt}; use crate::{IntoNewService, NewService, Service, ServiceExt};
struct Srv; struct Srv;
impl Service<()> for Srv { impl Service for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;

View File

@ -36,9 +36,10 @@ pub use self::then::{Then, ThenNewService};
pub use self::transform::{ApplyTransform, IntoTransform, Transform}; pub use self::transform::{ApplyTransform, IntoTransform, Transform};
/// An asynchronous function from `Request` to a `Response`. /// An asynchronous function from `Request` to a `Response`.
/// pub trait Service {
/// `Request` - requests handled by the service. /// Requests handled by the service.
pub trait Service<Request> { type Request;
/// Responses given by the service. /// Responses given by the service.
type Response; type Response;
@ -68,26 +69,22 @@ pub trait Service<Request> {
/// ///
/// Calling `call` without calling `poll_ready` is permitted. The /// Calling `call` without calling `poll_ready` is permitted. The
/// implementation must be resilient to this fact. /// implementation must be resilient to this fact.
fn call(&mut self, req: Request) -> Self::Future; fn call(&mut self, req: Self::Request) -> Self::Future;
} }
/// An extension trait for `Service`s that provides a variety of convenient /// An extension trait for `Service`s that provides a variety of convenient
/// adapters /// adapters
pub trait ServiceExt<Request>: Service<Request> { pub trait ServiceExt: Service {
/// Apply function to specified service and use it as a next service in /// Apply function to specified service and use it as a next service in
/// chain. /// chain.
fn apply_fn<F, B, B1, Out, Req>( fn apply_fn<F, B, B1, Out>(self, service: B1, f: F) -> AndThenApply<Self, B, F, Out>
self,
service: B1,
f: F,
) -> AndThenApply<Self, B, F, Out, Request, Req>
where where
Self: Sized, Self: Sized,
F: FnMut(Self::Response, &mut B) -> Out, F: FnMut(Self::Response, &mut B) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<Self::Error>, Out::Error: Into<Self::Error>,
B: Service<Req, Error = Self::Error>, B: Service<Error = Self::Error>,
B1: IntoService<B, Req>, B1: IntoService<B>,
{ {
AndThenApply::new(self, service, f) AndThenApply::new(self, service, f)
} }
@ -104,8 +101,8 @@ pub trait ServiceExt<Request>: Service<Request> {
fn and_then<F, B>(self, service: F) -> AndThen<Self, B> fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
where where
Self: Sized, Self: Sized,
F: IntoService<B, Self::Response>, F: IntoService<B>,
B: Service<Self::Response, Error = Self::Error>, B: Service<Request = Self::Response, Error = Self::Error>,
{ {
AndThen::new(self, service.into_service()) AndThen::new(self, service.into_service())
} }
@ -131,7 +128,7 @@ pub trait ServiceExt<Request>: Service<Request> {
fn then<B>(self, service: B) -> Then<Self, B> fn then<B>(self, service: B) -> Then<Self, B>
where where
Self: Sized, Self: Sized,
B: Service<Result<Self::Response, Self::Error>, Error = Self::Error>, B: Service<Request = Result<Self::Response, Self::Error>, Error = Self::Error>,
{ {
Then::new(self, service) Then::new(self, service)
} }
@ -170,7 +167,7 @@ pub trait ServiceExt<Request>: Service<Request> {
} }
} }
impl<T: ?Sized, R> ServiceExt<R> for T where T: Service<R> {} impl<T: ?Sized> ServiceExt for T where T: Service {}
/// Creates new `Service` values. /// Creates new `Service` values.
/// ///
@ -180,9 +177,11 @@ impl<T: ?Sized, R> ServiceExt<R> for T where T: Service<R> {}
/// `NewService` trait, and uses that new `Service` value to process inbound /// `NewService` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream. /// requests on that new TCP stream.
/// ///
/// * `Request` - requests handled by the service. /// `Config` is a service factory configuration type.
/// * `Config` - is a service factory configuration type. pub trait NewService<Config = ()> {
pub trait NewService<Request, Config = ()> { /// Requests handled by the service.
type Request;
/// Responses given by the service /// Responses given by the service
type Response; type Response;
@ -190,7 +189,11 @@ pub trait NewService<Request, Config = ()> {
type Error; type Error;
/// The `Service` value created by this factory /// The `Service` value created by this factory
type Service: Service<Request, Response = Self::Response, Error = Self::Error>; type Service: Service<
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
>;
/// Errors produced while building a service. /// Errors produced while building a service.
type InitError; type InitError;
@ -203,33 +206,33 @@ pub trait NewService<Request, Config = ()> {
/// Apply function to specified service and use it as a next service in /// Apply function to specified service and use it as a next service in
/// chain. /// chain.
fn apply<T, T1, B, B1, Req>( fn apply<T, T1, B, B1>(
self, self,
transform: T1, transform: T1,
service: B1, service: B1,
) -> AndThenTransform<T, Self, B, Req> ) -> AndThenTransform<T, Self, B, Config>
where where
Self: Sized, Self: Sized,
T: Transform<B::Service, Self::Response, InitError = Self::InitError>, T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>,
T::Error: From<Self::Error>, T::Error: From<Self::Error>,
T1: IntoTransform<T, B::Service, Self::Response>, T1: IntoTransform<T, B::Service>,
B: NewService<Req, Config, InitError = Self::InitError>, B: NewService<Config, InitError = Self::InitError>,
B1: IntoNewService<B, Req, Config>, B1: IntoNewService<B, Config>,
{ {
AndThenTransform::new(transform.into_transform(), self, service.into_new_service()) AndThenTransform::new(transform.into_transform(), self, service.into_new_service())
} }
/// Apply function to specified service and use it as a next service in /// Apply function to specified service and use it as a next service in
/// chain. /// chain.
fn apply_fn<B, I, F, Out, Req>( fn apply_fn<B, I, F, Out>(
self, self,
service: I, service: I,
f: F, f: F,
) -> AndThenApplyNewService<Self, B, F, Out, Request, Req, Config> ) -> AndThenApplyNewService<Self, B, F, Out, Config>
where where
Self: Sized, Self: Sized,
B: NewService<Req, Config, Error = Self::Error, InitError = Self::InitError>, B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Req, Config>, I: IntoNewService<B, Config>,
F: FnMut(Self::Response, &mut B::Service) -> Out, F: FnMut(Self::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<Self::Error>, Out::Error: Into<Self::Error>,
@ -238,11 +241,16 @@ pub trait NewService<Request, Config = ()> {
} }
/// Call another service after call to this one has resolved successfully. /// Call another service after call to this one has resolved successfully.
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B> fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B, Config>
where where
Self: Sized, Self: Sized,
F: IntoNewService<B, Self::Response, Config>, F: IntoNewService<B, Config>,
B: NewService<Self::Response, Config, Error = Self::Error, InitError = Self::InitError>, B: NewService<
Config,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
{ {
AndThenNewService::new(self, new_service) AndThenNewService::new(self, new_service)
} }
@ -270,15 +278,15 @@ pub trait NewService<Request, Config = ()> {
fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B, Config> fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B, Config>
where where
Self: Sized, Self: Sized,
F: IntoNewService<B, Result<Self::Response, Self::Error>, Config>, F: IntoNewService<B, Config>,
B: NewService< B: NewService<
Result<Self::Response, Self::Error>,
Config, Config,
Request = Result<Self::Response, Self::Error>,
Error = Self::Error, Error = Self::Error,
InitError = Self::InitError, InitError = Self::InitError,
>, >,
{ {
ThenNewService::new(self, new_service.into_new_service()) ThenNewService::new(self, new_service)
} }
/// Map this service's output to a different type, returning a new service /// Map this service's output to a different type, returning a new service
@ -310,10 +318,11 @@ pub trait NewService<Request, Config = ()> {
} }
} }
impl<'a, S, R> Service<R> for &'a mut S impl<'a, S> Service for &'a mut S
where where
S: Service<R> + 'a, S: Service + 'a,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Future = S::Future; type Future = S::Future;
@ -322,15 +331,16 @@ where
(**self).poll_ready() (**self).poll_ready()
} }
fn call(&mut self, request: R) -> S::Future { fn call(&mut self, request: Self::Request) -> S::Future {
(**self).call(request) (**self).call(request)
} }
} }
impl<S, R> Service<R> for Box<S> impl<S> Service for Box<S>
where where
S: Service<R> + ?Sized, S: Service + ?Sized,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Future = S::Future; type Future = S::Future;
@ -339,15 +349,16 @@ where
(**self).poll_ready() (**self).poll_ready()
} }
fn call(&mut self, request: R) -> S::Future { fn call(&mut self, request: Self::Request) -> S::Future {
(**self).call(request) (**self).call(request)
} }
} }
impl<S, R, C> NewService<R, C> for Rc<S> impl<S, C> NewService<C> for Rc<S>
where where
S: NewService<R, C>, S: NewService<C>,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Service = S::Service; type Service = S::Service;
@ -359,10 +370,11 @@ where
} }
} }
impl<S, R, C> NewService<R, C> for Arc<S> impl<S, C> NewService<C> for Arc<S>
where where
S: NewService<R, C>, S: NewService<C>,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Service = S::Service; type Service = S::Service;
@ -375,35 +387,35 @@ where
} }
/// Trait for types that can be converted to a `Service` /// Trait for types that can be converted to a `Service`
pub trait IntoService<T, R> pub trait IntoService<T>
where where
T: Service<R>, T: Service,
{ {
/// Convert to a `Service` /// Convert to a `Service`
fn into_service(self) -> T; fn into_service(self) -> T;
} }
/// Trait for types that can be converted to a `NewService` /// Trait for types that can be converted to a `NewService`
pub trait IntoNewService<T, R, C = ()> pub trait IntoNewService<T, C = ()>
where where
T: NewService<R, C>, T: NewService<C>,
{ {
/// Convert to an `NewService` /// Convert to an `NewService`
fn into_new_service(self) -> T; fn into_new_service(self) -> T;
} }
impl<T, R> IntoService<T, R> for T impl<T> IntoService<T> for T
where where
T: Service<R>, T: Service,
{ {
fn into_service(self) -> T { fn into_service(self) -> T {
self self
} }
} }
impl<T, R, C> IntoNewService<T, R, C> for T impl<T, C> IntoNewService<T, C> for T
where where
T: NewService<R, C>, T: NewService<C>,
{ {
fn into_new_service(self) -> T { fn into_new_service(self) -> T {
self self
@ -411,17 +423,17 @@ where
} }
/// Trait for types that can be converted to a configurable `NewService` /// Trait for types that can be converted to a configurable `NewService`
pub trait IntoConfigurableNewService<T, R, C> pub trait IntoConfigurableNewService<T, C>
where where
T: NewService<R, C>, T: NewService<C>,
{ {
/// Convert to an `NewService` /// Convert to an `NewService`
fn into_new_service(self) -> T; fn into_new_service(self) -> T;
} }
impl<T, R, C> IntoConfigurableNewService<T, R, C> for T impl<T, C> IntoConfigurableNewService<T, C> for T
where where
T: NewService<R, C>, T: NewService<C>,
{ {
fn into_new_service(self) -> T { fn into_new_service(self) -> T {
self self

View File

@ -15,9 +15,9 @@ pub struct Map<A, F, Response> {
impl<A, F, Response> Map<A, F, Response> { impl<A, F, Response> Map<A, F, Response> {
/// Create new `Map` combinator /// Create new `Map` combinator
pub fn new<R>(service: A, f: F) -> Self pub fn new(service: A, f: F) -> Self
where where
A: Service<R>, A: Service,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Response,
{ {
Self { Self {
@ -42,36 +42,37 @@ where
} }
} }
impl<A, F, R, Response> Service<R> for Map<A, F, Response> impl<A, F, Response> Service for Map<A, F, Response>
where where
A: Service<R>, A: Service,
F: FnMut(A::Response) -> Response + Clone, F: FnMut(A::Response) -> Response + Clone,
{ {
type Request = A::Request;
type Response = Response; type Response = Response;
type Error = A::Error; type Error = A::Error;
type Future = MapFuture<A, F, R, Response>; type Future = MapFuture<A, F, Response>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready() self.service.poll_ready()
} }
fn call(&mut self, req: R) -> Self::Future { fn call(&mut self, req: A::Request) -> Self::Future {
MapFuture::new(self.service.call(req), self.f.clone()) MapFuture::new(self.service.call(req), self.f.clone())
} }
} }
pub struct MapFuture<A, F, R, Response> pub struct MapFuture<A, F, Response>
where where
A: Service<R>, A: Service,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Response,
{ {
f: F, f: F,
fut: A::Future, fut: A::Future,
} }
impl<A, F, R, Response> MapFuture<A, F, R, Response> impl<A, F, Response> MapFuture<A, F, Response>
where where
A: Service<R>, A: Service,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Response,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@ -79,9 +80,9 @@ where
} }
} }
impl<A, F, R, Response> Future for MapFuture<A, F, R, Response> impl<A, F, Response> Future for MapFuture<A, F, Response>
where where
A: Service<R>, A: Service,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Response,
{ {
type Item = Response; type Item = Response;
@ -104,9 +105,9 @@ pub struct MapNewService<A, F, Res, Cfg> {
impl<A, F, Res, Cfg> MapNewService<A, F, Res, Cfg> { impl<A, F, Res, Cfg> MapNewService<A, F, Res, Cfg> {
/// Create new `Map` new service instance /// Create new `Map` new service instance
pub fn new<Req>(a: A, f: F) -> Self pub fn new(a: A, f: F) -> Self
where where
A: NewService<Req, Cfg>, A: NewService<Cfg>,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
Self { Self {
@ -131,35 +132,36 @@ where
} }
} }
impl<A, F, Req, Res, Cfg> NewService<Req, Cfg> for MapNewService<A, F, Res, Cfg> impl<A, F, Res, Cfg> NewService<Cfg> for MapNewService<A, F, Res, Cfg>
where where
A: NewService<Req, Cfg>, A: NewService<Cfg>,
F: FnMut(A::Response) -> Res + Clone, F: FnMut(A::Response) -> Res + Clone,
{ {
type Request = A::Request;
type Response = Res; type Response = Res;
type Error = A::Error; type Error = A::Error;
type Service = Map<A::Service, F, Res>; type Service = Map<A::Service, F, Res>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = MapNewServiceFuture<A, F, Req, Res, Cfg>; type Future = MapNewServiceFuture<A, F, Res, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future { fn new_service(&self, cfg: &Cfg) -> Self::Future {
MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
} }
} }
pub struct MapNewServiceFuture<A, F, Req, Res, Cfg> pub struct MapNewServiceFuture<A, F, Res, Cfg>
where where
A: NewService<Req, Cfg>, A: NewService<Cfg>,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
fut: A::Future, fut: A::Future,
f: Option<F>, f: Option<F>,
} }
impl<A, F, Req, Res, Cfg> MapNewServiceFuture<A, F, Req, Res, Cfg> impl<A, F, Res, Cfg> MapNewServiceFuture<A, F, Res, Cfg>
where where
A: NewService<Req, Cfg>, A: NewService<Cfg>,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@ -167,9 +169,9 @@ where
} }
} }
impl<A, F, Req, Res, Cfg> Future for MapNewServiceFuture<A, F, Req, Res, Cfg> impl<A, F, Res, Cfg> Future for MapNewServiceFuture<A, F, Res, Cfg>
where where
A: NewService<Req, Cfg>, A: NewService<Cfg>,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
type Item = Map<A::Service, F, Res>; type Item = Map<A::Service, F, Res>;
@ -192,7 +194,8 @@ mod tests {
use crate::{IntoNewService, Service, ServiceExt}; use crate::{IntoNewService, Service, ServiceExt};
struct Srv; struct Srv;
impl Service<()> for Srv { impl Service for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;

View File

@ -16,9 +16,9 @@ pub struct MapErr<A, F, E> {
impl<A, F, E> MapErr<A, F, E> { impl<A, F, E> MapErr<A, F, E> {
/// Create new `MapErr` combinator /// Create new `MapErr` combinator
pub fn new<R>(service: A, f: F) -> Self pub fn new(service: A, f: F) -> Self
where where
A: Service<R>, A: Service,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
Self { Self {
@ -43,39 +43,47 @@ where
} }
} }
impl<A, F, E, R> Service<R> for MapErr<A, F, E> impl<A, F, E> Service for MapErr<A, F, E>
where where
A: Service<R>, A: Service,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Future = MapErrFuture<A, F, E, R>; type Future = MapErrFuture<A, F, E>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready().map_err(&self.f) self.service.poll_ready().map_err(&self.f)
} }
fn call(&mut self, req: R) -> Self::Future { fn call(&mut self, req: A::Request) -> Self::Future {
MapErrFuture { MapErrFuture::new(self.service.call(req), self.f.clone())
fut: self.service.call(req),
f: self.f.clone(),
}
} }
} }
pub struct MapErrFuture<A, F, E, R> pub struct MapErrFuture<A, F, E>
where where
A: Service<R>, A: Service,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
f: F, f: F,
fut: A::Future, fut: A::Future,
} }
impl<A, F, E, R> Future for MapErrFuture<A, F, E, R> impl<A, F, E> MapErrFuture<A, F, E>
where where
A: Service<R>, A: Service,
F: Fn(A::Error) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
MapErrFuture { f, fut }
}
}
impl<A, F, E> Future for MapErrFuture<A, F, E>
where
A: Service,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
type Item = A::Response; type Item = A::Response;
@ -98,9 +106,9 @@ pub struct MapErrNewService<A, F, E, C> {
impl<A, F, E, C> MapErrNewService<A, F, E, C> { impl<A, F, E, C> MapErrNewService<A, F, E, C> {
/// Create new `MapErr` new service instance /// Create new `MapErr` new service instance
pub fn new<R>(a: A, f: F) -> Self pub fn new(a: A, f: F) -> Self
where where
A: NewService<R, C>, A: NewService<C>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
Self { Self {
@ -125,35 +133,36 @@ where
} }
} }
impl<A, F, E, R, C> NewService<R, C> for MapErrNewService<A, F, E, C> impl<A, F, E, C> NewService<C> for MapErrNewService<A, F, E, C>
where where
A: NewService<R, C>, A: NewService<C>,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Service = MapErr<A::Service, F, E>; type Service = MapErr<A::Service, F, E>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = MapErrNewServiceFuture<A, F, E, R, C>; type Future = MapErrNewServiceFuture<A, F, E, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
} }
} }
pub struct MapErrNewServiceFuture<A, F, E, R, C> pub struct MapErrNewServiceFuture<A, F, E, C>
where where
A: NewService<R, C>, A: NewService<C>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
fut: A::Future, fut: A::Future,
f: F, f: F,
} }
impl<A, F, E, R, C> MapErrNewServiceFuture<A, F, E, R, C> impl<A, F, E, C> MapErrNewServiceFuture<A, F, E, C>
where where
A: NewService<R, C>, A: NewService<C>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@ -161,9 +170,9 @@ where
} }
} }
impl<A, F, E, R, C> Future for MapErrNewServiceFuture<A, F, E, R, C> impl<A, F, E, C> Future for MapErrNewServiceFuture<A, F, E, C>
where where
A: NewService<R, C>, A: NewService<C>,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Item = MapErr<A::Service, F, E>; type Item = MapErr<A::Service, F, E>;
@ -187,7 +196,8 @@ mod tests {
struct Srv; struct Srv;
impl Service<()> for Srv { impl Service for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;

View File

@ -13,9 +13,9 @@ pub struct MapInitErr<A, F, E, C> {
impl<A, F, E, C> MapInitErr<A, F, E, C> { impl<A, F, E, C> MapInitErr<A, F, E, C> {
/// Create new `MapInitErr` combinator /// Create new `MapInitErr` combinator
pub fn new<R>(a: A, f: F) -> Self pub fn new(a: A, f: F) -> Self
where where
A: NewService<R, C>, A: NewService<C>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
Self { Self {
@ -40,38 +40,46 @@ where
} }
} }
impl<A, F, E, R, C> NewService<R, C> for MapInitErr<A, F, E, C> impl<A, F, E, C> NewService<C> for MapInitErr<A, F, E, C>
where where
A: NewService<R, C>, A: NewService<C>,
F: Fn(A::InitError) -> E + Clone, F: Fn(A::InitError) -> E + Clone,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = A::Error; type Error = A::Error;
type Service = A::Service; type Service = A::Service;
type InitError = E; type InitError = E;
type Future = MapInitErrFuture<A, F, E, R, C>; type Future = MapInitErrFuture<A, F, E, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
MapInitErrFuture { MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
fut: self.a.new_service(cfg),
f: self.f.clone(),
}
} }
} }
pub struct MapInitErrFuture<A, F, E, R, C> pub struct MapInitErrFuture<A, F, E, C>
where where
A: NewService<R, C>, A: NewService<C>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
f: F, f: F,
fut: A::Future, fut: A::Future,
} }
impl<A, F, E, R, C> Future for MapInitErrFuture<A, F, E, R, C> impl<A, F, E, C> MapInitErrFuture<A, F, E, C>
where where
A: NewService<R, C>, A: NewService<C>,
F: Fn(A::InitError) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
MapInitErrFuture { f, fut }
}
}
impl<A, F, E, C> Future for MapInitErrFuture<A, F, E, C>
where
A: NewService<C>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
type Item = A::Service; type Item = A::Service;

View File

@ -2,7 +2,7 @@ use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll}; use futures::{try_ready, Async, Future, Poll};
use super::{NewService, Service}; use super::{IntoNewService, NewService, Service};
use crate::cell::Cell; use crate::cell::Cell;
/// Service for the `then` combinator, chaining a computation onto the end of /// Service for the `then` combinator, chaining a computation onto the end of
@ -16,10 +16,10 @@ pub struct Then<A, B> {
impl<A, B> Then<A, B> { impl<A, B> Then<A, B> {
/// Create new `Then` combinator /// Create new `Then` combinator
pub fn new<R>(a: A, b: B) -> Then<A, B> pub fn new(a: A, b: B) -> Then<A, B>
where where
A: Service<R>, A: Service,
B: Service<Result<A::Response, A::Error>, Error = A::Error>, B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
{ {
Then { a, b: Cell::new(b) } Then { a, b: Cell::new(b) }
} }
@ -37,39 +37,40 @@ where
} }
} }
impl<A, B, R> Service<R> for Then<A, B> impl<A, B> Service for Then<A, B>
where where
A: Service<R>, A: Service,
B: Service<Result<A::Response, A::Error>, Error = A::Error>, B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = B::Error; type Error = B::Error;
type Future = ThenFuture<A, B, R>; type Future = ThenFuture<A, B>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.poll_ready()); try_ready!(self.a.poll_ready());
self.b.get_mut().poll_ready() self.b.get_mut().poll_ready()
} }
fn call(&mut self, req: R) -> Self::Future { fn call(&mut self, req: A::Request) -> Self::Future {
ThenFuture::new(self.a.call(req), self.b.clone()) ThenFuture::new(self.a.call(req), self.b.clone())
} }
} }
pub struct ThenFuture<A, B, R> pub struct ThenFuture<A, B>
where where
A: Service<R>, A: Service,
B: Service<Result<A::Response, A::Error>>, B: Service<Request = Result<A::Response, A::Error>>,
{ {
b: Cell<B>, b: Cell<B>,
fut_b: Option<B::Future>, fut_b: Option<B::Future>,
fut_a: Option<A::Future>, fut_a: Option<A::Future>,
} }
impl<A, B, R> ThenFuture<A, B, R> impl<A, B> ThenFuture<A, B>
where where
A: Service<R>, A: Service,
B: Service<Result<A::Response, A::Error>>, B: Service<Request = Result<A::Response, A::Error>>,
{ {
fn new(a: A::Future, b: Cell<B>) -> Self { fn new(a: A::Future, b: Cell<B>) -> Self {
ThenFuture { ThenFuture {
@ -80,10 +81,10 @@ where
} }
} }
impl<A, B, R> Future for ThenFuture<A, B, R> impl<A, B> Future for ThenFuture<A, B>
where where
A: Service<R>, A: Service,
B: Service<Result<A::Response, A::Error>>, B: Service<Request = Result<A::Response, A::Error>>,
{ {
type Item = B::Response; type Item = B::Response;
type Error = B::Error; type Error = B::Error;
@ -118,35 +119,42 @@ pub struct ThenNewService<A, B, C> {
impl<A, B, C> ThenNewService<A, B, C> { impl<A, B, C> ThenNewService<A, B, C> {
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<R>(a: A, f: B) -> Self pub fn new<F>(a: A, f: F) -> Self
where where
A: NewService<R, C>, A: NewService<C>,
B: NewService< B: NewService<
Result<A::Response, A::Error>,
C, C,
Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
InitError = A::InitError, InitError = A::InitError,
>, >,
F: IntoNewService<B, C>,
{ {
Self { Self {
a, a,
b: f, b: f.into_new_service(),
_t: PhantomData, _t: PhantomData,
} }
} }
} }
impl<A, B, R, C> NewService<R, C> for ThenNewService<A, B, C> impl<A, B, C> NewService<C> for ThenNewService<A, B, C>
where where
A: NewService<R, C>, A: NewService<C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>, B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Service = Then<A::Service, B::Service>; type Service = Then<A::Service, B::Service>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = ThenNewServiceFuture<A, B, R, C>; type Future = ThenNewServiceFuture<A, B, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
ThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) ThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg))
@ -167,10 +175,15 @@ where
} }
} }
pub struct ThenNewServiceFuture<A, B, R, C> pub struct ThenNewServiceFuture<A, B, C>
where where
A: NewService<R, C>, A: NewService<C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>, B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{ {
fut_b: B::Future, fut_b: B::Future,
fut_a: A::Future, fut_a: A::Future,
@ -178,10 +191,15 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B, R, C> ThenNewServiceFuture<A, B, R, C> impl<A, B, C> ThenNewServiceFuture<A, B, C>
where where
A: NewService<R, C>, A: NewService<C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>, B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{ {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self { fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
ThenNewServiceFuture { ThenNewServiceFuture {
@ -193,10 +211,15 @@ where
} }
} }
impl<A, B, R, C> Future for ThenNewServiceFuture<A, B, R, C> impl<A, B, C> Future for ThenNewServiceFuture<A, B, C>
where where
A: NewService<R, C>, A: NewService<C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>, B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{ {
type Item = Then<A::Service, B::Service>; type Item = Then<A::Service, B::Service>;
type Error = A::InitError; type Error = A::InitError;
@ -236,7 +259,8 @@ mod tests {
#[derive(Clone)] #[derive(Clone)]
struct Srv1(Rc<Cell<usize>>); struct Srv1(Rc<Cell<usize>>);
impl Service<Result<&'static str, &'static str>> for Srv1 { impl Service for Srv1 {
type Request = Result<&'static str, &'static str>;
type Response = &'static str; type Response = &'static str;
type Error = (); type Error = ();
type Future = FutureResult<Self::Response, Self::Error>; type Future = FutureResult<Self::Response, Self::Error>;
@ -256,7 +280,8 @@ mod tests {
struct Srv2(Rc<Cell<usize>>); struct Srv2(Rc<Cell<usize>>);
impl Service<Result<&'static str, ()>> for Srv2 { impl Service for Srv2 {
type Request = Result<&'static str, ()>;
type Response = (&'static str, &'static str); type Response = (&'static str, &'static str);
type Error = (); type Error = ();
type Future = FutureResult<Self::Response, ()>; type Future = FutureResult<Self::Response, ()>;

View File

@ -9,10 +9,11 @@ use crate::{NewService, Service};
/// `Transform` service factory. /// `Transform` service factory.
/// ///
/// Transform factory creates service that wraps other services. /// Transform factory creates service that wraps other services.
/// /// `Config` is a service factory configuration type.
/// * `S` is a wrapped service. pub trait Transform<S> {
/// * `R` requests handled by this transform service. /// Requests handled by the service.
pub trait Transform<S, R> { type Request;
/// Responses given by the service. /// Responses given by the service.
type Response; type Response;
@ -20,7 +21,11 @@ pub trait Transform<S, R> {
type Error; type Error;
/// The `TransformService` value created by this factory /// The `TransformService` value created by this factory
type Transform: Service<R, Response = Self::Response, Error = Self::Error>; type Transform: Service<
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
>;
/// Errors produced while building a service. /// Errors produced while building a service.
type InitError; type InitError;
@ -42,10 +47,11 @@ pub trait Transform<S, R> {
} }
} }
impl<T, S, R> Transform<S, R> for Rc<T> impl<T, S> Transform<S> for Rc<T>
where where
T: Transform<S, R>, T: Transform<S>,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type InitError = T::InitError; type InitError = T::InitError;
@ -57,10 +63,11 @@ where
} }
} }
impl<T, S, R> Transform<S, R> for Arc<T> impl<T, S> Transform<S> for Arc<T>
where where
T: Transform<S, R>, T: Transform<S>,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type InitError = T::InitError; type InitError = T::InitError;
@ -73,17 +80,17 @@ where
} }
/// Trait for types that can be converted to a *transform service* /// Trait for types that can be converted to a *transform service*
pub trait IntoTransform<T, S, R> pub trait IntoTransform<T, S>
where where
T: Transform<S, R>, T: Transform<S>,
{ {
/// Convert to a `TransformService` /// Convert to a `TransformService`
fn into_transform(self) -> T; fn into_transform(self) -> T;
} }
impl<T, S, R> IntoTransform<T, S, R> for T impl<T, S> IntoTransform<T, S> for T
where where
T: Transform<S, R>, T: Transform<S>,
{ {
fn into_transform(self) -> T { fn into_transform(self) -> T {
self self
@ -92,19 +99,19 @@ where
/// `Apply` transform new service /// `Apply` transform new service
#[derive(Clone)] #[derive(Clone)]
pub struct ApplyTransform<T, S, R, Req, Cfg> { pub struct ApplyTransform<T, A, C> {
a: S, a: A,
t: Rc<T>, t: Rc<T>,
_t: std::marker::PhantomData<(R, Req, Cfg)>, _t: std::marker::PhantomData<C>,
} }
impl<T, S, R, Req, Cfg> ApplyTransform<T, S, R, Req, Cfg> impl<T, A, C> ApplyTransform<T, A, C>
where where
S: NewService<Req, Cfg>, A: NewService<C>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>, T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{ {
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<F: IntoTransform<T, S::Service, R>>(t: F, a: S) -> Self { pub fn new<F: IntoTransform<T, A::Service>>(t: F, a: A) -> Self {
Self { Self {
a, a,
t: Rc::new(t.into_transform()), t: Rc::new(t.into_transform()),
@ -113,19 +120,20 @@ where
} }
} }
impl<T, S, R, Req, Cfg> NewService<R, Cfg> for ApplyTransform<T, S, R, Req, Cfg> impl<T, A, C> NewService<C> for ApplyTransform<T, A, C>
where where
S: NewService<Req, Cfg>, A: NewService<C>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>, T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type Service = T::Transform; type Service = T::Transform;
type InitError = T::InitError; type InitError = T::InitError;
type Future = ApplyTransformFuture<T, S, R, Req, Cfg>; type Future = ApplyTransformFuture<T, A, C>;
fn new_service(&self, cfg: &Cfg) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
ApplyTransformFuture { ApplyTransformFuture {
t_cell: self.t.clone(), t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg).into_future(), fut_a: self.a.new_service(cfg).into_future(),
@ -134,20 +142,20 @@ where
} }
} }
pub struct ApplyTransformFuture<T, S, R, Req, Cfg> pub struct ApplyTransformFuture<T, A, C>
where where
S: NewService<Req, Cfg>, A: NewService<C>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>, T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{ {
fut_a: S::Future, fut_a: A::Future,
fut_t: Option<T::Future>, fut_t: Option<<T::Future as IntoFuture>::Future>,
t_cell: Rc<T>, t_cell: Rc<T>,
} }
impl<T, S, R, Req, Cfg> Future for ApplyTransformFuture<T, S, R, Req, Cfg> impl<T, A, C> Future for ApplyTransformFuture<T, A, C>
where where
S: NewService<Req, Cfg>, A: NewService<C>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>, T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{ {
type Item = T::Transform; type Item = T::Transform;
type Error = T::InitError; type Error = T::InitError;

View File

@ -16,9 +16,9 @@ pub struct TransformMapInitErr<T, S, F, E> {
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> { impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
/// Create new `MapInitErr` new transform instance /// Create new `MapInitErr` new transform instance
pub fn new<R>(t: T, f: F) -> Self pub fn new(t: T, f: F) -> Self
where where
T: Transform<S, R>, T: Transform<S>,
F: Fn(T::InitError) -> E, F: Fn(T::InitError) -> E,
{ {
Self { Self {
@ -43,35 +43,36 @@ where
} }
} }
impl<T, R, S, F, E> Transform<S, R> for TransformMapInitErr<T, S, F, E> impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
where where
T: Transform<S, R>, T: Transform<S>,
F: Fn(T::InitError) -> E + Clone, F: Fn(T::InitError) -> E + Clone,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type Transform = T::Transform; type Transform = T::Transform;
type InitError = E; type InitError = E;
type Future = TransformMapInitErrFuture<T, R, S, F, E>; type Future = TransformMapInitErrFuture<T, S, F, E>;
fn new_transform(&self, service: S) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone()) TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone())
} }
} }
pub struct TransformMapInitErrFuture<T, R, S, F, E> pub struct TransformMapInitErrFuture<T, S, F, E>
where where
T: Transform<S, R>, T: Transform<S>,
F: Fn(T::InitError) -> E, F: Fn(T::InitError) -> E,
{ {
fut: T::Future, fut: T::Future,
f: F, f: F,
} }
impl<T, R, S, F, E> TransformMapInitErrFuture<T, R, S, F, E> impl<T, S, F, E> TransformMapInitErrFuture<T, S, F, E>
where where
T: Transform<S, R>, T: Transform<S>,
F: Fn(T::InitError) -> E, F: Fn(T::InitError) -> E,
{ {
fn new(fut: T::Future, f: F) -> Self { fn new(fut: T::Future, f: F) -> Self {
@ -79,9 +80,9 @@ where
} }
} }
impl<T, R, S, F, E> Future for TransformMapInitErrFuture<T, R, S, F, E> impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
where where
T: Transform<S, R>, T: Transform<S>,
F: Fn(T::InitError) -> E + Clone, F: Fn(T::InitError) -> E + Clone,
{ {
type Item = T::Transform; type Item = T::Transform;