How to implement Error::cause properly?

How to implement Error::cause properly?

我在实现 Error 特性时遇到问题。我想包装来自 Diesel 或其他数据库驱动程序的错误。我什至没有接近实现 From,因为我已经无法实现 Error。导致代码无法编译的行是代码块末尾的那一行。

use std::fmt;
use std::error::{self, Error};

#[derive(Debug)]
pub enum MyError {
    NotFound(String),
    PersistenceError(Box<Error + Send + Sync>),
}

pub type MyResult<T> = Result<T, MyError>;

impl fmt::Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            MyError::NotFound(ref msg) => write!(f, "Not found: {}", msg),
            MyError::PersistenceError(ref cause) => write!(f, "Persistence error: {}", cause),
        }
    }
}

impl Error for MyError {
    fn description(&self) -> &str {
        match *self {
            MyError::NotFound(ref msg) => msg,
            MyError::PersistenceError(ref cause) => cause.description(),
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            MyError::NotFound(_) => None,
            // `*cause` does not live long enough
            MyError::PersistenceError(cause) => Some(&*cause),
        }
    }
}

我也试过:

*cause does not live long enough

MyError::PersistenceError(cause) => Some(&*cause),

the trait core::marker::Sized is not implemented for the type std::error::Error + Send + Sync + 'static [E0277]

MyError::PersistenceError(ref cause) => Some(cause),

the trait std::error::Error is not implemented for the type `&Box

MyError::PersistenceError(ref cause) => Some(&cause)

但其中 none 有效。

在这种情况下 print the type of variables 很有用:

match *self {
    MyError::NotFound(_) => None,
    MyError::PersistenceError(ref cause) => {
        let () = cause;
    },
}

这会告诉你 cause&Box<std::error::Error + Send + Sync>

如果我们取消引用一次,我们将得到一个 Box<std::error::Error + Send + Sync>,如果我们第二次取消引用它,我们将得到一个 std::error::Error + Send + Sync(这不是真正的类型)。然后我们可以采用另一个引用,它可以隐式地变成 &Error:

match *self {
    MyError::NotFound(_) => None,
    MyError::PersistenceError(ref cause) => Some(&**cause),
}