Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
james7132 committed Jun 7, 2022
1 parent 56ac553 commit 56aa9d5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
16 changes: 15 additions & 1 deletion crates/bevy_ecs/src/storage/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComponentTicks>)> {
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl<'w, 's, T: Resource> SystemParamFetch<'w, 's> for ResState<T> {
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: {}",
Expand Down Expand Up @@ -393,7 +393,7 @@ impl<'w, 's, T: Resource> SystemParamFetch<'w, 's> for OptionResState<T> {
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(),
Expand Down Expand Up @@ -900,7 +900,7 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for NonSendState<T> {
) -> Self::Item {
world.validate_non_send_access::<T>();
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: {}",
Expand Down Expand Up @@ -948,7 +948,7 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for OptionNonSendState<T> {
) -> Self::Item {
world.validate_non_send_access::<T>();
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(),
Expand Down Expand Up @@ -1013,7 +1013,7 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for NonSendMutState<T> {
) -> Self::Item {
world.validate_non_send_access::<T>();
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: {}",
Expand Down Expand Up @@ -1059,7 +1059,7 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for OptionNonSendMutState<T> {
) -> Self::Item {
world.validate_non_send_access::<T>();
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 {
Expand Down
36 changes: 10 additions & 26 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComponentTicks>)> {
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.
Expand Down Expand Up @@ -1104,7 +1102,7 @@ impl World {
&self,
component_id: ComponentId,
) -> Option<Mut<'_, R>> {
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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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,
})
}
Expand All @@ -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`].
Expand Down

0 comments on commit 56aa9d5

Please sign in to comment.