I’m writing a library and the first thing I wanted to do is test it by sending it some malformed input and checking to see if it return the expected error condition.
I was then surprised to find that I can’t compare Errors (or even ErrorKinds) generated by the (defacto-standard) error-chain crate. So I inquired about the standard library and understand that not all Errors implement PartialEq either.
Prevailing wisdom is to use pattern-matching to determine Error specifics, but that is more verbose (or requires a new dependency in the form of the assert_matches crate). Why not just implement PartialEq and be done?
This is summarized well in this comment: https://github.com/rust-lang/rust/issues/34158#issuecomment-224910299.
@lambda goes on to actually implement PartialEq for io:Error, but @alexcrichton wrote that the libs team, after discussion, decided not to merge this: https://github.com/rust-lang/rust/pull/34192#issuecomment-227477665.
@alexcrichton cites reasons that include:
a) "It’s an unfortunate ergonomic loss that io::Result instances can’t be equated, but indicating that io::Error has an implementation of PartialEq is somewhat of a lie because the only type of error that can be equal is two OS errors"
b) that @brson stated “pointed out “equals” in this sense is very loosely defined because the actual error that happened could maybe be entirely different.”
It is indeed that ergonomic loss that motivates this post today.
With my (admittedly newb-by-comparison) understanding of this, my thoughts on the above rationales are:
a) then shouldn’t the other types should compare for equality as false? (I am probably misunderstanding, but don’t see this as a rationale for not implementing PartialEq)
b) not sure I understand this: if this is referring to the same error type with a different .cause(), I would not expect the two types to compare as equal (same as two containers of a given type holding different values wouldn’t be considered equal).
Again, I’m sure I’m misunderstanding Alex and Brian. So with that in mind, can some kind soul help me understand why implementing PartialEq for Rust errors in general (or specifically for std::io::Error) is not a good idea?
Thanks in advance,