From 2ab7dbadce151bf9b3b7e24b6694ae8f52021b12 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 9 Aug 2018 13:38:10 -0700 Subject: [PATCH] better ergonomics for Server::service() method --- src/server/http.rs | 21 +++++++++++---------- src/server/server.rs | 9 +++++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/server/http.rs b/src/server/http.rs index 5deaf029..edf8aef6 100644 --- a/src/server/http.rs +++ b/src/server/http.rs @@ -427,14 +427,19 @@ where } } -impl Into> for HttpServer { - fn into(self) -> Box { - Box::new(HttpService { +impl Into<(Box, Vec<(Token, net::TcpListener)>)> for HttpServer { + fn into(mut self) -> (Box, Vec<(Token, net::TcpListener)>) { + let sockets: Vec<_> = mem::replace(&mut self.sockets, Vec::new()) + .into_iter() + .map(|item| (item.token, item.lst)) + .collect(); + + (Box::new(HttpService { factory: self.factory, host: self.host, keep_alive: self.keep_alive, handlers: self.handlers, - }) + }), sockets) } } @@ -500,7 +505,7 @@ impl HttpServer { /// sys.run(); // <- Run actix system, this method starts all async processes /// } /// ``` - pub fn start(mut self) -> Addr { + pub fn start(self) -> Addr { let mut srv = Server::new() .workers(self.threads) .maxconn(self.maxconn) @@ -514,11 +519,7 @@ impl HttpServer { srv }; - let sockets: Vec<_> = mem::replace(&mut self.sockets, Vec::new()) - .into_iter() - .map(|item| (item.token, item.lst)) - .collect(); - srv.service(self, sockets).start() + srv.service(self).start() } /// Spawn new thread and start listening for incoming connections. diff --git a/src/server/server.rs b/src/server/server.rs index ff88040f..bef1ed16 100644 --- a/src/server/server.rs +++ b/src/server/server.rs @@ -145,11 +145,12 @@ impl Server { } /// Add new service to server - pub fn service(mut self, srv: T, sockets: Vec<(Token, net::TcpListener)>) -> Self - where - T: Into> + pub fn service(mut self, srv: T) -> Self + where + T: Into<(Box, Vec<(Token, net::TcpListener)>)> { - self.services.push(srv.into()); + let (srv, sockets) = srv.into(); + self.services.push(srv); self.sockets.push(sockets); self }