From 49867b5e9d0a50545d75acf1f042bfa48d010fda Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 8 Mar 2019 20:50:29 -0800 Subject: [PATCH] fix IntoService --- actix-service/src/fn_service.rs | 50 +++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 3d55b333..0fdfac14 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -1,7 +1,7 @@ use std::marker::PhantomData; -use futures::future::{ok, FutureResult}; -use futures::{Async, IntoFuture, Poll}; +use futures::future::{ok, Future, FutureResult}; +use futures::{try_ready, Async, IntoFuture, Poll}; use crate::{IntoConfigurableNewService, IntoNewService, IntoService, NewService, Service}; @@ -216,7 +216,8 @@ where pub struct FnNewServiceConfig where F: Fn(&C) -> R, - R: IntoFuture, + R: IntoFuture, + R::Item: IntoService, S: Service, { f: F, @@ -226,7 +227,8 @@ where impl FnNewServiceConfig where F: Fn(&C) -> R, - R: IntoFuture, + R: IntoFuture, + R::Item: IntoService, S: Service, { pub fn new(f: F) -> Self { @@ -237,7 +239,8 @@ where impl NewService for FnNewServiceConfig where F: Fn(&C) -> R, - R: IntoFuture, + R: IntoFuture, + R::Item: IntoService, S: Service, { type Response = S::Response; @@ -245,17 +248,45 @@ where type Service = S; type InitError = E; - type Future = R::Future; + type Future = FnNewServiceConfigFut; fn new_service(&self, cfg: &C) -> Self::Future { - (self.f)(cfg).into_future() + FnNewServiceConfigFut { + fut: (self.f)(cfg).into_future(), + _t: PhantomData, + } + } +} + +pub struct FnNewServiceConfigFut +where + R: IntoFuture, + R::Item: IntoService, + S: Service, +{ + fut: R::Future, + _t: PhantomData<(S, Req)>, +} + +impl Future for FnNewServiceConfigFut +where + R: IntoFuture, + R::Item: IntoService, + S: Service, +{ + type Item = S; + type Error = R::Error; + + fn poll(&mut self) -> Poll { + Ok(Async::Ready(try_ready!(self.fut.poll()).into_service())) } } impl Clone for FnNewServiceConfig where F: Fn(&C) -> R + Clone, - R: IntoFuture, + R: IntoFuture, + R::Item: IntoService, S: Service, { fn clone(&self) -> Self { @@ -267,7 +298,8 @@ impl IntoConfigurableNewService, Req, C> for F where F: Fn(&C) -> R, - R: IntoFuture, + R: IntoFuture, + R::Item: IntoService, S: Service, { fn into_new_service(self) -> FnNewServiceConfig {