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

Enable cloning EntityHashMap and PreHashMap #11178

Merged

Conversation

cBournhonesque
Copy link
Contributor

@cBournhonesque cBournhonesque commented Jan 2, 2024

Objective

  • EntityHashMap, EntityHashSet and PreHashMap are currently not Cloneable because of a missing trivial Clone bound for EntityHash and PreHash. This PR makes them Cloneable.

(the parent struct hashbrown::HashMap requires the HashBuilder to be Clone for the HashMap to be Clone, see: https://github.com/rust-lang/hashbrown/blob/master/src/map.rs#L195)

Solution

  • Add a Clone bound to PreHash and EntityHash

@cBournhonesque cBournhonesque added C-Usability A simple quality-of-life change that makes Bevy easier to use A-Utils Utility functions and types labels Jan 2, 2024
@alice-i-cecile
Copy link
Member

It would probably be better to also swap the Clone impls on the generic parent structs to a manual clone impl: the lack of "perfect derives" means that it incorrectly requires a Clone bound.

@cBournhonesque
Copy link
Contributor Author

It would probably be better to also swap the Clone impls on the generic parent structs to a manual clone impl: the lack of "perfect derives" means that it incorrectly requires a Clone bound.

The parent struct (hashbrown::HashMap) actually already as a manual clone impl, but it requires the HashBuilder to derive Clone. See: https://github.com/rust-lang/hashbrown/blob/master/src/map.rs#L195
I will update the PR description to make this clear

Copy link
Member

@james7132 james7132 left a comment

Choose a reason for hiding this comment

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

LGTM, though one open note about our use of compile tests.

use super::*;

#[test]
fn test_clone_entity_hash_map() {
Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty partial to having something similar to static_assertions (it's already in our dependency tree), which includes a assertion that will fail compilation if a type does not implement a trait. I think it should be fine to use it as a dev dependency over these tests which actually need to run when running our test suites.

This is a strict non-blocker and we can tackle this in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah i was thinking of re-using trybuild (https://docs.rs/trybuild/latest/trybuild/) since I see that it's already used in bevy somewhere else, but static_assertions seems to have more powerful checks

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jan 2, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 2, 2024
Merged via the queue into bevyengine:main with commit ab10e85 Jan 2, 2024
23 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Jan 2, 2024
# Objective

- We want to use `static_assertions` to perform precise compile time
checks at testing time. In this PR, we add those checks to make sure
that `EntityHashMap` and `PreHashMap` are `Clone` (and we replace the
more clumsy previous tests)
- Fixes #11181 

(will need to be rebased once
#11178 is merged)

---------

Co-authored-by: Charles Bournhonesque <cbournhonesque@snapchat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Utils Utility functions and types C-Usability A simple quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants