Skip to content

Commit

Permalink
Rename WorldQueryFilter to QueryFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
taizu-jin committed Nov 28, 2023
1 parent 5a3ad48 commit 5dc8686
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 111 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ pub fn derive_world_query_data(input: TokenStream) -> TokenStream {
derive_world_query_data_impl(input)
}

/// Implement `WorldQueryFilter` to use a struct as a filter parameter in a query
#[proc_macro_derive(WorldQueryFilter, attributes(world_query_filter))]
/// Implement `QueryFilter` to use a struct as a filter parameter in a query
#[proc_macro_derive(QueryFilter, attributes(query_filter))]
pub fn derive_world_query_filter(input: TokenStream) -> TokenStream {
derive_world_query_filter_impl(input)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/macros/src/world_query_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn derive_world_query_filter_impl(input: TokenStream) -> TokenStream {
);

let filter_impl = quote! {
impl #user_impl_generics #path::query::WorldQueryFilter
impl #user_impl_generics #path::query::QueryFilter
for #struct_name #user_ty_generics #user_where_clauses {
const IS_ARCHETYPAL: bool = true #(&& <#field_types>::IS_ARCHETYPAL)*;

Expand Down Expand Up @@ -163,7 +163,7 @@ pub fn derive_world_query_filter_impl(input: TokenStream) -> TokenStream {

fn assert_filter<T>()
where
T: #path::query::WorldQueryFilter,
T: #path::query::QueryFilter,
{
}

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod tests {
change_detection::Ref,
component::{Component, ComponentId},
entity::Entity,
query::{Added, Changed, FilteredAccess, With, Without, WorldQueryFilter},
query::{Added, Changed, FilteredAccess, QueryFilter, With, Without},
system::Resource,
world::{EntityRef, Mut, World},
};
Expand Down Expand Up @@ -903,7 +903,7 @@ mod tests {
}
}

fn get_filtered<F: WorldQueryFilter>(world: &mut World) -> Vec<Entity> {
fn get_filtered<F: QueryFilter>(world: &mut World) -> Vec<Entity> {
world
.query_filtered::<Entity, F>()
.iter(world)
Expand Down Expand Up @@ -986,7 +986,7 @@ mod tests {
}
}

fn get_filtered<F: WorldQueryFilter>(world: &mut World) -> Vec<Entity> {
fn get_filtered<F: QueryFilter>(world: &mut World) -> Vec<Entity> {
world
.query_filtered::<Entity, F>()
.iter(world)
Expand Down
36 changes: 18 additions & 18 deletions crates/bevy_ecs/src/query/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ use std::{cell::UnsafeCell, marker::PhantomData};
/// [`With`] and [`Without`] filters can be applied to check if the queried entity does or does not contain a particular component.
/// - **Change detection filters.**
/// [`Added`] and [`Changed`] filters can be applied to detect component changes to an entity.
/// - **`WorldQueryFilter` tuples.**
/// If every element of a tuple implements `WorldQueryFilter`, then the tuple itself also implements the same trait.
/// - **`QueryFilter` tuples.**
/// If every element of a tuple implements `QueryFilter`, then the tuple itself also implements the same trait.
/// This enables a single `Query` to filter over multiple conditions.
/// Due to the current lack of variadic generics in Rust, the trait has been implemented for tuples from 0 to 15 elements,
/// but nesting of tuples allows infinite `WorldQueryFilter`s.
/// but nesting of tuples allows infinite `QueryFilter`s.
/// - **Filter disjunction operator.**
/// By default, tuples compose query filters in such a way that all conditions must be satisfied to generate a query item for a given entity.
/// Wrapping a tuple inside an [`Or`] operator will relax the requirement to just one condition.
///
/// Implementing the trait manually can allow for a fundamentally new type of behavior.
///
/// Query design can be easily structured by deriving `WorldQueryFilter` for custom types.
/// Despite the added complexity, this approach has several advantages over using `WorldQueryFilter` tuples.
/// Query design can be easily structured by deriving `QueryFilter` for custom types.
/// Despite the added complexity, this approach has several advantages over using `QueryFilter` tuples.
/// The most relevant improvements are:
///
/// - Reusability across multiple systems.
/// - Filters can be composed together to create a more complex filter.
///
/// This trait can only be derived for structs if each field also implements `WorldQueryFilter`.
/// This trait can only be derived for structs if each field also implements `QueryFilter`.
///
/// ```
/// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::{query::WorldQueryFilter, component::Component};
/// # use bevy_ecs::{query::QueryFilter, component::Component};
/// #
/// # #[derive(Component)]
/// # struct ComponentA;
Expand All @@ -52,7 +52,7 @@ use std::{cell::UnsafeCell, marker::PhantomData};
/// # #[derive(Component)]
/// # struct ComponentE;
/// #
/// #[derive(WorldQueryFilter)]
/// #[derive(QueryFilter)]
/// struct MyFilter<T: Component, P: Component> {
/// // Field names are not relevant, since they are never manually accessed.
/// with_a: With<ComponentA>,
Expand All @@ -77,7 +77,7 @@ use std::{cell::UnsafeCell, marker::PhantomData};
/// [`With`]: crate::query::With
/// [`Without`]: crate::query::Without

pub trait WorldQueryFilter: WorldQuery {
pub trait QueryFilter: WorldQuery {
/// Returns true if (and only if) this Filter relies strictly on archetypes to limit which
/// components are accessed by the Query.
///
Expand Down Expand Up @@ -199,7 +199,7 @@ unsafe impl<T: Component> WorldQuery for With<T> {
}
}

impl<T: Component> WorldQueryFilter for With<T> {
impl<T: Component> QueryFilter for With<T> {
const IS_ARCHETYPAL: bool = true;

#[inline(always)]
Expand Down Expand Up @@ -311,7 +311,7 @@ unsafe impl<T: Component> WorldQuery for Without<T> {
}
}

impl<T: Component> WorldQueryFilter for Without<T> {
impl<T: Component> QueryFilter for Without<T> {
const IS_ARCHETYPAL: bool = true;

#[inline(always)]
Expand Down Expand Up @@ -381,7 +381,7 @@ macro_rules! impl_query_filter_tuple {
/// This is sound because `update_component_access` and `update_archetype_component_access` adds accesses according to the implementations of all the subqueries.
/// `update_component_access` replace the filters with a disjunction where every element is a conjunction of the previous filters and the filters of one of the subqueries.
/// This is sound because `matches_component_set` returns a disjunction of the results of the subqueries' implementations.
unsafe impl<$($filter: WorldQueryFilter),*> WorldQuery for Or<($($filter,)*)> {
unsafe impl<$($filter: QueryFilter),*> WorldQuery for Or<($($filter,)*)> {
type Fetch<'w> = ($(OrFetch<'w, $filter>,)*);
type Item<'w> = bool;
type State = ($($filter::State,)*);
Expand Down Expand Up @@ -475,7 +475,7 @@ macro_rules! impl_query_filter_tuple {
}
}

impl<$($filter: WorldQueryFilter),*> WorldQueryFilter for Or<($($filter,)*)> {
impl<$($filter: QueryFilter),*> QueryFilter for Or<($($filter,)*)> {
const IS_ARCHETYPAL: bool = true $(&& $filter::IS_ARCHETYPAL)*;

#[inline(always)]
Expand All @@ -496,7 +496,7 @@ macro_rules! impl_tuple_world_query_filter {
#[allow(non_snake_case)]
#[allow(clippy::unused_unit)]

impl<$($name: WorldQueryFilter),*> WorldQueryFilter for ($($name,)*) {
impl<$($name: QueryFilter),*> QueryFilter for ($($name,)*) {
const IS_ARCHETYPAL: bool = true $(&& $name::IS_ARCHETYPAL)*;

#[inline(always)]
Expand Down Expand Up @@ -674,7 +674,7 @@ unsafe impl<T: Component> WorldQuery for Added<T> {
}
}

impl<T: Component> WorldQueryFilter for Added<T> {
impl<T: Component> QueryFilter for Added<T> {
const IS_ARCHETYPAL: bool = false;
#[inline(always)]
unsafe fn filter_fetch(
Expand Down Expand Up @@ -850,7 +850,7 @@ unsafe impl<T: Component> WorldQuery for Changed<T> {
}
}

impl<T: Component> WorldQueryFilter for Changed<T> {
impl<T: Component> QueryFilter for Changed<T> {
const IS_ARCHETYPAL: bool = false;

#[inline(always)]
Expand All @@ -868,14 +868,14 @@ impl<T: Component> WorldQueryFilter for Changed<T> {
/// This is needed to implement [`ExactSizeIterator`] for
/// [`QueryIter`](crate::query::QueryIter) that contains archetype-level filters.
///
/// The trait must only be implemented for filters where its corresponding [`WorldQueryFilter::IS_ARCHETYPAL`]
/// The trait must only be implemented for filters where its corresponding [`QueryFilter::IS_ARCHETYPAL`]
/// is [`prim@true`]. As such, only the [`With`] and [`Without`] filters can implement the trait.
/// [Tuples](prim@tuple) and [`Or`] filters are automatically implemented with the trait only if its containing types
/// also implement the same trait.
///
/// [`Added`] and [`Changed`] works with entities, and therefore are not archetypal. As such
/// they do not implement [`ArchetypeFilter`].
pub trait ArchetypeFilter: WorldQueryFilter {}
pub trait ArchetypeFilter: QueryFilter {}

impl<T: Component> ArchetypeFilter for With<T> {}
impl<T: Component> ArchetypeFilter for Without<T> {}
Expand Down
36 changes: 17 additions & 19 deletions crates/bevy_ecs/src/query/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ use crate::{
};
use std::{borrow::Borrow, iter::FusedIterator, mem::MaybeUninit};

use super::{QueryData, ReadOnlyQueryData, WorldQueryFilter};
use super::{QueryData, QueryFilter, ReadOnlyQueryData};

/// An [`Iterator`] over query results of a [`Query`](crate::system::Query).
///
/// This struct is created by the [`Query::iter`](crate::system::Query::iter) and
/// [`Query::iter_mut`](crate::system::Query::iter_mut) methods.
pub struct QueryIter<'w, 's, Q: QueryData, F: WorldQueryFilter> {
pub struct QueryIter<'w, 's, Q: QueryData, F: QueryFilter> {
tables: &'w Tables,
archetypes: &'w Archetypes,
query_state: &'s QueryState<Q, F>,
cursor: QueryIterationCursor<'w, 's, Q, F>,
}

impl<'w, 's, Q: QueryData, F: WorldQueryFilter> QueryIter<'w, 's, Q, F> {
impl<'w, 's, Q: QueryData, F: QueryFilter> QueryIter<'w, 's, Q, F> {
/// # Safety
/// - `world` must have permission to access any of the components registered in `query_state`.
/// - `world` must be the same one used to initialize `query_state`.
Expand All @@ -41,7 +41,7 @@ impl<'w, 's, Q: QueryData, F: WorldQueryFilter> QueryIter<'w, 's, Q, F> {
}
}

impl<'w, 's, Q: QueryData, F: WorldQueryFilter> Iterator for QueryIter<'w, 's, Q, F> {
impl<'w, 's, Q: QueryData, F: QueryFilter> Iterator for QueryIter<'w, 's, Q, F> {
type Item = Q::Item<'w>;

#[inline(always)]
Expand All @@ -64,15 +64,15 @@ impl<'w, 's, Q: QueryData, F: WorldQueryFilter> Iterator for QueryIter<'w, 's, Q
}

// This is correct as [`QueryIter`] always returns `None` once exhausted.
impl<'w, 's, Q: QueryData, F: WorldQueryFilter> FusedIterator for QueryIter<'w, 's, Q, F> {}
impl<'w, 's, Q: QueryData, F: QueryFilter> FusedIterator for QueryIter<'w, 's, Q, F> {}

/// An [`Iterator`] over the query items generated from an iterator of [`Entity`]s.
///
/// Items are returned in the order of the provided iterator.
/// Entities that don't match the query are skipped.
///
/// This struct is created by the [`Query::iter_many`](crate::system::Query::iter_many) and [`Query::iter_many_mut`](crate::system::Query::iter_many_mut) methods.
pub struct QueryManyIter<'w, 's, Q: QueryData, F: WorldQueryFilter, I: Iterator>
pub struct QueryManyIter<'w, 's, Q: QueryData, F: QueryFilter, I: Iterator>
where
I::Item: Borrow<Entity>,
{
Expand All @@ -85,7 +85,7 @@ where
query_state: &'s QueryState<Q, F>,
}

impl<'w, 's, Q: QueryData, F: WorldQueryFilter, I: Iterator> QueryManyIter<'w, 's, Q, F, I>
impl<'w, 's, Q: QueryData, F: QueryFilter, I: Iterator> QueryManyIter<'w, 's, Q, F, I>
where
I::Item: Borrow<Entity>,
{
Expand Down Expand Up @@ -181,7 +181,7 @@ where
}
}

impl<'w, 's, Q: ReadOnlyQueryData, F: WorldQueryFilter, I: Iterator> Iterator
impl<'w, 's, Q: ReadOnlyQueryData, F: QueryFilter, I: Iterator> Iterator
for QueryManyIter<'w, 's, Q, F, I>
where
I::Item: Borrow<Entity>,
Expand All @@ -201,7 +201,7 @@ where
}

// This is correct as [`QueryManyIter`] always returns `None` once exhausted.
impl<'w, 's, Q: ReadOnlyQueryData, F: WorldQueryFilter, I: Iterator> FusedIterator
impl<'w, 's, Q: ReadOnlyQueryData, F: QueryFilter, I: Iterator> FusedIterator
for QueryManyIter<'w, 's, Q, F, I>
where
I::Item: Borrow<Entity>,
Expand Down Expand Up @@ -271,16 +271,14 @@ where
/// [`Query`]: crate::system::Query
/// [`Query::iter_combinations`]: crate::system::Query::iter_combinations
/// [`Query::iter_combinations_mut`]: crate::system::Query::iter_combinations_mut
pub struct QueryCombinationIter<'w, 's, Q: QueryData, F: WorldQueryFilter, const K: usize> {
pub struct QueryCombinationIter<'w, 's, Q: QueryData, F: QueryFilter, const K: usize> {
tables: &'w Tables,
archetypes: &'w Archetypes,
query_state: &'s QueryState<Q, F>,
cursors: [QueryIterationCursor<'w, 's, Q, F>; K],
}

impl<'w, 's, Q: QueryData, F: WorldQueryFilter, const K: usize>
QueryCombinationIter<'w, 's, Q, F, K>
{
impl<'w, 's, Q: QueryData, F: QueryFilter, const K: usize> QueryCombinationIter<'w, 's, Q, F, K> {
/// # Safety
/// - `world` must have permission to access any of the components registered in `query_state`.
/// - `world` must be the same one used to initialize `query_state`.
Expand Down Expand Up @@ -385,7 +383,7 @@ impl<'w, 's, Q: QueryData, F: WorldQueryFilter, const K: usize>
// Iterator type is intentionally implemented only for read-only access.
// Doing so for mutable references would be unsound, because calling `next`
// multiple times would allow multiple owned references to the same data to exist.
impl<'w, 's, Q: ReadOnlyQueryData, F: WorldQueryFilter, const K: usize> Iterator
impl<'w, 's, Q: ReadOnlyQueryData, F: QueryFilter, const K: usize> Iterator
for QueryCombinationIter<'w, 's, Q, F, K>
{
type Item = [Q::Item<'w>; K];
Expand Down Expand Up @@ -428,7 +426,7 @@ impl<'w, 's, Q: ReadOnlyQueryData, F: WorldQueryFilter, const K: usize> Iterator
}
}

impl<'w, 's, Q: QueryData, F: WorldQueryFilter> ExactSizeIterator for QueryIter<'w, 's, Q, F>
impl<'w, 's, Q: QueryData, F: QueryFilter> ExactSizeIterator for QueryIter<'w, 's, Q, F>
where
F: ArchetypeFilter,
{
Expand All @@ -438,12 +436,12 @@ where
}

// This is correct as [`QueryCombinationIter`] always returns `None` once exhausted.
impl<'w, 's, Q: ReadOnlyQueryData, F: WorldQueryFilter, const K: usize> FusedIterator
impl<'w, 's, Q: ReadOnlyQueryData, F: QueryFilter, const K: usize> FusedIterator
for QueryCombinationIter<'w, 's, Q, F, K>
{
}

struct QueryIterationCursor<'w, 's, Q: QueryData, F: WorldQueryFilter> {
struct QueryIterationCursor<'w, 's, Q: QueryData, F: QueryFilter> {
table_id_iter: std::slice::Iter<'s, TableId>,
archetype_id_iter: std::slice::Iter<'s, ArchetypeId>,
table_entities: &'w [Entity],
Expand All @@ -456,7 +454,7 @@ struct QueryIterationCursor<'w, 's, Q: QueryData, F: WorldQueryFilter> {
current_row: usize,
}

impl<Q: QueryData, F: WorldQueryFilter> Clone for QueryIterationCursor<'_, '_, Q, F> {
impl<Q: QueryData, F: QueryFilter> Clone for QueryIterationCursor<'_, '_, Q, F> {
fn clone(&self) -> Self {
Self {
table_id_iter: self.table_id_iter.clone(),
Expand All @@ -471,7 +469,7 @@ impl<Q: QueryData, F: WorldQueryFilter> Clone for QueryIterationCursor<'_, '_, Q
}
}

impl<'w, 's, Q: QueryData, F: WorldQueryFilter> QueryIterationCursor<'w, 's, Q, F> {
impl<'w, 's, Q: QueryData, F: QueryFilter> QueryIterationCursor<'w, 's, Q, F> {
const IS_DENSE: bool = Q::IS_DENSE && F::IS_DENSE;

unsafe fn init_empty(
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_ecs/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod state;
mod world_query;

pub use access::*;
pub use bevy_ecs_macros::{QueryData, WorldQueryFilter};
pub use bevy_ecs_macros::{QueryData, QueryFilter};
pub use error::*;
pub use fetch::*;
pub use filter::*;
Expand Down Expand Up @@ -67,7 +67,7 @@ impl<T> DebugCheckedUnwrap for Option<T> {

#[cfg(test)]
mod tests {
use bevy_ecs_macros::{QueryData, WorldQueryFilter};
use bevy_ecs_macros::{QueryData, QueryFilter};

use crate::prelude::{AnyOf, Changed, Entity, Or, QueryState, With, Without};
use crate::query::{ArchetypeFilter, Has, QueryCombinationIter, ReadOnlyQueryData};
Expand Down Expand Up @@ -618,11 +618,11 @@ mod tests {
}

{
#[derive(WorldQueryFilter)]
#[derive(QueryFilter)]
struct AOrBFilter {
a: Or<(With<A>, With<B>)>,
}
#[derive(WorldQueryFilter)]
#[derive(QueryFilter)]
struct NoSparseThatsSlow {
no: Without<Sparse>,
}
Expand All @@ -639,7 +639,7 @@ mod tests {
}

{
#[derive(WorldQueryFilter)]
#[derive(QueryFilter)]
struct CSparseFilter {
tuple_structs_pls: With<C>,
ugh: With<Sparse>,
Expand All @@ -657,7 +657,7 @@ mod tests {
}

{
#[derive(WorldQueryFilter)]
#[derive(QueryFilter)]
struct WithoutComps {
_1: Without<A>,
_2: Without<B>,
Expand Down
Loading

0 comments on commit 5dc8686

Please sign in to comment.