Expand description
§impass
impass is a utility crate that provides the fatal! macro for handling
unrecoverable errors in a declarative and safe way. It is built on the
philosophy that some errors are not meant to be handled gracefully, but
rather signal a critical failure in program logic or state.
The fatal! macro provides a clear, concise, and idiomatic way to express
this “fail-fast” intent. It is analogous to the assert! macro, where a
failed assertion indicates a bug that should crash the program.
§Why fatal!?
The idiomatic way to handle a Result that is expected to always be Ok is
to use .unwrap() or .expect(). While this works, it can become verbose
when chaining multiple fallible operations.
This macro provides a single, cohesive block for this “fail-fast” behavior, making your code more readable and your intent explicit.
§Quick Start
You can simply use the macro to wrap a block of code where you expect all operations to succeed:
use thiserror::Error;
use impass::fatal;
// Declare an error type for demonstration purposes.
#[derive(Error, Debug)]
pub enum MyError {
#[error("This operation failed")]
OperationFailed
}
// A fallible function for demonstration.
fn might_fail(value: i32) -> Result<i32, MyError> {
if value < 10 {
Err(MyError::OperationFailed)
} else {
Ok(value * 2)
}
}
fn main() {
// Usage with a custom message using the `#![reason]` attribute.
let final_value = fatal! {
#![reason("The crucial calculation failed. Program state is unrecoverable")]
let value = might_fail(15)?;
Ok(value)
};
println!("Execution succeeded. Final value: {}", final_value);
// An example that will cause a panic.
fatal! {
let result = might_fail(5)?;
Ok(())
}
}Macros§
- fatal
- A declarative macro for handling critical, unrecoverable errors.
Attribute Macros§
- fatal_
fn - An attribute macro that wraps a function’s body in the
fatal!macro.