From 56aa9d5d5b9da28f57b38ae312d091dd884d8a90 Mon Sep 17 00:00:00 2001 From: james7132 Date: Tue, 7 Jun 2022 01:47:25 -0700 Subject: [PATCH] Fix CI --- crates/bevy_ecs/src/storage/resource.rs | 16 +++++++++- crates/bevy_ecs/src/system/system_param.rs | 12 ++++---- crates/bevy_ecs/src/world/mod.rs | 36 ++++++---------------- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/crates/bevy_ecs/src/storage/resource.rs b/crates/bevy_ecs/src/storage/resource.rs index 4679ff84db23a6..ebf98aef634972 100644 --- a/crates/bevy_ecs/src/storage/resource.rs +++ b/crates/bevy_ecs/src/storage/resource.rs @@ -77,7 +77,7 @@ impl Resources { } #[inline] - pub(crate) fn get_with_ticks_unchecked( + pub(crate) fn get_with_ticks( &self, component_id: ComponentId, ) -> Option<(Ptr<'_>, &UnsafeCell)> { @@ -115,6 +115,20 @@ impl Resources { unsafe { Some(column.swap_remove_and_forget_unchecked(0)) } } + #[inline] + pub(crate) fn remove_and_drop(&mut self, component_id: ComponentId) -> Option<()> { + let column = self.resources.get_mut(component_id)?; + if column.is_empty() { + return None; + } + // SAFE: if a resource column exists, row 0 exists as well. The removed value is dropped + // immediately. + unsafe { + column.swap_remove_unchecked(0); + Some(()) + } + } + pub fn check_change_ticks(&mut self, change_tick: u32) { for column in self.resources.values_mut() { column.check_change_ticks(change_tick); diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 2049837b1aaed3..5b46dafe55aaac 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -347,7 +347,7 @@ impl<'w, 's, T: Resource> SystemParamFetch<'w, 's> for ResState { change_tick: u32, ) -> Self::Item { let (ptr, ticks) = world - .get_resource_with_ticks_unchecked(state.component_id) + .get_resource_with_ticks(state.component_id) .unwrap_or_else(|| { panic!( "Resource requested by {} does not exist: {}", @@ -393,7 +393,7 @@ impl<'w, 's, T: Resource> SystemParamFetch<'w, 's> for OptionResState { change_tick: u32, ) -> Self::Item { world - .get_resource_with_ticks_unchecked(state.0.component_id) + .get_resource_with_ticks(state.0.component_id) .map(|(ptr, ticks)| Res { value: ptr.deref(), ticks: ticks.deref(), @@ -900,7 +900,7 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for NonSendState { ) -> Self::Item { world.validate_non_send_access::(); let (ptr, ticks) = world - .get_resource_with_ticks_unchecked(state.component_id) + .get_resource_with_ticks(state.component_id) .unwrap_or_else(|| { panic!( "Non-send resource requested by {} does not exist: {}", @@ -948,7 +948,7 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for OptionNonSendState { ) -> Self::Item { world.validate_non_send_access::(); world - .get_resource_with_ticks_unchecked(state.0.component_id) + .get_resource_with_ticks(state.0.component_id) .map(|(ptr, ticks)| NonSend { value: ptr.deref(), ticks: ticks.read(), @@ -1013,7 +1013,7 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for NonSendMutState { ) -> Self::Item { world.validate_non_send_access::(); let (ptr, ticks) = world - .get_resource_with_ticks_unchecked(state.component_id) + .get_resource_with_ticks(state.component_id) .unwrap_or_else(|| { panic!( "Non-send resource requested by {} does not exist: {}", @@ -1059,7 +1059,7 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for OptionNonSendMutState { ) -> Self::Item { world.validate_non_send_access::(); world - .get_resource_with_ticks_unchecked(state.0.component_id) + .get_resource_with_ticks(state.0.component_id) .map(|(ptr, ticks)| NonSendMut { value: ptr.assert_unique().deref_mut(), ticks: Ticks { diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index a5da40825d93eb..fdbf8211a12534 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -875,13 +875,11 @@ impl World { // Shorthand helper function for getting the data and change ticks for a resource. #[inline] - pub(crate) fn get_resource_with_ticks_unchecked( + pub(crate) fn get_resource_with_ticks( &self, component_id: ComponentId, ) -> Option<(Ptr<'_>, &UnsafeCell)> { - self.storages - .resources - .get_with_ticks_unchecked(component_id) + self.storages.resources.get_with_ticks(component_id) } // Shorthand helper function for getting the [`ArchetypeComponentId`] for a resource. @@ -1104,7 +1102,7 @@ impl World { &self, component_id: ComponentId, ) -> Option> { - let (ptr, ticks) = self.get_resource_with_ticks_unchecked(component_id)?; + let (ptr, ticks) = self.get_resource_with_ticks(component_id)?; Some(Mut { value: ptr.assert_unique().deref_mut(), ticks: Ticks { @@ -1312,9 +1310,7 @@ impl World { if !info.is_send_and_sync() { self.validate_non_send_access_untyped(info.name()); } - - let column = self.get_populated_resource_column(component_id)?; - Some(column.get_data_ptr()) + self.storages.resources.get(component_id) } /// Gets a resource to the resource with the id [`ComponentId`] if it exists. @@ -1330,20 +1326,18 @@ impl World { self.validate_non_send_access_untyped(info.name()); } - let column = self.get_populated_resource_column(component_id)?; + let (ptr, ticks) = self.get_resource_with_ticks(component_id)?; - // SAFE: get_data_ptr requires that the mutability rules are not violated, and the caller promises - // to only modify the resource while the mutable borrow of the world is valid + // SAFE: This funtion xclusive access to the world. The ptr must have unique access. let ticks = Ticks { - // - index is in-bounds because the column is initialized and non-empty - // - no other reference to the ticks of the same row can exist at the same time - component_ticks: unsafe { &mut *column.get_ticks_unchecked(0).get() }, + component_ticks: unsafe { ticks.deref_mut() }, last_change_tick: self.last_change_tick(), change_tick: self.read_change_tick(), }; Some(MutUntyped { - value: unsafe { column.get_data_ptr().assert_unique() }, + // SAFE: This funtion xclusive access to the world. The ptr must have unique access. + value: unsafe { ptr.assert_unique() }, ticks, }) } @@ -1357,17 +1351,7 @@ impl World { if !info.is_send_and_sync() { self.validate_non_send_access_untyped(info.name()); } - - let resource_archetype = self.archetypes.resource_mut(); - let unique_components = resource_archetype.unique_components_mut(); - let column = unique_components.get_mut(component_id)?; - if column.is_empty() { - return None; - } - // SAFE: if a resource column exists, row 0 exists as well - unsafe { column.swap_remove_unchecked(0) }; - - Some(()) + self.storages.resources.remove_and_drop(component_id) } /// Retrieves a mutable untyped reference to the given `entity`'s [Component] of the given [`ComponentId`].