]> git.lizzy.rs Git - rust.git/commitdiff
std: Allow creating ExitStatus from raw values
authorAlex Crichton <alex@alexcrichton.com>
Tue, 26 Apr 2016 22:23:46 +0000 (15:23 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 27 Apr 2016 06:35:59 +0000 (23:35 -0700)
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

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

index 1d2516a4c4099b6fc7ce78410db6de0ea64239ed..3ff76a7bee30291ceecc20b64022b923a6a995e1 100644 (file)
@@ -459,6 +459,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 {
index 7f31cf9f3bf80690b7f01506e9bdf317b3b07b78..b0fed2f4694bfcd4163fc53509e1ab9e0de9d225 100644 (file)
@@ -132,6 +132,11 @@ fn exec(&mut self) -> io::Error {
 /// 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>;
@@ -139,6 +144,10 @@ pub trait ExitStatusExt {
 
 #[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()
     }
index 270c2096b2c3b33aa34f3fca35e8fbd2fa600ec6..72e02a487e36cb7a01e6ae1366b9d83225ce9962 100644 (file)
@@ -550,6 +550,12 @@ pub fn signal(&self) -> Option<i32> {
     }
 }
 
+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() {
index f6ee59eec327ae936f06b95dcaaf4a863e2552ca..56c6a73d4f82b768760c058e88ae44ebae40c7ea 100644 (file)
@@ -81,3 +81,18 @@ fn into_raw_handle(self) -> RawHandle {
         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))
+    }
+}
index f4957297581bb93f3ebbf0cd6747f3ac2ab09afa..a0ad866c69d413b8b77097ef274d0acf68b4445c 100644 (file)
@@ -337,6 +337,12 @@ pub fn code(&self) -> Option<i32> {
     }
 }
 
+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)