Skip to content

Instantly share code, notes, and snippets.

@praveenperera
Last active May 27, 2020 18:55
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 praveenperera/e605dd48f32cd0d42ad22a4b73d854e6 to your computer and use it in GitHub Desktop.
Save praveenperera/e605dd48f32cd0d42ad22a4b73d854e6 to your computer and use it in GitHub Desktop.
build a path list in parallel
use crossbeam_channel as channel;
use ignore::WalkBuilder;
use std::path::{Path, PathBuf};
#[derive(Debug)]
enum Message {
FoundPath(Result<ignore::DirEntry, ignore::Error>),
DoneScanning,
}
fn get_search_paths_from_starting_path(starting_path: &Path) -> Vec<PathBuf> {
let (tx, rx) = channel::unbounded();
let mut paths: Vec<ignore::DirEntry> = vec![];
WalkBuilder::new(starting_path).build_parallel().run(|| {
let tx = tx.clone();
Box::new(move |result| {
tx.send(Message::FoundPath(result)).unwrap();
ignore::WalkState::Continue
})
});
tx.send(Message::DoneScanning).unwrap();
loop {
match rx.recv() {
Ok(Message::DoneScanning) => break,
Ok(Message::FoundPath(Ok(path))) => paths.push(path),
_ => (),
}
}
drop(rx);
paths
.iter()
.filter(|f| f.path().is_file())
.map(|file| file.path().to_owned())
.collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment