From 52a217e8ea6b7dec49b70c8d60407cb9096163eb Mon Sep 17 00:00:00 2001 From: james7132 Date: Sun, 19 Jun 2022 05:38:18 -0700 Subject: [PATCH] Update init_state to WorldQuery --- crates/bevy_ecs/src/query/fetch.rs | 63 +++++++++++++---------------- crates/bevy_ecs/src/query/filter.rs | 32 +++++++-------- crates/bevy_ecs/src/query/state.rs | 4 +- 3 files changed, 47 insertions(+), 52 deletions(-) diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index c634b30359cdf..4de24d59053e0 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -323,6 +323,8 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w, _State = Self::State> { type ReadOnly: ReadOnlyWorldQuery; type State: Send + Sync; + fn init(world: &mut World) -> Self::State; + /// This function manually implements variance for the query items. fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self>; } @@ -453,8 +455,6 @@ pub unsafe trait Fetch<'world>: Sized { true } - fn init_state(world: &mut World) -> Self::State; - // This does not have a default body of `{}` because 99% of cases need to add accesses // and forgetting to do so would be unsound. fn update_component_access(state: &Self::State, access: &mut FilteredAccess); @@ -477,6 +477,8 @@ unsafe impl WorldQuery for Entity { type ReadOnly = Self; type State = (); + fn init(_world: &mut World) -> Self::State {} + fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { item } @@ -542,8 +544,6 @@ unsafe impl<'w> Fetch<'w> for EntityFetch<'w> { *entities.get(archetype_index) } - fn init_state(_world: &mut World) -> Self::State {} - fn update_component_access(_state: &Self::State, _access: &mut FilteredAccess) {} fn update_archetype_component_access( @@ -567,6 +567,10 @@ unsafe impl WorldQuery for &T { type ReadOnly = Self; type State = ComponentId; + fn init(world: &mut World) -> Self::State { + world.init_component::() + } + fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { item } @@ -687,10 +691,6 @@ unsafe impl<'w, T: Component> Fetch<'w> for ReadFetch<'w, T> { components.get(table_row).deref() } - fn init_state(world: &mut World) -> Self::State { - world.init_component::() - } - fn update_component_access(state: &Self::State, access: &mut FilteredAccess) { assert!( !access.access().has_write(*state), @@ -723,6 +723,10 @@ unsafe impl<'w, T: Component> WorldQuery for &'w mut T { type ReadOnly = &'w T; type State = ComponentId; + fn init(world: &mut World) -> Self::State { + world.init_component::() + } + fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { item } @@ -875,10 +879,6 @@ unsafe impl<'w, T: Component> Fetch<'w> for WriteFetch<'w, T> { } } - fn init_state(world: &mut World) -> Self::State { - world.init_component::() - } - fn update_component_access(state: &Self::State, access: &mut FilteredAccess) { assert!( !access.access().has_read(*state), @@ -911,6 +911,10 @@ unsafe impl WorldQuery for Option { type ReadOnly = Option; type State = T::State; + fn init(world: &mut World) -> Self::State { + T::init(world) + } + fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { item.map(T::shrink) } @@ -993,10 +997,6 @@ unsafe impl<'w, T: Fetch<'w>> Fetch<'w> for OptionFetch { } } - fn init_state(world: &mut World) -> Self::State { - T::init_state(world) - } - fn update_component_access(state: &Self::State, access: &mut FilteredAccess) { // We don't want to add the `with`/`without` of `T` as `Option` will match things regardless of // `T`'s filters. for example `Query<(Option<&U>, &mut V)>` will match every entity with a `V` component @@ -1094,6 +1094,10 @@ unsafe impl WorldQuery for ChangeTrackers { type ReadOnly = Self; type State = ComponentId; + fn init(world: &mut World) -> Self::State { + world.init_component::() + } + fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { item } @@ -1245,10 +1249,6 @@ unsafe impl<'w, T: Component> Fetch<'w> for ChangeTrackersFetch<'w, T> { } } - fn init_state(world: &mut World) -> Self::State { - world.init_component::() - } - fn update_component_access(state: &Self::State, access: &mut FilteredAccess) { assert!( !access.access().has_write(*state), @@ -1343,11 +1343,6 @@ macro_rules! impl_tuple_fetch { true $(&& $name.archetype_filter_fetch(archetype_index))* } - #[allow(clippy::unused_unit)] - fn init_state(_world: &mut World) -> Self::State { - ($($name::init_state(_world),)*) - } - fn update_component_access(_state: &Self::State, _access: &mut FilteredAccess) { let ($($name,)*) = _state; $($name::update_component_access($name, _access);)* @@ -1371,6 +1366,11 @@ macro_rules! impl_tuple_fetch { type ReadOnly = ($($name::ReadOnly,)*); type State = ($($name::State,)*); + #[allow(clippy::unused_unit)] + fn init(_world: &mut World) -> Self::State { + ($($name::init(_world),)*) + } + fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { let ($($name,)*) = item; ($( @@ -1459,10 +1459,6 @@ macro_rules! impl_anytuple_fetch { )*) } - fn init_state(_world: &mut World) -> Self::State { - AnyOf(($($name::init_state(_world),)*)) - } - fn update_component_access(state: &Self::State, _access: &mut FilteredAccess) { let ($($name,)*) = &state.0; @@ -1517,6 +1513,10 @@ macro_rules! impl_anytuple_fetch { type ReadOnly = AnyOf<($($name::ReadOnly,)*)>; type State = AnyOf<($($name::State,)*)>; + fn init(_world: &mut World) -> Self::State { + AnyOf(($($name::init(_world),)*)) + } + fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { let ($($name,)*) = item; ($( @@ -1578,11 +1578,6 @@ unsafe impl<'w, State: Send + Sync> Fetch<'w> for NopFetch { #[inline(always)] unsafe fn table_fetch(&mut self, _table_row: usize) -> Self::Item {} - #[inline(always)] - fn init_state(_world: &mut World) -> Self::State { - unimplemented!(); - } - fn update_component_access(_state: &Self::State, _access: &mut FilteredAccess) {} fn update_archetype_component_access( diff --git a/crates/bevy_ecs/src/query/filter.rs b/crates/bevy_ecs/src/query/filter.rs index 5924b4dd43e19..191040c68f3e5 100644 --- a/crates/bevy_ecs/src/query/filter.rs +++ b/crates/bevy_ecs/src/query/filter.rs @@ -49,6 +49,10 @@ unsafe impl WorldQuery for With { type ReadOnly = Self; type State = ComponentId; + fn init(world: &mut World) -> Self::State { + world.init_component::() + } + #[allow(clippy::semicolon_if_nothing_returned)] fn shrink<'wlong: 'wshort, 'wshort>( item: super::QueryItem<'wlong, Self>, @@ -111,10 +115,6 @@ unsafe impl<'w, T: Component> Fetch<'w> for WithFetch { #[inline] unsafe fn table_fetch(&mut self, _table_row: usize) {} - fn init_state(world: &mut World) -> Self::State { - world.init_component::() - } - #[inline] fn update_component_access(state: &Self::State, access: &mut FilteredAccess) { access.add_with(*state); @@ -180,6 +180,10 @@ unsafe impl WorldQuery for Without { type ReadOnly = Self; type State = ComponentId; + fn init(world: &mut World) -> Self::State { + world.init_component::() + } + #[allow(clippy::semicolon_if_nothing_returned)] fn shrink<'wlong: 'wshort, 'wshort>( item: super::QueryItem<'wlong, Self>, @@ -242,10 +246,6 @@ unsafe impl<'w, T: Component> Fetch<'w> for WithoutFetch { #[inline] unsafe fn table_fetch(&mut self, _table_row: usize) {} - fn init_state(world: &mut World) -> Self::State { - world.init_component::() - } - #[inline] fn update_component_access(state: &Self::State, access: &mut FilteredAccess) { access.add_without(*state); @@ -330,6 +330,10 @@ macro_rules! impl_query_filter_tuple { type ReadOnly = Or<($($filter::ReadOnly,)*)>; type State = Or<($($filter::State,)*)>; + fn init(world: &mut World) -> Self::State { + Or(($($filter::init(world),)*)) + } + fn shrink<'wlong: 'wshort, 'wshort>(item: super::QueryItem<'wlong, Self>) -> super::QueryItem<'wshort, Self> { item } @@ -408,10 +412,6 @@ macro_rules! impl_query_filter_tuple { self.archetype_fetch(archetype_index) } - fn init_state(world: &mut World) -> Self::State { - Or(($($filter::init_state(world),)*)) - } - fn update_component_access(state: &Self::State, access: &mut FilteredAccess) { let ($($filter,)*) = &state.0; @@ -490,6 +490,10 @@ macro_rules! impl_tick_filter { type ReadOnly = Self; type State = ComponentId; + fn init(world: &mut World) -> Self::State { + world.init_component::() + } + fn shrink<'wlong: 'wshort, 'wshort>(item: super::QueryItem<'wlong, Self>) -> super::QueryItem<'wshort, Self> { item } @@ -576,10 +580,6 @@ macro_rules! impl_tick_filter { self.archetype_fetch(archetype_index) } - fn init_state(world: &mut World) -> Self::State { - world.init_component::() - } - #[inline] fn update_component_access(state: &Self::State, access: &mut FilteredAccess) { if access.access().has_write(*state) { diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 0661ea2658331..42eeac0c7c98c 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -40,8 +40,8 @@ impl FromWorld for QueryState { impl QueryState { /// Creates a new [`QueryState`] from a given [`World`] and inherits the result of `world.id()`. pub fn new(world: &mut World) -> Self { - let fetch_state = Q::Fetch::init_state(world); - let filter_state = F::Fetch::init_state(world); + let fetch_state = Q::init(world); + let filter_state = F::init(world); let mut component_access = FilteredAccess::default(); Q::Fetch::update_component_access(&fetch_state, &mut component_access);