Skip to content

Commit 7892f9e

Browse files
Newtype hashbrown (#18694)
# Objective - Fixes #18690 - Closes [#2065](bevyengine/bevy-website#2065) - Alternative to #18691 The changes to the Hash made in #15801 to the [BuildHasher](https://doc.rust-lang.org/std/hash/trait.BuildHasher.html) resulted in serious migration problems and downgraded UX for users of Bevy's re-exported hashmaps. Once merged, we need to go in and remove the migration guide added as part of #15801. ## Solution - Newtype `HashMap` and `HashSet` instead of type aliases - Added `Deref/Mut` to allow accessing future `hashbrown` methods without maintenance from Bevy - Added bidirectional `From` implementations to provide escape hatch for API incompatibility - Added inlinable re-exports of all methods directly to Bevy's types. This ensures `HashMap::new()` works (since the `Deref` implementation wont cover these kinds of invocations). ## Testing - CI --- ## Migration Guide - If you relied on Bevy's `HashMap` and/or `HashSet` types to be identical to `hashbrown`, consider using `From` and `Into` to convert between the `hashbrown` and Bevy types as required. - If you relied on `hashbrown/serde` or `hashbrown/rayon` features, you may need to enable `bevy_platform_support/serialize` and/or `bevy_platform_support/rayon` respectively. --- ## Notes - Did not replicate the Rayon traits, users will need to rely on the `Deref/Mut` or `From` implementations for those methods. - Did not re-expose the `unsafe` methods from `hashbrown`. In most cases users will still have access via `Deref/Mut` anyway. - I have added `inline` to all methods as they are trivial wrappings of existing methods. - I chose to make `HashMap::new` and `HashSet::new` const, which is different to `hashbrown`. We can do this because we default to a fixed-state build-hasher. Mild ergonomic win over using `HashMap::with_hasher(FixedHasher)`.
1 parent 2f80d08 commit 7892f9e

File tree

8 files changed

+2393
-77
lines changed

8 files changed

+2393
-77
lines changed

crates/bevy_picking/src/hover.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ fn build_over_map(
131131
.filter(|e| !cancelled_pointers.contains(&e.pointer))
132132
{
133133
let pointer = entities_under_pointer.pointer;
134-
let layer_map = pointer_over_map
135-
.entry(pointer)
136-
.or_insert_with(BTreeMap::new);
134+
let layer_map = pointer_over_map.entry(pointer).or_default();
137135
for (entity, pick_data) in entities_under_pointer.picks.iter() {
138136
let layer = entities_under_pointer.order;
139137
let hits = layer_map.entry(FloatOrd(layer)).or_default();

crates/bevy_platform_support/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ default = ["std"]
1414
# Functionality
1515

1616
## Adds serialization support through `serde`.
17-
serialize = ["hashbrown/serde"]
17+
serialize = ["dep:serde", "hashbrown/serde"]
18+
19+
## Adds integration with Rayon.
20+
rayon = ["dep:rayon", "hashbrown/rayon"]
1821

1922
# Platform Compatibility
2023

@@ -28,10 +31,11 @@ std = [
2831
"portable-atomic-util/std",
2932
"spin/std",
3033
"foldhash/std",
34+
"serde?/std",
3135
]
3236

3337
## Allows access to the `alloc` crate.
34-
alloc = ["portable-atomic-util/alloc", "dep:hashbrown"]
38+
alloc = ["portable-atomic-util/alloc", "dep:hashbrown", "serde?/alloc"]
3539

3640
## `critical-section` provides the building blocks for synchronization primitives
3741
## on all platforms, including `no_std`.
@@ -57,6 +61,8 @@ hashbrown = { version = "0.15.1", features = [
5761
"equivalent",
5862
"raw-entry",
5963
], optional = true, default-features = false }
64+
serde = { version = "1", default-features = false, optional = true }
65+
rayon = { version = "1", default-features = false, optional = true }
6066

6167
[target.'cfg(target_arch = "wasm32")'.dependencies]
6268
web-time = { version = "1.1", default-features = false, optional = true }

crates/bevy_platform_support/src/collections.rs

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)