#io #error-handling #value #option-t #str

oops

轻量级错误处理。提供 Option<T> -> std::io::Result<T>

2 个不稳定版本

0.2.0 2021 年 1 月 3 日
0.1.0 2020 年 2 月 15 日

#2727Rust 模式

Download history 25/week @ 2024-03-18 30/week @ 2024-03-25 98/week @ 2024-04-01 19/week @ 2024-04-08 26/week @ 2024-04-15 27/week @ 2024-04-22 19/week @ 2024-04-29 13/week @ 2024-05-06 30/week @ 2024-05-13 34/week @ 2024-05-20 18/week @ 2024-05-27 19/week @ 2024-06-03 14/week @ 2024-06-10 31/week @ 2024-06-17 18/week @ 2024-06-24 1/week @ 2024-07-01

64 次每月下载
用于 5 crates

MIT/Apache

6KB
90

将值转换为 std::io::Result 的轻量级错误处理。提供

  • Option<T>::oops(self, &str) -> std::io::Result<T>
  • Result<T, E>::oops(self, &str) -> std::io::Result<T>
  • Option<T>::lazy_oops(self, FnOnce() ->String) -> std::io::Result<T>
  • Resukt<T, E>::lazy_oops(self, FnOnce() ->String) -> std::io::Result<T>

示例

use std::io::Result;

fn third_element(slice: &[usize]) -> Result<&usize> {
    // Using oops to add context to a None
    slice.iter().nth(3).oops("No third element")
}

fn parse_batch(slice: &[&str]) -> Result<Vec<usize>> {
    slice
        .iter()
        .map(|v| {
            v
                .parse::<usize>()

                // Using lazy_oops to add debug messages
                .lazy_oops(|| format!("Failed to parse {} from {:?}", v, slice))
        })
        .collect()
}

assert_eq!(
    // No third element
    third_element(&[1, 2, 3]).err().unwrap().kind(),
    std::io::ErrorKind::Other
);

assert_eq!(
    // Failed to parse lo from ["2", "3", "7", "lo", "11"]
    parse_batch(&["2", "3", "7", "lo", "11"]).err().unwrap().kind(),
    std::io::ErrorKind::Other
);

无运行时依赖