Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #49 from withoutboats/incoming-by-ref
Browse files Browse the repository at this point in the history
TcpListener::incoming should be by reference
  • Loading branch information
aturon committed Dec 7, 2018
2 parents 5b204d6 + 5245aab commit dcc9a25
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/echo.rs
Expand Up @@ -13,7 +13,7 @@ fn main() -> io::Result<()> {
executor::block_on(async {
let mut threadpool = ThreadPool::new()?;

let listener = TcpListener::bind(&"127.0.0.1:7878".parse().unwrap())?;
let mut listener = TcpListener::bind(&"127.0.0.1:7878".parse().unwrap())?;
let mut incoming = listener.incoming();

println!("Listening on 127.0.0.1:7878");
Expand Down
2 changes: 1 addition & 1 deletion examples/shakespeare.rs
Expand Up @@ -26,7 +26,7 @@ fn main() -> io::Result<()> {
executor::block_on(async {
let mut threadpool = ThreadPool::new()?;

let listener = TcpListener::bind(&"127.0.0.1:7878".parse().unwrap())?;
let mut listener = TcpListener::bind(&"127.0.0.1:7878".parse().unwrap())?;
let mut incoming = listener.incoming();

println!("Listening on 127.0.0.1:7878");
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -28,7 +28,7 @@
//!
//! async fn listen() -> Result<(), Box<dyn std::error::Error + 'static>> {
//! let socket_addr = "127.0.0.1:8080".parse()?;
//! let listener = TcpListener::bind(&socket_addr)?;
//! let mut listener = TcpListener::bind(&socket_addr)?;
//! let mut incoming = listener.incoming();
//!
//! // accept connections and process them serially
Expand Down
20 changes: 7 additions & 13 deletions src/tcp/listener.rs
Expand Up @@ -38,7 +38,7 @@ use crate::reactor::PollEvented;
///
/// async fn listen() -> Result<(), Box<dyn Error + 'static>> {
/// let socket_addr = "127.0.0.1:80".parse()?;
/// let listener = TcpListener::bind(&socket_addr)?;
/// let mut listener = TcpListener::bind(&socket_addr)?;
/// let mut incoming = listener.incoming();
///
/// // accept connections and process them serially
Expand Down Expand Up @@ -130,7 +130,7 @@ impl TcpListener {
///
/// # async fn work () -> Result<(), Box<dyn std::error::Error + 'static>> {
/// let socket_addr = "127.0.0.1:80".parse()?;
/// let listener = TcpListener::bind(&socket_addr)?;
/// let mut listener = TcpListener::bind(&socket_addr)?;
/// let mut incoming = listener.incoming();
///
/// // accept connections and process them serially
Expand All @@ -144,8 +144,8 @@ impl TcpListener {
/// }
/// # Ok(())}
/// ```
pub fn incoming(self) -> Incoming {
Incoming::new(self)
pub fn incoming(&mut self) -> Incoming<'_> {
Incoming { inner: self }
}

/// Gets the value of the `IP_TTL` option for this socket.
Expand Down Expand Up @@ -242,17 +242,11 @@ mod sys {
/// stream of sockets received from a listener.
#[must_use = "streams do nothing unless polled"]
#[derive(Debug)]
pub struct Incoming {
inner: TcpListener,
}

impl Incoming {
pub(crate) fn new(listener: TcpListener) -> Incoming {
Incoming { inner: listener }
}
pub struct Incoming<'a> {
inner: &'a mut TcpListener,
}

impl Stream for Incoming {
impl<'a> Stream for Incoming<'a> {
type Item = io::Result<TcpStream>;

fn poll_next(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Option<Self::Item>> {
Expand Down
2 changes: 1 addition & 1 deletion src/tcp/mod.rs
Expand Up @@ -29,7 +29,7 @@
//!
//! async fn listen() -> Result<(), Box<dyn std::error::Error + 'static>> {
//! let socket_addr = "127.0.0.1:80".parse()?;
//! let listener = TcpListener::bind(&socket_addr)?;
//! let mut listener = TcpListener::bind(&socket_addr)?;
//! let mut incoming = listener.incoming();
//!
//! // accept connections and process them serially
Expand Down
6 changes: 3 additions & 3 deletions tests/tcp.rs
Expand Up @@ -21,7 +21,7 @@ const THE_WINTERS_TALE: &[u8] = b"
#[test]
fn listener_reads() {
drop(env_logger::try_init());
let server = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let mut server = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = server.local_addr().unwrap();

// client thread
Expand All @@ -42,7 +42,7 @@ fn listener_reads() {
#[test]
fn listener_writes() {
drop(env_logger::try_init());
let server = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let mut server = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = server.local_addr().unwrap();

// client thread
Expand All @@ -63,7 +63,7 @@ fn listener_writes() {
#[test]
fn both_sides_async_using_threadpool() {
drop(env_logger::try_init());
let server = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let mut server = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = server.local_addr().unwrap();

let mut pool = executor::ThreadPool::new().unwrap();
Expand Down

0 comments on commit dcc9a25

Please sign in to comment.