summaryrefslogtreecommitdiffstats
path: root/examples/udp-client.rs
diff options
context:
space:
mode:
authorLiran Ringel <5730310+liranringel@users.noreply.github.com>2018-11-20 18:10:36 +0200
committerToby Lawrence <tobz@users.noreply.github.com>2018-11-20 11:10:36 -0500
commit9b1a45cc6a15f5d2be17531dffc2f50d2b019646 (patch)
treeda66c5c9574f2cd7ad11745e414fc34da2e35c6f /examples/udp-client.rs
parent477fa5580aa3796f97e3e0eb1325d4690b3b4e96 (diff)
tests: handle errors properly in examples (#748)
Diffstat (limited to 'examples/udp-client.rs')
-rw-r--r--examples/udp-client.rs27
1 files changed, 11 insertions, 16 deletions
diff --git a/examples/udp-client.rs b/examples/udp-client.rs
index 3af7c3be..d2d4bc99 100644
--- a/examples/udp-client.rs
+++ b/examples/udp-client.rs
@@ -35,29 +35,27 @@ use std::net::SocketAddr;
use tokio::net::UdpSocket;
use tokio::prelude::*;
-fn get_stdin_data() -> Vec<u8> {
+fn get_stdin_data() -> Result<Vec<u8>, Box<std::error::Error>> {
let mut buf = Vec::new();
- stdin().read_to_end(&mut buf).unwrap();
- buf
+ stdin().read_to_end(&mut buf)?;
+ Ok(buf)
}
-fn main() {
+fn main() -> Result<(), Box<std::error::Error>> {
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() {
"0.0.0.0:0"
} else {
"[::]:0"
- }.parse()
- .unwrap();
- let socket = UdpSocket::bind(&local_addr).unwrap();
+ }.parse()?;
+ let socket = UdpSocket::bind(&local_addr)?;
const MAX_DATAGRAM_SIZE: usize = 65_507;
- let processing = socket
- .send_dgram(get_stdin_data(), &remote_addr)
+ socket
+ .send_dgram(get_stdin_data()?, &remote_addr)
.and_then(|(socket, _)| socket.recv_dgram(vec![0u8; MAX_DATAGRAM_SIZE]))
.map(|(_, data, len, _)| {
println!(
@@ -66,9 +64,6 @@ fn main() {
String::from_utf8_lossy(&data[..len])
)
})
- .wait();
- match processing {
- Ok(_) => {}
- Err(e) => eprintln!("Encountered an error: {}", e),
- }
+ .wait()?;
+ Ok(())
}