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

Add AcknowledgeCheckFailedReason #2862

Merged
merged 6 commits into from
Jan 10, 2025

Conversation

bjoernQ
Copy link
Contributor

@bjoernQ bjoernQ commented Dec 23, 2024

Thank you for your contribution!

We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:

Submission Checklist 📝

  • I have updated existing examples or added new ones (if applicable).
  • I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • My changes were added to the CHANGELOG.md in the proper section.
  • I have added necessary changes to user code to the Migration Guide.
  • My changes are in accordance to the esp-rs API guidelines

Extra:

Pull Request Details 📖

Description

This prepares for a maybe-future breaking change when we want to give the user an indication of an AckCheckFailed reason. For now it's just using Unknown but changing that shouldn't be a breaking change in future

Testing

CI

@bjoernQ bjoernQ force-pushed the prepare-i2c-ackcheckfailedreason branch from 620d33a to 420fdba Compare December 23, 2024 10:57
@bjoernQ
Copy link
Contributor Author

bjoernQ commented Dec 23, 2024

Prepares #2814

/// I2C no acknowledge error reason.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this non_exhaustive, isn't this enum complete? What else is there for the device to not acknowledge?

(Using both Unknown and non_exhaustive seems wrong to me at a glance)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In future we might want to provide more information with the error (e.g. at which byte count the nack was detected) - adding such a variant would be a breaking change then. I imagine user-code will do the same when matching Unknown or _ so I don't think it's a big deal

I agree for now we wouldn't even need Address and Data for now, then

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't such a change be breaking (as we'd add the info to Data)? Or how do you imagine such change to manifest?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we change the variant that would be breaking - but we could add another variant - but I agree we should just remove the currently unused variants for now

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we change the variant that would be breaking - but we could add another variant - but I agree we should just remove the currently unused variants for now

Adding a variant can still be a breaking change, not at compile time but at runtime. Errors are tricky.

If you add a new variant that is a subset of an existing variant, this is a breaking change.

Imagine a user doing something like this. (Based on the documentation this is acceptable to do)

if err == AcknowledgeCheckFailedReason::Unknown {
    // do something because address might not have been acknowledged.
}

If you split Unknown into Unknown and Address, once the user pulls in the new version of esp-hal, this if statement no longer executes where they expected it to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but that is true for pretty much every error-enum we have

checking an error like that is also bad for exhaustive enums

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My code example isn't fantastic but it's easy for code to semantically look like that.

Perhaps I'm being paranoid, I can't think of a nice solution besides completing the enum.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is precedent in rust std to introduce such breaking changes. We can't prevent users from explicitly matching against Unknown but we can document that we don't guarantee that we don't split error variants out of it, and it should be handled as the "nothing else applies" case.

That being said, we should be extra careful with these cases. We should probably add and handle Address and Data right now, and accept that they won't carry additional data, or include an empty struct in them if we know we might have something useful in them later.

Copy link
Contributor

@JurajSadel JurajSadel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jessebraham jessebraham enabled auto-merge January 10, 2025 08:34
@jessebraham jessebraham added this pull request to the merge queue Jan 10, 2025
Merged via the queue into esp-rs:main with commit 69031e6 Jan 10, 2025
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants