-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Partialeq #206
base: main
Are you sure you want to change the base?
Partialeq #206
Conversation
b576045
to
e5f3851
Compare
Signed-off-by: Alexandru Cihodaru <[email protected]>
Implement PartialEq for Error enum and update tests. Signed-off-by: Alexandru Cihodaru <[email protected]>
Implemented PartialEq for Error enum. Considering that from this file the only io error returned are OS error usage of raw_os_error offers anything that is needed. Updated tests to use PartialEq for error cases. Signed-off-by: Alexandru Cihodaru <[email protected]>
Derive PartialEq and update tests to check for equality on error cases. Signed-off-by: Alexandru Cihodaru <[email protected]>
e5f3851
to
0ac2b35
Compare
}, | ||
) => left_expected == right_expected && left_completed == right_completed, | ||
(Error::IOError(left), Error::IOError(right)) => { | ||
// error.kind should be enough to assert equallity because each error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this is true, it does not mean that kind
can be used to compare errors. The reason why io::Error does not have PartialEq implemented is because it cannot be implemented truthfully. The first example that comes to mind is NotFound
. A not found error thrown for file1
is not equal to an error thrown for file2
. This is one of the reasons why we cannot actually compare the io::Error
like that because it has other metadata that is specific to the error type. We can say instead that we are willing to accept the loss of information in comparing errors because vm-memory errors are widely used in other crates as well and it is very limiting to not have PartialEq
implemented.
@@ -73,6 +73,33 @@ pub enum Error { | |||
HostAddressNotAvailable, | |||
} | |||
|
|||
impl PartialEq for Error { | |||
fn eq(&self, other: &Self) -> bool { | |||
match (self, other) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One reason I don't particularly like these kinds of implementations for PartialEq is that it will silently fail when you add a new error type if you don't explicitly add it in the below match as well. I would propose we add a test that just fails when a new variant is added and has a comment something like: "If this failed it means you added a new error type. Make sure you also add that to the PartialEq
implementation. Once you've done that feel free to fix this test". This will serve as a warning when adding new types. I am also open to other ideas as this is rather dummy 😆
Summary of the PR
Implement PartialEq for Error enums.
Fixes: #194
Requirements
Before submitting your PR, please make sure you addressed the following
requirements:
git commit -s
), and the commitmessage has max 60 characters for the summary and max 75 characters for each
description line.
test.
unsafe
code is properly documented.