"Fossies" - the Fresh Open Source Software Archive

Member "fd-8.1.1/src/exec/job.rs" (25 May 2020, 2051 Bytes) of package /linux/privat/fd-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Rust source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "job.rs": 7.5.0_vs_8.0.0.

    1 use std::path::PathBuf;
    2 use std::sync::mpsc::Receiver;
    3 use std::sync::{Arc, Mutex};
    4 
    5 use crate::error::print_error;
    6 use crate::exit_codes::{merge_exitcodes, ExitCode};
    7 use crate::walk::WorkerResult;
    8 
    9 use super::CommandTemplate;
   10 
   11 /// An event loop that listens for inputs from the `rx` receiver. Each received input will
   12 /// generate a command with the supplied command template. The generated command will then
   13 /// be executed, and this process will continue until the receiver's sender has closed.
   14 pub fn job(
   15     rx: Arc<Mutex<Receiver<WorkerResult>>>,
   16     cmd: Arc<CommandTemplate>,
   17     out_perm: Arc<Mutex<()>>,
   18     show_filesystem_errors: bool,
   19 ) -> ExitCode {
   20     let mut results: Vec<ExitCode> = Vec::new();
   21     loop {
   22         // Create a lock on the shared receiver for this thread.
   23         let lock = rx.lock().unwrap();
   24 
   25         // Obtain the next result from the receiver, else if the channel
   26         // has closed, exit from the loop
   27         let value: PathBuf = match lock.recv() {
   28             Ok(WorkerResult::Entry(val)) => val,
   29             Ok(WorkerResult::Error(err)) => {
   30                 if show_filesystem_errors {
   31                     print_error(err.to_string());
   32                 }
   33                 continue;
   34             }
   35             Err(_) => break,
   36         };
   37 
   38         // Drop the lock so that other threads can read from the receiver.
   39         drop(lock);
   40         // Generate a command, execute it and store its exit code.
   41         results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm)))
   42     }
   43     // Returns error in case of any error.
   44     merge_exitcodes(&results)
   45 }
   46 
   47 pub fn batch(
   48     rx: Receiver<WorkerResult>,
   49     cmd: &CommandTemplate,
   50     show_filesystem_errors: bool,
   51 ) -> ExitCode {
   52     let paths = rx.iter().filter_map(|value| match value {
   53         WorkerResult::Entry(val) => Some(val),
   54         WorkerResult::Error(err) => {
   55             if show_filesystem_errors {
   56                 print_error(err.to_string());
   57             }
   58             None
   59         }
   60     });
   61     cmd.generate_and_execute_batch(paths)
   62 }