From 3a5642db1f55acaeb80ebbd873e183defebb27c4 Mon Sep 17 00:00:00 2001 From: Wonwoo Choi Date: Sun, 21 Apr 2019 17:46:26 +0900 Subject: [PATCH 1/2] http-service-hyper: Add serve_async that returns a Future --- http-service-hyper/src/lib.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/http-service-hyper/src/lib.rs b/http-service-hyper/src/lib.rs index b0a1523..62d7327 100644 --- a/http-service-hyper/src/lib.rs +++ b/http-service-hyper/src/lib.rs @@ -80,16 +80,20 @@ where /// Serve the given `HttpService` at the given address, using `hyper` as backend. pub fn serve(s: S, addr: SocketAddr) { + let server = serve_async(s, addr) + .map(|_| Result::<_, ()>::Ok(())) + .compat(); + hyper::rt::run(server); +} + +/// Serve the given `HttpService` at the given address, using `hyper` as backend, and return a +/// `Future` that can be `await`ed on. +pub fn serve_async( + s: S, + addr: SocketAddr, +) -> impl Future> { let service = WrapHttpService { service: Arc::new(s), }; - let server = hyper::Server::bind(&addr) - .serve(service) - .compat() - .map(|_| { - let res: Result<(), ()> = Ok(()); - res - }) - .compat(); - hyper::rt::run(server); + hyper::Server::bind(&addr).serve(service).compat() } From a4870c160e92001eba8029966c31e5001d8277e0 Mon Sep 17 00:00:00 2001 From: Wonwoo Choi Date: Sun, 21 Apr 2019 18:06:26 +0900 Subject: [PATCH 2/2] Rename `serve` to `run`, and `serve_async` to `serve` --- http-service-hyper/src/lib.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/http-service-hyper/src/lib.rs b/http-service-hyper/src/lib.rs index 62d7327..9722e81 100644 --- a/http-service-hyper/src/lib.rs +++ b/http-service-hyper/src/lib.rs @@ -78,17 +78,9 @@ where } } -/// Serve the given `HttpService` at the given address, using `hyper` as backend. -pub fn serve(s: S, addr: SocketAddr) { - let server = serve_async(s, addr) - .map(|_| Result::<_, ()>::Ok(())) - .compat(); - hyper::rt::run(server); -} - /// Serve the given `HttpService` at the given address, using `hyper` as backend, and return a /// `Future` that can be `await`ed on. -pub fn serve_async( +pub fn serve( s: S, addr: SocketAddr, ) -> impl Future> { @@ -97,3 +89,10 @@ pub fn serve_async( }; hyper::Server::bind(&addr).serve(service).compat() } + +/// Run the given `HttpService` at the given address on the default runtime, using `hyper` as +/// backend. +pub fn run(s: S, addr: SocketAddr) { + let server = serve(s, addr).map(|_| Result::<_, ()>::Ok(())).compat(); + hyper::rt::run(server); +}