Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the Termination trait's impls #96819

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2107,13 +2107,24 @@ pub trait Termination {
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl Termination for () {
#[inline]
/// Returns `ExitCode::SUCCESS`.
fn report(self) -> ExitCode {
ExitCode::SUCCESS.report()
}
}

#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl<E: fmt::Debug> Termination for Result<(), E> {
/// Returns a meaningful exit code, and outputs an error message if
/// appropriate.
///
/// If the result is `Ok`, returns `ExitCode::SUCCESS`.
///
/// If the result is `Err`, writes the [`fmt::Debug`] representation of
/// the error value to [`io::stderr`] and returns `ExitCode::FAILURE`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to guarantee this behavior, because I expect us to change it in the future.

In particular, if we add the "generic member access" mechanism, we could then change this trait implementation to attempt to retrieve an ExitCode from E, and use that if available.

///
/// [`fmt::Debug`]: crate::fmt::Debug
/// [`io::stderr`]: crate::io::stderr
fn report(self) -> ExitCode {
match self {
Ok(()) => ().report(),
Expand All @@ -2131,6 +2142,13 @@ impl Termination for ! {

#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl<E: fmt::Debug> Termination for Result<!, E> {
/// Returns a meaningful exit code and outputs an error message.
///
/// Writes the [`fmt::Debug`] representation of the error value to
/// [`io::stderr`] and returns `ExitCode::FAILURE`.
Comment on lines +2147 to +2148
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, I expect us to change this in the future.

///
/// [`fmt::Debug`]: crate::fmt::Debug
/// [`io::stderr`]: crate::io::stderr
fn report(self) -> ExitCode {
let Err(err) = self;
eprintln!("Error: {err:?}");
Expand All @@ -2140,6 +2158,13 @@ impl<E: fmt::Debug> Termination for Result<!, E> {

#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl<E: fmt::Debug> Termination for Result<Infallible, E> {
/// Returns a meaningful exit code and outputs an error message.
///
/// Writes the [`fmt::Debug`] representation of the error value to
/// [`io::stderr`] and returns `ExitCode::FAILURE`.
Comment on lines +2163 to +2164
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, I expect us to change this in the future.

///
/// [`fmt::Debug`]: crate::fmt::Debug
/// [`io::stderr`]: crate::io::stderr
fn report(self) -> ExitCode {
let Err(err) = self;
Err::<!, _>(err).report()
Expand All @@ -2148,6 +2173,7 @@ impl<E: fmt::Debug> Termination for Result<Infallible, E> {

#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl Termination for ExitCode {
/// Returns the `ExitCode` unchanged.
#[inline]
fn report(self) -> ExitCode {
self
Expand Down