diff --git a/tokio/examples/echo-udp.rs b/tokio/examples/echo-udp.rs index 58a73335f..ffaa9efc5 100644 --- a/tokio/examples/echo-udp.rs +++ b/tokio/examples/echo-udp.rs @@ -13,6 +13,7 @@ #![feature(async_await)] #![deny(warnings, rust_2018_idioms)] +use std::error::Error; use std::net::SocketAddr; use std::{env, io}; use tokio; @@ -50,12 +51,12 @@ impl Server { } #[tokio::main] -async fn main() { +async fn main() -> Result<(), Box> { let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); - let addr = addr.parse::().unwrap(); + let addr = addr.parse::()?; - let socket = UdpSocket::bind(&addr).unwrap(); - println!("Listening on: {}", socket.local_addr().unwrap()); + let socket = UdpSocket::bind(&addr)?; + println!("Listening on: {}", socket.local_addr()?); let server = Server { socket, @@ -64,5 +65,7 @@ async fn main() { }; // This starts the server task. - server.run().await.unwrap(); + server.run().await?; + + Ok(()) } diff --git a/tokio/examples/echo.rs b/tokio/examples/echo.rs index 54ae870b5..40dbcdaef 100644 --- a/tokio/examples/echo.rs +++ b/tokio/examples/echo.rs @@ -27,25 +27,26 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpListener; use std::env; +use std::error::Error; use std::net::SocketAddr; #[tokio::main] -async fn main() { +async fn main() -> Result<(), Box> { // Allow passing an address to listen on as the first argument of this // program, but otherwise we'll just set up our TCP listener on // 127.0.0.1:8080 for connections. let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); - let addr = addr.parse::().unwrap(); + let addr = addr.parse::()?; // 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).unwrap(); + let mut listener = TcpListener::bind(&addr)?; println!("Listening on: {}", addr); loop { // Asynchronously wait for an inbound socket. - let (mut socket, _) = listener.accept().await.unwrap(); + let (mut socket, _) = listener.accept().await?; // And this is where much of the magic of this server happens. We // crucially want all clients to make progress concurrently, rather than @@ -60,13 +61,19 @@ async fn main() { // In a loop, read data from the socket and write the data back. loop { - let n = socket.read(&mut buf).await.unwrap(); + let n = socket + .read(&mut buf) + .await + .expect("failed to read data from socket"); if n == 0 { return; } - socket.write_all(&buf[0..n]).await.unwrap(); + socket + .write_all(&buf[0..n]) + .await + .expect("failed to write data to socket"); } }); } diff --git a/tokio/examples/hello_world.rs b/tokio/examples/hello_world.rs index 8e77c93ee..e58e7d1d4 100644 --- a/tokio/examples/hello_world.rs +++ b/tokio/examples/hello_world.rs @@ -18,16 +18,20 @@ use tokio; use tokio::io::AsyncWriteExt; use tokio::net::TcpStream; +use std::error::Error; + #[tokio::main] -pub async fn main() { - let addr = "127.0.0.1:6142".parse().unwrap(); +pub async fn main() -> Result<(), Box> { + let addr = "127.0.0.1:6142".parse()?; // Open a TCP stream to the socket address. // // Note that this is the Tokio TcpStream, which is fully async. - let mut stream = TcpStream::connect(&addr).await.unwrap(); + let mut stream = TcpStream::connect(&addr).await?; println!("created stream"); let result = stream.write(b"hello world\n").await; println!("wrote to stream; success={:?}", result.is_ok()); + + Ok(()) } diff --git a/tokio/examples/tinydb.rs b/tokio/examples/tinydb.rs index f59c0de44..2b86a543e 100644 --- a/tokio/examples/tinydb.rs +++ b/tokio/examples/tinydb.rs @@ -85,7 +85,7 @@ enum Response { } #[tokio::main] -async fn main() -> Result<(), Box> { +async fn main() -> Result<(), Box> { // Parse the address we're going to run this server on // and set up our TCP listener to accept connections. let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); diff --git a/tokio/examples/udp-client.rs b/tokio/examples/udp-client.rs index b26c0a37a..fb7e18f04 100644 --- a/tokio/examples/udp-client.rs +++ b/tokio/examples/udp-client.rs @@ -30,6 +30,7 @@ #![deny(warnings, rust_2018_idioms)] use std::env; +use std::error::Error; use std::io::{stdin, Read}; use std::net::SocketAddr; use tokio::net::UdpSocket; @@ -41,12 +42,11 @@ fn get_stdin_data() -> Result, Box> { } #[tokio::main] -async fn main() { +async fn main() -> Result<(), Box> { let remote_addr: SocketAddr = env::args() .nth(1) .unwrap_or("127.0.0.1:8080".into()) - .parse() - .unwrap(); + .parse()?; // We use port 0 to let the operating system allocate an available port for us. let local_addr: SocketAddr = if remote_addr.is_ipv4() { @@ -54,19 +54,20 @@ async fn main() { } else { "[::]:0" } - .parse() - .unwrap(); + .parse()?; - let mut socket = UdpSocket::bind(&local_addr).unwrap(); + let mut socket = UdpSocket::bind(&local_addr)?; const MAX_DATAGRAM_SIZE: usize = 65_507; - socket.connect(&remote_addr).unwrap(); - let data = get_stdin_data().unwrap(); - socket.send(&data).await.unwrap(); + socket.connect(&remote_addr)?; + let data = get_stdin_data()?; + socket.send(&data).await?; let mut data = vec![0u8; MAX_DATAGRAM_SIZE]; - let len = socket.recv(&mut data).await.unwrap(); + let len = socket.recv(&mut data).await?; println!( "Received {} bytes:\n{}", len, String::from_utf8_lossy(&data[..len]) ); + + Ok(()) } diff --git a/tokio/examples/udp-codec.rs b/tokio/examples/udp-codec.rs index db6ce52f5..820ef4038 100644 --- a/tokio/examples/udp-codec.rs +++ b/tokio/examples/udp-codec.rs @@ -19,7 +19,7 @@ use tokio::net::UdpSocket; use tokio::util::FutureExt; #[tokio::main] -async fn main() -> Result<(), Box> { +async fn main() -> Result<(), Box> { let _ = env_logger::init(); let addr = env::args().nth(1).unwrap_or("127.0.0.1:0".to_string());