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 super::{IntoNewService, NewService, Service};
@ -14,10 +16,10 @@ pub struct AndThen<A, B> {
impl<A, B> AndThen<A, B> {
/// Create new `AndThen` combinator
pub fn new<R>(a: A, b: B) -> Self
pub fn new(a: A, b: B) -> Self
where
A: Service<R>,
B: Service<A::Response, Error = A::Error>,
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
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
A: Service<R>,
B: Service<A::Response, Error = A::Error>,
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Future = AndThenFuture<A, B, R>;
type Future = AndThenFuture<A, B>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.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())
}
}
pub struct AndThenFuture<A, B, R>
pub struct AndThenFuture<A, B>
where
A: Service<R>,
B: Service<A::Response, Error = A::Error>,
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
b: Cell<B>,
fut_b: Option<B::Future>,
fut_a: Option<A::Future>,
}
impl<A, B, R> AndThenFuture<A, B, R>
impl<A, B> AndThenFuture<A, B>
where
A: Service<R>,
B: Service<A::Response, Error = A::Error>,
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
fn new(a: A::Future, b: Cell<B>) -> Self {
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
A: Service<R>,
B: Service<A::Response, Error = A::Error>,
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
type Item = B::Response;
type Error = A::Error;
@ -104,43 +107,46 @@ where
}
/// `AndThenNewService` new service combinator
pub struct AndThenNewService<A, B> {
pub struct AndThenNewService<A, B, C> {
a: A,
b: B,
_t: PhantomData<C>,
}
impl<A, B> AndThenNewService<A, B> {
impl<A, B, C> AndThenNewService<A, B, C> {
/// 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
A: NewService<R, C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{
Self {
a,
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
A: NewService<R, C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Service = AndThen<A::Service, B::Service>;
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 {
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
A: Clone,
B: Clone,
@ -149,14 +155,15 @@ where
Self {
a: self.a.clone(),
b: self.b.clone(),
_t: PhantomData,
}
}
}
pub struct AndThenNewServiceFuture<A, B, R, C>
pub struct AndThenNewServiceFuture<A, B, C>
where
A: NewService<R, C>,
B: NewService<A::Response, C>,
A: NewService<C>,
B: NewService<C, Request = A::Response>,
{
fut_b: B::Future,
fut_a: A::Future,
@ -164,10 +171,10 @@ where
b: Option<B::Service>,
}
impl<A, B, R, C> AndThenNewServiceFuture<A, B, R, C>
impl<A, B, C> AndThenNewServiceFuture<A, B, C>
where
A: NewService<R, C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<C, Request = A::Response>,
{
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
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
A: NewService<R, C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{
type Item = AndThen<A::Service, B::Service>;
type Error = A::InitError;
@ -222,7 +229,8 @@ mod tests {
use crate::{NewService, Service, ServiceExt};
struct Srv1(Rc<Cell<usize>>);
impl Service<&'static str> for Srv1 {
impl Service for Srv1 {
type Request = &'static str;
type Response = &'static str;
type Error = ();
type Future = FutureResult<Self::Response, ()>;
@ -240,7 +248,8 @@ mod tests {
#[derive(Clone)]
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 Error = ();
type Future = FutureResult<Self::Response, ()>;

View File

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

View File

@ -6,23 +6,30 @@ use super::{IntoNewService, IntoService, NewService, Service};
use crate::cell::Cell;
/// `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,
b: Cell<B>,
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
A: Service<AReq>,
B: Service<BReq, Error = A::Error>,
A: Service,
B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
/// 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 {
f: Cell::new(f),
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
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 {
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
A: Service<AReq>,
B: Service<BReq, Error = A::Error>,
A: Service,
B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
type Request = A::Request;
type Response = Out::Item;
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> {
try_ready!(self.a.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 {
b: self.b.clone(),
f: self.f.clone(),
fut_b: None,
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
A: Service<AReq>,
B: Service<BReq, Error = A::Error>,
A: Service,
B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
@ -86,13 +97,12 @@ where
f: Cell<F>,
fut_a: Option<A::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
A: Service<AReq>,
B: Service<BReq, Error = A::Error>,
A: Service,
B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
@ -119,23 +129,23 @@ where
}
/// `ApplyNewService` new service combinator
pub struct AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg> {
pub struct AndThenApplyNewService<A, B, F, Out, Cfg> {
a: A,
b: B,
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
A: NewService<AReq, Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
/// 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,
b: B1,
f: F,
@ -149,8 +159,7 @@ where
}
}
impl<A, B, F, Out, AReq, BReq, Cfg> Clone
for AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg>
impl<A, B, F, Out, Cfg> Clone for AndThenApplyNewService<A, B, F, Out, Cfg>
where
A: Clone,
B: Clone,
@ -165,21 +174,21 @@ where
}
}
impl<A, B, F, Out, AReq, BReq, Cfg> NewService<AReq, Cfg>
for AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg>
impl<A, B, F, Out, Cfg> NewService<Cfg> for AndThenApplyNewService<A, B, F, Out, Cfg>
where
A: NewService<AReq, Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
type Request = A::Request;
type Response = Out::Item;
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 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 {
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
A: NewService<AReq, Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
@ -207,16 +216,15 @@ where
b: Option<B::Service>,
}
impl<A, B, F, Out, AReq, BReq, Cfg> Future
for AndThenApplyNewServiceFuture<A, B, F, Out, AReq, BReq, Cfg>
impl<A, B, F, Out, Cfg> Future for AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>
where
A: NewService<AReq, Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
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;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -255,7 +263,8 @@ mod tests {
#[derive(Clone)]
struct Srv;
impl Service<()> for Srv {
impl Service for Srv {
type Request = ();
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;

View File

@ -5,23 +5,24 @@ use futures::{Async, Future, IntoFuture, Poll};
use super::{IntoNewService, IntoService, NewService, Service};
/// `Apply` service combinator
pub struct Apply<T, R, F, In, Out> {
pub struct Apply<T, F, In, Out>
where
T: Service,
{
service: T,
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
T: Service,
F: FnMut(In, &mut T) -> Out,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
/// Create new `Apply` combinator
pub fn new<I: IntoService<T, R>>(service: I, f: F) -> Self
where
T: Service<R>,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
pub fn new<I: IntoService<T>>(service: I, f: F) -> Self {
Self {
service: service.into_service(),
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
T: Clone,
T: Service + Clone,
F: Clone,
{
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
T: Service<R>,
T: Service,
F: FnMut(In, &mut T) -> Out,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
type Request = In;
type Response = Out::Item;
type Error = Out::Error;
type Future = Out::Future;
@ -65,21 +67,24 @@ where
}
/// `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,
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
pub fn new<Cfg, F1: IntoNewService<T, Req, 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>,
{
pub fn new<F1: IntoNewService<T, Cfg>>(service: F1, f: F) -> Self {
Self {
f,
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
T: Clone,
F: Clone,
T: NewService<Cfg> + Clone,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
fn clone(&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
T: NewService<Req, Cfg>,
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
type Request = In;
type Response = Out::Item;
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 Future = ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>;
type Future = ApplyNewServiceFuture<T, F, In, Out, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
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
T: NewService<Req, Cfg>,
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
@ -132,9 +139,9 @@ where
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
T: NewService<Req, Cfg>,
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
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
T: NewService<Req, Cfg>,
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
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;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -176,7 +183,8 @@ mod tests {
#[derive(Clone)]
struct Srv;
impl Service<()> for Srv {
impl Service for Srv {
type Request = ();
type Response = ();
type Error = ();
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 Error = 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 Error = E1;
type Service = Blank<R, E1>;

View File

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

View File

@ -15,22 +15,22 @@ where
}
/// 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
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
FnNewServiceNoConfig::new(f)
}
/// 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
F: Fn(&C) -> R,
R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>,
S: Service<Req>,
R::Item: IntoService<S>,
S: Service,
{
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
F: FnMut(Req) -> Out,
Out: IntoFuture,
{
type Request = Req;
type Response = Out::Item;
type Error = Out::Error;
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
F: FnMut(Req) -> Out + 'static,
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
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
{
type Request = Req;
type Response = Out::Item;
type Error = Out::Error;
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
F: Fn(Req) -> Out + Clone,
Out: IntoFuture,
@ -152,33 +154,33 @@ where
}
/// Converter for `Fn() -> Future<Service>` fn
pub struct FnNewServiceNoConfig<F, R, S, E, Req>
pub struct FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
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
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
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
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
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
F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
fn clone(&self) -> Self {
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
F: Fn() -> R,
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)
}
}
/// 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
F: Fn(&C) -> R,
R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>,
S: Service<Req>,
R::Item: IntoService<S>,
S: Service,
{
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
F: Fn(&C) -> R,
R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>,
S: Service<Req>,
R::Item: IntoService<S>,
S: Service,
{
pub fn new(f: F) -> Self {
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
F: Fn(&C) -> R,
R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>,
S: Service<Req>,
R::Item: IntoService<S>,
S: Service,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S;
type InitError = E;
type Future = FnNewServiceConfigFut<R, S, E, Req>;
type Future = FnNewServiceConfigFut<R, S, E>;
fn new_service(&self, cfg: &C) -> Self::Future {
FnNewServiceConfigFut {
@ -259,21 +262,21 @@ where
}
}
pub struct FnNewServiceConfigFut<R, S, E, Req>
pub struct FnNewServiceConfigFut<R, S, E>
where
R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>,
S: Service<Req>,
R::Item: IntoService<S>,
S: Service,
{
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
R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>,
S: Service<Req>,
R::Item: IntoService<S>,
S: Service,
{
type Item = S;
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
F: Fn(&C) -> R + Clone,
R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>,
S: Service<Req>,
R::Item: IntoService<S>,
S: Service,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
}
}
impl<F, C, R, S, E, Req>
IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E, Req>, Req, C> for F
impl<F, C, R, S, E> IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E>, C> for F
where
F: Fn(&C) -> R,
R: IntoFuture<Error = E>,
R::Item: IntoService<S, Req>,
S: Service<Req>,
R::Item: IntoService<S>,
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)
}
}
#[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};
pub struct FnTransform<F, S, R, In, Out, Err>
pub struct FnTransform<F, S, In, Out, Err>
where
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
{
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
F: FnMut(In, &mut S) -> Out + Clone,
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
S: Service<R>,
S: Service,
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<S::Error>,
{
type Request = In;
type Response = Out::Item;
type Error = Out::Error;
type Transform = Apply<S, R, F, In, Out>;
type Transform = Apply<S, F, In, Out>;
type InitError = Err;
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
S: Service<R>,
S: Service,
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
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)
}
}
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
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,

View File

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

View File

@ -36,9 +36,10 @@ pub use self::then::{Then, ThenNewService};
pub use self::transform::{ApplyTransform, IntoTransform, Transform};
/// An asynchronous function from `Request` to a `Response`.
///
/// `Request` - requests handled by the service.
pub trait Service<Request> {
pub trait Service {
/// Requests handled by the service.
type Request;
/// Responses given by the service.
type Response;
@ -68,26 +69,22 @@ pub trait Service<Request> {
///
/// Calling `call` without calling `poll_ready` is permitted. The
/// 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
/// adapters
pub trait ServiceExt<Request>: Service<Request> {
pub trait ServiceExt: Service {
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply_fn<F, B, B1, Out, Req>(
self,
service: B1,
f: F,
) -> AndThenApply<Self, B, F, Out, Request, Req>
fn apply_fn<F, B, B1, Out>(self, service: B1, f: F) -> AndThenApply<Self, B, F, Out>
where
Self: Sized,
F: FnMut(Self::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<Self::Error>,
B: Service<Req, Error = Self::Error>,
B1: IntoService<B, Req>,
B: Service<Error = Self::Error>,
B1: IntoService<B>,
{
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>
where
Self: Sized,
F: IntoService<B, Self::Response>,
B: Service<Self::Response, Error = Self::Error>,
F: IntoService<B>,
B: Service<Request = Self::Response, Error = Self::Error>,
{
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>
where
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)
}
@ -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.
///
@ -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
/// requests on that new TCP stream.
///
/// * `Request` - requests handled by the service.
/// * `Config` - is a service factory configuration type.
pub trait NewService<Request, Config = ()> {
/// `Config` is a service factory configuration type.
pub trait NewService<Config = ()> {
/// Requests handled by the service.
type Request;
/// Responses given by the service
type Response;
@ -190,7 +189,11 @@ pub trait NewService<Request, Config = ()> {
type Error;
/// 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.
type InitError;
@ -203,33 +206,33 @@ pub trait NewService<Request, Config = ()> {
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply<T, T1, B, B1, Req>(
fn apply<T, T1, B, B1>(
self,
transform: T1,
service: B1,
) -> AndThenTransform<T, Self, B, Req>
) -> AndThenTransform<T, Self, B, Config>
where
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>,
T1: IntoTransform<T, B::Service, Self::Response>,
B: NewService<Req, Config, InitError = Self::InitError>,
B1: IntoNewService<B, Req, Config>,
T1: IntoTransform<T, B::Service>,
B: NewService<Config, InitError = Self::InitError>,
B1: IntoNewService<B, Config>,
{
AndThenTransform::new(transform.into_transform(), self, service.into_new_service())
}
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply_fn<B, I, F, Out, Req>(
fn apply_fn<B, I, F, Out>(
self,
service: I,
f: F,
) -> AndThenApplyNewService<Self, B, F, Out, Request, Req, Config>
) -> AndThenApplyNewService<Self, B, F, Out, Config>
where
Self: Sized,
B: NewService<Req, Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Req, Config>,
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Config>,
F: FnMut(Self::Response, &mut B::Service) -> Out,
Out: IntoFuture,
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.
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
Self: Sized,
F: IntoNewService<B, Self::Response, Config>,
B: NewService<Self::Response, Config, Error = Self::Error, InitError = Self::InitError>,
F: IntoNewService<B, Config>,
B: NewService<
Config,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
{
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>
where
Self: Sized,
F: IntoNewService<B, Result<Self::Response, Self::Error>, Config>,
F: IntoNewService<B, Config>,
B: NewService<
Result<Self::Response, Self::Error>,
Config,
Request = Result<Self::Response, Self::Error>,
Error = Self::Error,
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
@ -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
S: Service<R> + 'a,
S: Service + 'a,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@ -322,15 +331,16 @@ where
(**self).poll_ready()
}
fn call(&mut self, request: R) -> S::Future {
fn call(&mut self, request: Self::Request) -> S::Future {
(**self).call(request)
}
}
impl<S, R> Service<R> for Box<S>
impl<S> Service for Box<S>
where
S: Service<R> + ?Sized,
S: Service + ?Sized,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@ -339,15 +349,16 @@ where
(**self).poll_ready()
}
fn call(&mut self, request: R) -> S::Future {
fn call(&mut self, request: Self::Request) -> S::Future {
(**self).call(request)
}
}
impl<S, R, C> NewService<R, C> for Rc<S>
impl<S, C> NewService<C> for Rc<S>
where
S: NewService<R, C>,
S: NewService<C>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
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
S: NewService<R, C>,
S: NewService<C>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;
@ -375,35 +387,35 @@ where
}
/// Trait for types that can be converted to a `Service`
pub trait IntoService<T, R>
pub trait IntoService<T>
where
T: Service<R>,
T: Service,
{
/// Convert to a `Service`
fn into_service(self) -> T;
}
/// Trait for types that can be converted to a `NewService`
pub trait IntoNewService<T, R, C = ()>
pub trait IntoNewService<T, C = ()>
where
T: NewService<R, C>,
T: NewService<C>,
{
/// Convert to an `NewService`
fn into_new_service(self) -> T;
}
impl<T, R> IntoService<T, R> for T
impl<T> IntoService<T> for T
where
T: Service<R>,
T: Service,
{
fn into_service(self) -> T {
self
}
}
impl<T, R, C> IntoNewService<T, R, C> for T
impl<T, C> IntoNewService<T, C> for T
where
T: NewService<R, C>,
T: NewService<C>,
{
fn into_new_service(self) -> T {
self
@ -411,17 +423,17 @@ where
}
/// Trait for types that can be converted to a configurable `NewService`
pub trait IntoConfigurableNewService<T, R, C>
pub trait IntoConfigurableNewService<T, C>
where
T: NewService<R, C>,
T: NewService<C>,
{
/// Convert to an `NewService`
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
T: NewService<R, C>,
T: NewService<C>,
{
fn into_new_service(self) -> T {
self

View File

@ -15,9 +15,9 @@ pub struct Map<A, F, Response> {
impl<A, F, Response> Map<A, F, Response> {
/// Create new `Map` combinator
pub fn new<R>(service: A, f: F) -> Self
pub fn new(service: A, f: F) -> Self
where
A: Service<R>,
A: Service,
F: FnMut(A::Response) -> Response,
{
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
A: Service<R>,
A: Service,
F: FnMut(A::Response) -> Response + Clone,
{
type Request = A::Request;
type Response = Response;
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> {
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())
}
}
pub struct MapFuture<A, F, R, Response>
pub struct MapFuture<A, F, Response>
where
A: Service<R>,
A: Service,
F: FnMut(A::Response) -> Response,
{
f: F,
fut: A::Future,
}
impl<A, F, R, Response> MapFuture<A, F, R, Response>
impl<A, F, Response> MapFuture<A, F, Response>
where
A: Service<R>,
A: Service,
F: FnMut(A::Response) -> Response,
{
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
A: Service<R>,
A: Service,
F: FnMut(A::Response) -> 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> {
/// Create new `Map` new service instance
pub fn new<Req>(a: A, f: F) -> Self
pub fn new(a: A, f: F) -> Self
where
A: NewService<Req, Cfg>,
A: NewService<Cfg>,
F: FnMut(A::Response) -> Res,
{
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
A: NewService<Req, Cfg>,
A: NewService<Cfg>,
F: FnMut(A::Response) -> Res + Clone,
{
type Request = A::Request;
type Response = Res;
type Error = A::Error;
type Service = Map<A::Service, F, Res>;
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 {
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
A: NewService<Req, Cfg>,
A: NewService<Cfg>,
F: FnMut(A::Response) -> Res,
{
fut: A::Future,
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
A: NewService<Req, Cfg>,
A: NewService<Cfg>,
F: FnMut(A::Response) -> Res,
{
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
A: NewService<Req, Cfg>,
A: NewService<Cfg>,
F: FnMut(A::Response) -> Res,
{
type Item = Map<A::Service, F, Res>;
@ -192,7 +194,8 @@ mod tests {
use crate::{IntoNewService, Service, ServiceExt};
struct Srv;
impl Service<()> for Srv {
impl Service for Srv {
type Request = ();
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;

View File

@ -16,9 +16,9 @@ pub struct MapErr<A, F, E> {
impl<A, F, E> MapErr<A, F, E> {
/// Create new `MapErr` combinator
pub fn new<R>(service: A, f: F) -> Self
pub fn new(service: A, f: F) -> Self
where
A: Service<R>,
A: Service,
F: Fn(A::Error) -> E,
{
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
A: Service<R>,
A: Service,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = E;
type Future = MapErrFuture<A, F, E, R>;
type Future = MapErrFuture<A, F, E>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready().map_err(&self.f)
}
fn call(&mut self, req: R) -> Self::Future {
MapErrFuture {
fut: self.service.call(req),
f: self.f.clone(),
}
fn call(&mut self, req: A::Request) -> Self::Future {
MapErrFuture::new(self.service.call(req), self.f.clone())
}
}
pub struct MapErrFuture<A, F, E, R>
pub struct MapErrFuture<A, F, E>
where
A: Service<R>,
A: Service,
F: Fn(A::Error) -> E,
{
f: F,
fut: A::Future,
}
impl<A, F, E, R> Future for MapErrFuture<A, F, E, R>
impl<A, F, E> MapErrFuture<A, F, E>
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,
{
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> {
/// Create new `MapErr` new service instance
pub fn new<R>(a: A, f: F) -> Self
pub fn new(a: A, f: F) -> Self
where
A: NewService<R, C>,
A: NewService<C>,
F: Fn(A::Error) -> E,
{
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
A: NewService<R, C>,
A: NewService<C>,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = E;
type Service = MapErr<A::Service, F, E>;
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 {
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
A: NewService<R, C>,
A: NewService<C>,
F: Fn(A::Error) -> E,
{
fut: A::Future,
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
A: NewService<R, C>,
A: NewService<C>,
F: Fn(A::Error) -> E,
{
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
A: NewService<R, C>,
A: NewService<C>,
F: Fn(A::Error) -> E + Clone,
{
type Item = MapErr<A::Service, F, E>;
@ -187,7 +196,8 @@ mod tests {
struct Srv;
impl Service<()> for Srv {
impl Service for Srv {
type Request = ();
type Response = ();
type Error = ();
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> {
/// Create new `MapInitErr` combinator
pub fn new<R>(a: A, f: F) -> Self
pub fn new(a: A, f: F) -> Self
where
A: NewService<R, C>,
A: NewService<C>,
F: Fn(A::InitError) -> E,
{
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
A: NewService<R, C>,
A: NewService<C>,
F: Fn(A::InitError) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type Service = A::Service;
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 {
MapInitErrFuture {
fut: self.a.new_service(cfg),
f: self.f.clone(),
}
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
}
}
pub struct MapInitErrFuture<A, F, E, R, C>
pub struct MapInitErrFuture<A, F, E, C>
where
A: NewService<R, C>,
A: NewService<C>,
F: Fn(A::InitError) -> E,
{
f: F,
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
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,
{
type Item = A::Service;

View File

@ -2,7 +2,7 @@ use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll};
use super::{NewService, Service};
use super::{IntoNewService, NewService, Service};
use crate::cell::Cell;
/// 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> {
/// 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
A: Service<R>,
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
{
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
A: Service<R>,
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
{
type Request = A::Request;
type Response = B::Response;
type Error = B::Error;
type Future = ThenFuture<A, B, R>;
type Future = ThenFuture<A, B>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.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())
}
}
pub struct ThenFuture<A, B, R>
pub struct ThenFuture<A, B>
where
A: Service<R>,
B: Service<Result<A::Response, A::Error>>,
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
{
b: Cell<B>,
fut_b: Option<B::Future>,
fut_a: Option<A::Future>,
}
impl<A, B, R> ThenFuture<A, B, R>
impl<A, B> ThenFuture<A, B>
where
A: Service<R>,
B: Service<Result<A::Response, A::Error>>,
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
{
fn new(a: A::Future, b: Cell<B>) -> Self {
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
A: Service<R>,
B: Service<Result<A::Response, A::Error>>,
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
{
type Item = B::Response;
type Error = B::Error;
@ -118,35 +119,42 @@ pub struct ThenNewService<A, B, C> {
impl<A, B, C> ThenNewService<A, B, C> {
/// Create new `AndThen` combinator
pub fn new<R>(a: A, f: B) -> Self
pub fn new<F>(a: A, f: F) -> Self
where
A: NewService<R, C>,
A: NewService<C>,
B: NewService<
Result<A::Response, A::Error>,
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
F: IntoNewService<B, C>,
{
Self {
a,
b: f,
b: f.into_new_service(),
_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
A: NewService<R, C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Service = Then<A::Service, B::Service>;
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 {
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
A: NewService<R, C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{
fut_b: B::Future,
fut_a: A::Future,
@ -178,10 +191,15 @@ where
b: Option<B::Service>,
}
impl<A, B, R, C> ThenNewServiceFuture<A, B, R, C>
impl<A, B, C> ThenNewServiceFuture<A, B, C>
where
A: NewService<R, C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
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 {
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
A: NewService<R, C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{
type Item = Then<A::Service, B::Service>;
type Error = A::InitError;
@ -236,7 +259,8 @@ mod tests {
#[derive(Clone)]
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 Error = ();
type Future = FutureResult<Self::Response, Self::Error>;
@ -256,7 +280,8 @@ mod tests {
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 Error = ();
type Future = FutureResult<Self::Response, ()>;

View File

@ -9,10 +9,11 @@ use crate::{NewService, Service};
/// `Transform` service factory.
///
/// Transform factory creates service that wraps other services.
///
/// * `S` is a wrapped service.
/// * `R` requests handled by this transform service.
pub trait Transform<S, R> {
/// `Config` is a service factory configuration type.
pub trait Transform<S> {
/// Requests handled by the service.
type Request;
/// Responses given by the service.
type Response;
@ -20,7 +21,11 @@ pub trait Transform<S, R> {
type Error;
/// 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.
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
T: Transform<S, R>,
T: Transform<S>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
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
T: Transform<S, R>,
T: Transform<S>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
@ -73,17 +80,17 @@ where
}
/// Trait for types that can be converted to a *transform service*
pub trait IntoTransform<T, S, R>
pub trait IntoTransform<T, S>
where
T: Transform<S, R>,
T: Transform<S>,
{
/// Convert to a `TransformService`
fn into_transform(self) -> T;
}
impl<T, S, R> IntoTransform<T, S, R> for T
impl<T, S> IntoTransform<T, S> for T
where
T: Transform<S, R>,
T: Transform<S>,
{
fn into_transform(self) -> T {
self
@ -92,19 +99,19 @@ where
/// `Apply` transform new service
#[derive(Clone)]
pub struct ApplyTransform<T, S, R, Req, Cfg> {
a: S,
pub struct ApplyTransform<T, A, C> {
a: A,
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
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
/// 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 {
a,
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
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Service = T::Transform;
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 {
t_cell: self.t.clone(),
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
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
fut_a: S::Future,
fut_t: Option<T::Future>,
fut_a: A::Future,
fut_t: Option<<T::Future as IntoFuture>::Future>,
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
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
type Item = T::Transform;
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> {
/// Create new `MapInitErr` new transform instance
pub fn new<R>(t: T, f: F) -> Self
pub fn new(t: T, f: F) -> Self
where
T: Transform<S, R>,
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
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
T: Transform<S, R>,
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
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 {
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
T: Transform<S, R>,
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fut: T::Future,
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
T: Transform<S, R>,
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
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
T: Transform<S, R>,
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Item = T::Transform;