lib.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. extern crate crypto_hash;
  2. extern crate crossbeam_channel;
  3. use std::io::BufReader;
  4. use crypto_hash::{Algorithm, hex_digest};
  5. use crossbeam_channel as channel;
  6. use std::thread;
  7. use std::io::prelude::*;
  8. use std::fs::File;
  9. const NWORKERS: usize = 10;
  10. pub fn rainbow(file: &str) {
  11. let f = File::open(file).unwrap();
  12. let reader = BufReader::new(f);
  13. let (sx, rx) = channel::unbounded();
  14. let prod = thread::spawn(move|| {
  15. for line in reader.lines() {
  16. let _ = sx.send(line.unwrap());
  17. }
  18. });
  19. let mut workers = Vec::with_capacity(NWORKERS);
  20. for _ in 0..NWORKERS {
  21. let rx = rx.clone();
  22. workers.push(thread::spawn(move|| {
  23. loop {
  24. match rx.try_recv() {
  25. Some(line) => {
  26. let _digest = hex_digest(Algorithm::MD5, line.as_bytes());
  27. // println!("{}", _digest);
  28. },
  29. None => break
  30. }
  31. }
  32. }));
  33. }
  34. let _ = prod.join();
  35. for _ in 0..NWORKERS {
  36. workers.pop().unwrap().join().unwrap();
  37. }
  38. }