简体   繁体   English

为什么Rust编译器不能使用From特性将库错误转换为我的错误?

[英]Why does the Rust compiler not convert from a library error to my error using the From trait?

Given this crate referencing an error from another crate, I would normally write a From implementation to convert types. 给定这个板条箱引用了另一个板条箱的错误,我通常会写一个From实现来转换类型。

use xladd::variant::{Variant, XLAddError};
use failure::Fail;
use std::convert::TryInto;
use std::convert::From;
use std::error::Error;

#[derive(Debug, Fail)]
enum AARCError {
    #[fail(display = "F64 Conversion failure")]
    ExcelF64ConversionError,
    #[fail(display = "Bool Conversion failure")]
    ExcelBoolConversionError,
    #[fail(display = "Conversion failure")]
    ExcelStrConversionError,
}

impl From<XLAddError> for AARCError {
    fn from(err: XLAddError) -> Self {
        AARCError::ExcelF64ConversionError // Test for now
    }
}

pub fn normalize(array: Variant, min: Variant, max: Variant, scale: Variant) -> Result<Variant, AARCError> {
    let min: f64 = min.try_into().map_err(|e| AARCError::from(e))?;
    Ok(Variant::from_str("foo"))
}

But in this case I get an error: 但是在这种情况下,我得到一个错误:

error[E0277]: the trait bound `basic_stats::AARCError: std::convert::From<!>` is not satisfied
  --> src\basic_stats.rs:24:48
   |
24 |     let min: f64 = min.try_into().map_err(|e| AARCError::from(e))?;
   |                                               ^^^^^^^^^^^^^^^ the trait `std::convert::From<!>` is not implemented for `basic_stats::AARCError`
   |
   = help: the following implementations were found:
             <basic_stats::AARCError as std::convert::From<xladd::variant::XLAddError>>
   = note: required by `std::convert::From::from`

I don't understand what the From<!> trait is and trying to implement something like that gives an error for unnamed types. 我不明白From<!>特质是什么,并且试图实现类似这样的操作会给未命名的类型带来错误。

What should I be doing to enable Rust to convert the external crate's errors to my ones? 我应该怎么做才能使Rust将外部包装箱的错误转换为我的错误?

I don't understand what the From<!> trait is and trying to implement something like that gives an error for unnamed types. 我不明白From<!>特质是什么,并且试图实现类似这样的操作会给未命名的类型带来错误。

! is the "never" or uninhabited type; “从不”无人居住的类型; the type that has no possible values. 没有可能值的类型。

If a Result has ! 如果Result! for its error type, that means the operation cannot fail. 对于其错误类型,这意味着操作不会失败。 It is impossible to convert from it to some other error type, because an error value cannot exist in the first place. 无法将其转换为其他错误类型,因为错误值不能一开始就存在。

The never type is currently an experimental feature, requiring a nightly build of Rust. Never类型目前是一项实验功能,需要每晚构建Rust。 As such, it likely has a few rough edges, and it isn't as ergonomic as it could be. 因此,它可能具有一些粗糙的边缘,并且没有达到人体工程学。 For example, I would expect the final feature to provide a blanket From<T> implementation for all types that implement TryFrom<T> with associated type Error = ! 例如,我希望最终功能为所有实现了TryFrom<T>且关联类型为Error = !类型提供一揽子From<T>实现Error = ! . It should be made easy to not have to handle the error that can't happen. 不必处理不可能发生的错误应该变得很容易。

To fix your immediate problem, you can map that error to unreachable!() . 要解决当前的问题,您可以将该错误映射到unreachable!() The only issue with that approach is forwards-compatibility - if the third party crate later introduces a reachable error then your code would have an unhandled error, and no compile-time error to protect you. 这种方法的唯一问题是转发兼容性-如果第三方包装箱后来引入了可到达的错误,则您的代码将有未处理的错误,并且没有编译时错误可以保护您。 That's probably a part of why ! 这可能就是原因之一! is not yet stabilised. 尚未稳定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Rust 在使用“?”时会在错误类型之间隐式转换? 但不是返回值? - Why does Rust implicitly convert between Error types when using '?' but not in return value? 为什么? 运算符报错误“特征绑定NoneError:错误不满足”? - Why does the ? operator report the error "the trait bound NoneError: Error is not satisfied"? 如何为自定义错误类型实现From trait? - How to implement From trait for custom error types? 为什么这些错误不会自动转换为我的自定义错误,即使它们同时实现了 std::error::Error 特征? - Why these errors are not automagically converted to my custom error even if they are implementing both std::error::Error trait? Rust:从 stdin 读取和映射行并处理不同的错误类型 - Rust: Read and map lines from stdin and handling different error types 为什么我的Scala未来无法传播错误? - Why does my Scala future not propagate an Error? 为什么错误处理程序不处理我的错误? - Why the error handler does not handle my errors? 如何在 rust 中使用 if else 方法出错? - How to get error using if else method in rust? 在 Rust 中使用 and_then 有条件地处理特定错误 - Conditionally Acting on Specific Error Using and_then in Rust 除了已加载的C库中的Python错误外 - Except an error in Python from a loaded C library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM