Skip to content

Commit 0e7cb8b

Browse files
committed
Auto merge of #33224 - alexcrichton:create-exit-status, r=aturon
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
2 parents faca79f + 7f09b1f commit 0e7cb8b

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

src/libstd/process.rs

+6
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,12 @@ impl AsInner<imp::ExitStatus> for ExitStatus {
605605
fn as_inner(&self) -> &imp::ExitStatus { &self.0 }
606606
}
607607

608+
impl FromInner<imp::ExitStatus> for ExitStatus {
609+
fn from_inner(s: imp::ExitStatus) -> ExitStatus {
610+
ExitStatus(s)
611+
}
612+
}
613+
608614
#[stable(feature = "process", since = "1.0.0")]
609615
impl fmt::Display for ExitStatus {
610616
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

src/libstd/sys/unix/ext/process.rs

+9
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,22 @@ impl CommandExt for process::Command {
132132
/// Unix-specific extensions to `std::process::ExitStatus`
133133
#[stable(feature = "rust1", since = "1.0.0")]
134134
pub trait ExitStatusExt {
135+
/// Creates a new `ExitStatus` from the raw underlying `i32` return value of
136+
/// a process.
137+
#[unstable(feature = "exit_status_from", issue = "32713")]
138+
fn from_raw(raw: i32) -> Self;
139+
135140
/// If the process was terminated by a signal, returns that signal.
136141
#[stable(feature = "rust1", since = "1.0.0")]
137142
fn signal(&self) -> Option<i32>;
138143
}
139144

140145
#[stable(feature = "rust1", since = "1.0.0")]
141146
impl ExitStatusExt for process::ExitStatus {
147+
fn from_raw(raw: i32) -> Self {
148+
process::ExitStatus::from_inner(From::from(raw))
149+
}
150+
142151
fn signal(&self) -> Option<i32> {
143152
self.as_inner().signal()
144153
}

src/libstd/sys/unix/process.rs

+6
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,12 @@ impl ExitStatus {
550550
}
551551
}
552552

553+
impl From<c_int> for ExitStatus {
554+
fn from(a: c_int) -> ExitStatus {
555+
ExitStatus(a)
556+
}
557+
}
558+
553559
impl fmt::Display for ExitStatus {
554560
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
555561
if let Some(code) = self.code() {

src/libstd/sys/windows/ext/process.rs

+15
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,18 @@ impl IntoRawHandle for process::ChildStderr {
8181
self.into_inner().into_handle().into_raw() as *mut _
8282
}
8383
}
84+
85+
/// Windows-specific extensions to `std::process::ExitStatus`
86+
#[unstable(feature = "exit_status_from", issue = "32713")]
87+
pub trait ExitStatusExt {
88+
/// Creates a new `ExitStatus` from the raw underlying `u32` return value of
89+
/// a process.
90+
fn from_raw(raw: u32) -> Self;
91+
}
92+
93+
#[stable(feature = "rust1", since = "1.0.0")]
94+
impl ExitStatusExt for process::ExitStatus {
95+
fn from_raw(raw: u32) -> Self {
96+
process::ExitStatus::from_inner(From::from(raw))
97+
}
98+
}

src/libstd/sys/windows/process.rs

+6
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ impl ExitStatus {
337337
}
338338
}
339339

340+
impl From<c::DWORD> for ExitStatus {
341+
fn from(u: c::DWORD) -> ExitStatus {
342+
ExitStatus(u)
343+
}
344+
}
345+
340346
impl fmt::Display for ExitStatus {
341347
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
342348
write!(f, "exit code: {}", self.0)

0 commit comments

Comments
 (0)