From 066965cd59d01fd9d999152e32169a24dfe434fa Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 8 Oct 2020 12:12:56 -0700 Subject: net: use &self with TcpListener::accept (#2919) Uses the infrastructure added by #2828 to enable switching `TcpListener::accept` to use `&self`. This also switches `poll_accept` to use `&self`. While doing introduces a hazard, `poll_*` style functions are considered low-level. Most users will use the `async fn` variants which are more misuse-resistant. TcpListener::incoming() is temporarily removed as it has the same problem as `TcpSocket::by_ref()` and will be implemented later. --- examples/chat.rs | 2 +- examples/echo.rs | 2 +- examples/print_each_packet.rs | 2 +- examples/proxy.rs | 2 +- examples/tinydb.rs | 2 +- examples/tinyhttp.rs | 8 +++----- 6 files changed, 8 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/chat.rs b/examples/chat.rs index c4b8c6a2..3f945039 100644 --- a/examples/chat.rs +++ b/examples/chat.rs @@ -77,7 +77,7 @@ async fn main() -> Result<(), Box> { // Bind a TCP listener to the socket address. // // Note that this is the Tokio TcpListener, which is fully async. - let mut listener = TcpListener::bind(&addr).await?; + let listener = TcpListener::bind(&addr).await?; tracing::info!("server running on {}", addr); diff --git a/examples/echo.rs b/examples/echo.rs index f3068074..d492e07e 100644 --- a/examples/echo.rs +++ b/examples/echo.rs @@ -39,7 +39,7 @@ async fn main() -> Result<(), Box> { // Next up we create a TCP listener which will listen for incoming // connections. This TCP listener is bound to the address we determined // above and must be associated with an event loop. - let mut listener = TcpListener::bind(&addr).await?; + let listener = TcpListener::bind(&addr).await?; println!("Listening on: {}", addr); loop { diff --git a/examples/print_each_packet.rs b/examples/print_each_packet.rs index d650b5bd..b3e1b17e 100644 --- a/examples/print_each_packet.rs +++ b/examples/print_each_packet.rs @@ -74,7 +74,7 @@ async fn main() -> Result<(), Box> { // above and must be associated with an event loop, so we pass in a handle // to our event loop. After the socket's created we inform that we're ready // to go and start accepting connections. - let mut listener = TcpListener::bind(&addr).await?; + let listener = TcpListener::bind(&addr).await?; println!("Listening on: {}", addr); loop { diff --git a/examples/proxy.rs b/examples/proxy.rs index 144f0179..2d9b7ce3 100644 --- a/examples/proxy.rs +++ b/examples/proxy.rs @@ -43,7 +43,7 @@ async fn main() -> Result<(), Box> { println!("Listening on: {}", listen_addr); println!("Proxying to: {}", server_addr); - let mut listener = TcpListener::bind(listen_addr).await?; + let listener = TcpListener::bind(listen_addr).await?; while let Ok((inbound, _)) = listener.accept().await { let transfer = transfer(inbound, server_addr.clone()).map(|r| { diff --git a/examples/tinydb.rs b/examples/tinydb.rs index c1af2541..f0db7fa8 100644 --- a/examples/tinydb.rs +++ b/examples/tinydb.rs @@ -89,7 +89,7 @@ async fn main() -> Result<(), Box> { .nth(1) .unwrap_or_else(|| "127.0.0.1:8080".to_string()); - let mut listener = TcpListener::bind(&addr).await?; + let listener = TcpListener::bind(&addr).await?; println!("Listening on: {}", addr); // Create the shared state of this server that will be shared amongst all diff --git a/examples/tinyhttp.rs b/examples/tinyhttp.rs index 4870aea2..c561bbd3 100644 --- a/examples/tinyhttp.rs +++ b/examples/tinyhttp.rs @@ -30,19 +30,17 @@ async fn main() -> Result<(), Box> { let addr = env::args() .nth(1) .unwrap_or_else(|| "127.0.0.1:8080".to_string()); - let mut server = TcpListener::bind(&addr).await?; - let mut incoming = server.incoming(); + let server = TcpListener::bind(&addr).await?; println!("Listening on: {}", addr); - while let Some(Ok(stream)) = incoming.next().await { + loop { + let (stream, _) = server.accept().await?; tokio::spawn(async move { if let Err(e) = process(stream).await { println!("failed to process connection; error = {}", e); } }); } - - Ok(()) } async fn process(stream: TcpStream) -> Result<(), Box> { -- cgit v1.2.3