gpt4 book ai didi

rust - 如何在 Rust 中正确实现 Error::cause?

转载 作者:行者123 更新时间:2023-11-29 07:50:37 25 4
gpt4 key购买 nike

我在实现 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)

但这些都不起作用。

最佳答案

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),
}

关于rust - 如何在 Rust 中正确实现 Error::cause?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36184843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com