From 10628d453c4a8dafccdd8165d5679aedc724ce22 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Mon, 6 Aug 2018 17:55:39 +0200 Subject: [PATCH] feat(server): add from_tcp method Adds the `from_tcp()` method as per https://github.com/hyperium/hyper/issues/1602. fix clippy changes --- src/server/mod.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index f07ee03554..3a0585cf7e 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -51,21 +51,31 @@ //! ``` pub mod conn; -#[cfg(feature = "runtime")] mod tcp; +#[cfg(feature = "runtime")] +mod tcp; use std::fmt; -#[cfg(feature = "runtime")] use std::net::SocketAddr; -#[cfg(feature = "runtime")] use std::time::Duration; +#[cfg(feature = "runtime")] +use std::net; +#[cfg(feature = "runtime")] +use std::net::SocketAddr; +#[cfg(feature = "runtime")] +use std::time::Duration; -use futures::{Future, Stream, Poll}; +use futures::{Future, Poll, Stream}; use tokio_io::{AsyncRead, AsyncWrite}; +#[cfg(feature = "runtime")] +use tokio_reactor; +#[cfg(feature = "runtime")] +use tokio_tcp; use body::{Body, Payload}; use service::{NewService, Service}; // Renamed `Http` as `Http_` for now so that people upgrading don't see an // error that `hyper::server::Http` is private... use self::conn::{Http as Http_, SpawnAll}; -#[cfg(feature = "runtime")] use self::tcp::{AddrIncoming}; +#[cfg(feature = "runtime")] +use self::tcp::AddrIncoming; /// A listening HTTP server that accepts connections in both HTTP1 and HTTP2 by default. /// @@ -96,6 +106,19 @@ impl Server { } } +impl Server { + /// Create a new instance from a `std::net::TcpListener` instance. + #[cfg(feature = "runtime")] + pub fn from_tcp( + listener: net::TcpListener, + ) -> Result, ::Error> { + let handle = tokio_reactor::Handle::current(); + let listener = tokio_tcp::TcpListener::from_std(listener, &handle) + .map_err(|err| ::Error::new_listen(err))?; + Ok(Self::builder(listener.incoming())) + } +} + #[cfg(feature = "runtime")] impl Server { /// Binds to the provided address, and returns a [`Builder`](Builder). @@ -275,4 +298,3 @@ impl Builder { self } } -