Skip to content

Commit de3a63d

Browse files
committed
Rollup merge of rust-lang#48618 - scottmcm:elaborate-exitcode, r=alexcrichton
Better docs and associated SUCCESS/FAILURE for process::ExitCode Follow-up to rust-lang#48497 (comment), since that PR was the minimal thing to unblock rust-lang#48453 (comment). r? @nikomatsakis
2 parents a06aed1 + 74c5c6e commit de3a63d

File tree

8 files changed

+108
-32
lines changed

8 files changed

+108
-32
lines changed

src/libstd/process.rs

+43-28
Original file line numberDiff line numberDiff line change
@@ -1080,14 +1080,45 @@ 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`]: #associatedconstant.SUCCESS
1093+
/// [`FAILURE`]: #associatedconstant.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)]
1089-
#[unstable(feature = "process_exitcode_placeholder", issue = "43301")]
1090-
pub struct ExitCode(pub i32);
1101+
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
1102+
pub struct ExitCode(imp::ExitCode);
1103+
1104+
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
1105+
impl ExitCode {
1106+
/// The canonical ExitCode for successful termination on this platform.
1107+
///
1108+
/// Note that a `()`-returning `main` implicitly results in a successful
1109+
/// termination, so there's no need to return this from `main` unless
1110+
/// you're also returning other possible codes.
1111+
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
1112+
pub const SUCCESS: ExitCode = ExitCode(imp::ExitCode::SUCCESS);
1113+
1114+
/// The canonical ExitCode for unsuccessful termination on this platform.
1115+
///
1116+
/// If you're only returning this and `SUCCESS` from `main`, consider
1117+
/// instead returning `Err(_)` and `Ok(())` respectively, which will
1118+
/// return the same codes (but will also `eprintln!` the error).
1119+
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
1120+
pub const FAILURE: ExitCode = ExitCode(imp::ExitCode::FAILURE);
1121+
}
10911122

10921123
impl Child {
10931124
/// Forces the child to exit. This is equivalent to sending a
@@ -1401,18 +1432,6 @@ pub fn id() -> u32 {
14011432
::sys::os::getpid()
14021433
}
14031434

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-
14161435
/// A trait for implementing arbitrary return types in the `main` function.
14171436
///
14181437
/// The c-main function only supports to return integers as return type.
@@ -1433,18 +1452,15 @@ pub trait Termination {
14331452

14341453
#[unstable(feature = "termination_trait_lib", issue = "43301")]
14351454
impl Termination for () {
1436-
fn report(self) -> i32 { exit::SUCCESS }
1455+
fn report(self) -> i32 { ExitCode::SUCCESS.report() }
14371456
}
14381457

14391458
#[unstable(feature = "termination_trait_lib", issue = "43301")]
14401459
impl<E: fmt::Debug> Termination for Result<(), E> {
14411460
fn report(self) -> i32 {
14421461
match self {
1443-
Ok(val) => val.report(),
1444-
Err(err) => {
1445-
eprintln!("Error: {:?}", err);
1446-
exit::FAILURE
1447-
}
1462+
Ok(()) => ().report(),
1463+
Err(err) => Err::<!, _>(err).report(),
14481464
}
14491465
}
14501466
}
@@ -1459,15 +1475,14 @@ impl<E: fmt::Debug> Termination for Result<!, E> {
14591475
fn report(self) -> i32 {
14601476
let Err(err) = self;
14611477
eprintln!("Error: {:?}", err);
1462-
exit::FAILURE
1478+
ExitCode::FAILURE.report()
14631479
}
14641480
}
14651481

14661482
#[unstable(feature = "termination_trait_lib", issue = "43301")]
14671483
impl Termination for ExitCode {
14681484
fn report(self) -> i32 {
1469-
let ExitCode(code) = self;
1470-
code
1485+
self.0.as_i32()
14711486
}
14721487
}
14731488

src/libstd/sys/cloudabi/shims/process.rs

+12
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ impl fmt::Display for ExitStatus {
126126
}
127127
}
128128

129+
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
130+
pub struct ExitCode(bool);
131+
132+
impl ExitCode {
133+
pub const SUCCESS: ExitCode = ExitCode(false);
134+
pub const FAILURE: ExitCode = ExitCode(true);
135+
136+
pub fn as_i32(&self) -> i32 {
137+
self.0 as i32
138+
}
139+
}
140+
129141
pub struct Process(Void);
130142

131143
impl Process {

src/libstd/sys/redox/process.rs

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use ffi::OsStr;
1313
use os::unix::ffi::OsStrExt;
1414
use fmt;
1515
use io::{self, Error, ErrorKind};
16+
use libc::{EXIT_SUCCESS, EXIT_FAILURE};
1617
use path::{Path, PathBuf};
1718
use sys::fd::FileDesc;
1819
use sys::fs::{File, OpenOptions};
@@ -480,6 +481,18 @@ impl fmt::Display for ExitStatus {
480481
}
481482
}
482483

484+
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
485+
pub struct ExitCode(u8);
486+
487+
impl ExitCode {
488+
pub const SUCCESS: ExitCode = ExitCode(EXIT_SUCCESS as _);
489+
pub const FAILURE: ExitCode = ExitCode(EXIT_FAILURE as _);
490+
491+
pub fn as_i32(&self) -> i32 {
492+
self.0 as i32
493+
}
494+
}
495+
483496
/// The unique id of the process (this should never be negative).
484497
pub struct Process {
485498
pid: usize,

src/libstd/sys/unix/process/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
pub use self::process_common::{Command, ExitStatus, Stdio, StdioPipes};
11+
pub use self::process_common::{Command, ExitStatus, ExitCode, Stdio, StdioPipes};
1212
pub use self::process_inner::Process;
1313

1414
mod process_common;

src/libstd/sys/unix/process/process_common.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use os::unix::prelude::*;
1313
use ffi::{OsString, OsStr, CString, CStr};
1414
use fmt;
1515
use io;
16-
use libc::{self, c_int, gid_t, uid_t, c_char};
16+
use libc::{self, c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE};
1717
use ptr;
1818
use sys::fd::FileDesc;
1919
use sys::fs::{File, OpenOptions};
@@ -393,6 +393,18 @@ impl fmt::Display for ExitStatus {
393393
}
394394
}
395395

396+
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
397+
pub struct ExitCode(u8);
398+
399+
impl ExitCode {
400+
pub const SUCCESS: ExitCode = ExitCode(EXIT_SUCCESS as _);
401+
pub const FAILURE: ExitCode = ExitCode(EXIT_FAILURE as _);
402+
403+
pub fn as_i32(&self) -> i32 {
404+
self.0 as i32
405+
}
406+
}
407+
396408
#[cfg(all(test, not(target_os = "emscripten")))]
397409
mod tests {
398410
use super::*;

src/libstd/sys/wasm/process.rs

+12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ impl fmt::Display for ExitStatus {
129129
}
130130
}
131131

132+
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
133+
pub struct ExitCode(bool);
134+
135+
impl ExitCode {
136+
pub const SUCCESS: ExitCode = ExitCode(false);
137+
pub const FAILURE: ExitCode = ExitCode(true);
138+
139+
pub fn as_i32(&self) -> i32 {
140+
self.0 as i32
141+
}
142+
}
143+
132144
pub struct Process(Void);
133145

134146
impl Process {

src/libstd/sys/windows/process.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ffi::{OsString, OsStr};
1818
use fmt;
1919
use fs;
2020
use io::{self, Error, ErrorKind};
21-
use libc::c_void;
21+
use libc::{c_void, EXIT_SUCCESS, EXIT_FAILURE};
2222
use mem;
2323
use os::windows::ffi::OsStrExt;
2424
use path::Path;
@@ -408,6 +408,18 @@ impl fmt::Display for ExitStatus {
408408
}
409409
}
410410

411+
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
412+
pub struct ExitCode(c::DWORD);
413+
414+
impl ExitCode {
415+
pub const SUCCESS: ExitCode = ExitCode(EXIT_SUCCESS as _);
416+
pub const FAILURE: ExitCode = ExitCode(EXIT_FAILURE as _);
417+
418+
pub fn as_i32(&self) -> i32 {
419+
self.0 as i32
420+
}
421+
}
422+
411423
fn zeroed_startupinfo() -> c::STARTUPINFO {
412424
c::STARTUPINFO {
413425
cb: 0,

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

+1-1
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)