Skip to content

Commit

Permalink
Update init_state to WorldQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
james7132 committed Jun 19, 2022
1 parent 42abc95 commit 52a217e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 52 deletions.
63 changes: 29 additions & 34 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w, _State = Self::State> {
type ReadOnly: ReadOnlyWorldQuery<State = Self::State>;
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>;
}
Expand Down Expand Up @@ -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<ComponentId>);
Expand All @@ -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
}
Expand Down Expand Up @@ -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<ComponentId>) {}

fn update_archetype_component_access(
Expand All @@ -567,6 +567,10 @@ unsafe impl<T: Component> WorldQuery for &T {
type ReadOnly = Self;
type State = ComponentId;

fn init(world: &mut World) -> Self::State {
world.init_component::<T>()
}

fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> {
item
}
Expand Down Expand Up @@ -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::<T>()
}

fn update_component_access(state: &Self::State, access: &mut FilteredAccess<ComponentId>) {
assert!(
!access.access().has_write(*state),
Expand Down Expand Up @@ -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::<T>()
}

fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> {
item
}
Expand Down Expand Up @@ -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::<T>()
}

fn update_component_access(state: &Self::State, access: &mut FilteredAccess<ComponentId>) {
assert!(
!access.access().has_read(*state),
Expand Down Expand Up @@ -911,6 +911,10 @@ unsafe impl<T: WorldQuery> WorldQuery for Option<T> {
type ReadOnly = Option<T::ReadOnly>;
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)
}
Expand Down Expand Up @@ -993,10 +997,6 @@ unsafe impl<'w, T: Fetch<'w>> Fetch<'w> for OptionFetch<T> {
}
}

fn init_state(world: &mut World) -> Self::State {
T::init_state(world)
}

fn update_component_access(state: &Self::State, access: &mut FilteredAccess<ComponentId>) {
// We don't want to add the `with`/`without` of `T` as `Option<T>` will match things regardless of
// `T`'s filters. for example `Query<(Option<&U>, &mut V)>` will match every entity with a `V` component
Expand Down Expand Up @@ -1094,6 +1094,10 @@ unsafe impl<T: Component> WorldQuery for ChangeTrackers<T> {
type ReadOnly = Self;
type State = ComponentId;

fn init(world: &mut World) -> Self::State {
world.init_component::<T>()
}

fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> {
item
}
Expand Down Expand Up @@ -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::<T>()
}

fn update_component_access(state: &Self::State, access: &mut FilteredAccess<ComponentId>) {
assert!(
!access.access().has_write(*state),
Expand Down Expand Up @@ -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<ComponentId>) {
let ($($name,)*) = _state;
$($name::update_component_access($name, _access);)*
Expand All @@ -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;
($(
Expand Down Expand Up @@ -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<ComponentId>) {
let ($($name,)*) = &state.0;

Expand Down Expand Up @@ -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;
($(
Expand Down Expand Up @@ -1578,11 +1578,6 @@ unsafe impl<'w, State: Send + Sync> Fetch<'w> for NopFetch<State> {
#[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<ComponentId>) {}

fn update_archetype_component_access(
Expand Down
32 changes: 16 additions & 16 deletions crates/bevy_ecs/src/query/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ unsafe impl<T: Component> WorldQuery for With<T> {
type ReadOnly = Self;
type State = ComponentId;

fn init(world: &mut World) -> Self::State {
world.init_component::<T>()
}

#[allow(clippy::semicolon_if_nothing_returned)]
fn shrink<'wlong: 'wshort, 'wshort>(
item: super::QueryItem<'wlong, Self>,
Expand Down Expand Up @@ -111,10 +115,6 @@ unsafe impl<'w, T: Component> Fetch<'w> for WithFetch<T> {
#[inline]
unsafe fn table_fetch(&mut self, _table_row: usize) {}

fn init_state(world: &mut World) -> Self::State {
world.init_component::<T>()
}

#[inline]
fn update_component_access(state: &Self::State, access: &mut FilteredAccess<ComponentId>) {
access.add_with(*state);
Expand Down Expand Up @@ -180,6 +180,10 @@ unsafe impl<T: Component> WorldQuery for Without<T> {
type ReadOnly = Self;
type State = ComponentId;

fn init(world: &mut World) -> Self::State {
world.init_component::<T>()
}

#[allow(clippy::semicolon_if_nothing_returned)]
fn shrink<'wlong: 'wshort, 'wshort>(
item: super::QueryItem<'wlong, Self>,
Expand Down Expand Up @@ -242,10 +246,6 @@ unsafe impl<'w, T: Component> Fetch<'w> for WithoutFetch<T> {
#[inline]
unsafe fn table_fetch(&mut self, _table_row: usize) {}

fn init_state(world: &mut World) -> Self::State {
world.init_component::<T>()
}

#[inline]
fn update_component_access(state: &Self::State, access: &mut FilteredAccess<ComponentId>) {
access.add_without(*state);
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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<ComponentId>) {
let ($($filter,)*) = &state.0;

Expand Down Expand Up @@ -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::<T>()
}

fn shrink<'wlong: 'wshort, 'wshort>(item: super::QueryItem<'wlong, Self>) -> super::QueryItem<'wshort, Self> {
item
}
Expand Down Expand Up @@ -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::<T>()
}

#[inline]
fn update_component_access(state: &Self::State, access: &mut FilteredAccess<ComponentId>) {
if access.access().has_write(*state) {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl<Q: WorldQuery, F: WorldQuery> FromWorld for QueryState<Q, F> {
impl<Q: WorldQuery, F: WorldQuery> QueryState<Q, F> {
/// 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);
Expand Down

0 comments on commit 52a217e

Please sign in to comment.