diff --git a/crates/bevy_ecs/src/resource.rs b/crates/bevy_ecs/src/resource.rs index 5bf2846d4a16a..3e8099f53b057 100644 --- a/crates/bevy_ecs/src/resource.rs +++ b/crates/bevy_ecs/src/resource.rs @@ -106,13 +106,6 @@ impl ResourceEntities { self.deref().get(id).copied() } - /// Removes the entry for the given resource component. - /// Returns the entity that was removed, or `None` if there was no entity. - #[inline] - pub(crate) fn remove(&mut self, id: ComponentId) -> Option { - self.0.get_mut().remove(id) - } - #[inline] fn deref(&self) -> &SparseArray { // SAFETY: There are no other mutable references to the map. @@ -247,27 +240,38 @@ mod tests { let mut world = World::new(); let start = world.entities().count_spawned(); - world.init_resource::(); + let id1 = world.init_resource::(); assert_eq!(world.entities().count_spawned(), start + 1); world.insert_resource(TestResource2(String::from("Foo"))); assert_eq!(world.entities().count_spawned(), start + 2); // like component registration, which just makes it known to the world that a component exists, // registering a resource should not spawn an entity. - let id = world.register_resource::(); + let id3 = world.register_resource::(); assert_eq!(world.entities().count_spawned(), start + 2); OwningPtr::make(20_u8, |ptr| { // SAFETY: id was just initialized and corresponds to a resource. unsafe { - world.insert_resource_by_id(id, ptr, MaybeLocation::caller()); + world.insert_resource_by_id(id3, ptr, MaybeLocation::caller()); } }); assert_eq!(world.entities().count_spawned(), start + 3); - assert!(world.remove_resource_by_id(id)); + let e3 = world.resource_entities().get(id3).unwrap(); + assert!(world.remove_resource_by_id(id3)); // the entity is stable: removing the resource should only remove the component from the entity, not despawn the entity assert_eq!(world.entities().count_spawned(), start + 3); + OwningPtr::make(20_u8, |ptr| { + // SAFETY: id was just initialized and corresponds to a resource. + unsafe { + world.insert_resource_by_id(id3, ptr, MaybeLocation::caller()); + } + }); + assert_eq!(e3, world.resource_entities().get(id3).unwrap()); // again, the entity is stable: see previous explanation + let e1 = world.resource_entities().get(id1).unwrap(); world.remove_resource::(); assert_eq!(world.entities().count_spawned(), start + 3); + world.init_resource::(); + assert_eq!(e1, world.resource_entities().get(id1).unwrap()); // make sure that trying to add a resource twice results, doesn't change the entity count world.insert_resource(TestResource2(String::from("Bar"))); assert_eq!(world.entities().count_spawned(), start + 3); diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index a391968e3c41b..8ee75de95f147 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -3653,7 +3653,7 @@ impl World { /// **You should prefer to use the typed API [`World::remove_resource`] where possible and only /// use this in cases where the actual types are not known at compile time.** pub fn remove_resource_by_id(&mut self, component_id: ComponentId) -> bool { - if let Some(entity) = self.resource_entities.remove(component_id) + if let Some(entity) = self.resource_entities.get(component_id) && let Ok(mut entity_mut) = self.get_entity_mut(entity) && entity_mut.contains_id(component_id) {