Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(dead_code)]
- #![deny(bare_trait_objects)]
- use ::std::{*,
- fs,
- path::PathBuf,
- };
- use ::rayon::prelude::*;
- use ::crossbeam::channel::{
- self,
- Receiver,
- };
- macro_rules! trait_aliases {(
- $(
- trait
- alias
- $Trait:ident
- $(
- ( $($ty_params:tt)* )
- )?
- = {
- $($traits:tt)*
- }
- $(
- where {
- $($wc:tt)*
- }
- )?
- ;
- )*
- ) => (
- $(
- trait $Trait $(<$($ty_params)*>)? :
- $($traits)*
- $(
- where
- $($wc)*
- )?
- {}
- impl<Slf : ?Sized, $($($ty_params)*)?> $Trait $(<$($ty_params)*>)?
- for Slf
- where
- Slf : $($traits)*,
- $($($wc)*)?
- {}
- )*
- )}
- trait_aliases! {
- trait alias IsContext = {
- Clone + Send + 'static
- };
- trait alias IsFileProcessor(Context) = {
- Fn(&mut Context, &str) + Send + Sync + 'static
- } where {
- Context : IsContext,
- };
- }
- struct WorkTreeProcessor<Context>
- where
- Context : IsContext,
- {
- file_processors: Vec<Box<dyn IsFileProcessor<Context>>>,
- // context: Context,
- }
- impl<Context> WorkTreeProcessor<Context>
- where
- Context : IsContext,
- {
- fn add_file_processor<C, F, R> (
- self: &'_ mut Self,
- function: F,
- ) -> Receiver<R>
- where
- F : Fn(&mut Context, &str) -> R,
- F : Send + Sync + 'static,
- R : Send + 'static,
- {
- let (sender, receiver) = channel::unbounded();
- self.file_processors
- .push(Box::new(move |context, file_contents| {
- let result = function(context, file_contents);
- let _ = sender.send(result);
- }));
- receiver
- }
- fn run (
- self: &'_ Self,
- context: Context, // or self.context.clone()
- work_tree_paths: Vec<PathBuf>,
- )
- {
- let file_processors = &self.file_processors;
- work_tree_paths
- .into_par_iter()
- .filter_map(|path| fs::read_to_string(path).ok())
- .for_each_with(context, |context, file_contents| {
- file_processors
- .iter()
- .for_each(|processor| {
- processor(context, &file_contents)
- })
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement