diff options
| author | Aria Beingessner <a.beingessner@gmail.com> | 2022-06-06 15:14:41 -0400 |
|---|---|---|
| committer | Aria Beingessner <a.beingessner@gmail.com> | 2022-06-17 13:54:17 -0400 |
| commit | 1502713f994d595e4ae1ffbbe8ef3e373bbb11bc (patch) | |
| tree | 492ecf3d27e8dcfb1ab447c65a00a741be30fb80 /library/std/src | |
| parent | 43c47db0b04627dbd0e16a1e7cad14a4a5a60d3a (diff) | |
| download | rust-1502713f994d595e4ae1ffbbe8ef3e373bbb11bc.tar.gz rust-1502713f994d595e4ae1ffbbe8ef3e373bbb11bc.zip | |
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'
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/process.rs | 37 |
1 files changed, 15 insertions, 22 deletions
diff --git a/library/std/src/process.rs b/library/std/src/process.rs index e733766741d..da8eee9030b 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -2141,16 +2141,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 { self @@ -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 + } + } } } |
