5 unstable releases
0.3.0 | Aug 19, 2024 |
---|---|
0.2.0 | Aug 18, 2024 |
0.1.2 | Aug 1, 2024 |
0.1.1 | Aug 1, 2024 |
0.1.0 | Aug 1, 2024 |
#367 in Debugging
383 downloads per month
Used in pyri_tooltip
21KB
487 lines
tiny_bail
Bailing is an error-handling pattern that takes the middle path between unwrap
and ?
:
- Compared to
unwrap
: Bail willreturn
,continue
, orbreak
instead of panicking. - Compared to
?
: Bail will log or ignore the error instead of propagating it.
The middle path avoids unwanted panics without the ergonomic challenges of propagating errors with ?
.
Getting started
This crate provides six macro variants:
Along with their tiny aliases:
r!
,
rq!
,
c!
,
cq!
,
b!
, and
bq!
.
The macros support bool
, Option
, and Result
types out of the box. Implement
Success
to extend this to other types.
You can specify a return value as an optional first argument to the macro, or omit it to default to
Default::default()
—which even works in functions with no return value.
Example
use tiny_bail::prelude::*;
// With `tiny_bail`:
fn increment_last(arr: &mut [i32]) {
*r!(arr.last_mut()) += 1;
}
// Without `tiny_bail`:
fn increment_last_manually(arr: &mut [i32]) {
if let Some(x) = arr.last_mut() {
*x += 1;
} else {
println!("Bailed at src/example.rs:34:18: `arr.last_mut()`");
return;
}
}
License
This crate is available under either of MIT or Apache-2.0 at your choice.
Dependencies
~355–500KB