Skip to content

Commit

Permalink
Remove initialize_resource<T> and friends (#12307)
Browse files Browse the repository at this point in the history
# Objective
`initialize_resource<T>` 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.
  • Loading branch information
james7132 authored Mar 5, 2024
1 parent fc202f2 commit ab6a5ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
16 changes: 12 additions & 4 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>();
let component_id = world.components.init_resource::<T>();
world.initialize_resource_internal(component_id);

let combined_access = system_meta.component_access_set.combined_access();
assert!(
!combined_access.has_write(component_id),
Expand Down Expand Up @@ -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::<T>();
let component_id = world.components.init_resource::<T>();
world.initialize_resource_internal(component_id);

let combined_access = system_meta.component_access_set.combined_access();
if combined_access.has_write(component_id) {
panic!(
Expand Down Expand Up @@ -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::<T>();
let component_id = world.components.init_non_send::<T>();
world.initialize_non_send_internal(component_id);

let combined_access = system_meta.component_access_set.combined_access();
assert!(
!combined_access.has_write(component_id),
Expand Down Expand Up @@ -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::<T>();
let component_id = world.components.init_non_send::<T>();
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!(
Expand Down
16 changes: 2 additions & 14 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<true> {
Expand All @@ -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<false> {
Expand All @@ -1862,18 +1862,6 @@ impl World {
})
}

pub(crate) fn initialize_resource<R: Resource>(&mut self) -> ComponentId {
let component_id = self.components.init_resource::<R>();
self.initialize_resource_internal(component_id);
component_id
}

pub(crate) fn initialize_non_send_resource<R: 'static>(&mut self) -> ComponentId {
let component_id = self.components.init_non_send::<R>();
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`].
Expand Down

0 comments on commit ab6a5ca

Please sign in to comment.