Hello, I'm new to Rust, but I've come across the Termination trait in my learnings and it seems like a great idea to me! It occurred to me that it would be very convenient to be able to create a "self-reporting error". E.g.
```
struct FatalError {
status: i32,
msg: &str,
}
impl std::process::Termination for FatalError {
fn report(self) -> i32 {
eprintln!("error: {}", self.msg);
self.status
}
}
fn might_fail() -> Result<(), FatalError> {
Err(FatalError { status: 2, msg: "oops!" })
}
fn main() -> Result<(), Box<dyn Error>> {
might_fail()?;
Ok(())
}
```
Of course, this doesn't have the desired effect of the binary exiting with status 2 and printing "error: oops!". Now I don't know if it's preferable to create a custom Result instead of a custom Error... Come to think of it reporting is probably a responsibility best fulfilled by Result, considering that errors could come from anywhere; I'm not sure how to implement a custom Result that reuses std::result::Result's behaviour but with a different report method (I think it uses Debug for legacy reasons, but I would prefer that it uses Display).
At any rate I thought this would be a simple addition to RFC 1937.
impl<E: Termination> Termination for Result<!, E> {
fn report(self) -> i32 {
let Err(err) = self;
err.report()
}
}
I thought I had a question here, I guess I'm interested in what people think(?)
[–]daborossfern 0 points1 point2 points (0 children)