包裹错误
把错误装箱这种做法也可以改成把它包裹到你自己的错误类型中。
use std::error;
use std::num::ParseIntError;
use std::fmt;
type Result<T> = std::result::Result<T, DoubleError>;
#[derive(Debug)]
enum DoubleError {
EmptyVec,
// 在这个错误类型中,我们采用 `parse` 的错误类型中 `Err` 部分的实现。
// 若想提供更多信息,则该类型中还需要加入更多数据。
Parse(ParseIntError),
}
impl fmt::Display for DoubleError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
DoubleError::EmptyVec =>
write!(f, "please use a vector with at least one element"),
// 这是一个封装(wrapper),它采用内部各类型对 `fmt` 的实现。
DoubleError::Parse(ref e) => e.fmt(f),
}
}
}
impl error::Error for DoubleError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
DoubleError::EmptyVec => None,
// 原因采取内部对错误类型的实现。它隐式地转换成了 trait 对象 `&error:Error`。
// 这可以工作,因为内部的类型已经实现了 `Error` trait。
DoubleError::Parse(ref e) => Some(e),
}
}
}
// 实现从 `ParseIntError` 到 `DoubleError` 的转换。
// 在使用 `?` 时,或者一个 `ParseIntError` 需要转换成 `DoubleError` 时,它会被自动调用。
impl From<ParseIntError> for DoubleError {
fn from(err: ParseIntError) -> DoubleError {
DoubleError::Parse(err)
}
}
fn double_first(vec: Vec<&str>) -> Result<i32> {
let first = vec.first().ok_or(DoubleError::EmptyVec)?;
let parsed = first.parse::<i32>()?;
Ok(2 * parsed)
}
fn print(result: Result<i32>) {
match result {
Ok(n) => println!("The first doubled is {}", n),
Err(e) => println!("Error: {}", e),
}
}
fn main() {
let numbers = vec!["42", "93", "18"];
let empty = vec![];
let strings = vec!["tofu", "93", "18"];
print(double_first(numbers));
print(double_first(empty));
print(double_first(strings));
}
这种做法会在错误处理中增加一些模板化的代码,而且也不是所有的应用都需要这样做。一些 库可以帮你处理模板化代码的问题。
See also:
From::from
and 枚举类型