Skip to content

Commit 9de575a

Browse files
committed
Add an example of process::Termination
1 parent 545a9c7 commit 9de575a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

posts/2022-05-19-Rust-1.61.0.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ type that wraps platform-specific return types. That has `SUCCESS` and `FAILURE`
4141
implements `From<u8>` for more arbitrary values. The `Termination` trait can also be implemented for
4242
your own types, allowing you to customize any kind of reporting before converting to an `ExitCode`.
4343

44+
For example, here's a type-safe way to write exit codes for a `git bisect run` script:
45+
46+
```rust
47+
use std::process::{ExitCode, Termination};
48+
49+
#[repr(u8)]
50+
pub enum GitBisectResult {
51+
Good = 0,
52+
Bad = 1,
53+
Skip = 125,
54+
Abort = 255,
55+
}
56+
57+
impl Termination for GitBisectResult {
58+
fn report(self) -> ExitCode {
59+
// Maybe print a message here
60+
ExitCode::from(self as u8)
61+
}
62+
}
63+
64+
fn main() -> GitBisectResult {
65+
std::panic::catch_unwind(|| {
66+
todo!("test the commit")
67+
}).unwrap_or(GitBisectResult::Abort)
68+
}
69+
```
70+
4471
### More capabilities for `const fn`
4572

4673
Several incremental features have been stabilized in this release to enable more functionality in

0 commit comments

Comments
 (0)