From 506a27b3819d64115919447bc0b7abee70962236 Mon Sep 17 00:00:00 2001 From: Aron Parker Date: Thu, 9 Jun 2022 15:29:58 +0200 Subject: [PATCH 1/4] Implement ExitCodeExt for Windows --- std/src/os/windows/process.rs | 19 +++++++++++++++++++ std/src/process.rs | 16 ++++++++++++++++ std/src/sys/windows/process.rs | 6 ++++++ 3 files changed, 41 insertions(+) diff --git a/std/src/os/windows/process.rs b/std/src/os/windows/process.rs index 1c7e361c2..179c6e788 100644 --- a/std/src/os/windows/process.rs +++ b/std/src/os/windows/process.rs @@ -194,3 +194,22 @@ impl ChildExt for process::Child { self.handle.main_thread_handle() } } + +/// Windows-specific extensions to [`process::ExitCode`]. +/// +/// This trait is sealed: it cannot be implemented outside the standard library. +/// This is so that future additional methods are not breaking changes. +#[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] +pub trait ExitCodeExt: Sealed { + /// Creates a new `ExitStatus` from the raw underlying `u32` return value of + /// a process. + #[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] + fn from_raw(raw: u32) -> Self; +} + +#[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] +impl ExitCodeExt for process::ExitCode { + fn from_raw(raw: u32) -> Self { + process::ExitCode::from_inner(From::from(raw)) + } +} diff --git a/std/src/process.rs b/std/src/process.rs index 1def9fe09..903ad01a2 100644 --- a/std/src/process.rs +++ b/std/src/process.rs @@ -1708,6 +1708,10 @@ impl crate::error::Error for ExitStatusError {} #[stable(feature = "process_exitcode", since = "1.61.0")] pub struct ExitCode(imp::ExitCode); +/// Allows extension traits within `std`. +#[unstable(feature = "sealed", issue = "none")] +impl crate::sealed::Sealed for ExitCode {} + #[stable(feature = "process_exitcode", since = "1.61.0")] impl ExitCode { /// The canonical `ExitCode` for successful termination on this platform. @@ -1798,6 +1802,18 @@ impl From for ExitCode { } } +impl AsInner for ExitCode { + fn as_inner(&self) -> &imp::ExitCode { + &self.0 + } +} + +impl FromInner for ExitCode { + fn from_inner(s: imp::ExitCode) -> ExitCode { + ExitCode(s) + } +} + impl Child { /// Forces the child process to exit. If the child has already exited, an [`InvalidInput`] /// error is returned. diff --git a/std/src/sys/windows/process.rs b/std/src/sys/windows/process.rs index 9fd399f4b..02d5af471 100644 --- a/std/src/sys/windows/process.rs +++ b/std/src/sys/windows/process.rs @@ -707,6 +707,12 @@ impl From for ExitCode { } } +impl From for ExitCode { + fn from(code: u32) -> Self { + ExitCode(c::DWORD::from(code)) + } +} + fn zeroed_startupinfo() -> c::STARTUPINFO { c::STARTUPINFO { cb: 0, From 402f8fa2c8d71f2371f3fb234e99447cdf999f6f Mon Sep 17 00:00:00 2001 From: Aron Parker Date: Fri, 10 Jun 2022 14:21:49 +0200 Subject: [PATCH 2/4] Fix copy paste error --- std/src/os/windows/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/src/os/windows/process.rs b/std/src/os/windows/process.rs index 179c6e788..027a901c0 100644 --- a/std/src/os/windows/process.rs +++ b/std/src/os/windows/process.rs @@ -201,7 +201,7 @@ impl ChildExt for process::Child { /// This is so that future additional methods are not breaking changes. #[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] pub trait ExitCodeExt: Sealed { - /// Creates a new `ExitStatus` from the raw underlying `u32` return value of + /// Creates a new `ExitCode` from the raw underlying `u32` return value of /// a process. #[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] fn from_raw(raw: u32) -> Self; From f6261d7e49c0a61d0a5cf1ac79ab180d9a17246b Mon Sep 17 00:00:00 2001 From: Aron Parker Date: Fri, 10 Jun 2022 14:33:19 +0200 Subject: [PATCH 3/4] Incorporate warning for potential exit code ambiguities --- std/src/os/windows/process.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/std/src/os/windows/process.rs b/std/src/os/windows/process.rs index 027a901c0..2da1b159d 100644 --- a/std/src/os/windows/process.rs +++ b/std/src/os/windows/process.rs @@ -203,6 +203,10 @@ impl ChildExt for process::Child { pub trait ExitCodeExt: Sealed { /// Creates a new `ExitCode` from the raw underlying `u32` return value of /// a process. + /// + /// The exit code should not be 259, as this conflicts with the `STILL_ACTIVE` + /// macro returned from the `GetExitCodeProcess` function to signal that the + /// process has yet to run to completion. #[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] fn from_raw(raw: u32) -> Self; } From cbd2fe86b714c671cde4fa868adaf2c9d1b7e1b1 Mon Sep 17 00:00:00 2001 From: Aron Parker Date: Fri, 10 Jun 2022 14:55:13 +0200 Subject: [PATCH 4/4] Make "windows_process_exit_code_from" unstable --- std/src/os/windows/process.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/std/src/os/windows/process.rs b/std/src/os/windows/process.rs index 2da1b159d..164da2a49 100644 --- a/std/src/os/windows/process.rs +++ b/std/src/os/windows/process.rs @@ -199,7 +199,7 @@ impl ChildExt for process::Child { /// /// This trait is sealed: it cannot be implemented outside the standard library. /// This is so that future additional methods are not breaking changes. -#[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] +#[unstable(feature = "windows_process_exit_code_from", issue = "none")] pub trait ExitCodeExt: Sealed { /// Creates a new `ExitCode` from the raw underlying `u32` return value of /// a process. @@ -207,11 +207,11 @@ pub trait ExitCodeExt: Sealed { /// The exit code should not be 259, as this conflicts with the `STILL_ACTIVE` /// macro returned from the `GetExitCodeProcess` function to signal that the /// process has yet to run to completion. - #[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] + #[unstable(feature = "windows_process_exit_code_from", issue = "none")] fn from_raw(raw: u32) -> Self; } -#[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] +#[unstable(feature = "windows_process_exit_code_from", issue = "none")] impl ExitCodeExt for process::ExitCode { fn from_raw(raw: u32) -> Self { process::ExitCode::from_inner(From::from(raw))