Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert queries to a more general form #10037

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/bevy_ecs/macros/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ pub fn derive_world_query_impl(input: TokenStream) -> TokenStream {
}
}

fn new_state(components: &#path::component::Components) -> Option<#state_struct_name #user_ty_generics> {
Some(#state_struct_name {
#(#named_field_idents: <#field_types>::new_state(components)?,)*
})
}

fn matches_component_set(state: &Self::State, _set_contains_id: &impl Fn(#path::component::ComponentId) -> bool) -> bool {
true #(&& <#field_types>::matches_component_set(&state.#named_field_idents, _set_contains_id))*
}
Expand Down
31 changes: 31 additions & 0 deletions crates/bevy_ecs/src/query/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,32 @@ impl<T: SparseSetIndex> Access<T> {
&& other.writes.is_disjoint(&self.reads_and_writes)
}

/// [`Access`] is subset of `other` access.
pub fn is_subset(&self, other: &Access<T>) -> bool {
if self.writes_all {
return other.writes_all;
}

if other.writes_all {
return true;
}

if self.reads_all {
return other.reads_all || other.writes_all;
}

if other.reads_all {
return self.has_any_write();
}

let reads = self
.reads_and_writes
.difference(&self.writes)
.collect::<FixedBitSet>();

reads.is_subset(&other.reads_and_writes) && self.writes.is_subset(&other.writes)
}

/// Returns a vector of elements that the access and `other` cannot access at the same time.
pub fn get_conflicts(&self, other: &Access<T>) -> Vec<T> {
let mut conflicts = FixedBitSet::default();
Expand Down Expand Up @@ -374,6 +400,11 @@ impl<T: SparseSetIndex> FilteredAccess<T> {
})
}

/// access is compatible with other access. This does not take into account the filtered access.
pub fn is_subset(&self, other: &FilteredAccess<T>) -> bool {
self.access.is_subset(&other.access)
}

