Skip to content

benfrankel/tiny_bail

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Tiny macros for bailing on failure

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-Apache-2.0
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages