Skip to content

Commit 5e8c33c

Browse files
authored
Implement Reflect for indexmap::IndexMapand indexmap::IndexSet (#20734)
# Objective - Fixes #19681 ## Solution - Implement Map, PartialReflect, Reflect, Typed, FromReflect, GetTypeRegistration for `indexmap::IndexMap` and `indexmap::IndexSet` - Add `#[derive(Reflect)]` to `EntityIndexSet` - Add indexmap feature to `bevy_reflect` and Update docs accordingly ## Testing - Added test on indexmap::IndexMap --- I initially tried using `impl_reflect_for_hashmap`, but ran into two issues: 1. `IndexMap::drain` requires a range parameter, unlike `HashMap::drain`. 2. `IndexMap::remove` defaults to `swap_remove`, and is now deprecated. I went with `shift_remove`, since I think it matches user expectations. That said, it’s `O(n)`, while `swap_remove` is `O(1)`. Would love feedback on which behavior we should prefer.
1 parent de5bcee commit 5e8c33c

File tree

5 files changed

+538
-5
lines changed

5 files changed

+538
-5
lines changed

crates/bevy_ecs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ hotpatching = ["dep:subsecond"]
9191
bevy_ptr = { path = "../bevy_ptr", version = "0.18.0-dev" }
9292
bevy_reflect = { path = "../bevy_reflect", version = "0.18.0-dev", features = [
9393
"smallvec",
94+
"indexmap",
9495
], default-features = false, optional = true }
9596
bevy_tasks = { path = "../bevy_tasks", version = "0.18.0-dev", default-features = false }
9697
bevy_utils = { path = "../bevy_utils", version = "0.18.0-dev", default-features = false }

crates/bevy_ecs/src/entity/index_set.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ use super::{Entity, EntityHash, EntitySetIterator};
2222

2323
use bevy_platform::prelude::Box;
2424

25+
#[cfg(feature = "bevy_reflect")]
26+
use bevy_reflect::Reflect;
27+
2528
/// An [`IndexSet`] pre-configured to use [`EntityHash`] hashing.
29+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
2630
#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
2731
#[derive(Debug, Clone, Default)]
2832
pub struct EntityIndexSet(pub(crate) IndexSet<Entity, EntityHash>);

crates/bevy_reflect/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ keywords = ["bevy"]
1010
rust-version = "1.85.0"
1111

1212
[features]
13-
default = ["std", "smallvec", "debug", "auto_register_inventory"]
13+
default = ["std", "smallvec", "indexmap", "debug", "auto_register_inventory"]
1414

1515
# Features
1616

@@ -36,6 +36,9 @@ glam = ["dep:glam"]
3636
## Adds reflection support to `hashbrown` types.
3737
hashbrown = ["dep:hashbrown"]
3838

39+
## Adds reflection support to `indexmap` types.
40+
indexmap = ["dep:indexmap"]
41+
3942
## Adds reflection support to `petgraph` types.
4043
petgraph = ["dep:petgraph", "std"]
4144

@@ -116,6 +119,7 @@ smallvec = { version = "1", default-features = false, optional = true }
116119
glam = { version = "0.30.7", default-features = false, features = [
117120
"serde",
118121
], optional = true }
122+
indexmap = { version = "2.5.0", default-features = false, optional = true }
119123
petgraph = { version = "0.8", features = ["serde-1"], optional = true }
120124
smol_str = { version = "0.2.0", default-features = false, features = [
121125
"serde",

0 commit comments

Comments
 (0)