main.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. use walkdir::WalkDir;
  2. struct Args {
  3. targets: Vec<&'static str>,
  4. }
  5. fn is_ruby_source(path: &std::path::Path) -> bool {
  6. if let Some(ext) = path.extension() {
  7. return ext == "rb";
  8. }
  9. false
  10. }
  11. fn main() {
  12. let args = Args {
  13. targets: vec!["."],
  14. };
  15. let (work_send, work_recv) = crossbeam::channel::unbounded();
  16. // produce a thread which populates the channel with work
  17. let producer = std::thread::spawn(move || {
  18. for target in args.targets {
  19. for entry in WalkDir::new(target).into_iter().filter_map(|e| e.ok()) {
  20. if is_ruby_source(entry.path()) {
  21. work_send.send(entry).expect("Unable to send work from producer thread");
  22. }
  23. }
  24. }
  25. });
  26. // produce a set of threads which
  27. let workers: Vec<std::thread::JoinHandle<()>> = (0..8).map(|id| {
  28. let receiver = work_recv.clone();
  29. std::thread::spawn(move || {
  30. while let Ok(msg) = receiver.recv() {
  31. println!("thread {} processing {:?}", id, msg);
  32. }
  33. println!("thread {} done", id);
  34. })
  35. }).collect();
  36. producer.join().unwrap();
  37. for w in workers {
  38. w.join().unwrap();
  39. }
  40. }