From d97f78afbe225e071356ceda30f6a8ee08bdc58b Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 23 Aug 2018 13:16:14 -0700 Subject: [PATCH] use factory function instead of NewService for service registration --- src/server.rs | 27 +++++++++------------------ src/server_service.rs | 21 +++++++++------------ 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/src/server.rs b/src/server.rs index 6f129ac9..efdd5541 100644 --- a/src/server.rs +++ b/src/server.rs @@ -19,7 +19,7 @@ use actix::{ use super::accept::{AcceptLoop, AcceptNotify, Command}; use super::server_config::{Config, ServerConfig}; use super::server_service::{ServerNewService, ServerServiceFactory}; -use super::service::{IntoNewService, NewService}; +use super::service::NewService; use super::worker::{Conn, StopWorker, Worker, WorkerClient}; use super::{PauseServer, ResumeServer, StopServer, Token}; @@ -168,14 +168,11 @@ impl Server { } /// Add new service to server - pub fn bind(mut self, addr: U, srv: T) -> io::Result + pub fn bind(mut self, addr: U, factory: F) -> io::Result where + F: Fn() -> N + Copy + Send + 'static, U: net::ToSocketAddrs, - T: IntoNewService + Clone, - N: NewService - + Clone - + Send - + 'static, + N: NewService + 'static, N::Service: 'static, N::Future: 'static, N::Error: fmt::Display, @@ -183,28 +180,22 @@ impl Server { let sockets = bind_addr(addr)?; for lst in sockets { - self = self.listen(lst, srv.clone()) + self = self.listen(lst, factory.clone()) } Ok(self) } /// Add new service to server - pub fn listen(mut self, lst: net::TcpListener, srv: T) -> Self + pub fn listen(mut self, lst: net::TcpListener, factory: F) -> Self where - T: IntoNewService, - N: NewService - + Clone - + Send - + 'static, + F: Fn() -> N + Copy + Send + 'static, + N: NewService + 'static, N::Service: 'static, N::Future: 'static, N::Error: fmt::Display, { let token = Token(self.services.len()); - self.services.push(ServerNewService::create( - srv.into_new_service(), - self.config.clone(), - )); + self.services.push(ServerNewService::create(factory, self.config.clone())); self.sockets.push((token, lst)); self } diff --git a/src/server_service.rs b/src/server_service.rs index 15083a6a..4d99c182 100644 --- a/src/server_service.rs +++ b/src/server_service.rs @@ -56,23 +56,21 @@ where } } -pub(crate) struct ServerNewService { - inner: T, +pub(crate) struct ServerNewService where F: Fn() -> T + Send + Clone { + inner: F, config: C, counter: Arc, } -impl ServerNewService +impl ServerNewService where - T: NewService - + Clone - + Send - + 'static, + F: Fn() -> T + Send + Clone + 'static, + T: NewService + 'static, T::Service: 'static, T::Future: 'static, T::Error: fmt::Display, { - pub(crate) fn create(inner: T, config: C) -> Box + Send> { + pub(crate) fn create(inner: F, config: C) -> Box + Send> { Box::new(Self { inner, config, @@ -89,11 +87,10 @@ pub trait ServerServiceFactory { fn create(&self) -> Box>; } -impl ServerServiceFactory for ServerNewService +impl ServerServiceFactory for ServerNewService where + F: Fn() -> T + Send + Clone + 'static, T: NewService - + Clone - + Send + 'static, T::Service: 'static, T::Future: 'static, @@ -114,7 +111,7 @@ where fn create(&self) -> Box> { let counter = self.counter.clone(); Box::new( - self.inner + (self.inner)() .new_service(self.config.clone()) .map_err(|_| ()) .map(move |inner| {