From ee1807395e70bfcfe164ff84f2693ea71b083028 Mon Sep 17 00:00:00 2001 From: Alix Bott Date: Wed, 2 Jul 2025 16:50:55 +0200 Subject: [PATCH] Implement `MapEntities` for arrays, `HashMap`, `BTreeMap`, `IndexMap` (#19908) # Objective - `MapEntities` is not implemented for arrays, `HashMap`, `BTreeMap`, and `IndexMap`. ## Solution - Implement `MapEntities` for arrays, `HashMap`, `BTreeMap`, `IndexMap` ## Testing - I didn't add a test for this as the implementations seems pretty trivial --- crates/bevy_ecs/src/entity/map_entities.rs | 66 +++++++++++++++++++--- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/crates/bevy_ecs/src/entity/map_entities.rs b/crates/bevy_ecs/src/entity/map_entities.rs index 647bde983a..2c59655275 100644 --- a/crates/bevy_ecs/src/entity/map_entities.rs +++ b/crates/bevy_ecs/src/entity/map_entities.rs @@ -1,5 +1,5 @@ pub use bevy_ecs_macros::MapEntities; -use indexmap::IndexSet; +use indexmap::{IndexMap, IndexSet}; use crate::{ entity::{hash_map::EntityHashMap, Entity}, @@ -7,11 +7,14 @@ use crate::{ }; use alloc::{ - collections::{BTreeSet, VecDeque}, + collections::{BTreeMap, BTreeSet, VecDeque}, vec::Vec, }; -use bevy_platform::collections::HashSet; -use core::{hash::BuildHasher, mem}; +use bevy_platform::collections::{HashMap, HashSet}; +use core::{ + hash::{BuildHasher, Hash}, + mem, +}; use smallvec::SmallVec; use super::EntityIndexSet; @@ -72,9 +75,22 @@ impl MapEntities for Option { } } -impl MapEntities - for HashSet +impl MapEntities + for HashMap { + fn map_entities(&mut self, entity_mapper: &mut E) { + *self = self + .drain() + .map(|(mut key_entities, mut value_entities)| { + key_entities.map_entities(entity_mapper); + value_entities.map_entities(entity_mapper); + (key_entities, value_entities) + }) + .collect(); + } +} + +impl MapEntities for HashSet { fn map_entities(&mut self, entity_mapper: &mut E) { *self = self .drain() @@ -86,9 +102,22 @@ impl MapEntiti } } -impl MapEntities - for IndexSet +impl MapEntities + for IndexMap { + fn map_entities(&mut self, entity_mapper: &mut E) { + *self = self + .drain(..) + .map(|(mut key_entities, mut value_entities)| { + key_entities.map_entities(entity_mapper); + value_entities.map_entities(entity_mapper); + (key_entities, value_entities) + }) + .collect(); + } +} + +impl MapEntities for IndexSet { fn map_entities(&mut self, entity_mapper: &mut E) { *self = self .drain(..) @@ -109,6 +138,19 @@ impl MapEntities for EntityIndexSet { } } +impl MapEntities for BTreeMap { + fn map_entities(&mut self, entity_mapper: &mut E) { + *self = mem::take(self) + .into_iter() + .map(|(mut key_entities, mut value_entities)| { + key_entities.map_entities(entity_mapper); + value_entities.map_entities(entity_mapper); + (key_entities, value_entities) + }) + .collect(); + } +} + impl MapEntities for BTreeSet { fn map_entities(&mut self, entity_mapper: &mut E) { *self = mem::take(self) @@ -121,6 +163,14 @@ impl MapEntities for BTreeSet { } } +impl MapEntities for [T; N] { + fn map_entities(&mut self, entity_mapper: &mut E) { + for entities in self.iter_mut() { + entities.map_entities(entity_mapper); + } + } +} + impl MapEntities for Vec { fn map_entities(&mut self, entity_mapper: &mut E) { for entities in self.iter_mut() {