From 0cf48910e7705490200a208e429d418f5c17c2f8 Mon Sep 17 00:00:00 2001 From: Kotauskas Date: Fri, 5 Mar 2021 13:24:58 +0300 Subject: [PATCH] Miscellaneous fixes to local socket examples --- examples/local_socket_client.rs | 6 +++--- examples/local_socket_server.rs | 9 +++------ src/local_socket.rs | 8 +++----- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/examples/local_socket_client.rs b/examples/local_socket_client.rs index 6baa1ba1..18b14f04 100644 --- a/examples/local_socket_client.rs +++ b/examples/local_socket_client.rs @@ -5,12 +5,12 @@ use std::{ }; fn main() -> Result<(), Box> { - let mut conn = LocalSocketStream::connect("/tmp/example.sock").unwrap(); - conn.write_all(b"Hello from client!\n").unwrap(); + let mut conn = LocalSocketStream::connect("/tmp/example.sock")?; + conn.write_all(b"Hello from client!\n")?; let mut conn = BufReader::new(conn); let mut buffer = String::new(); - conn.read_line(&mut buffer).unwrap(); + conn.read_line(&mut buffer)?; println!("Server answered: {}", buffer); diff --git a/examples/local_socket_server.rs b/examples/local_socket_server.rs index f534045d..c4813dea 100644 --- a/examples/local_socket_server.rs +++ b/examples/local_socket_server.rs @@ -6,12 +6,9 @@ use std::{ fn main() -> Result<(), Box> { fn handle_error(connection: io::Result) -> Option { - match connection { - Ok(val) => Some(val), - Err(error) => { - panic!("Incoming connection failed: {}", error); - } - } + connection + .map_err(|error| eprintln!("Incoming connection failed: {}", error)) + .ok() } let listener = LocalSocketListener::bind("/tmp/example.sock")?; diff --git a/src/local_socket.rs b/src/local_socket.rs index f86fab82..870432ba 100644 --- a/src/local_socket.rs +++ b/src/local_socket.rs @@ -480,11 +480,9 @@ mod test { let server_barrier = Arc::clone(&barrier); std::thread::spawn(move || { - fn handle_error( - connection: io::Result, - ) -> Option { + fn handle_error(connection: io::Result) -> LocalSocketStream { match connection { - Ok(val) => Some(val), + Ok(val) => val, Err(error) => { panic!("Incoming connection failed: {}", error); } @@ -493,7 +491,7 @@ mod test { let listener = LocalSocketListener::bind("/tmp/example.sock").unwrap(); server_barrier.wait(); - for mut conn in listener.incoming().filter_map(handle_error) { + for mut conn in listener.incoming().map(handle_error) { println!("Incoming connection!"); conn.write_all(b"Hello from server!\n").unwrap(); // Add buffering to the connection to read a line.