diff --git a/crates/bevy_ecs/src/entity/map_entities.rs b/crates/bevy_ecs/src/entity/map_entities.rs index 122e418179301..e109b43dd5667 100644 --- a/crates/bevy_ecs/src/entity/map_entities.rs +++ b/crates/bevy_ecs/src/entity/map_entities.rs @@ -1,11 +1,11 @@ use crate::{entity::Entity, world::World}; -use bevy_utils::HashMap; +use bevy_utils::EntityHashMap; /// Operation to map all contained [`Entity`] fields in a type to new values. /// /// As entity IDs are valid only for the [`World`] they're sourced from, using [`Entity`] /// as references in components copied from another world will be invalid. This trait -/// allows defining custom mappings for these references via [`HashMap`] +/// allows defining custom mappings for these references via [`EntityHashMap`] /// /// Implementing this trait correctly is required for properly loading components /// with entity references from scenes. @@ -39,7 +39,7 @@ pub trait MapEntities { fn map_entities(&mut self, entity_mapper: &mut EntityMapper); } -/// A wrapper for [`HashMap`], augmenting it with the ability to allocate new [`Entity`] references in a destination +/// A wrapper for [`EntityHashMap`], augmenting it with the ability to allocate new [`Entity`] references in a destination /// world. These newly allocated references are guaranteed to never point to any living entity in that world. /// /// References are allocated by returning increasing generations starting from an internally initialized base @@ -52,9 +52,9 @@ pub struct EntityMapper<'m> { /// or over the network. This is required as [`Entity`] identifiers are opaque; you cannot and do not want to reuse /// identifiers directly. /// - /// On its own, a [`HashMap`] is not capable of allocating new entity identifiers, which is needed to map references + /// On its own, a [`EntityHashMap`] is not capable of allocating new entity identifiers, which is needed to map references /// to entities that lie outside the source entity set. This functionality can be accessed through [`EntityMapper::world_scope()`]. - map: &'m mut HashMap, + map: &'m mut EntityHashMap, /// A base [`Entity`] used to allocate new references. dead_start: Entity, /// The number of generations this mapper has allocated thus far. @@ -80,18 +80,18 @@ impl<'m> EntityMapper<'m> { new } - /// Gets a reference to the underlying [`HashMap`]. - pub fn get_map(&'m self) -> &'m HashMap { + /// Gets a reference to the underlying [`EntityHashMap`]. + pub fn get_map(&'m self) -> &'m EntityHashMap { self.map } - /// Gets a mutable reference to the underlying [`HashMap`]. - pub fn get_map_mut(&'m mut self) -> &'m mut HashMap { + /// Gets a mutable reference to the underlying [`EntityHashMap`]. + pub fn get_map_mut(&'m mut self) -> &'m mut EntityHashMap { self.map } /// Creates a new [`EntityMapper`], spawning a temporary base [`Entity`] in the provided [`World`] - fn new(map: &'m mut HashMap, world: &mut World) -> Self { + fn new(map: &'m mut EntityHashMap, world: &mut World) -> Self { Self { map, // SAFETY: Entities data is kept in a valid state via `EntityMapper::world_scope` @@ -111,14 +111,14 @@ impl<'m> EntityMapper<'m> { assert!(entities.reserve_generations(self.dead_start.index, self.generations)); } - /// Creates an [`EntityMapper`] from a provided [`World`] and [`HashMap`], then calls the + /// Creates an [`EntityMapper`] from a provided [`World`] and [`EntityHashMap`], then calls the /// provided function with it. This allows one to allocate new entity references in this [`World`] that are /// guaranteed to never point at a living entity now or in the future. This functionality is useful for safely /// mapping entity identifiers that point at entities outside the source world. The passed function, `f`, is called /// within the scope of this world. Its return value is then returned from `world_scope` as the generic type /// parameter `R`. pub fn world_scope( - entity_map: &'m mut HashMap, + entity_map: &'m mut EntityHashMap, world: &mut World, f: impl FnOnce(&mut World, &mut Self) -> R, ) -> R { @@ -131,7 +131,7 @@ impl<'m> EntityMapper<'m> { #[cfg(test)] mod tests { - use bevy_utils::HashMap; + use bevy_utils::EntityHashMap; use crate::{ entity::{Entity, EntityMapper}, @@ -143,7 +143,7 @@ mod tests { const FIRST_IDX: u32 = 1; const SECOND_IDX: u32 = 2; - let mut map = HashMap::default(); + let mut map = EntityHashMap::default(); let mut world = World::new(); let mut mapper = EntityMapper::new(&mut map, &mut world); @@ -170,7 +170,7 @@ mod tests { #[test] fn world_scope_reserves_generations() { - let mut map = HashMap::default(); + let mut map = EntityHashMap::default(); let mut world = World::new(); let dead_ref = EntityMapper::world_scope(&mut map, &mut world, |_, mapper| { diff --git a/crates/bevy_ecs/src/reflect/map_entities.rs b/crates/bevy_ecs/src/reflect/map_entities.rs index 7a5fe4257f0e5..27bcb03653546 100644 --- a/crates/bevy_ecs/src/reflect/map_entities.rs +++ b/crates/bevy_ecs/src/reflect/map_entities.rs @@ -4,7 +4,7 @@ use crate::{ world::World, }; use bevy_reflect::FromType; -use bevy_utils::HashMap; +use bevy_utils::EntityHashMap; /// For a specific type of component, this maps any fields with values of type [`Entity`] to a new world. /// Since a given `Entity` ID is only valid for the world it came from, when performing deserialization @@ -18,29 +18,33 @@ pub struct ReflectMapEntities { } impl ReflectMapEntities { - /// A general method for applying [`MapEntities`] behavior to all elements in an [`HashMap`]. + /// A general method for applying [`MapEntities`] behavior to all elements in an [`EntityHashMap`]. /// - /// Be mindful in its usage: Works best in situations where the entities in the [`HashMap`] are newly + /// Be mindful in its usage: Works best in situations where the entities in the [`EntityHashMap`] are newly /// created, before systems have a chance to add new components. If some of the entities referred to - /// by the [`HashMap`] might already contain valid entity references, you should use [`map_entities`](Self::map_entities). + /// by the [`EntityHashMap`] might already contain valid entity references, you should use [`map_entities`](Self::map_entities). /// /// An example of this: A scene can be loaded with `Parent` components, but then a `Parent` component can be added /// to these entities after they have been loaded. If you reload the scene using [`map_all_entities`](Self::map_all_entities), those `Parent` /// components with already valid entity references could be updated to point at something else entirely. - pub fn map_all_entities(&self, world: &mut World, entity_map: &mut HashMap) { + pub fn map_all_entities( + &self, + world: &mut World, + entity_map: &mut EntityHashMap, + ) { EntityMapper::world_scope(entity_map, world, self.map_all_entities); } - /// A general method for applying [`MapEntities`] behavior to elements in an [`HashMap`]. Unlike + /// A general method for applying [`MapEntities`] behavior to elements in an [`EntityHashMap`]. Unlike /// [`map_all_entities`](Self::map_all_entities), this is applied to specific entities, not all values - /// in the [`HashMap`]. + /// in the [`EntityHashMap`]. /// /// This is useful mostly for when you need to be careful not to update components that already contain valid entity /// values. See [`map_all_entities`](Self::map_all_entities) for more details. pub fn map_entities( &self, world: &mut World, - entity_map: &mut HashMap, + entity_map: &mut EntityHashMap, entities: &[Entity], ) { EntityMapper::world_scope(entity_map, world, |world, mapper| { diff --git a/crates/bevy_scene/src/dynamic_scene.rs b/crates/bevy_scene/src/dynamic_scene.rs index 4cbe89980a065..fa7100652fd1c 100644 --- a/crates/bevy_scene/src/dynamic_scene.rs +++ b/crates/bevy_scene/src/dynamic_scene.rs @@ -5,7 +5,7 @@ use bevy_ecs::{ world::World, }; use bevy_reflect::{Reflect, TypePath, TypeRegistryArc}; -use bevy_utils::HashMap; +use bevy_utils::{EntityHashMap, HashMap}; use std::any::TypeId; #[cfg(feature = "serialize")] @@ -65,7 +65,7 @@ impl DynamicScene { pub fn write_to_world_with( &self, world: &mut World, - entity_map: &mut HashMap, + entity_map: &mut EntityHashMap, type_registry: &AppTypeRegistry, ) -> Result<(), SceneSpawnError> { let type_registry = type_registry.read(); @@ -163,7 +163,7 @@ impl DynamicScene { pub fn write_to_world( &self, world: &mut World, - entity_map: &mut HashMap, + entity_map: &mut EntityHashMap, ) -> Result<(), SceneSpawnError> { let registry = world.resource::().clone(); self.write_to_world_with(world, entity_map, ®istry) @@ -193,7 +193,7 @@ where mod tests { use bevy_ecs::{reflect::AppTypeRegistry, system::Command, world::World}; use bevy_hierarchy::{AddChild, Parent}; - use bevy_utils::HashMap; + use bevy_utils::EntityHashMap; use crate::dynamic_scene_builder::DynamicSceneBuilder; @@ -222,7 +222,7 @@ mod tests { .extract_entity(original_parent_entity) .extract_entity(original_child_entity) .build(); - let mut entity_map = HashMap::default(); + let mut entity_map = EntityHashMap::default(); scene.write_to_world(&mut world, &mut entity_map).unwrap(); let &from_scene_parent_entity = entity_map.get(&original_parent_entity).unwrap(); diff --git a/crates/bevy_scene/src/scene.rs b/crates/bevy_scene/src/scene.rs index e6e9d4ddfb3f6..d09df9d0c7119 100644 --- a/crates/bevy_scene/src/scene.rs +++ b/crates/bevy_scene/src/scene.rs @@ -5,7 +5,7 @@ use bevy_ecs::{ world::World, }; use bevy_reflect::TypePath; -use bevy_utils::HashMap; +use bevy_utils::EntityHashMap; /// To spawn a scene, you can use either: /// * [`SceneSpawner::spawn`](crate::SceneSpawner::spawn) @@ -31,7 +31,7 @@ impl Scene { type_registry: &AppTypeRegistry, ) -> Result { let mut world = World::new(); - let mut entity_map = HashMap::default(); + let mut entity_map = EntityHashMap::default(); dynamic_scene.write_to_world_with(&mut world, &mut entity_map, type_registry)?; Ok(Self { world }) @@ -57,7 +57,7 @@ impl Scene { type_registry: &AppTypeRegistry, ) -> Result { let mut instance_info = InstanceInfo { - entity_map: HashMap::default(), + entity_map: EntityHashMap::default(), }; let type_registry = type_registry.read(); diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index b45ffa91cdb8f..11192337b6ac9 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -8,7 +8,7 @@ use bevy_ecs::{ world::{Mut, World}, }; use bevy_hierarchy::{AddChild, Parent}; -use bevy_utils::{tracing::error, HashMap, HashSet}; +use bevy_utils::{tracing::error, EntityHashMap, HashMap, HashSet}; use thiserror::Error; use uuid::Uuid; @@ -25,7 +25,7 @@ pub struct SceneInstanceReady { #[derive(Debug)] pub struct InstanceInfo { /// Mapping of entities from the scene world to the instance world. - pub entity_map: HashMap, + pub entity_map: EntityHashMap, } /// Unique id identifying a scene instance. @@ -199,7 +199,7 @@ impl SceneSpawner { world: &mut World, id: impl Into>, ) -> Result<(), SceneSpawnError> { - let mut entity_map = HashMap::default(); + let mut entity_map = EntityHashMap::default(); let id = id.into(); Self::spawn_dynamic_internal(world, id, &mut entity_map)?; let instance_id = InstanceId::new(); @@ -213,7 +213,7 @@ impl SceneSpawner { fn spawn_dynamic_internal( world: &mut World, id: AssetId, - entity_map: &mut HashMap, + entity_map: &mut EntityHashMap, ) -> Result<(), SceneSpawnError> { world.resource_scope(|world, scenes: Mut>| { let scene = scenes @@ -297,7 +297,7 @@ impl SceneSpawner { let scenes_to_spawn = std::mem::take(&mut self.dynamic_scenes_to_spawn); for (id, instance_id) in scenes_to_spawn { - let mut entity_map = HashMap::default(); + let mut entity_map = EntityHashMap::default(); match Self::spawn_dynamic_internal(world, id, &mut entity_map) { Ok(_) => { diff --git a/crates/bevy_scene/src/serde.rs b/crates/bevy_scene/src/serde.rs index df580b30e46c3..7a8cfe090f7c2 100644 --- a/crates/bevy_scene/src/serde.rs +++ b/crates/bevy_scene/src/serde.rs @@ -504,7 +504,7 @@ mod tests { use bevy_ecs::reflect::{AppTypeRegistry, ReflectMapEntities}; use bevy_ecs::world::FromWorld; use bevy_reflect::{Reflect, ReflectSerialize}; - use bevy_utils::HashMap; + use bevy_utils::EntityHashMap; use bincode::Options; use serde::de::DeserializeSeed; use serde::Serialize; @@ -678,7 +678,7 @@ mod tests { "expected `entities` to contain 3 entities" ); - let mut map = HashMap::default(); + let mut map = EntityHashMap::default(); let mut dst_world = create_world(); scene.write_to_world(&mut dst_world, &mut map).unwrap(); @@ -717,7 +717,7 @@ mod tests { let deserialized_scene = scene_deserializer.deserialize(&mut deserializer).unwrap(); - let mut map = HashMap::default(); + let mut map = EntityHashMap::default(); let mut dst_world = create_world(); deserialized_scene .write_to_world(&mut dst_world, &mut map)