Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Implement Eq on ErrorKind? #95

Closed
sourcefrog opened this issue Dec 11, 2016 · 7 comments
Closed

Implement Eq on ErrorKind? #95

sourcefrog opened this issue Dec 11, 2016 · 7 comments

Comments

@sourcefrog
Copy link
Contributor

I might be holding this wrong, but I'd like to write eg

assert_eq!(*af.last_band_id().unwrap_err().kind(), ErrorKind::ArchiveEmpty);

this complains

error[E0369]: binary operation `==` cannot be applied to type `errors::ErrorKind

Is that feasible to add? Or could we add to the documentation some example of the intended way to write this?

I see I can use a destructuring match but that seems a little unnatural in a test.

@colin-kiegel
Copy link

@Yamakaky
Copy link
Contributor

That would be a good idea, but that's not practical. io::Error, a very common error type, doesn't implement Eq, so auto-deriving would generate a compile time error which is not fixable by the end-user.
Since ErrorKind is generated, you can always manually implement Eq. Not ideal, but it works.

@sourcefrog
Copy link
Contributor Author

Is there a particular recommended way to check the .kind() or should I use match?

@Yamakaky
Copy link
Contributor

I think a match over ErrorKind is fine. If you also want to implement Eq on Error, you may want to pay attention to the backtrace and the error chain.

@sourcefrog
Copy link
Contributor Author

ok thanks

@Yamakaky
Copy link
Contributor

With https://github.com/Arnavion/derive-error-chain/ we may be able to derive Eq if possible.

@mjc-gh
Copy link

mjc-gh commented Dec 16, 2016

I wrote a small macro to assert against the error types that are returned by my library. Not sure if this will be useful to anyone else but I figured I would share 😃

// usage:
//    assert_error_kind!(some_err, ErrorKind::MyErrorType)
macro_rules! assert_error_kind {
    ($err:expr, $kind:pat) => (match *$err.kind() {
        $kind => assert!(true, "{:?} is of kind {:?}", $err, stringify!($kind)),
        _     => assert!(false, "{:?} is NOT of kind {:?}", $err, stringify!($kind))
    });
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants