Repository: sharkdp/hyperfine
File: src/hyperfine/benchmark.rs (Rust)

Code before:
                        Append ' || true' to the command if you are sure that this can be ignored.";

    run_intermediate_command(shell, command, show_output, error_output)
}

/// Run the command specified by `--cleanup`.
fn run_cleanup_command(
    shell: &str,
    command: &Option<Command<'_>>,
    show_output: bool,
) -> io::Result<TimingResult> {
    let error_output = "The cleanup command terminated with a non-zero exit code. \
                        Append ' || true' to the command if you are sure that this can be ignored.";

    run_intermediate_command(shell, command, show_output, error_output)
}

#[cfg(unix)]
fn extract_exit_code(status: ExitStatus) -> Option<i32> {
    use std::os::unix::process::ExitStatusExt;

    /* From the ExitStatus::code documentation:
       "On Unix, this will return None if the process was terminated by a signal."
       In that case, ExitStatusExt::signal should never return None.
    */
    status.code().or_else(|| status.signal())
}

#[cfg(not(unix))]
fn extract_exit_code(status: ExitStatus) -> Option<i32> {
    status.code()
}

/// Run the benchmark for a single shell command
pub fn run_benchmark(
    num: usize,
    cmd: &Command<'_>,
    shell_spawning_time: TimingResult,
    options: &HyperfineOptions,
) -> io::Result<BenchmarkResult> {
    let shell_cmd = cmd.get_shell_command();
    let command_name = if let Some(names) = &options.names {
        names.get(num).unwrap_or(&shell_cmd)
    } else {
        &shell_cmd
    };
    let command_name = command_name.to_string();

    if options.output_style != OutputStyleOption::Disabled {
        println!(
            "{}{}: {}",

Review comment (suggestion):
I think this should either return the exit code (if it's there) OR the signal n + 128. This is a standard procedure, e.g. in Bash: https://tldp.org/LDP/abs/html/exitcodes.html

Returning just the signal number (e.g. `9` for `KILL`) could be confused with the actual exit code 9.

Code after:
                        Append ' || true' to the command if you are sure that this can be ignored.";

    run_intermediate_command(shell, command, show_output, error_output)
}

/// Run the command specified by `--cleanup`.
fn run_cleanup_command(
    shell: &str,
    command: &Option<Command<'_>>,
    show_output: bool,
) -> io::Result<TimingResult> {
    let error_output = "The cleanup command terminated with a non-zero exit code. \
                        Append ' || true' to the command if you are sure that this can be ignored.";

    run_intermediate_command(shell, command, show_output, error_output)
}

#[cfg(unix)]
fn extract_exit_code(status: ExitStatus) -> Option<i32> {
    use std::os::unix::process::ExitStatusExt;

    /* From the ExitStatus::code documentation:
       "On Unix, this will return None if the process was terminated by a signal."
       In that case, ExitStatusExt::signal should never return None.
    */
    status.code().or_else(||
        /* To differentiate between "normal" exit codes and signals, we are using
           something similar to bash exit codes (https://tldp.org/LDP/abs/html/exitcodes.html)
           by adding 128 to a signal integer value.
         */
        status.signal().map(|s| 128 + s))
}

#[cfg(not(unix))]
fn extract_exit_code(status: ExitStatus) -> Option<i32> {
    status.code()
}

/// Run the benchmark for a single shell command
pub fn run_benchmark(
    num: usize,
    cmd: &Command<'_>,
    shell_spawning_time: TimingResult,
    options: &HyperfineOptions,
) -> io::Result<BenchmarkResult> {
    let shell_cmd = cmd.get_shell_command();
    let command_name = if let Some(names) = &options.names {
        names.get(num).unwrap_or(&shell_cmd)
    } else {
        &shell_cmd
    };