【发布时间】:2019-09-15 09:43:44
【问题描述】:
我目前正在尝试为我的 CLI 应用程序编写自定义错误类型。现在我想编写一个From trait 的实现,这样我的自定义错误类型就可以包含所有可能发生的第三方库错误。
错误枚举:
#[derive(Debug)] // Allow the use of "{:?}" format specifier
pub enum CustomError {
Git(git2::Error),
Other
}
现在我想为git2 库中的git2::Error 实现From 特征,以便在我的函数中使用? 运算符。
impl From<(git2::Error)> for CustomError {
fn from(cause: git2::Error) -> Self {
CustomError::Git(cause)
}
}
但是当我尝试使用我的自定义错误来映射这样的错误时:
let repo = Repository::open(path).map_err(|err| CustomError::Git)?;
我收到以下错误消息:
the trait `std::convert::From<fn(git2::error::Error) -> error::CustomError {error::CustomError::Git}>` is not implemented for `error::CustomError `
谁能帮我理解为什么会出现这个错误以及如何解决这个问题?
感谢任何帮助
【问题讨论】:
标签: error-handling rust