Impl Termination for Infallible and then make the Result impls of Termination into a blanket
This allows things like `Result<ExitCode, E>` to 'just work'
This commit is contained in:
parent
43c47db0b0
commit
1502713f99
1 changed files with 17 additions and 24 deletions
|
@ -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")]
|
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
|
||||||
impl Termination for ! {
|
impl Termination for ! {
|
||||||
fn report(self) -> ExitCode {
|
fn report(self) -> ExitCode {
|
||||||
|
@ -2158,21 +2148,9 @@ impl Termination for ! {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
|
#[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 {
|
fn report(self) -> ExitCode {
|
||||||
let Err(err) = self;
|
match 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
|
|
||||||
impl<E: fmt::Debug> Termination for Result<Infallible, E> {
|
|
||||||
fn report(self) -> ExitCode {
|
|
||||||
let Err(err) = self;
|
|
||||||
Err::<!, _>(err).report()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2183,3 +2161,18 @@ impl Termination for ExitCode {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
|
||||||
|
impl<T: Termination, E: fmt::Debug> Termination for Result<T, E> {
|
||||||
|
fn report(self) -> ExitCode {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue