Skip to content

Commit

Permalink
refactor(server): remove double nested listen errors in from_tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Aug 8, 2018
1 parent bb4c5e2 commit 0ea3b83
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
9 changes: 3 additions & 6 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,10 @@ impl Server<AddrIncoming, ()> {
}

/// Create a new instance from a `std::net::TcpListener` instance.
pub fn from_tcp(
listener: StdTcpListener,
) -> Result<Builder<AddrIncoming>, ::Error> {
pub fn from_tcp(listener: StdTcpListener) -> Result<Builder<AddrIncoming>, ::Error> {
let handle = tokio_reactor::Handle::current();
let incoming = AddrIncoming::from_tcp(listener, &handle)
.map_err(|err| ::Error::new_listen(err))?;
Ok(Self::builder(incoming))
AddrIncoming::from_std(listener, &handle)
.map(Server::builder)
}
}

Expand Down
28 changes: 9 additions & 19 deletions src/server/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,19 @@ pub struct AddrIncoming {
}

impl AddrIncoming {
pub(super) fn new(addr: &SocketAddr, handle: Option<&Handle>) -> ::Result<AddrIncoming> {
let listener = if let Some(handle) = handle {
let std_listener = StdTcpListener::bind(addr)
pub(super) fn new(addr: &SocketAddr, handle: Option<&Handle>) -> ::Result<Self> {
let std_listener = StdTcpListener::bind(addr)
.map_err(::Error::new_listen)?;
TcpListener::from_std(std_listener, handle)
.map_err(::Error::new_listen)?
} else {
TcpListener::bind(addr).map_err(::Error::new_listen)?
};

let addr = listener.local_addr().map_err(::Error::new_listen)?;

Ok(AddrIncoming {
addr: addr,
listener: listener,
sleep_on_errors: true,
tcp_keepalive_timeout: None,
tcp_nodelay: false,
timeout: None,
})
if let Some(handle) = handle {
AddrIncoming::from_std(std_listener, handle)
} else {
let handle = Handle::current();
AddrIncoming::from_std(std_listener, &handle)
}
}

pub(super) fn from_tcp(std_listener: StdTcpListener, handle: &Handle) -> ::Result<Self>{
pub(super) fn from_std(std_listener: StdTcpListener, handle: &Handle) -> ::Result<Self> {
let listener = TcpListener::from_std(std_listener, &handle)
.map_err(::Error::new_listen)?;
let addr = listener.local_addr().map_err(::Error::new_listen)?;
Expand Down

0 comments on commit 0ea3b83

Please sign in to comment.