From ae627db266600f8d010b6eeb9d1be0fff677f0ce Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 30 Jan 2018 13:01:34 -0800 Subject: Change `net::Incoming` signature to match std. (#89) std's `Incoming` iterator yields `TcpStream` instances. This patch updates the `Incoming` future to match this signature. This changes the yielded value from `(TcpStream, SocketAddr)` -> `TcpStream`. --- examples/chat.rs | 4 +++- examples/compress.rs | 3 ++- examples/echo-threads.rs | 2 +- examples/echo.rs | 11 +++++------ examples/hello.rs | 2 +- examples/proxy.rs | 6 +++--- examples/sink.rs | 4 ++-- examples/tinydb.rs | 2 +- 8 files changed, 18 insertions(+), 16 deletions(-) (limited to 'examples') diff --git a/examples/chat.rs b/examples/chat.rs index d141f62b..76e689b9 100644 --- a/examples/chat.rs +++ b/examples/chat.rs @@ -49,7 +49,9 @@ fn main() { // Once the same thread executor lands, transition to single threaded. let connections = Arc::new(Mutex::new(HashMap::new())); - let srv = socket.incoming().for_each(move |(stream, addr)| { + let srv = socket.incoming().for_each(move |stream| { + let addr = stream.peer_addr().unwrap(); + println!("New Connection: {}", addr); let (reader, writer) = stream.split(); diff --git a/examples/compress.rs b/examples/compress.rs index f0c2e23f..3098abf7 100644 --- a/examples/compress.rs +++ b/examples/compress.rs @@ -50,7 +50,8 @@ fn main() { // The compress logic will happen in the function below, but everything's // still a future! Each client is spawned to concurrently get processed. - let server = socket.incoming().for_each(move |(socket, addr)| { + let server = socket.incoming().for_each(move |socket| { + let addr = socket.peer_addr().unwrap(); pool.execute(compress(socket, &pool).then(move |result| { match result { Ok((r, w)) => println!("{}: compressed {} bytes to {}", addr, r, w), diff --git a/examples/echo-threads.rs b/examples/echo-threads.rs index 8ac42d0f..6ce8b156 100644 --- a/examples/echo-threads.rs +++ b/examples/echo-threads.rs @@ -56,7 +56,7 @@ fn main() { // shipped round-robin to a particular thread which will associate the // socket with the corresponding event loop and process the connection. let mut next = 0; - let srv = listener.incoming().for_each(|(socket, _)| { + let srv = listener.incoming().for_each(|socket| { channels[next].unbounded_send(socket).expect("worker thread died"); next = (next + 1) % channels.len(); Ok(()) diff --git a/examples/echo.rs b/examples/echo.rs index f7219113..558f3a68 100644 --- a/examples/echo.rs +++ b/examples/echo.rs @@ -60,12 +60,11 @@ fn main() { // connections made to the server). The return value of the `for_each` // method is itself a future representing processing the entire stream of // connections, and ends up being our server. - let done = socket.incoming().for_each(move |(socket, addr)| { + let done = socket.incoming().for_each(move |socket| { // Once we're inside this closure this represents an accepted client - // from our server. The `socket` is the client connection and `addr` is - // the remote address of the client (similar to how the standard library - // operates). + // from our server. The `socket` is the client connection (similar to + // how the standard library operates). // // We just want to copy all data read from the socket back onto the // socket itself (e.g. "echo"). We can use the standard `io::copy` @@ -88,8 +87,8 @@ fn main() { // information. let msg = amt.then(move |result| { match result { - Ok((amt, _, _)) => println!("wrote {} bytes to {}", amt, addr), - Err(e) => println!("error on {}: {}", addr, e), + Ok((amt, _, _)) => println!("wrote {} bytes", amt), + Err(e) => println!("error: {}", e), } Ok(()) diff --git a/examples/hello.rs b/examples/hello.rs index 46b36b5d..5ceb431b 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -33,7 +33,7 @@ fn main() { println!("Listening for connections on {}", addr); let clients = listener.incoming(); - let welcomes = clients.and_then(|(socket, _peer_addr)| { + let welcomes = clients.and_then(|socket| { tokio_io::io::write_all(socket, b"Hello!\n") }); let server = welcomes.for_each(|(_socket, _welcome)| { diff --git a/examples/proxy.rs b/examples/proxy.rs index c64ead6f..98b86e9f 100644 --- a/examples/proxy.rs +++ b/examples/proxy.rs @@ -48,7 +48,7 @@ fn main() { println!("Listening on: {}", listen_addr); println!("Proxying to: {}", server_addr); - let done = socket.incoming().for_each(move |(client, client_addr)| { + let done = socket.incoming().for_each(move |client| { let server = TcpStream::connect(&server_addr); let amounts = server.and_then(move |server| { // Create separate read/write handles for the TCP clients that we're @@ -82,8 +82,8 @@ fn main() { }); let msg = amounts.map(move |(from_client, from_server)| { - println!("client at {} wrote {} bytes and received {} bytes", - client_addr, from_client, from_server); + println!("client wrote {} bytes and received {} bytes", + from_client, from_server); }).map_err(|e| { // Don't panic. Maybe the client just disconnected too soon. println!("error: {}", e); diff --git a/examples/sink.rs b/examples/sink.rs index 82bde294..21456ada 100644 --- a/examples/sink.rs +++ b/examples/sink.rs @@ -41,8 +41,8 @@ fn main() { let socket = TcpListener::bind(&addr).unwrap(); println!("Listening on: {}", addr); - let server = socket.incoming().for_each(|(socket, addr)| { - println!("got a socket: {}", addr); + let server = socket.incoming().for_each(|socket| { + println!("got a socket: {}", socket.peer_addr().unwrap()); pool.execute(write(socket).or_else(|_| Ok(()))).unwrap(); Ok(()) }); diff --git a/examples/tinydb.rs b/examples/tinydb.rs index 19a7396d..0a68a314 100644 --- a/examples/tinydb.rs +++ b/examples/tinydb.rs @@ -100,7 +100,7 @@ fn main() { map: Mutex::new(initial_db), }); - let done = listener.incoming().for_each(move |(socket, _addr)| { + let done = listener.incoming().for_each(move |socket| { // As with many other small examples, the first thing we'll do is // *split* this TCP stream into two separately owned halves. This'll // allow us to work with the read and write halves independently. -- cgit v1.2.3