From f94db99810f321fd9a2232ff3535bf3f7955f069 Mon Sep 17 00:00:00 2001 From: 132nd-Professor <132nd-Professor> Date: Fri, 12 Aug 2022 20:22:45 +0200 Subject: [PATCH] add error messages for threads --- src/main.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index ac41d83..223fef4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,25 +29,36 @@ fn main_inner() -> Result<(), Box> { let body = Arc::new(body); let input_filename = Arc::new(input_filename); - let handles: Vec<_> = vec![Coalition::Blue, Coalition::Red, Coalition::Purple] - .into_iter() - .map(|c| { + let coalitions = vec![Coalition::Blue, Coalition::Red, Coalition::Purple]; + + // clippy wants us to combine both ierators into one. this is not what we want, because then + // we would spawn a thread, join it, and only then spawn a new one. + #[allow(clippy::needless_collect)] + let handles: Vec<_> = coalitions + .iter() + .map(|coalition| { let z = is_zip; let b = body.clone(); - let coalitions = coalition_per_line.clone(); + let cpl = coalition_per_line.clone(); + let c = coalition.clone(); let h = header.clone(); let i = input_filename.clone(); thread::spawn(move || { let mut writer = writer::create_writer(z, &*i.clone(), &c).unwrap(); writer.write_strings(&*h).unwrap(); - writer.write_for_coalition(&*b, &*coalitions, c).unwrap(); + writer.write_for_coalition(&*b, &*cpl, c).unwrap(); }) }) .collect(); - for handle in handles { - handle.join().unwrap(); - } + let _: Vec<_> = handles + .into_iter() + .zip(coalitions) + .map(|(h, c)| { + h.join() + .unwrap_or_else(|_| println!("could not write data for {c}")) + }) + .collect(); Ok(()) }