【发布时间】:2018-05-13 22:59:08
【问题描述】:
给定以下代码:
extern crate dotenv; // v0.11.0
#[macro_use]
extern crate failure; // v0.1.1
#[derive(Debug, Fail)]
#[fail(display = "oh no")]
pub struct Error(dotenv::Error);
由于error-chain(v0.11,最新版本)不能保证它的包装错误是Sync(open PR to fix the issue),我收到一条错误消息,指出Sync 没有为@987654326 实现@:
error[E0277]: the trait bound `std::error::Error + std::marker::Send + 'static: std::marker::Sync` is not satisfied
--> src/main.rs:5:17
|
5 | #[derive(Debug, Fail)]
| ^^^^ `std::error::Error + std::marker::Send + 'static` cannot be shared between threads safely
|
= help: the trait `std::marker::Sync` is not implemented for `std::error::Error + std::marker::Send + 'static`
= note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<std::error::Error + std::marker::Send + 'static>`
= note: required because it appears within the type `std::boxed::Box<std::error::Error + std::marker::Send + 'static>`
= note: required because it appears within the type `std::option::Option<std::boxed::Box<std::error::Error + std::marker::Send + 'static>>`
= note: required because it appears within the type `error_chain::State`
= note: required because it appears within the type `dotenv::Error`
= note: required because it appears within the type `Error`
为了让这些类型能够很好地协同工作,我能做的最少的样板/工作是多少?我能想到的最短的事情是添加一个ErrorWrapper newtype,这样我就可以在Arc<Mutex<ERROR_CHAIN_TYPE>> 上实现Error:
use std::fmt::{self, Debug, Display, Formatter};
use std::sync::{Arc, Mutex};
#[derive(Debug, Fail)]
#[fail(display = "oh no")]
pub struct Error(ErrorWrapper<dotenv::Error>);
#[derive(Debug)]
struct ErrorWrapper<T>(Arc<Mutex<T>>)
where
T: Debug;
impl<T> Display for ErrorWrapper<T>
where
T: Debug,
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "oh no!")
}
}
impl<T> ::std::error::Error for ErrorWrapper<T>
where
T: Debug,
{
fn description(&self) -> &str {
"whoops"
}
}
// ... plus a bit more for each `From<T>` that needs to be converted into
// `ErrorWrapper`
这是正确的方法吗?对于不需要它的东西,有没有什么东西可以减少代码/运行时成本来转换为Arc<Mutex<T>>?
【问题讨论】:
标签: error-handling rust