Skip to content

Commit fc2e4e7

Browse files
committed
Put some thought and documentation effort into process::ExitCode
1 parent e20f7b2 commit fc2e4e7

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

src/libstd/process.rs

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,15 +1080,58 @@ impl fmt::Display for ExitStatus {
10801080
}
10811081
}
10821082

1083-
/// This is ridiculously unstable, as it's a completely-punted-upon part
1084-
/// of the `?`-in-`main` RFC. It's here only to allow experimenting with
1085-
/// returning a code directly from main. It will definitely change
1086-
/// drastically before being stabilized, if it doesn't just get deleted.
1087-
#[doc(hidden)]
1083+
/// This type represents the status code a process can return to its
1084+
/// parent under normal termination.
1085+
///
1086+
/// Numeric values used in this type don't have portable meanings, and
1087+
/// different platforms may mask different amounts of them.
1088+
///
1089+
/// For the platform's canonical successful and unsuccessful codes, see
1090+
/// the [`SUCCESS`] and [`FAILURE`] associated items.
1091+
///
1092+
/// [`SUCCESS`]: #constant.SUCCESS
1093+
/// [`FAILURE`]: #constant.FAILURE
1094+
///
1095+
/// **Warning**: While various forms of this were discussed in [RFC #1937],
1096+
/// it was ultimately cut from that RFC, and thus this type is more subject
1097+
/// to change even than the usual unstable item churn.
1098+
///
1099+
/// [RFC #1937]: https://github.com/rust-lang/rfcs/pull/1937
10881100
#[derive(Clone, Copy, Debug)]
10891101
#[unstable(feature = "process_exitcode_placeholder", issue = "43301")]
10901102
pub struct ExitCode(pub i32);
10911103

1104+
#[cfg(target_arch = "wasm32")]
1105+
mod rawexit {
1106+
pub const SUCCESS: i32 = 0;
1107+
pub const FAILURE: i32 = 1;
1108+
}
1109+
#[cfg(not(target_arch = "wasm32"))]
1110+
mod rawexit {
1111+
use libc;
1112+
pub const SUCCESS: i32 = libc::EXIT_SUCCESS;
1113+
pub const FAILURE: i32 = libc::EXIT_FAILURE;
1114+
}
1115+
1116+
#[unstable(feature = "process_exitcode_placeholder", issue = "43301")]
1117+
impl ExitCode {
1118+
/// The canonical ExitCode for successful termination on this platform.
1119+
///
1120+
/// Note that a `()`-returning `main` implicitly results in a successful
1121+
/// termination, so there's no need to return this from `main` unless
1122+
/// you're also returning other possible codes.
1123+
#[unstable(feature = "process_exitcode_placeholder", issue = "43301")]
1124+
pub const SUCCESS: ExitCode = ExitCode(rawexit::SUCCESS);
1125+
1126+
/// The canonical ExitCode for unsuccessful termination on this platform.
1127+
///
1128+
/// If you're only returning this and `SUCCESS` from `main`, consider
1129+
/// instead returning `Err(_)` and `Ok(())` respectively, which will
1130+
/// return the same codes (but will also `eprintln!` the error).
1131+
#[unstable(feature = "process_exitcode_placeholder", issue = "43301")]
1132+
pub const FAILURE: ExitCode = ExitCode(rawexit::FAILURE);
1133+
}
1134+
10921135
impl Child {
10931136
/// Forces the child to exit. This is equivalent to sending a
10941137
/// SIGKILL on unix platforms.
@@ -1401,18 +1444,6 @@ pub fn id() -> u32 {
14011444
::sys::os::getpid()
14021445
}
14031446

1404-
#[cfg(target_arch = "wasm32")]
1405-
mod exit {
1406-
pub const SUCCESS: i32 = 0;
1407-
pub const FAILURE: i32 = 1;
1408-
}
1409-
#[cfg(not(target_arch = "wasm32"))]
1410-
mod exit {
1411-
use libc;
1412-
pub const SUCCESS: i32 = libc::EXIT_SUCCESS;
1413-
pub const FAILURE: i32 = libc::EXIT_FAILURE;
1414-
}
1415-
14161447
/// A trait for implementing arbitrary return types in the `main` function.
14171448
///
14181449
/// The c-main function only supports to return integers as return type.
@@ -1433,18 +1464,15 @@ pub trait Termination {
14331464

14341465
#[unstable(feature = "termination_trait_lib", issue = "43301")]
14351466
impl Termination for () {
1436-
fn report(self) -> i32 { exit::SUCCESS }
1467+
fn report(self) -> i32 { ExitCode::SUCCESS.report() }
14371468
}
14381469

14391470
#[unstable(feature = "termination_trait_lib", issue = "43301")]
14401471
impl<E: fmt::Debug> Termination for Result<(), E> {
14411472
fn report(self) -> i32 {
14421473
match self {
1443-
Ok(val) => val.report(),
1444-
Err(err) => {
1445-
eprintln!("Error: {:?}", err);
1446-
exit::FAILURE
1447-
}
1474+
Ok(()) => ().report(),
1475+
Err(err) => Err::<!, _>(err).report(),
14481476
}
14491477
}
14501478
}
@@ -1459,7 +1487,7 @@ impl<E: fmt::Debug> Termination for Result<!, E> {
14591487
fn report(self) -> i32 {
14601488
let Err(err) = self;
14611489
eprintln!("Error: {:?}", err);
1462-
exit::FAILURE
1490+
ExitCode::FAILURE.report()
14631491
}
14641492
}
14651493

src/test/run-pass/rfc-1937-termination-trait/termination-trait-for-exitcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
use std::process::ExitCode;
1515

1616
fn main() -> ExitCode {
17-
ExitCode(0)
17+
ExitCode::SUCCESS
1818
}

0 commit comments

Comments
 (0)