#try #ok #wrap #catch #throw

no-std tryvial

Small crate for ok-wrapping and try blocks

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

#1404 in Rust patterns

Download history 175/week @ 2024-11-16 361/week @ 2024-11-23 52/week @ 2024-11-30 298/week @ 2024-12-07 132/week @ 2024-12-14 1/week @ 2024-12-21 103/week @ 2024-12-28 53/week @ 2025-01-04 97/week @ 2025-01-11 204/week @ 2025-01-18 183/week @ 2025-01-25 271/week @ 2025-02-01 510/week @ 2025-02-08 497/week @ 2025-02-15 427/week @ 2025-02-22 514/week @ 2025-03-01

1,964 downloads per month

MIT/Apache

8KB
62 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

~41KB