Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
Expunge all instances of unwrap in examples
Browse files Browse the repository at this point in the history
Using `unwrap` is bad practice in most cases. Code examples should not
promote that practice, as they may induce the user into believing it's
acceptable.
  • Loading branch information
meqif committed Oct 25, 2015
1 parent 8c27edc commit fbfa939
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/echo-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ fn handle_client(mut s: UtpSocket) {

fn main() {
// Start logger
env_logger::init().unwrap();
env_logger::init().expect("Error starting logger");

// Create a listener
let addr = "127.0.0.1:8080";
let listener = UtpListener::bind(addr).unwrap();
let listener = UtpListener::bind(addr).expect("Error binding listener");

for connection in listener.incoming() {
// Spawn a new handler for each new connection
Expand Down
19 changes: 7 additions & 12 deletions examples/utpcat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ extern crate utp;

use std::process;

// A little macro to make it easier to unwrap return values or halt the program
macro_rules! iotry {
($e:expr) => (match $e { Ok(v) => v, Err(e) => panic!("{}", e), })
}

fn usage() -> ! {
println!("Usage: utp [-s|-c] <address> <port>");
process::exit(1);
Expand All @@ -25,7 +20,7 @@ fn main() {
enum Mode {Server, Client}

// Start logging
env_logger::init().unwrap();
env_logger::init().expect("Error starting logger");

// Fetch arguments
let mut args = std::env::args();
Expand All @@ -51,7 +46,7 @@ fn main() {
match mode {
Mode::Server => {
// Create a listening stream
let mut stream = iotry!(UtpStream::bind(addr));
let mut stream = UtpStream::bind(addr).expect("Error binding stream");
let mut writer = stdout();
let _ = writeln!(&mut stderr(), "Serving on {}", addr);

Expand All @@ -64,14 +59,14 @@ fn main() {
loop {
match stream.read(&mut payload) {
Ok(0) => break,
Ok(read) => iotry!(writer.write(&payload[..read])),
Ok(read) => writer.write(&payload[..read]).expect("Error writing to stdout"),
Err(e) => panic!("{}", e)
};
}
}
Mode::Client => {
// Create a stream and try to connect to the remote address
let mut stream = iotry!(UtpStream::connect(addr));
let mut stream = UtpStream::connect(addr).expect("Error connecting to remote peer");
let mut reader = stdin();

// Create a reasonably sized buffer
Expand All @@ -84,16 +79,16 @@ fn main() {
loop {
match reader.read(&mut payload) {
Ok(0) => break,
Ok(read) => iotry!(stream.write(&payload[..read])),
Ok(read) => stream.write(&payload[..read]).expect("Error writing to stream"),
Err(e) => {
iotry!(stream.close());
stream.close().expect("Error closing stream");
panic!("{:?}", e);
}
};
}

// Explicitly close the stream.
iotry!(stream.close());
stream.close().expect("Error closing stream");
}
}
}

0 comments on commit fbfa939

Please sign in to comment.