Rust 对于带有`#From`指令的`'std::Error::Error`自动实现`From`失败

Rust 对于带有`#From`指令的`'std::Error::Error`自动实现`From`失败,rust,Rust,我收到了这个错误: error[E0277]: `?` couldn't convert the error to `&ArchiveError` --> src/archive.rs:45:38 | 42 | fn extract<R: Read + Seek>(filename: &'static str) -> Result<(), &ArchiveError> { |

我收到了这个错误:

error[E0277]: `?` couldn't convert the error to `&ArchiveError`
  --> src/archive.rs:45:38
   |
42 | fn extract<R: Read + Seek>(filename: &'static str) -> Result<(), &ArchiveError> {
   |                                                       ------------------------- expected `&ArchiveError` because of this
...
45 |             ArchiveZip::new(filename)?
   |                                      ^ the trait `From<&dyn std::error::Error>` is not implemented for `&ArchiveError`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following implementations were found:
             <ArchiveError as From<&'static (dyn std::error::Error + 'static)>>
   = note: required by `from`
    ArchiveZipError(#[from] &dyn std::error::Error),
    => Missing lifetime specifier [E0106]
删除
'静态
生存期说明符会导致此错误:

error[E0277]: `?` couldn't convert the error to `&ArchiveError`
  --> src/archive.rs:45:38
   |
42 | fn extract<R: Read + Seek>(filename: &'static str) -> Result<(), &ArchiveError> {
   |                                                       ------------------------- expected `&ArchiveError` because of this
...
45 |             ArchiveZip::new(filename)?
   |                                      ^ the trait `From<&dyn std::error::Error>` is not implemented for `&ArchiveError`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following implementations were found:
             <ArchiveError as From<&'static (dyn std::error::Error + 'static)>>
   = note: required by `from`
    ArchiveZipError(#[from] &dyn std::error::Error),
    => Missing lifetime specifier [E0106]
使用此调用函数:

error[E0277]: `?` couldn't convert the error to `&ArchiveError`
  --> src/archive.rs:45:38
   |
42 | fn extract<R: Read + Seek>(filename: &'static str) -> Result<(), &ArchiveError> {
   |                                                       ------------------------- expected `&ArchiveError` because of this
...
45 |             ArchiveZip::new(filename)?
   |                                      ^ the trait `From<&dyn std::error::Error>` is not implemented for `&ArchiveError`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following implementations were found:
             <ArchiveError as From<&'static (dyn std::error::Error + 'static)>>
   = note: required by `from`
    ArchiveZipError(#[from] &dyn std::error::Error),
    => Missing lifetime specifier [E0106]
fn提取(文件名:&'static str)->结果{
匹配从文件名(文件名)获取扩展名{
一些(“zip”)=>{
ArchiveZip::新建(文件名)?
.摘录(“foo”)
.或(错误(&ArchiveError::archiveziperor(&ziperor::InvalidArchive(文件名)))
}
无=>错误(&ArchiveError::ArchiveTypeNotDetectedError),
}
}
使用这些功能:

error[E0277]: `?` couldn't convert the error to `&ArchiveError`
  --> src/archive.rs:45:38
   |
42 | fn extract<R: Read + Seek>(filename: &'static str) -> Result<(), &ArchiveError> {
   |                                                       ------------------------- expected `&ArchiveError` because of this
...
45 |             ArchiveZip::new(filename)?
   |                                      ^ the trait `From<&dyn std::error::Error>` is not implemented for `&ArchiveError`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following implementations were found:
             <ArchiveError as From<&'static (dyn std::error::Error + 'static)>>
   = note: required by `from`
    ArchiveZipError(#[from] &dyn std::error::Error),
    => Missing lifetime specifier [E0106]
impl for ArchiveZip结果{
让reader=ArchiveZip::获取文件\读取器(路径)?;
Ok(&ArchiveZip{path,archive:ZipArchive::new(reader)?})
}
fn获取文件读取器(路径:&str)->结果{
确定(&mut文件::打开(路径)?)
}
}
来自指令的
#如何不工作?

使用
而不是
&dyn Error
工作

我也改变了:

archiveziperor(#[from]&dyn std::error::error),
为此:

archiveziperor(#[来自]ziperor),

您的代码是。。。有问题的引用,
&T
,只借用它们引用的值,这意味着其他东西必须拥有它们。不幸的是,很多对象的所有者只是超出范围的函数中的变量/临时变量,这将导致生成的引用立即无效。错误通常拥有它们的数据,因此,如果您用
Box
@user4815162342替换
&'static dyn std::error::error
,您可能会得到更好的结果:如何让
Box
thisrerror::error
自动转换东西的
指令一起工作?(并不是说我得到了它与参考无论如何,你可以看到从我的问题…)。我以前试过对结果类型和错误类型都使用
Box
,但它无法转换。@Neil您能提供一个小的、自给自足的示例来说明什么不适合您吗?我发现很难从你的问题中理解这一点。而
可提取的
特征来自哪里?如果这是您定义的,那么可能这就是问题的根源,因为它的
new
方法指定了极其奇怪的
Result
返回类型。