Skip to content

Partialeq#206

Closed
AlexandruCihodaru wants to merge 4 commits into
rust-vmm:mainfrom
AlexandruCihodaru:partialeq
Closed

Partialeq#206
AlexandruCihodaru wants to merge 4 commits into
rust-vmm:mainfrom
AlexandruCihodaru:partialeq

Conversation

@AlexandruCihodaru
Copy link
Copy Markdown

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.

Alexandru Cihodaru added 4 commits October 10, 2022 10:03
Signed-off-by: Alexandru Cihodaru <alexandru.ciprian.cihodaru@intel.com>
Implement PartialEq for Error enum and update tests.

Signed-off-by: Alexandru Cihodaru <alexandru.ciprian.cihodaru@intel.com>
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 <alexandru.ciprian.cihodaru@intel.com>
Derive PartialEq and update tests to check for equality on error
cases.

Signed-off-by: Alexandru Cihodaru <alexandru.ciprian.cihodaru@intel.com>
Comment thread src/guest_memory.rs
},
) => 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
Copy Markdown
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.

Comment thread src/guest_memory.rs

impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
Copy link
Copy Markdown
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 😆

@bonzini
Copy link
Copy Markdown
Member

bonzini commented Aug 8, 2025

Given the issues that @andreeaflorescu mentioned, I'd rather add an assert_matches! macro and use it to improve the tests.

@roypat
Copy link
Copy Markdown
Member

roypat commented Aug 8, 2025

Given the issues that @andreeaflorescu mentioned, I'd rather add an assert_matches! macro and use it to improve the tests.

Agreed.

We've actually replaced all the assert_eq!(error.to_string(), "string representation of error") things with assert!(matches!()) over the last few months. And we actually already have a dev-dependency on the matches crate, which provides an assert_matches! macro (and we even use it in a few places), soo.... #336.

As for all the assertions on .is_err()/is_ok(), I suspect a lot of them we dont really care about matching the actual error, and just replacing it with .unwrap()/.unwrap_err() is fine (and produces significantly better error messages in case of failure). There's a clippy lint against these as well (clippy::assertion_on_result_states) - I'll open an issue for converting these, in case someone is really bored one day (Edit: #337)

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

4 participants