From 24475646a1911354bcd9d1ccd6cb3c33fbb51bd4 Mon Sep 17 00:00:00 2001 From: Avril Date: Wed, 3 Mar 2021 17:17:26 +0000 Subject: [PATCH] better error reporting in proc skel --- shuffle3rs/src/main.rs | 4 ++-- shuffle3rs/src/proc.rs | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/shuffle3rs/src/main.rs b/shuffle3rs/src/main.rs index db3f935..3be70e8 100644 --- a/shuffle3rs/src/main.rs +++ b/shuffle3rs/src/main.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] -#[cfg(feature="deferred-drop")] #[macro_use] extern crate lazy_static; +#[macro_use] extern crate lazy_static; #[macro_use] extern crate cfg_if; #[macro_use] mod ext; use ext::*; @@ -17,7 +17,7 @@ use arg::Mode; /// /// # Method /// On `Err`, print the error message and then exit with error code provided -fn handle_err_fatal(op: Result, errcd: i32) -> T +#[inline] fn handle_err_fatal(op: Result, errcd: i32) -> T where E: std::error::Error { match op diff --git a/shuffle3rs/src/proc.rs b/shuffle3rs/src/proc.rs index 4100d0d..05602ce 100644 --- a/shuffle3rs/src/proc.rs +++ b/shuffle3rs/src/proc.rs @@ -11,12 +11,12 @@ pub enum ModeKind Unshuffle } -pub fn shuffle_file(file: impl AsRef) -> io::Result<()> +pub fn shuffle_file_ip(file: impl AsRef) -> io::Result<()> { todo!() } -pub fn unshuffle_file(file: impl AsRef) -> io::Result<()> +pub fn unshuffle_file_ip(file: impl AsRef) -> io::Result<()> { todo!() } @@ -25,6 +25,40 @@ pub fn unshuffle_file(file: impl AsRef) -> io::Result<()> pub fn process_files_ip(files: I, mode: ModeKind) -> io::Result<()> where I: IntoIterator { + cfg_if! { + if #[cfg(feature="threads")] { + let handles: Vec<_> = files.into_iter().map(|file| { + std::thread::spawn(move || { + let res = match mode { + ModeKind::Shuffle => shuffle_file_ip(&file), + ModeKind::Unshuffle => unshuffle_file_ip(&file), + }; + (res, file) + }) + }).collect(); - todo!() + for h in handles + { + let (res, file) = h.join().expect("Worker thread panicked"); // TODO: How do we communicate which thread failed here? Should we even try? + + res.map_err(move |x| { + eprintln!("Failed to process {:?}", file); + x + })?; + } + Ok(()) + } else { + for file in files.into_iter() { + let res = match mode { + ModeKind::Shuffle => shuffle_file_ip(&file), + ModeKind::Unshuffle => unshuffle_file_ip(&file), + }; + res.map_err(move |x| { + eprintln!("Failed to process {:?}", file); + x + })?; + } + Ok(()) + } + } }