Crates.io | tiny_bail |
lib.rs | tiny_bail |
version | |
source | src |
created_at | 2024-08-01 07:41:02.822725 |
updated_at | 2024-11-05 23:49:20.289099 |
description | Small but flexible macros for bailing on failure. |
homepage | |
repository | https://github.com/benfrankel/tiny_bail |
max_upload_size | |
id | 1321727 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Bailing is an error-handling pattern that takes the middle path between unwrap
and ?
:
unwrap
: Bail will return
, continue
, or break
instead of panicking.?
: 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 ?
.
This crate provides six macro variants:
Along with their tiny aliases:
r!
,
rq!
,
c!
,
cq!
,
b!
, and
bq!
.
The macros support Result
, Option
, and bool
types out of the box. You can implement
IntoResult
to extend this to other types.
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 {
tracing::warn!("Bailed at src/example.rs:34:18: `arr.last_mut()` is `None`");
return;
}
}
This crate is available under either of MIT or Apache-2.0 at your choice.