Skip to content

Commit e5158ed

Browse files
Update docs to explain default Hasher issue (#18350)
# Objective I experienced an issue where `HashMap::new` was not returning a value typed appropriately for a `HashMap<K,V>` declaration that omitted the Hasher- e.g. the Default Hasher for the type is different than what the `new` method produces. After discussion on discord, this appears to be an issue in `hashbrown`, and working around it would be very nontrivial, requiring a newtype on top of the `hashbrown` implementation. Rather than doing that, it was suggested that we add docs to make the issue more visible and provide a clear workaround. ## Solution Updated the docs for `bevy_platform_support::collections`. I couldn't update Struct docs because they're re-exports, so I had to settle for the module. Note that the `[HashMap::new]` link wasn't generating properly- I'm not sure why. I see the method in the docs.rs site, https://docs.rs/hashbrown/0.15.1/hashbrown/struct.HashMap.html#method.new, but not on the generated internal documentation. I wonder if `hashbrown` isn't actually implementing the new or something? ## Testing n/a although I did generate and open the docs on my Ubuntu machine. --- ## Showcase before: ![image](https://github.com/user-attachments/assets/5c80417d-9c4e-4b57-825f-911203ed8558) after: ![image](https://github.com/user-attachments/assets/420f6775-2f32-466f-a580-bb1344016bda) --------- Co-authored-by: Zachary Harrold <zac@harrold.com.au>
1 parent 35bf975 commit e5158ed

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

crates/bevy_platform_support/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ hashbrown = { version = "0.15.1", features = [
5858
"raw-entry",
5959
], optional = true, default-features = false }
6060

61+
[dev-dependencies]
62+
bevy_ecs = { path = "../bevy_ecs", version = "0.16.0-dev" }
63+
6164
[target.'cfg(target_arch = "wasm32")'.dependencies]
6265
web-time = { version = "1.1", default-features = false, optional = true }
6366
getrandom = { version = "0.2.0", default-features = false, optional = true, features = [

crates/bevy_platform_support/src/collections.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
//! Provides [`HashMap`] and [`HashSet`] from [`hashbrown`] with some customized defaults.\
1+
//! Provides [`HashMap`] and [`HashSet`] from [`hashbrown`] with some customized defaults.
22
//!
33
//! Also provides the [`HashTable`] type, which is specific to [`hashbrown`].
4+
//!
5+
//! Note that due to the implementation details of [`hashbrown`], [`HashMap::new`] is only implemented for `HashMap<K, V, RandomState>`.
6+
//! Whereas, Bevy exports `HashMap<K, V, FixedHasher>` as its default [`HashMap`] type, meaning [`HashMap::new`] will typically fail.
7+
//! To bypass this issue, use [`HashMap::default`] instead.
8+
//!
9+
//! ```
10+
//! # use bevy_ecs::component::Component;
11+
//! # use bevy_ecs::system::Commands;
12+
//! # use bevy_platform_support::collections::HashMap;
13+
//!
14+
//! #[derive(Component)]
15+
//! struct MyComponent {
16+
//! map: HashMap<String, String>
17+
//! }
18+
//!
19+
//! fn my_system(mut commands: Commands) {
20+
//! commands.spawn(MyComponent {
21+
//! map: HashMap::default(),
22+
//! });
23+
//! }
24+
//! ```
425
526
pub use hash_map::HashMap;
627
pub use hash_set::HashSet;

0 commit comments

Comments
 (0)