2 unstable releases

0.2.0 Oct 18, 2022
0.1.2 Oct 11, 2022
0.1.1 Oct 11, 2022
0.1.0 Oct 10, 2022

#8 in #try-block

Download history 436/week @ 2025-07-28 471/week @ 2025-08-04 438/week @ 2025-08-11 273/week @ 2025-08-18 288/week @ 2025-08-25 286/week @ 2025-09-01 200/week @ 2025-09-08 408/week @ 2025-09-15 325/week @ 2025-09-22 435/week @ 2025-09-29 255/week @ 2025-10-06 335/week @ 2025-10-13 338/week @ 2025-10-20 461/week @ 2025-10-27 344/week @ 2025-11-03 279/week @ 2025-11-10

1,426 downloads per month
Used in tryvial

MIT/Apache

4KB
50 lines

tryvial

A small crate for Ok-wrapping and try blocks. This is compatible with Result, Option, and any type implementing the unstable std::ops::Try trait.

This crate does not require nightly Rust.

Overview

The macro try_fn is used to perform Ok-wrapping on the return value of a function.

Before:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("Enter your name: ");
    let mut name = String::new();
    std::io::stdin().read_line(&mut name)?;
    println!("Hello, {name}!");
    Ok(()) // this is ugly
}

After:

#[try_fn]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("Enter your name: ");
    let mut name = String::new();
    std::io::stdin().read_line(&mut name)?;
    println!("Hello, {name}!");
}

The macro try_block is an implementation of "try blocks" from nightly rust.

let result: Result<T, E> = try_block! {
   let a = do_one(x)?;
   let b = do_two(a)?;
   b
};

The macro wrap_ok simply wraps an expression with the "ok" variant for a given Try type.

assert_eq!(Some(42), wrap_ok!(42));

License

MIT or Apache-2.0

Dependencies

~270KB