Skip to content

Commit

Permalink
Auto merge of #33224 - alexcrichton:create-exit-status, r=aturon
Browse files Browse the repository at this point in the history
std: Allow creating ExitStatus from raw values

Sometimes a process may be waited on externally from the standard library, in
which case it can be useful to create a raw `ExitStatus` structure to return.
This commit extends the existing Unix `ExitStatusExt` extension trait and adds a
new Windows-specific `ExitStatusExt` extension trait to do this. The methods are
currently called `ExitStatus::from_raw`.

cc #32713
  • Loading branch information
bors committed May 9, 2016
2 parents faca79f + 7f09b1f commit 0e7cb8b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,12 @@ impl AsInner<imp::ExitStatus> for ExitStatus {
fn as_inner(&self) -> &imp::ExitStatus { &self.0 }
}

impl FromInner<imp::ExitStatus> for ExitStatus {
fn from_inner(s: imp::ExitStatus) -> ExitStatus {
ExitStatus(s)
}
}

#[stable(feature = "process", since = "1.0.0")]
impl fmt::Display for ExitStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
9 changes: 9 additions & 0 deletions src/libstd/sys/unix/ext/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,22 @@ impl CommandExt for process::Command {
/// Unix-specific extensions to `std::process::ExitStatus`
#[stable(feature = "rust1", since = "1.0.0")]
pub trait ExitStatusExt {
/// Creates a new `ExitStatus` from the raw underlying `i32` return value of
/// a process.
#[unstable(feature = "exit_status_from", issue = "32713")]
fn from_raw(raw: i32) -> Self;

/// If the process was terminated by a signal, returns that signal.
#[stable(feature = "rust1", since = "1.0.0")]
fn signal(&self) -> Option<i32>;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ExitStatusExt for process::ExitStatus {
fn from_raw(raw: i32) -> Self {
process::ExitStatus::from_inner(From::from(raw))
}

fn signal(&self) -> Option<i32> {
self.as_inner().signal()
}
Expand Down
6 changes: 6 additions & 0 deletions src/libstd/sys/unix/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ impl ExitStatus {
}
}

impl From<c_int> for ExitStatus {
fn from(a: c_int) -> ExitStatus {
ExitStatus(a)
}
}

impl fmt::Display for ExitStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(code) = self.code() {
Expand Down
15 changes: 15 additions & 0 deletions src/libstd/sys/windows/ext/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,18 @@ impl IntoRawHandle for process::ChildStderr {
self.into_inner().into_handle().into_raw() as *mut _
}
}

/// Windows-specific extensions to `std::process::ExitStatus`
#[unstable(feature = "exit_status_from", issue = "32713")]
pub trait ExitStatusExt {
/// Creates a new `ExitStatus` from the raw underlying `u32` return value of
/// a process.
fn from_raw(raw: u32) -> Self;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ExitStatusExt for process::ExitStatus {
fn from_raw(raw: u32) -> Self {
process::ExitStatus::from_inner(From::from(raw))
}
}
6 changes: 6 additions & 0 deletions src/libstd/sys/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ impl ExitStatus {
}
}

impl From<c::DWORD> for ExitStatus {
fn from(u: c::DWORD) -> ExitStatus {
ExitStatus(u)
}
}

impl fmt::Display for ExitStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "exit code: {}", self.0)
Expand Down

0 comments on commit 0e7cb8b

Please sign in to comment.