Skip to content

Commit

Permalink
Move term_if_signal_rcvd macro into context mod
Browse files Browse the repository at this point in the history
  • Loading branch information
exokernel committed Sep 20, 2024
1 parent a0aa98b commit ca1359f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 42 deletions.
16 changes: 16 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

#[macro_export]
macro_rules! term_if_signal_rcvd {
($context:expr) => {
if $context.term_signal_rcvd() {
log::info!("PFP: Received termination signal, exiting early...");
return Ok(());
}
};
($context:expr, $ret:expr) => {
if $context.term_signal_rcvd() {
log::info!("PFP: Received termination signal, exiting early...");
return Ok($ret);
}
};
}

pub struct ProcessingContext<'a> {
pub chunk_size: usize,
pub extensions: &'a Option<Vec<&'a OsStr>>,
Expand Down
43 changes: 1 addition & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,7 @@ use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;

/// Checks if a termination signal has been received and exits the current function if so.
///
/// This macro can be used in two ways:
///
/// 1. With a single argument: `term_if_signal_rcvd!(context)`
/// This version returns `Ok(())` if a termination signal is received.
///
/// 2. With two arguments: `term_if_signal_rcvd!(context, return_value)`
/// This version returns `Ok(return_value)` if a termination signal is received.
///
/// # Arguments
///
/// * `context` - A reference to a `ProcessingContext` struct.
/// * `return_value` - (Optional) The value to return wrapped in `Ok()` if a termination signal is received.
///
/// # Examples
///
/// ```
/// fn example_function(context: &ProcessingContext) -> Result<()> {
/// term_if_signal_rcvd!(context);
/// // Rest of the function
/// }
///
/// fn example_function_with_return(context: &ProcessingContext) -> Result<Vec<String>> {
/// term_if_signal_rcvd!(context, Vec::new());
/// // Rest of the function
/// }
/// ```
macro_rules! term_if_signal_rcvd {
($context:expr) => {
if $context.term_signal_rcvd() {
log::info!("PFP: Received termination signal, exiting early...");
return Ok(());
}
};
($context:expr, $ret:expr) => {
if $context.term_signal_rcvd() {
log::info!("PFP: Received termination signal, exiting early...");
return Ok($ret);
}
};
}
use pfp::term_if_signal_rcvd;

#[derive(Parser, Debug)]
#[clap(name = "pfp", about = "Parallel File Processor")]
Expand Down

0 comments on commit ca1359f

Please sign in to comment.