/// Returns a vector of elements that this and `other` cannot access at the same time.
pub fn get_conflicts(&self, other: &FilteredAccess<T>) -> Vec<T> {
if !self.is_compatible(other) {
Expand Down
54 changes: 53 additions & 1 deletion crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
archetype::{Archetype, ArchetypeComponentId},
change_detection::{Ticks, TicksMut},
component::{Component, ComponentId, ComponentStorage, StorageType, Tick},
component::{Component, ComponentId, ComponentStorage, Components, StorageType, Tick},
entity::Entity,
query::{Access, DebugCheckedUnwrap, FilteredAccess},
storage::{ComponentSparseSet, Table, TableRow},
Expand Down Expand Up @@ -440,6 +440,10 @@ pub unsafe trait WorldQuery {
/// Creates and initializes a [`State`](WorldQuery::State) for this [`WorldQuery`] type.
fn init_state(world: &mut World) -> Self::State;

/// Creates a [`State`](WorldQuery::State) for this [`WorldQuery`] type. This should return
/// the same value as [`init_state`](Self::init_state).
fn new_state(components: &Components) -> Option<Self::State>;
hymm marked this conversation as resolved.
Show resolved Hide resolved

/// Returns `true` if this query matches a set of components. Otherwise, returns `false`.
fn matches_component_set(
state: &Self::State,
Expand Down Expand Up @@ -515,6 +519,10 @@ unsafe impl WorldQuery for Entity {

fn init_state(_world: &mut World) {}

fn new_state(_components: &Components) -> Option<()> {
Some(())
}

fn matches_component_set(
_state: &Self::State,
_set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -595,6 +603,10 @@ unsafe impl WorldQuery for EntityRef<'_> {

fn init_state(_world: &mut World) {}

fn new_state(_components: &Components) -> Option<()> {
Some(())
}

fn matches_component_set(
_state: &Self::State,
_set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -675,6 +687,10 @@ unsafe impl<'a> WorldQuery for EntityMut<'a> {

fn init_state(_world: &mut World) {}

fn new_state(_components: &Components) -> Option<()> {
Some(())
}

fn matches_component_set(
_state: &Self::State,
_set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -815,6 +831,10 @@ unsafe impl<T: Component> WorldQuery for &T {
world.init_component::<T>()
}

fn new_state(components: &Components) -> Option<ComponentId> {
components.component_id::<T>()
}

fn matches_component_set(
&state: &ComponentId,
set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -976,6 +996,10 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> {
world.init_component::<T>()
}

fn new_state(components: &Components) -> Option<ComponentId> {
components.component_id::<T>()
}

fn matches_component_set(
&state: &ComponentId,
set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -1137,6 +1161,10 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
world.init_component::<T>()
}

fn new_state(components: &Components) -> Option<ComponentId> {
components.component_id::<T>()
}

fn matches_component_set(
&state: &ComponentId,
set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -1249,6 +1277,10 @@ unsafe impl<T: WorldQuery> WorldQuery for Option<T> {
T::init_state(world)
}

fn new_state(components: &Components) -> Option<T::State> {
T::new_state(components)
}

fn matches_component_set(
_state: &T::State,
_set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -1384,6 +1416,10 @@ unsafe impl<T: Component> WorldQuery for Has<T> {
world.init_component::<T>()
}

fn new_state(components: &Components) -> Option<ComponentId> {
components.component_id::<T>()
}

fn matches_component_set(
_state: &Self::State,
_set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -1480,6 +1516,10 @@ macro_rules! impl_tuple_fetch {
($($name::init_state(_world),)*)
}

fn new_state(_components: &Components) -> Option<Self::State> {
Some(($($name::new_state(_components)?,)*))
}

fn matches_component_set(state: &Self::State, _set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
let ($($name,)*) = state;
true $(&& $name::matches_component_set($name, _set_contains_id))*
Expand Down Expand Up @@ -1603,6 +1643,10 @@ macro_rules! impl_anytuple_fetch {
($($name::init_state(_world),)*)
}

fn new_state(_components: &Components) -> Option<Self::State> {
Some(($($name::new_state(_components)?,)*))
}

fn matches_component_set(_state: &Self::State, _set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
let ($($name,)*) = _state;
false $(|| $name::matches_component_set($name, _set_contains_id))*
Expand Down Expand Up @@ -1677,6 +1721,10 @@ unsafe impl<Q: WorldQuery> WorldQuery for NopWorldQuery<Q> {
Q::init_state(world)
}

fn new_state(components: &Components) -> Option<Self::State> {
Q::new_state(components)
}

fn matches_component_set(
state: &Self::State,
set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -1740,6 +1788,10 @@ unsafe impl<T: ?Sized> WorldQuery for PhantomData<T> {

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

fn new_state(_components: &Components) -> Option<Self::State> {
Some(())
}

fn matches_component_set(
_state: &Self::State,
_set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down
18 changes: 17 additions & 1 deletion crates/bevy_ecs/src/query/filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
archetype::{Archetype, ArchetypeComponentId},
component::{Component, ComponentId, ComponentStorage, StorageType, Tick},
component::{Component, ComponentId, ComponentStorage, Components, StorageType, Tick},
entity::Entity,
query::{Access, DebugCheckedUnwrap, FilteredAccess, WorldQuery},
storage::{Column, ComponentSparseSet, Table, TableRow},
Expand Down Expand Up @@ -105,6 +105,10 @@ unsafe impl<T: Component> WorldQuery for With<T> {
world.init_component::<T>()
}

fn new_state(components: &Components) -> Option<Self::State> {
components.component_id::<T>()
}

fn matches_component_set(
&id: &ComponentId,
set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -206,6 +210,10 @@ unsafe impl<T: Component> WorldQuery for Without<T> {
world.init_component::<T>()
}

fn new_state(components: &Components) -> Option<Self::State> {
components.component_id::<T>()
}

fn matches_component_set(
&id: &ComponentId,
set_contains_id: &impl Fn(ComponentId) -> bool,
Expand Down Expand Up @@ -370,6 +378,10 @@ macro_rules! impl_query_filter_tuple {
($($filter::init_state(world),)*)
}

fn new_state(components: &Components) -> Option<Self::State> {
Some(($($filter::new_state(components)?,)*))
}

fn matches_component_set(_state: &Self::State, _set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
let ($($filter,)*) = _state;
false $(|| $filter::matches_component_set($filter, _set_contains_id))*
Expand Down Expand Up @@ -533,6 +545,10 @@ macro_rules! impl_tick_filter {
world.init_component::<T>()
}

fn new_state(components: &Components) -> Option<ComponentId> {
components.component_id::<T>()
}

fn matches_component_set(&id: &ComponentId, set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
set_contains_id(id)
}
Expand Down
Loading