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

Remove PartialOrd and Ord implementations from WindowId, DeviceId, MonitorHandle and VideoModeHandle #3866

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

daxpedda
Copy link
Member

@daxpedda daxpedda commented Aug 14, 2024

As discussed in the last meeting, this PR removes PartialOrd/Ord from:

  • WindowId
  • DeviceId
  • FingerId
  • MonitorHandle
  • VideoModeHandle

The reason being that Ord is supposed to tell users something meaningful, which it doesn't in this case. E.g. what does comparing the order of two DeviceIds mean?

On VideoModeHandle there was actually some useful implementation, but it had a couple of issues:

  • The significance to a user was unclear because it wasn't documented in what order we are ordering them. The alternative to this PR is to just document this behavior.
  • Significant overhead because VideoModeHandle is a handle and doesn't contain the data readily available (on most backends). We might want to consider implementing Ord again on VideoModeData.
  • Unclear benefits; users can sort them based on their specific needs themselves.

FingerId would have been useful to compare with Ord if the ID actually signifies the finger used in order, in which case we should expose that information directly to the user as well. It isn't clear yet if every backend functions this way, something to ponder about when we overhaul touch events.

Removes `PartialOrd` and `Ord` from:
- `WindowId`
- `DeviceId`
- `FingerId`
- `MonitorHandle`
- `VideoModeHandle`
@daxpedda daxpedda added the S - api Design and usability label Aug 14, 2024
@MarijnS95
Copy link
Member

I ran into this in the NDK crate as well, PR rust-mobile/ndk#483 (reviews welcome!). We have some pointer wrappers (and a few random non-sortable enums) that derive Ord while others do not. After planning on removing them - because they likewise do not have any semantical meaning - this does break the ability to store these pointers inside structures like BTreeMap.

Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

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

I believe that there is value in PartialOrd and Ord for all of these types! Even though the ordering may not have semantic meaning, it is still stable and correct (if we're comparing e.g. pointer addresses), and as said, allows using these types in BTreeMap/BTreeSet.

Comment on lines -35 to -43
self.monitor().cmp(&other.monitor()).then(
self.size()
.cmp(&other.size())
.then(
self.refresh_rate_millihertz()
.cmp(&other.refresh_rate_millihertz())
.then(self.bit_depth().cmp(&other.bit_depth())),
)
.reverse(),
Copy link
Member

Choose a reason for hiding this comment

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

I agree that VideoModeHandle's Ord is weird (and likely incorrect, I could imagine two video modes that have the same monitor, size, refresh rate and bit depth, but differed on some other parameter, in which case Ord would say they were equal, but Eq would say they weren't).

@daxpedda
Copy link
Member Author

daxpedda commented Aug 14, 2024

I believe that there is value in PartialOrd and Ord for all of these types! Even though the ordering may not have semantic meaning, it is still stable and correct (if we're comparing e.g. pointer addresses), and as said, allows using these types in BTreeMap/BTreeSet.

The argument is that without semantic meaning its not a good idea to implement Ord because it exposes API to the user with no apparent meaning.

But yes, the implementation is stable and correct, but I don't know what the point is of being able to store it in BTreeMap/BTreeSet if the order has no meaning.

I believe that whatever consensus we come here to, we should be able to formulate a rule that applies to our whole API, if we say that implementing PartialOrd/Ord without semantic meaning is desirable, then we should do so for all other types, if possible, as well.

Personally, my view until now was to only implement PartialOrd/Ord if it actually makes any sense, otherwise the API should be kept opaque unless otherwise desirable.

Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

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

Thinking it over a bit more, I took a look at the APIs in std, and it seems like a fair number of them also don't implement PartialOrd and Ord, including BTreeMap itself.

I guess I was thinking of the C-COMMON-TRAITS API guideline, and wanted to follow that, but even that says to implement all the applicable traits.

what the point is of being able to store it in BTreeMap/BTreeSet if the order has no meaning.

Hmm, yeah, I thought it would make the order consistent between e.g. re-runs of the same code, but that clearly can't happen if the ordering comes from a pointer.

src/platform_impl/windows/monitor.rs Outdated Show resolved Hide resolved
Comment on lines 226 to 229
// EnumDisplaySettingsExW can return duplicate values (or some of the
// fields are probably changing, but we aren't looking at those fields
// anyway), so we're using a BTreeSet deduplicate
let mut modes = BTreeSet::<RootVideoModeHandle>::new();
let mut modes = Vec::new();
Copy link
Member

Choose a reason for hiding this comment

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

I think we may still need to deduplicate here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, good catch!

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, though I believe that we should actually not de-duplicate here but instead expose the underlying type with an extension specific trait; a matter for another time.

Co-Authored-By: Mads Marquart <[email protected]>
Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

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

Thanks!

@MarijnS95
Copy link
Member

Thinking it over a bit more, I took a look at the APIs in std, and it seems like a fair number of them also don't implement PartialOrd and Ord, including BTreeMap itself.

Vec neither, likely because its pointer can (also) change when pushing new items causing a realloc().

I guess I was thinking of the C-COMMON-TRAITS API guideline, and wanted to follow that, but even that says to implement all the applicable traits.

Wish applicable was clarified a bit more explicitly here.

what the point is of being able to store it in BTreeMap/BTreeSet if the order has no meaning.

Hmm, yeah, I thought it would make the order consistent between e.g. re-runs of the same code, but that clearly can't happen if the ordering comes from a pointer.

The only thing I could think of is different implementation speeds, but HashMap is always faster than BTreeMap (except amortization): https://doc.rust-lang.org/std/collections/index.html#maps.

@madsmtm
Copy link
Member

madsmtm commented Aug 22, 2024

Thinking it over a bit more, I took a look at the APIs in std, and it seems like a fair number of them also don't implement PartialOrd and Ord, including BTreeMap itself.

Vec neither, likely because its pointer can (also) change when pushing new items causing a realloc().

Vec does implement PartialOrd.

@MarijnS95
Copy link
Member

Vec does implement PartialOrd.

Right, and that piggybacks off of the lexicographical ordering of the underlying slices. Not of the pointer that could change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S - api Design and usability
Development

Successfully merging this pull request may close these issues.

4 participants