Crate orfail

source ·
Expand description

An error handling library for portable unrecoverable errors.

This crate provides,

Examples

use orfail::{OrFail, Result};

fn check_non_zero(n: isize) -> Result<()> {
    (n != 0).or_fail()?;
    Ok(())
}

fn safe_div(x: isize, y: isize) -> Result<isize> {
    check_non_zero(y).or_fail()?;
    Ok(x / y)
}

// OK
assert_eq!(safe_div(4, 2), Ok(2));

// NG
assert_eq!(safe_div(4, 0).err().map(|e| e.to_string()),
           Some(
r#"expected `true` but got `false`
  at src/lib.rs:8
  at src/lib.rs:13
"#.to_owned()));

Structs

Traits

  • This trait allows for converting a value into Result<_, Failure>.

Type Definitions