| Crates.io | tryvial |
| lib.rs | tryvial |
| version | 0.2.0 |
| created_at | 2022-10-10 20:50:12.270391+00 |
| updated_at | 2022-10-18 08:10:37.386493+00 |
| description | Small crate for ok-wrapping and try blocks |
| homepage | |
| repository | https://github.com/JoJoJet/tryvial |
| max_upload_size | |
| id | 684885 |
| size | 8,463 |
tryvialA 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.
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));
MIT or Apache-2.0