From ab6a5cac9f6161a8012e8d98a57eda62f18f4ad0 Mon Sep 17 00:00:00 2001 From: James Liu Date: Tue, 5 Mar 2024 08:09:13 -0800 Subject: [PATCH] Remove initialize_resource and friends (#12307) # Objective `initialize_resource` and it's non-send equivalent is only used in two locations each. Fix #6285. ## Solution Remove them, replace their calls with their internals. Cut down on a bit of generic codegen. This does mean that `initialize_resource_internal` is now `pub(crate)`, but that's likely OK given that only one variant will remain once NonSend resources are removed from the World. --- crates/bevy_ecs/src/system/system_param.rs | 16 ++++++++++++---- crates/bevy_ecs/src/world/mod.rs | 16 ++-------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index e5b3016f5c222..f9ecc9aaf62d6 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -442,7 +442,9 @@ unsafe impl<'a, T: Resource> SystemParam for Res<'a, T> { type Item<'w, 's> = Res<'w, T>; fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State { - let component_id = world.initialize_resource::(); + let component_id = world.components.init_resource::(); + world.initialize_resource_internal(component_id); + let combined_access = system_meta.component_access_set.combined_access(); assert!( !combined_access.has_write(component_id), @@ -532,7 +534,9 @@ unsafe impl<'a, T: Resource> SystemParam for ResMut<'a, T> { type Item<'w, 's> = ResMut<'w, T>; fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State { - let component_id = world.initialize_resource::(); + let component_id = world.components.init_resource::(); + world.initialize_resource_internal(component_id); + let combined_access = system_meta.component_access_set.combined_access(); if combined_access.has_write(component_id) { panic!( @@ -1027,7 +1031,9 @@ unsafe impl<'a, T: 'static> SystemParam for NonSend<'a, T> { fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State { system_meta.set_non_send(); - let component_id = world.initialize_non_send_resource::(); + let component_id = world.components.init_non_send::(); + world.initialize_non_send_internal(component_id); + let combined_access = system_meta.component_access_set.combined_access(); assert!( !combined_access.has_write(component_id), @@ -1114,7 +1120,9 @@ unsafe impl<'a, T: 'static> SystemParam for NonSendMut<'a, T> { fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State { system_meta.set_non_send(); - let component_id = world.initialize_non_send_resource::(); + let component_id = world.components.init_non_send::(); + world.initialize_non_send_internal(component_id); + let combined_access = system_meta.component_access_set.combined_access(); if combined_access.has_write(component_id) { panic!( diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index a36c244ef7cc0..74c7ffb7f5cdb 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1835,7 +1835,7 @@ impl World { /// # Panics /// Panics if `component_id` is not registered as a `Send` component type in this `World` #[inline] - fn initialize_resource_internal( + pub(crate) fn initialize_resource_internal( &mut self, component_id: ComponentId, ) -> &mut ResourceData { @@ -1850,7 +1850,7 @@ impl World { /// # Panics /// panics if `component_id` is not registered in this world #[inline] - fn initialize_non_send_internal( + pub(crate) fn initialize_non_send_internal( &mut self, component_id: ComponentId, ) -> &mut ResourceData { @@ -1862,18 +1862,6 @@ impl World { }) } - pub(crate) fn initialize_resource(&mut self) -> ComponentId { - let component_id = self.components.init_resource::(); - self.initialize_resource_internal(component_id); - component_id - } - - pub(crate) fn initialize_non_send_resource(&mut self) -> ComponentId { - let component_id = self.components.init_non_send::(); - self.initialize_non_send_internal(component_id); - component_id - } - /// Empties queued entities and adds them to the empty [`Archetype`](crate::archetype::Archetype). /// This should be called before doing operations that might operate on queued entities, /// such as inserting a [`Component`].