use super::{ExitCode, Signal, WaitState};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
pub struct WaitStatus(i32);
impl WaitStatus {
#[must_use]
pub const fn from_raw(status: i32) -> Self {
Self(status)
}
#[must_use]
pub const fn to_raw(&self) -> i32 {
self.0
}
#[must_use]
pub const fn state(&self) -> WaitState {
WaitState::from_raw(self.0)
}
#[must_use]
pub const fn is_terminated(&self) -> bool {
matches!(
self.state(),
WaitState::Exited { .. } | WaitState::Signaled { .. }
)
}
#[must_use]
pub const fn is_exited(&self) -> bool {
matches!(self.state(), WaitState::Exited { .. })
}
#[must_use]
pub const fn is_signaled(&self) -> bool {
matches!(self.state(), WaitState::Signaled { .. })
}
#[must_use]
pub const fn exit_code(&self) -> Option<ExitCode> {
match self.state() {
WaitState::Exited { exit_code: code } => Some(code),
_ => None,
}
}
#[must_use]
pub const fn signal(&self) -> Option<Signal> {
match self.state() {
WaitState::Signaled { signal, .. } => Some(signal),
_ => None,
}
}
}
#[cfg(all(unix, feature = "std"))]
impl From<std::process::ExitStatus> for WaitStatus {
fn from(status: std::process::ExitStatus) -> Self {
if let Some(code) = status.code() {
WaitStatus::from_raw(code)
} else {
use std::os::unix::process::ExitStatusExt;
WaitStatus::from_raw(status.into_raw())
}
}
}
#[cfg(all(unix, feature = "std"))]
impl From<WaitStatus> for std::process::ExitStatus {
fn from(status: WaitStatus) -> Self {
use std::os::unix::process::ExitStatusExt;
std::process::ExitStatus::from_raw(status.to_raw())
}
}