Skip to content

Commit 62c7f1d

Browse files
authored
Rollup merge of rust-lang#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 - rust-lang#48711 - rust-lang#43301 - https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Termination.2FExit.20Status.20Stabilization
2 parents bf72ed1 + cf4ac6b commit 62c7f1d

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

library/std/src/process.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,14 @@ impl ExitCode {
16911691
}
16921692
}
16931693

1694+
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
1695+
impl From<u8> for ExitCode {
1696+
/// Construct an exit code from an arbitrary u8 value.
1697+
fn from(code: u8) -> Self {
1698+
ExitCode(imp::ExitCode::from(code))
1699+
}
1700+
}
1701+
16941702
impl Child {
16951703
/// Forces the child process to exit. If the child has already exited, an [`InvalidInput`]
16961704
/// error is returned.

library/std/src/sys/unix/process/process_common.rs

+6
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,12 @@ impl ExitCode {
476476
}
477477
}
478478

479+
impl From<u8> for ExitCode {
480+
fn from(code: u8) -> Self {
481+
Self(code)
482+
}
483+
}
484+
479485
pub struct CommandArgs<'a> {
480486
iter: crate::slice::Iter<'a, CString>,
481487
}

library/std/src/sys/unsupported/process.rs

+9
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ impl ExitCode {
162162
}
163163
}
164164

165+
impl From<u8> for ExitCode {
166+
fn from(code: u8) -> Self {
167+
match code {
168+
0 => Self::SUCCESS,
169+
1..255 => Self::FAILURE,
170+
}
171+
}
172+
}
173+
165174
pub struct Process(!);
166175

167176
impl Process {

library/std/src/sys/windows/process.rs

+6
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,12 @@ impl ExitCode {
666666
}
667667
}
668668

669+
impl From<u8> for ExitCode {
670+
fn from(code: u8) -> Self {
671+
ExitCode(c::DWORD::from(code))
672+
}
673+
}
674+
669675
fn zeroed_startupinfo() -> c::STARTUPINFO {
670676
c::STARTUPINFO {
671677
cb: 0,

0 commit comments

Comments
 (0)