Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

AlexandruCihodaru
Copy link

Summary of the PR

Implement PartialEq for Error enums.

Fixes: #194

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR are signed (with git commit -s), and the commit
    message has max 60 characters for the summary and max 75 characters for each
    description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • Any newly added unsafe code is properly documented.

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]>
},
) => 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
Copy link
Member

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) {
Copy link
Member

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 😆

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

Successfully merging this pull request may close these issues.

Implement PartialEq for errors in vm-memory
2 participants