From a36543353ed275d989fc3d5340e44d57bb6001f1 Mon Sep 17 00:00:00 2001 From: Daiderd Jordan Date: Sat, 4 Aug 2018 13:21:11 +0200 Subject: [PATCH] asynccmd: simplify wait return type --- ofborg/src/asynccmd.rs | 40 ++++++++++++++++++--------------------- ofborg/src/tasks/build.rs | 4 ++-- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/ofborg/src/asynccmd.rs b/ofborg/src/asynccmd.rs index 0ea65d8..eb48e20 100644 --- a/ofborg/src/asynccmd.rs +++ b/ofborg/src/asynccmd.rs @@ -3,9 +3,10 @@ use std::thread; use std::collections::HashMap; use std::process::Stdio; use std::process::ExitStatus; -use std::sync::mpsc::sync_channel; use std::process::Command; use std::io::Read; +use std::sync::mpsc; +use std::sync::mpsc::sync_channel; use std::sync::mpsc::{SyncSender, Receiver}; use std::io::BufReader; use std::io::BufRead; @@ -188,12 +189,15 @@ impl AsyncCmd { impl SpawnedAsyncCmd { - pub fn lines<'a>(&'a mut self) -> &'a Receiver { - &self.rx + pub fn lines<'a>(&'a mut self) -> mpsc::Iter<'a, String> { + self.rx.iter() } - pub fn wait(self) -> thread::Result>> { + pub fn wait(self) -> Result { self.waiter.join() + .map_err(|_err| io::Error::new(io::ErrorKind::Other, "Couldn't join thread.")) + .and_then(|opt| opt.ok_or(io::Error::new(io::ErrorKind::Other, "Thread didn't return an exit status."))) + .and_then(|res| res) } } @@ -214,9 +218,9 @@ mod tests { let acmd = AsyncCmd::new(cmd); let mut spawned = acmd.spawn(); - let lines: Vec = spawned.lines().into_iter().collect(); + let lines: Vec = spawned.lines().collect(); assert_eq!(lines, vec!["hi"]); - let ret = spawned.wait().unwrap().unwrap().unwrap().success(); + let ret = spawned.wait().unwrap().success(); assert_eq!(true, ret); } @@ -234,9 +238,9 @@ mod tests { let acmd = AsyncCmd::new(cmd); let mut spawned = acmd.spawn(); - let lines: Vec = spawned.lines().into_iter().collect(); + let lines: Vec = spawned.lines().collect(); assert_eq!(lines, vec!["stdout", "stderr", "stdout2", "stderr2"]); - let ret = spawned.wait().unwrap().unwrap().unwrap().success(); + let ret = spawned.wait().unwrap().success(); assert_eq!(true, ret); } @@ -250,14 +254,10 @@ mod tests { let acmd = AsyncCmd::new(cmd); let mut spawned = acmd.spawn(); - let lines: Vec = spawned.lines().into_iter().collect(); + let lines: Vec = spawned.lines().collect(); assert_eq!(lines.len(), 20000); let thread_result = spawned.wait(); - let child_result_opt = thread_result.expect("Thread should exit correctly"); - let child_result = child_result_opt.expect( - "Thread should have properly properly returned the child's status", - ); - let exit_status = child_result.expect("The child should have no problem exiting"); + let exit_status = thread_result.expect("Thread should exit correctly"); assert_eq!(true, exit_status.success()); } @@ -270,14 +270,10 @@ mod tests { let acmd = AsyncCmd::new(cmd); let mut spawned = acmd.spawn(); - let lines: Vec = spawned.lines().into_iter().collect(); + let lines: Vec = spawned.lines().collect(); assert_eq!(lines.len(), 200000); let thread_result = spawned.wait(); - let child_result_opt = thread_result.expect("Thread should exit correctly"); - let child_result = child_result_opt.expect( - "Thread should have properly properly returned the child's status", - ); - let exit_status = child_result.expect("The child should have no problem exiting"); + let exit_status = thread_result.expect("Thread should exit correctly"); assert_eq!(true, exit_status.success()); } @@ -293,12 +289,12 @@ mod tests { let acmd = AsyncCmd::new(cmd); let mut spawned = acmd.spawn(); - let lines: Vec = spawned.lines().into_iter().collect(); + let lines: Vec = spawned.lines().collect(); assert_eq!( lines, vec!["hi", "Non-UTF8 data omitted from the log.", "there"] ); - let ret = spawned.wait().unwrap().unwrap().unwrap().success(); + let ret = spawned.wait().unwrap().success(); assert_eq!(true, ret); } } diff --git a/ofborg/src/tasks/build.rs b/ofborg/src/tasks/build.rs index 9580487..b181085 100644 --- a/ofborg/src/tasks/build.rs +++ b/ofborg/src/tasks/build.rs @@ -369,12 +369,12 @@ impl notifyworker::SimpleNotifyWorker for BuildWorker { can_build.clone(), ); - for line in spawned.lines().iter() { + for line in spawned.lines() { actions.log_line(&line); } let success = match spawned.wait() { - Ok(Some(Ok(status))) => status.success(), + Ok(status) => status.success(), e => { println!("Failed on the interior command: {:?}", e); false