From 0eae4d84b1879896678a786e1b1f1835aaafe992 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 24 Aug 2018 13:18:05 -0700 Subject: [PATCH] fix and and then new service --- src/configurable.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- src/service.rs | 12 +++++--- 3 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/configurable.rs b/src/configurable.rs index 0a6e5f601..6be5c2490 100644 --- a/src/configurable.rs +++ b/src/configurable.rs @@ -90,6 +90,79 @@ where } } +pub struct Fn2NewConfigurableService +where + S: Service, + F: Fn(Cfg) -> Fut, + Fut: IntoFuture, +{ + f: F, + err: marker::PhantomData, + cfg: marker::PhantomData, + fut: marker::PhantomData, + s: marker::PhantomData, +} + +impl Fn2NewConfigurableService +where + S: Service, + F: Fn(Cfg) -> Fut + 'static, + Fut: IntoFuture, +{ + fn new(f: F) -> Self { + Fn2NewConfigurableService{ + f, + err: marker::PhantomData, + cfg: marker::PhantomData, + fut: marker::PhantomData, + s: marker::PhantomData + } + } +} + +impl + IntoNewConfigurableService> + for F +where + S: Service, + F: Fn(Cfg) -> Fut + 'static, + Fut: IntoFuture, +{ + fn into_new_service(self) -> Fn2NewConfigurableService { + Fn2NewConfigurableService::new(self) + } +} + +impl Clone for Fn2NewConfigurableService +where + S: Service, + F: Fn(Cfg) -> Fut + Clone + 'static, + Fut: IntoFuture, +{ + fn clone(&self) -> Self { + Self::new(self.f.clone()) + } +} + +impl NewConfigurableService for Fn2NewConfigurableService +where + S: Service, + F: Fn(Cfg) -> Fut, + Fut: IntoFuture, +{ + type Request = S::Request; + type Response = S::Response; + type Error = S::Error; + type Service = S; + type Config = Cfg; + type InitError = Err; + type Future = Fut::Future; + + fn new_service(&self, cfg: Cfg) -> Self::Future { + (self.f)(cfg).into_future() + } +} + pub struct FnNewConfigurableService where F: Fn(Req) -> Fut, diff --git a/src/lib.rs b/src/lib.rs index c90d0041a..1abeff3c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,7 +60,7 @@ pub mod service; pub mod ssl; mod worker; -pub use configurable::NewConfigurableService; +pub use configurable::{NewConfigurableService, IntoNewConfigurableService}; pub use connector::{Connector, ConnectorError}; pub use server::Server; pub use service::{IntoNewService, IntoService, NewServiceExt}; diff --git a/src/service.rs b/src/service.rs index 5ead53cef..b655f0e7e 100644 --- a/src/service.rs +++ b/src/service.rs @@ -521,12 +521,16 @@ where type Error = B::InitError; fn poll(&mut self) -> Poll { - if let Async::Ready(service) = self.fut_a.poll()? { - self.a = Some(service); + if self.a.is_none() { + if let Async::Ready(service) = self.fut_a.poll()? { + self.a = Some(service); + } } - if let Async::Ready(service) = self.fut_b.poll()? { - self.b = Some(service); + if self.b.is_none() { + if let Async::Ready(service) = self.fut_b.poll()? { + self.b = Some(service); + } } if self.a.is_some() && self.b.is_some() {