我是anywhere的新手,我想知道是否有办法将anywhere错误转换为std错误
代码
fn m() {
let a: anyhow::Result<String> = Ok("".into());
n(a);
}
fn n(r: std::result::Result<String, impl std::error::Error>) {
println!("{:?}", r);
}
错误消息
error[E0277]: the trait bound `anyhow::Error: std::error::Error` is not satisfied
--> srcmain.rs:82:7
|
82 | n(a);
| - ^ the trait `std::error::Error` is not implemented for `anyhow::Error`
| |
| required by a bound introduced by this call
使用Deref
或AsRef
impl:
match a {
Ok(v) => n(Ok::<_, &dyn std::error::Error>(v)),
Err(e) => n(Err::<_, &dyn std::error::Error>(e.as_ref())),
}
// Or
match a {
Ok(v) => n(Ok::<_, &dyn std::error::Error>(v)),
Err(e) => n(Err::<_, &dyn std::error::Error>(&*e)),
}