-
Notifications
You must be signed in to change notification settings - Fork 92
Add PartialEq, Eq and Hash to DeviceInfo
#154
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
Open
alvaro-cuesta
wants to merge
1
commit into
ruabmbua:main
Choose a base branch
from
alvaro-cuesta:add-partialeq
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I am not sure if its a good idea to implement Eq operator directly for this struct.
Out of interest, what is you usecase for it?
Uh oh!
There was an error while loading. Please reload this page.
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.
You mean
Eqin particular or bothPartialEq+Eq?For
Eqin particular I think the equality relation is total so you're supposed to implementEq, right? So just semantics, no particular use case.For rationale on
PartialEqsee comment in PR description. I'm already using it (andHashthroughHashSet) to track devices over time.Is there any other way to uniquely identify a device? Am I supposed to just compare all relevant fields individually?
Uh oh!
There was an error while loading. Please reload this page.
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.
Actually I think I'm using strict
Eqtoo throughHashSet::difference(bound on itsimpl Iterator).Uh oh!
There was an error while loading. Please reload this page.
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.
My concern with implementing Eq (as an operator overload) is, that it is unclear what device equality means. If we implement it for the DeviceInfo struct, it just means that one instance is equal in terms of data representation to the
I fear people might misinterpret that as a way to check if two devices are the same, but due to differences in the platforms it might not quite reflect reality. Even if you check all the platform implementations now and see no problem, you have to take newer versions into account. Can you guarantee that all new fields we may want to add in the future will not break this assumption?
A much better way to do this would be an explicit method with documentation, that can be used to identify if two devices are the same. But even that is kind of hard to pull off, since two different applications might have different expectations about device equality.
USB is a bit of a pain in that regard in general, with devices having cloned serial numbers, certain hubs screwing up usb descriptors ....
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.
Maybe we want multiple methods. One of them for the more general case of trying to providing equality between actual physical devices. (e.g. detecting that the same keyboard was plugged into a different port, but you can still tell its the same).
Another that just looks at the topology / ports etc...
Uh oh!
There was an error while loading. Please reload this page.
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.
Great insights!
I didn't consider the "physical device" perspective -- I was thinking in hidapi
hid_device_infoterms, in particular because I've e.g. learnt not to trust S/N (it's""for my particular device)...The problem with having explicit methods instead of implementing traits is the consumer would still have to newtype if they want to use the struct for e.g.
HashSet, but I see your point.Just thinking out loud here: a solution that won't force consumer to newtype around the desired method might be to split some of the fields into a inner physical-device-related struct, so that consumer could use that one for
Partial/Eq/Hash, or the wholeDeviceInfo, as desired. That'd be a breaking change though, and I'm not sure it addresses your future-proofing concerns.Would you still accept if I rework this PR only for
WcharStringandBusType, so I can easily newtypeDeviceInfofor now? I think those should be safe, right?Uh oh!
There was an error while loading. Please reload this page.
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.
For reference: libusb/hidapi#291 implemented stable paths in hidapi libusb backend but signal11/hidapi#405 doesn't seem like it's been addressed in libusb/hidapi (and I guess just depends on whatever the underlying
hidrawdriver does). No idea about other backends' guarantees (I can only confirm Windows keep the path stable for device+USB port across reconnections).This means equality in
DeviceInfowould only make semantic sense if it is meant to have strictly the same meaning ashid_device_infoinhidapi, i.e. identify a particular HID path+device (as in "connected device" this is whatDeviceInfois, am I right?), but cannot tell anything about physical devices or even reconnections of the same device on same USB port (would be nice to document this non-guarantee regardless of outcome).DeviceInfoequality could still be useful for e.g. tracking this "connected device" across time. I think this would be needed for Windows hotplug which only reports that there are new devices, but not which (at least withWM_DEVICECHANGE, unsure aboutCM_Register_Notification), also might be useful for 1+2 here so you can rule out the duplicate. Whether that warrants implementing the traits is debatable.