9 unstable releases (3 breaking)

0.4.3 Nov 5, 2024
0.4.2 Nov 4, 2024
0.3.0 Aug 19, 2024
0.2.0 Aug 18, 2024
0.1.2 Aug 1, 2024

#288 in Debugging

Download history 313/week @ 2024-08-16 32/week @ 2024-08-23 13/week @ 2024-08-30 14/week @ 2024-09-06 46/week @ 2024-09-13 24/week @ 2024-09-20 29/week @ 2024-09-27 113/week @ 2024-10-04 65/week @ 2024-10-11 64/week @ 2024-10-18 33/week @ 2024-10-25 550/week @ 2024-11-01 53/week @ 2024-11-08 20/week @ 2024-11-15 24/week @ 2024-11-22 66/week @ 2024-11-29

198 downloads per month
Used in pyri_tooltip

MIT/Apache

22KB
518 lines

tiny_bail

Crates.io Docs License

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

  • Compared to unwrap: Bail will return, continue, or break 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 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;
    }
}

License

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

Dependencies

~350–500KB