Skip to content

Derive Hash for Rect2i #1241

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

Merged
merged 1 commit into from
Jul 22, 2025
Merged

Conversation

ttencate
Copy link
Contributor

Also add Hash, PartialOrd and Ord for some enums while I have my eyes on them.

Related issue: #505. There seems to be no objective to keep Godot and Rust hashes the same, so this change should be fine, and is indeed consistent with prior art of deriving Hash on e.g. Vector2i.

Also add `Hash`, `PartialOrd` and `Ord` for some enums while I have my
eyes on them.

Related issue: godot-rust#505. There seems to be no objective to keep Godot and
Rust hashes the same, so this change should be fine, and is indeed
consistent with prior art of deriving `Hash` on e.g. `Vector2i`.
@GodotRust
Copy link

API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-1241

@Bromeon
Copy link
Member

Bromeon commented Jul 21, 2025

Thank you! I see deriving Hash for the enums, but what prompted it for Rect2i?

@Bromeon Bromeon added feature Adds functionality to the library c: core Core components labels Jul 21, 2025
@ttencate
Copy link
Contributor Author

A bit obscure, I'll admit. I've built a framework similar to LayerProcGen to generate terrain procedurally in chunks. However, mine is a bit more generic: a "chunk" can be identified by anything, not just by (x, y) coordinates – as long as it implements Eq and Hash (since the chunks in each layer are stored in a hash map internally).

Now I've come to the point where I want to generate maps of part of the world, which will be on a different procgen layer. A map is identified by, among other things, a Rect2 indicating which part of the world it covers. So this is part of its key, and therefore needs to be hashed as well.

We all know that floats shouldn't be hashed, so I did self.rect.cast_int().hash(hasher); which is still a valid hash function. It's good enough for me since these floats represent meters and maps cover several kilometers, so the rounding almost never leads to hash collisions. Except it didn't compile because Rect2i doesn't implement Hash :)

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

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

Thanks for explaining! Makes sense 👍

@Bromeon Bromeon added this to the 0.3.x milestone Jul 22, 2025
@ttencate ttencate added this pull request to the merge queue Jul 22, 2025
Merged via the queue into godot-rust:master with commit 8845d8e Jul 22, 2025
18 checks passed
@ttencate ttencate deleted the feature/rect2i_hash branch July 22, 2025 07:51
@Bromeon
Copy link
Member

Bromeon commented Jul 22, 2025

For future, please don't merge directly. I have to prepare a hotfix release today (see #1247) and did not plan to add any features in that version.

I see that the approval was misleading, I meant it in a "no longer needs any changes" sense, otherwise I could have merged immediately. But this change is low-risk, so no big deal 😉

@ttencate
Copy link
Contributor Author

Apologies. Won't happen again :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: core Core components feature Adds functionality to the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants