Skip to content

Commit

Permalink
Implement TryFrom<WaitStatus> for ExitStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed May 8, 2022
1 parent ce843da commit bfb8358
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions yash-env/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! Type definitions for command execution.

use nix::sys::signal::Signal;
use nix::sys::wait::WaitStatus;
use std::ops::ControlFlow;
use std::os::raw::c_int;
use yash_syntax::source::Location;
Expand Down Expand Up @@ -106,6 +107,26 @@ impl From<Signal> for ExitStatus {
}
}

/// Error returned when a [`WaitStatus`] could not be converted to an
/// [`ExitStatus`]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct StillAliveError;

/// Converts a `WaitStatus` to an `ExitStatus` if the status is `Exited`,
/// `Signaled`, or `Stopped`.
impl TryFrom<WaitStatus> for ExitStatus {
type Error = StillAliveError;
fn try_from(status: WaitStatus) -> std::result::Result<Self, StillAliveError> {
match status {
WaitStatus::Exited(_, exit_status) => Ok(ExitStatus(exit_status)),
WaitStatus::Signaled(_, signal, _) | WaitStatus::Stopped(_, signal) => {
Ok(ExitStatus::from(signal))
}
_ => Err(StillAliveError),
}
}
}

impl TryFrom<ExitStatus> for Signal {
type Error = nix::Error;
/// Converts an exit status to the corresponding signal.
Expand Down

0 comments on commit bfb8358

Please sign in to comment.