Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

Rust Snafu Crate: no method named `fail` found for enum `Error` in the current scope

I'm trying to use the Snafu crate for some basic error handling. In this case, I'm trying to return an Error when check_value() is given anything but a CustomInputValue::CiFloat. Based on what I was seeing in examples on this page from the docs, I thought this would work:

use snafu::{Backtrace, ResultExt, Snafu, ensure};

#[derive(Debug, Snafu)]
pub enum Error{
    #[snafu(display("Incorrect Type: {:?}"), kind)]
    IncorrectInputType{kind: CustomInputValue},
}

#[derive(Debug, Clone, PartialEq)]
pub enum CustomInputValue{
    CiBool(bool),
    CiInt(i32),
    CiFloat(f64),
}

type Result<T, E = Error> = std::result::Result<T, E>;

fn main(){
    check_value(CustomInputValue::CiFloat(10.0));
}

fn check_value(val: CustomInputValue )->Result<()>{
    match val {
         CustomInputValue::CiFloat(inp)=>inp,
         _=>Error::IncorrectInputType{kind: val}.fail()?
    };
    Ok(())
}

However, this produces the error:

error[E0599]: no method named `fail` found for enum `Error` in the current scope
  --> src/main.rs:24:57
   |
4  | pub enum Error{
   | -------------- method `fail` not found for this
...
24 |                 _=>Error::IncorrectInputType{kind: val}.fail()?
   |                                                         ^^^^ method not found in `Error`

What's causing this error? Do I need to implement a fail function? I don't see anywhere in the docs such a custom fail() function being written for Error, and can't find anything bout requiring a fail() function for Error in the docs.

question from:https://stackoverflow.com/questions/66058836/rust-snafu-crate-no-method-named-fail-found-for-enum-error-in-the-current-s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The #[derive(Snafu)] attribute creates "context selectors" for each enum variant. That means that Error::IncorrectInputType refers to the variant, while IncorrectInputType is a generated struct which has the fail() method.

The fix is to use this selector instead of the enum:

match val {
     CustomInputValue::CiFloat(inp) => inp,
     _ => IncorrectInputType { kind: val }.fail()?
       // ^^^^^^^^^^^^^^^^^^ no Error::
};

You can browse the rest of the SNAFU user's guide to know more about the macro.


Also, the kind in the #[snafu(display(...))] attribute is misplaced. It should be a parameter within the display portion:

#[snafu(display("Incorrect Type: {:?}", kind))]
IncorrectInputType { kind: CustomInputValue },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...