Skip to content

Commit

Permalink
Miscellaneous fixes to local socket examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kotauskas committed Mar 5, 2021
1 parent 0bb223b commit 0cf4891
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
6 changes: 3 additions & 3 deletions examples/local_socket_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::{
};

fn main() -> Result<(), Box<dyn Error>> {
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);

Expand Down
9 changes: 3 additions & 6 deletions examples/local_socket_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ use std::{

fn main() -> Result<(), Box<dyn Error>> {
fn handle_error(connection: io::Result<LocalSocketStream>) -> Option<LocalSocketStream> {
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")?;
Expand Down
8 changes: 3 additions & 5 deletions src/local_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,9 @@ mod test {
let server_barrier = Arc::clone(&barrier);

std::thread::spawn(move || {
fn handle_error(
connection: io::Result<LocalSocketStream>,
) -> Option<LocalSocketStream> {
fn handle_error(connection: io::Result<LocalSocketStream>) -> LocalSocketStream {
match connection {
Ok(val) => Some(val),
Ok(val) => val,
Err(error) => {
panic!("Incoming connection failed: {}", error);
}
Expand All @@ -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.
Expand Down

0 comments on commit 0cf4891

Please sign in to comment.