SHARE
TWEET

Untitled

a guest Jul 16th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![allow(dead_code)]
  2. #![deny(bare_trait_objects)]
  3.  
  4. use ::std::{*,
  5.     fs,
  6.     path::PathBuf,
  7. };
  8. use ::rayon::prelude::*;
  9. use ::crossbeam::channel::{
  10.     self,
  11.     Receiver,
  12. };
  13.  
  14. macro_rules! trait_aliases {(
  15.     $(
  16.             trait
  17.             alias
  18.             $Trait:ident
  19.         $(
  20.             (  $($ty_params:tt)*  )
  21.         )?
  22.             = {
  23.                 $($traits:tt)*
  24.             }
  25.         $(
  26.             where {
  27.                 $($wc:tt)*
  28.             }
  29.         )?
  30.             ;
  31.     )*
  32. ) => (
  33.     $(
  34.         trait $Trait $(<$($ty_params)*>)? :
  35.             $($traits)*
  36.         $(
  37.             where
  38.                 $($wc)*
  39.         )?
  40.         {}
  41.        
  42.         impl<Slf : ?Sized, $($($ty_params)*)?> $Trait $(<$($ty_params)*>)?
  43.             for Slf
  44.         where
  45.             Slf : $($traits)*,
  46.             $($($wc)*)?
  47.         {}
  48.     )*
  49. )}
  50.  
  51. trait_aliases! {
  52.     trait alias IsContext = {
  53.         Clone + Send + 'static
  54.     };
  55.    
  56.     trait alias IsFileProcessor(Context) = {
  57.         Fn(&mut Context, &str) + Send + Sync + 'static
  58.     } where {
  59.         Context : IsContext,
  60.     };
  61. }
  62.  
  63. struct WorkTreeProcessor<Context>
  64. where
  65.     Context : IsContext,
  66. {
  67.     file_processors: Vec<Box<dyn IsFileProcessor<Context>>>,
  68.     // context: Context,
  69. }
  70.  
  71. impl<Context> WorkTreeProcessor<Context>
  72. where
  73.     Context : IsContext,
  74. {
  75.     fn add_file_processor<C, F, R> (
  76.         self: &'_ mut Self,
  77.         function: F,
  78.     ) -> Receiver<R>
  79.     where
  80.         F : Fn(&mut Context, &str) -> R,
  81.         F : Send + Sync + 'static,
  82.         R : Send + 'static,
  83.     {
  84.         let (sender, receiver) = channel::unbounded();
  85.         self.file_processors
  86.             .push(Box::new(move |context, file_contents| {
  87.                 let result = function(context, file_contents);
  88.                 let _ = sender.send(result);
  89.             }));
  90.         receiver
  91.     }
  92.  
  93.     fn run (
  94.         self: &'_ Self,
  95.         context: Context, // or self.context.clone()
  96.         work_tree_paths: Vec<PathBuf>,
  97.     )
  98.     {
  99.         let file_processors = &self.file_processors;
  100.         work_tree_paths
  101.             .into_par_iter()
  102.             .filter_map(|path| fs::read_to_string(path).ok())
  103.             .for_each_with(context, |context, file_contents| {
  104.                 file_processors
  105.                     .iter()
  106.                     .for_each(|processor| {
  107.                         processor(context, &file_contents)
  108.                     })
  109.             });
  110.     }
  111. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top