From 27af05de9a4d74660de8805047d26c53ac0fbf3f Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 31 Aug 2018 12:51:26 -0700 Subject: [PATCH] simplify Apply combinator --- src/service/apply.rs | 93 +++++++++++++++++--------------------------- src/service/mod.rs | 12 +++--- 2 files changed, 42 insertions(+), 63 deletions(-) diff --git a/src/service/apply.rs b/src/service/apply.rs index a39663d32..58755f6fa 100644 --- a/src/service/apply.rs +++ b/src/service/apply.rs @@ -1,24 +1,21 @@ use std::marker::PhantomData; -use futures::{Async, Future, Poll}; +use futures::{Async, Future, IntoFuture, Poll}; use {IntoNewService, NewService, Service}; -/// `ApplyService` service combinator -pub struct Apply { +/// `Apply` service combinator +pub struct Apply { service: T, f: F, - r: PhantomData, - r1: PhantomData, - r2: PhantomData, - e: PhantomData, + r: PhantomData R>, } -impl Apply +impl Apply where T: Service, - T::Error: Into, + T::Error: Into<::Error>, F: Fn(Req, &mut T) -> R, - R: Future, + R: IntoFuture, { /// Create new `Apply` combinator pub fn new(f: F, service: T) -> Self { @@ -26,49 +23,43 @@ where service, f, r: PhantomData, - r1: PhantomData, - r2: PhantomData, - e: PhantomData, } } } -impl Service for Apply +impl Service for Apply where T: Service, - T::Error: Into, + T::Error: Into<::Error>, F: Fn(Req, &mut T) -> R, - R: Future, + R: IntoFuture, { type Request = Req; - type Response = Resp; - type Error = Err; - type Future = R; + type Response = ::Item; + type Error = ::Error; + type Future = R::Future; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready().map_err(|e| e.into()) } fn call(&mut self, req: Self::Request) -> Self::Future { - (self.f)(req, &mut self.service) + (self.f)(req, &mut self.service).into_future() } } /// `ApplyNewService` new service combinator -pub struct ApplyNewService { +pub struct ApplyNewService { service: T, f: F, - r: PhantomData, - r1: PhantomData, - r2: PhantomData, - e: PhantomData, + r: PhantomData R>, } -impl ApplyNewService +impl ApplyNewService where T: NewService, F: Fn(Req, &mut T::Service) -> R, - R: Future, + R: IntoFuture, { /// Create new `ApplyNewService` new service instance pub fn new>(f: F, service: F1) -> Self { @@ -76,91 +67,79 @@ where f, service: service.into_new_service(), r: PhantomData, - r1: PhantomData, - r2: PhantomData, - e: PhantomData, } } } -impl Clone for ApplyNewService +impl Clone for ApplyNewService where T: NewService + Clone, F: Fn(Req, &mut T::Service) -> R + Clone, - R: Future, + R: IntoFuture, { fn clone(&self) -> Self { Self { service: self.service.clone(), f: self.f.clone(), r: PhantomData, - r1: PhantomData, - r2: PhantomData, - e: PhantomData, } } } -impl NewService for ApplyNewService +impl NewService for ApplyNewService where T: NewService, - T::Error: Into, + T::Error: Into<::Error>, F: Fn(Req, &mut T::Service) -> R + Clone, - R: Future, + R: IntoFuture, { type Request = Req; - type Response = Resp; - type Error = Err; - type Service = Apply; + type Response = ::Item; + type Error = ::Error; + type Service = Apply; type InitError = T::InitError; - type Future = ApplyNewServiceFuture; + type Future = ApplyNewServiceFuture; fn new_service(&self) -> Self::Future { ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone()) } } -pub struct ApplyNewServiceFuture +pub struct ApplyNewServiceFuture where T: NewService, F: Fn(Req, &mut T::Service) -> R, - R: Future, + R: IntoFuture, { fut: T::Future, f: Option, - r: PhantomData, - r1: PhantomData, - r2: PhantomData, - e: PhantomData, + r: PhantomData R>, } -impl ApplyNewServiceFuture +impl ApplyNewServiceFuture where T: NewService, F: Fn(Req, &mut T::Service) -> R, - R: Future, + R: IntoFuture, { fn new(fut: T::Future, f: F) -> Self { ApplyNewServiceFuture { f: Some(f), fut, r: PhantomData, - r1: PhantomData, - r2: PhantomData, - e: PhantomData, } } } -impl Future for ApplyNewServiceFuture +impl Future for ApplyNewServiceFuture where T: NewService, - T::Error: Into, + T::Error: Into<::Error>, F: Fn(Req, &mut T::Service) -> R, - R: Future, + R: IntoFuture, { - type Item = Apply; + type Item = Apply; type Error = T::InitError; fn poll(&mut self) -> Poll { diff --git a/src/service/mod.rs b/src/service/mod.rs index d8e13136e..940ab861d 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -20,12 +20,12 @@ pub use self::map_request::{MapReq, MapReqNewService}; use {NewService, Service}; pub trait ServiceExt: Service { - fn apply(self, f: F) -> Apply + fn apply(self, f: F) -> Apply where Self: Sized, - Self::Error: Into, + Self::Error: Into<::Error>, F: Fn(Req, &mut Self) -> R, - R: Future, + R: IntoFuture, { Apply::new(f, self) } @@ -57,12 +57,12 @@ pub trait ServiceExt: Service { } pub trait NewServiceExt: NewService { - fn apply(self, f: F) -> ApplyNewService + fn apply(self, f: F) -> ApplyNewService where Self: Sized, - Self::Error: Into, + Self::Error: Into<::Error>, F: Fn(Req, &mut Self::Service) -> R + Clone, - R: Future, + R: IntoFuture, { ApplyNewService::new(f, self) }