Skip to content

Instantly share code, notes, and snippets.

@nocduro
Last active August 27, 2017 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nocduro/e6982ed1087a2fbbb81e1e21ce6773ba to your computer and use it in GitHub Desktop.
Save nocduro/e6982ed1087a2fbbb81e1e21ce6773ba to your computer and use it in GitHub Desktop.
extern crate rayon;
use rayon::prelude::*;
use std::path::PathBuf;
use std::fs::File;
use std::io;
use std::io::Write;
use std::sync::{Arc, Mutex};
fn sequential(filenames: &[PathBuf]) -> io::Result<()> {
for path in filenames {
let mut output = File::create(path)?;
write!(output, "hello!")?;
}
Ok(())
}
fn parallel_no_err_handling(filenames: &[PathBuf]) -> io::Result<()> {
filenames.par_iter().for_each(|path| {
let mut output = File::create(path).expect("parallel_run: couldn't open file!");
write!(output, "hello parallel!").expect("parallel_run: couldn't write to file!");
});
Ok(())
}
#[derive(Debug)]
struct FileProblem {
file_path: PathBuf,
err: io::Error,
}
fn file_worker(file: &PathBuf) -> io::Result<()> {
let mut output = File::create(file)?;
write!(output, "hello from parallel_with_err_handling!")?;
Ok(())
}
fn parallel_with_err_handling(filenames: &[PathBuf]) -> Result<(), Vec<FileProblem>> {
let error_files = Arc::new(Mutex::new(Vec::new()));
filenames.par_iter().for_each(
|path| match file_worker(path) {
Ok(_) => (),
Err(e) => {
error_files.lock().unwrap().push(FileProblem {
file_path: path.clone(),
err: e,
})
}
},
);
// done parallel code, so convert back to a normal vec
let error_files = Arc::try_unwrap(error_files).unwrap().into_inner().unwrap();
if error_files.len() == 0 {
Ok(())
} else {
Err(error_files)
}
}
fn main() {
let mut filenames = Vec::new();
filenames.push(PathBuf::from("hello.txt"));
filenames.push(PathBuf::from("hi.txt"));
filenames.push(PathBuf::from("cat.txt"));
filenames.push(PathBuf::from("dog.txt"));
match sequential(&filenames) {
Ok(_) => println!("success"),
Err(e) => println!("sequential error: {}", e),
}
match parallel_with_err_handling(&filenames) {
Ok(_) => println!("parallel_with_err_handling finished all files"),
Err(errored_files) => {
for file in errored_files {
println!("{:?} failed with error: {}", file.file_path, file.err);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment