Exit rust apps safely
std::process::exit warns:
"Note that because this function never returns, and that it terminates the process, no destructors on the current stack or any other thread’s stack will be run. If a clean shutdown is needed it is recommended to ... simply return a type implementing Termination ... from the main function and avoid this function altogether"
exit_safely provides a simple and highly transparent option to derive(Termination) from your own enum with a very simple API which still provides you full control over exit codes and what to (safely) output to stderr.
Minimal magic, maximum flexibility, zero boilerplate.
Example
Here's the full example from the integration tests, showing how this plays really nicely with Try (derived via try_v2)
use Termination;
use *;
use io;
use Termination as _T; // Needed as trait bound for Exit
// Allows for simple `?` propogation of `io::Error`s
// And for validation of data you can impl From<foo> for Exit
// if given too few args it will fail with ExitCode 2 (InvocationError) and a message
// if given `<FILENAME> <FAIL>` it will fail with ExitCode (Other) 3,
// if given `<FILENAME> <ANYTHING_ELSE>` it will check file exists
Stability
🔬 Experimental Features
This crate makes use of the following experimental features:
#![feature(if_let_guard)]#![feature(iterator_try_collect)]#![feature(never_type)]#![feature(proc_macro_diagnostic)]#![feature(try_trait_v2)]Since
Terminationworks best for types which also implement the experimentalTry, we hope this is acceptable to you.The authors consider all of the above features to be reliable and already well advanced in the stabilisation process. Nevertheless, we run automated tests every month to ensure no fundamental changes affect this crate.