From 0f8cd0f44da8edf871328ca29a489d02a67c5a47 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 21 Aug 2018 20:42:43 -0700 Subject: [PATCH] better FnNewService definition --- examples/basic.rs | 12 +++--------- src/service.rs | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 0650a078..1b60cdf3 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -68,19 +68,13 @@ fn main() { SslAcceptorExt::accept_async(&acceptor, stream) .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) }) - // convert closure to a `NewService` and convert init error - .into_new_service().map_init_err(|_| io::Error::new(io::ErrorKind::Other, "")) + // convert closure to a `NewService` + .into_new_service() // .and_then() combinator uses other service to convert incoming `Request` to a `Response` // and then uses that response as an input for next service. // in this case, on success we use `logger` service - .and_then( - // convert function to a Service and related NewService impl - logger.into_new_service() - // if service is a function actix-net generate NewService impl with InitError=(), - // but actix_net::Server requires io::Error as InitError, - // we need to convert it to a `io::Error` - .map_init_err(|_| io::Error::new(io::ErrorKind::Other, ""))) + .and_then(logger) // next service uses two components, service state and service function // actix-net generates `NewService` impl that creates `ServiceState` instance for each new service diff --git a/src/service.rs b/src/service.rs index 930b7bda..e1440c6a 100644 --- a/src/service.rs +++ b/src/service.rs @@ -163,7 +163,7 @@ where } } -pub struct FnNewService +pub struct FnNewService where F: Fn(Req) -> Fut, Fut: IntoFuture, @@ -172,10 +172,11 @@ where req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, + ierr: marker::PhantomData, cfg: marker::PhantomData, } -impl FnNewService +impl FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, @@ -186,12 +187,13 @@ where req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, + ierr: marker::PhantomData, cfg: marker::PhantomData, } } } -impl NewService for FnNewService +impl NewService for FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, @@ -202,27 +204,27 @@ where type Error = Err; type Service = FnService; type Config = Cfg; - type InitError = (); - type Future = FutureResult; + type InitError = IErr; + type Future = FutureResult; fn new_service(&self, cfg: Cfg) -> Self::Future { future::ok(FnService::new(self.f.clone())) } } -impl IntoNewService> +impl IntoNewService> for F where F: Fn(Req) -> Fut + Clone + 'static, Fut: IntoFuture, Cfg: Clone, { - fn into_new_service(self) -> FnNewService { + fn into_new_service(self) -> FnNewService { FnNewService::new(self) } } -impl Clone for FnNewService +impl Clone for FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture,