Rollup merge of #93445 - yaahc:exitcode-constructor, r=dtolnay

Add From<u8> for ExitCode

This should cover a mostly cross-platform subset of supported exit codes.

We decided to stick with `u8` initially since its the common subset between all platforms that we support (excluding wasm which I think only works with `true` or `false`). Posix is supposed to take i32s, but in practice many unix platforms mask out all but the low 8 bits or in some cases the 8-15th bits. Windows takes a u32 instead of an i32. Bourne-compatible shells also report signals as exitcode 128 + `signal_no`, so there's some ambiguity there when returning exit codes > 127, but it is possible to disambiguate them on the other side so we decided against restricting the possible codes further than to `u8`.

## Related

- Detailed analysis of exit code support on various platforms: https://internals.rust-lang.org/t/mini-pre-rfc-redesigning-process-exitstatus/5426
- https://github.com/rust-lang/rust/issues/48711
- https://github.com/rust-lang/rust/issues/43301
- https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Termination.2FExit.20Status.20Stabilization
This commit is contained in:
Yuki Okushi 2022-02-09 14:12:17 +09:00 committed by GitHub
commit ec2fd8a35f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 0 deletions

View File

@ -1691,6 +1691,14 @@ pub fn to_i32(self) -> i32 {
}
}
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
impl From<u8> for ExitCode {
/// Construct an exit code from an arbitrary u8 value.
fn from(code: u8) -> Self {
ExitCode(imp::ExitCode::from(code))
}
}
impl Child {
/// Forces the child process to exit. If the child has already exited, an [`InvalidInput`]
/// error is returned.

View File

@ -476,6 +476,12 @@ pub fn as_i32(&self) -> i32 {
}
}
impl From<u8> for ExitCode {
fn from(code: u8) -> Self {
Self(code)
}
}
pub struct CommandArgs<'a> {
iter: crate::slice::Iter<'a, CString>,
}

View File

@ -162,6 +162,15 @@ pub fn as_i32(&self) -> i32 {
}
}
impl From<u8> for ExitCode {
fn from(code: u8) -> Self {
match code {
0 => Self::SUCCESS,
1..=255 => Self::FAILURE,
}
}
}
pub struct Process(!);
impl Process {

View File

@ -666,6 +666,12 @@ pub fn as_i32(&self) -> i32 {
}
}
impl From<u8> for ExitCode {
fn from(code: u8) -> Self {
ExitCode(c::DWORD::from(code))
}
}
fn zeroed_startupinfo() -> c::STARTUPINFO {
c::STARTUPINFO {
cb: 0,