tiny_bail

Crates.iotiny_bail
lib.rstiny_bail
version0.7.0
created_at2024-08-01 07:41:02.822725+00
updated_at2025-09-24 06:48:52.747072+00
descriptionSmall but flexible macros for bailing on failure.
homepage
repositoryhttps://github.com/benfrankel/tiny_bail
max_upload_size
id1321727
size51,134
Ben Frankel (benfrankel)

documentation

README

tiny_bail

Crates.io Docs License

Bailing is an error-handling pattern that takes the middle path between unwrap and ?:

  • Compared to unwrap: Bailing will return, continue, or break instead of panicking.
  • Compared to ?: Bailing will log or quietly discard the error instead of returning it.

The middle path avoids unwanted panics without the ergonomic challenges of propagating errors with ?.

This crate provides the following macro variants to determine the preferred behavior on failure:

Along with their tiny aliases: r!, rq!, ro!, c!, cq!, co!, b!, bq!, and bo!.

The macros support Result, Option, and bool types out of the box. You can implement IntoResult to extend this to other types.

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 {
        tracing::warn!("Bailed at src/example.rs:34:18: `arr.last_mut()` is `None`");
        return;
    }
}

Getting started

To use this crate, add it to your Cargo.toml:

cargo add tiny_bail

You can set features to customize the logging behavior on bail:

# Log with `println!` instead of `tracing::warn!`.
cargo add tiny_bail --no-default-features
# Log with `log::info!` instead of `tracing::warn!`.
cargo add tiny_bail --no-default-features --features log,info

This crate has zero dependencies other than the logging backend you choose (log, tracing, or nothing).

License

This crate is available under either of MIT or Apache-2.0 at your choice.

Commit count: 60

cargo fmt