From 329bca15a6d8e00e4de967e40da8aaf346cf54cc Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 5 Dec 2017 08:17:08 -0800 Subject: [PATCH] Remove `TcpListener::pending_accept` field No need for oneshot shenanigans as now we'll always have the `Handle` available to us regardless of what thread we're on to associate a new socket --- src/net/tcp.rs | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/src/net/tcp.rs b/src/net/tcp.rs index d68cef398..07fae1e61 100644 --- a/src/net/tcp.rs +++ b/src/net/tcp.rs @@ -6,7 +6,6 @@ use std::time::Duration; use bytes::{Buf, BufMut}; use futures::stream::Stream; -use futures::sync::oneshot; use futures::{Future, Poll, Async}; use iovec::IoVec; use mio; @@ -20,7 +19,6 @@ use reactor::{Handle, PollEvented}; /// various forms of processing. pub struct TcpListener { io: PollEvented, - pending_accept: Option>>, } /// Stream returned by the `TcpListener::incoming` function representing the @@ -59,33 +57,21 @@ impl TcpListener { /// future's task. It's recommended to only call this from the /// implementation of a `Future::poll`, if necessary. pub fn accept(&mut self) -> io::Result<(TcpStream, SocketAddr)> { - loop { - if let Some(mut pending) = self.pending_accept.take() { - match pending.poll().expect("shouldn't be canceled") { - Async::NotReady => { - self.pending_accept = Some(pending); - return Err(io::ErrorKind::WouldBlock.into()) - }, - Async::Ready(r) => return r, - } - } + if let Async::NotReady = self.io.poll_read() { + return Err(io::Error::new(io::ErrorKind::WouldBlock, "not ready")) + } - if let Async::NotReady = self.io.poll_read() { - return Err(io::Error::new(io::ErrorKind::WouldBlock, "not ready")) - } - - match self.io.get_ref().accept() { - Err(e) => { - if e.kind() == io::ErrorKind::WouldBlock { - self.io.need_read(); - } - return Err(e) - }, - Ok((sock, addr)) => { - let handle = self.io.handle(); - let io = try!(PollEvented::new(sock, &handle)); - return Ok((TcpStream { io: io }, addr)) + match self.io.get_ref().accept() { + Err(e) => { + if e.kind() == io::ErrorKind::WouldBlock { + self.io.need_read(); } + Err(e) + }, + Ok((sock, addr)) => { + let handle = self.io.handle(); + let io = try!(PollEvented::new(sock, &handle)); + Ok((TcpStream { io: io }, addr)) } } } @@ -127,7 +113,7 @@ impl TcpListener { fn new(listener: mio::net::TcpListener, handle: &Handle) -> io::Result { let io = try!(PollEvented::new(listener, handle)); - Ok(TcpListener { io: io, pending_accept: None }) + Ok(TcpListener { io: io }) } /// Test whether this socket is ready to be read or not.