Skip to content

Commit 9ad4bde

Browse files
committed
Auto merge of #95356 - coolreader18:exitstatus-exit-method, r=<try>
ExitCode::exit_process() method cc `@yaahc` / #93840 (eeek, hit ctrl-enter before I meant to and right after realizing the branch name was wrong. oh, well) I feel like it makes sense to have the `exit(ExitCode)` function as a method or at least associated function on ExitCode, but maybe that would hurt discoverability? Probably not as much if it's at the top of the `process::exit()` documentation or something, but idk. Also very unsure about the name, I'd like something that communicates that you are exiting with *this* ExitCode, but with a method name being postfix it doesn't seem to flow. `code.exit_process_with()` ? `.exit_process_with_self()` ? Blech. Maybe it doesn't matter, since ideally just `code.exit()` or something would be clear simply by the name and single parameter but 🤷 Also I'd like to touch up the `ExitCode` docs (which I did a bit here), but that would probably be good in a separate PR, right? Since I think the beta deadline is coming up.
2 parents a7d6408 + a9e29d2 commit 9ad4bde

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

library/std/src/process.rs

+56-28
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,49 @@ impl ExitCode {
17251725
/// return the same codes (but will also `eprintln!` the error).
17261726
#[stable(feature = "process_exitcode", since = "1.61.0")]
17271727
pub const FAILURE: ExitCode = ExitCode(imp::ExitCode::FAILURE);
1728+
1729+
/// Exit the current process with the given `ExitCode`.
1730+
///
1731+
/// Note that this has the same caveats as [`process::exit()`][exit], namely that this function
1732+
/// terminates the process immediately, so no destructors on the current stack or any other
1733+
/// thread's stack will be run. If a clean shutdown is needed, it is recommended to simply
1734+
/// return this ExitCode from the `main` function, as demonstrated in the [type
1735+
/// documentation](#examples).
1736+
///
1737+
/// # Differences from `process::exit()`
1738+
///
1739+
/// `process::exit()` accepts any `i32` value as the exit code for the process; however, there
1740+
/// are platforms that only use a subset of that value (see [`process::exit` platform-specific
1741+
/// behavior][exit#platform-specific-behavior]). `ExitCode` exists because of this; only
1742+
/// `ExitCode`s that are supported by a majority of our platforms can be created, so those
1743+
/// problems don't exist (as much) with this method.
1744+
///
1745+
/// # Examples
1746+
///
1747+
/// ```
1748+
/// #![feature(exitcode_exit_method)]
1749+
/// # use std::process::ExitCode;
1750+
/// # use std::fmt;
1751+
/// # enum UhOhError { GenericProblem, Specific, WithCode { exit_code: ExitCode, _x: () } }
1752+
/// # impl fmt::Display for UhOhError {
1753+
/// # fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { unimplemented!() }
1754+
/// # }
1755+
/// // there's no way to gracefully recover from an UhOhError, so we just
1756+
/// // print a message and exit
1757+
/// fn handle_unrecoverable_error(err: UhOhError) -> ! {
1758+
/// eprintln!("UH OH! {err}");
1759+
/// let code = match err {
1760+
/// UhOhError::GenericProblem => ExitCode::FAILURE,
1761+
/// UhOhError::Specific => ExitCode::from(3),
1762+
/// UhOhError::WithCode { exit_code, .. } => exit_code,
1763+
/// };
1764+
/// code.exit_process()
1765+
/// }
1766+
/// ```
1767+
#[unstable(feature = "exitcode_exit_method", issue = "none")]
1768+
pub fn exit_process(self) -> ! {
1769+
exit(self.to_i32())
1770+
}
17281771
}
17291772

17301773
impl ExitCode {
@@ -1944,47 +1987,32 @@ impl Child {
19441987
/// process, no destructors on the current stack or any other thread's stack
19451988
/// will be run. If a clean shutdown is needed it is recommended to only call
19461989
/// this function at a known point where there are no more destructors left
1947-
/// to run.
1990+
/// to run; or, preferably, simply return a type implementing [`Termination`]
1991+
/// (such as [`ExitCode`] or `Result`) from the `main` function and avoid this
1992+
/// function altogether:
1993+
///
1994+
/// ```
1995+
/// # use std::io::Error as MyError;
1996+
/// fn main() -> Result<(), MyError> {
1997+
/// // ...
1998+
/// Ok(())
1999+
/// }
2000+
/// ```
19482001
///
19492002
/// ## Platform-specific behavior
19502003
///
19512004
/// **Unix**: On Unix-like platforms, it is unlikely that all 32 bits of `exit`
19522005
/// will be visible to a parent process inspecting the exit code. On most
19532006
/// Unix-like platforms, only the eight least-significant bits are considered.
19542007
///
1955-
/// # Examples
1956-
///
1957-
/// Due to this function’s behavior regarding destructors, a conventional way
1958-
/// to use the function is to extract the actual computation to another
1959-
/// function and compute the exit code from its return value:
1960-
///
1961-
/// ```
1962-
/// fn run_app() -> Result<(), ()> {
1963-
/// // Application logic here
1964-
/// Ok(())
1965-
/// }
1966-
///
1967-
/// fn main() {
1968-
/// std::process::exit(match run_app() {
1969-
/// Ok(_) => 0,
1970-
/// Err(err) => {
1971-
/// eprintln!("error: {err:?}");
1972-
/// 1
1973-
/// }
1974-
/// });
1975-
/// }
1976-
/// ```
1977-
///
1978-
/// Due to [platform-specific behavior], the exit code for this example will be
1979-
/// `0` on Linux, but `256` on Windows:
2008+
/// For example, the exit code for this example will be `0` on Linux, but `256`
2009+
/// on Windows:
19802010
///
19812011
/// ```no_run
19822012
/// use std::process;
19832013
///
19842014
/// process::exit(0x0100);
19852015
/// ```
1986-
///
1987-
/// [platform-specific behavior]: #platform-specific-behavior
19882016
#[stable(feature = "rust1", since = "1.0.0")]
19892017
pub fn exit(code: i32) -> ! {
19902018
crate::rt::cleanup();

0 commit comments

Comments
 (0)