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
This commit is contained in:
Alix Bott 2025-07-02 16:50:55 +02:00 committed by GitHub
parent 9e0c66bd65
commit ee1807395e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<T: MapEntities> MapEntities for Option<T> {
}
}
impl<T: MapEntities + Eq + core::hash::Hash, S: BuildHasher + Default> MapEntities
for HashSet<T, S>
impl<K: MapEntities + Eq + Hash, V: MapEntities, S: BuildHasher + Default> MapEntities
for HashMap<K, V, S>
{
fn map_entities<E: EntityMapper>(&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<T: MapEntities + Eq + Hash, S: BuildHasher + Default> MapEntities for HashSet<T, S> {
fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E) {
*self = self
.drain()
@ -86,9 +102,22 @@ impl<T: MapEntities + Eq + core::hash::Hash, S: BuildHasher + Default> MapEntiti
}
}
impl<T: MapEntities + Eq + core::hash::Hash, S: BuildHasher + Default> MapEntities
for IndexSet<T, S>
impl<K: MapEntities + Eq + Hash, V: MapEntities, S: BuildHasher + Default> MapEntities
for IndexMap<K, V, S>
{
fn map_entities<E: EntityMapper>(&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<T: MapEntities + Eq + Hash, S: BuildHasher + Default> MapEntities for IndexSet<T, S> {
fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E) {
*self = self
.drain(..)
@ -109,6 +138,19 @@ impl MapEntities for EntityIndexSet {
}
}
impl<K: MapEntities + Ord, V: MapEntities> MapEntities for BTreeMap<K, V> {
fn map_entities<E: EntityMapper>(&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<T: MapEntities + Ord> MapEntities for BTreeSet<T> {
fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E) {
*self = mem::take(self)
@ -121,6 +163,14 @@ impl<T: MapEntities + Ord> MapEntities for BTreeSet<T> {
}
}
impl<T: MapEntities, const N: usize> MapEntities for [T; N] {
fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E) {
for entities in self.iter_mut() {
entities.map_entities(entity_mapper);
}
}
}
impl<T: MapEntities> MapEntities for Vec<T> {
fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E) {
for entities in self.iter_mut() {