Skip to content

Commit

Permalink
Impl Termination for Infallible and then make the Result impls of Ter…
Browse files Browse the repository at this point in the history
…mination into a blanket

This allows things like `Result<ExitCode, E>` to 'just work'
  • Loading branch information
Gankra committed Jun 17, 2022
1 parent 48347fb commit d3cd707
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,16 +2140,6 @@ impl Termination for () {
}
}

#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl<E: fmt::Debug> Termination for Result<(), E> {
fn report(self) -> ExitCode {
match self {
Ok(()) => ().report(),
Err(err) => Err::<!, _>(err).report(),
}
}
}

#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl Termination for ! {
fn report(self) -> ExitCode {
Expand All @@ -2158,28 +2148,31 @@ impl Termination for ! {
}

#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl<E: fmt::Debug> Termination for Result<!, E> {
impl Termination for Infallible {
fn report(self) -> ExitCode {
let Err(err) = self;
// Ignore error if the write fails, for example because stderr is
// already closed. There is not much point panicking at this point.
let _ = writeln!(io::stderr(), "Error: {err:?}");
ExitCode::FAILURE
match self {}
}
}

#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl<E: fmt::Debug> Termination for Result<Infallible, E> {
impl Termination for ExitCode {
#[inline]
fn report(self) -> ExitCode {
let Err(err) = self;
Err::<!, _>(err).report()
self
}
}

#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl Termination for ExitCode {
#[inline]
impl<T: Termination, E: fmt::Debug> Termination for Result<T, E> {
fn report(self) -> ExitCode {
self
match self {
Ok(val) => val.report(),
Err(err) => {
// Ignore error if the write fails, for example because stderr is
// already closed. There is not much point panicking at this point.
let _ = writeln!(io::stderr(), "Error: {err:?}");
ExitCode::FAILURE
}
}
}
}

0 comments on commit d3cd707

Please sign in to comment.