Skip to content

implement proof of concept query filter Predicate #14715

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ pub mod prelude {
entity::{Entity, EntityMapper},
event::{Event, EventMutator, EventReader, EventWriter, Events},
observer::{Observer, Trigger},
query::{Added, AnyOf, Changed, Has, Or, QueryBuilder, QueryState, With, Without},
query::{
Added, AnyOf, Changed, Has, Or, Predicate, PredicateFilter, QueryBuilder, QueryState,
With, Without,
},
removal_detection::RemovedComponents,
schedule::{
apply_deferred, common_conditions::*, Condition, IntoSystemConfigs, IntoSystemSet,
Expand Down
33 changes: 32 additions & 1 deletion crates/bevy_ecs/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod fetch;
mod filter;
mod iter;
mod par_iter;
mod predicate;
mod state;
mod world_query;

Expand All @@ -18,6 +19,7 @@ pub use fetch::*;
pub use filter::*;
pub use iter::*;
pub use par_iter::*;
pub use predicate::*;
pub use state::*;
pub use world_query::*;

Expand Down Expand Up @@ -71,7 +73,7 @@ impl<T> DebugCheckedUnwrap for Option<T> {
mod tests {
use bevy_ecs_macros::{QueryData, QueryFilter};

use crate::prelude::{AnyOf, Changed, Entity, Or, QueryState, With, Without};
use crate::prelude::{AnyOf, Changed, Entity, Or, Predicate, QueryState, With, Without};
use crate::query::{ArchetypeFilter, Has, QueryCombinationIter, ReadOnlyQueryData};
use crate::schedule::{IntoSystemConfigs, Schedule};
use crate::system::{IntoSystem, Query, System, SystemState};
Expand All @@ -81,6 +83,8 @@ mod tests {
use std::fmt::Debug;
use std::hash::Hash;

use super::PredicateFilter;

#[derive(Component, Debug, Hash, Eq, PartialEq, Clone, Copy, PartialOrd, Ord)]
struct A(usize);
#[derive(Component, Debug, Hash, Eq, PartialEq, Clone, Copy)]
Expand Down Expand Up @@ -757,4 +761,31 @@ mod tests {
let values = world.query::<&B>().iter(&world).collect::<Vec<&B>>();
assert_eq!(values, vec![&B(2)]);
}

#[derive(Component, Debug, Clone, Copy)]
pub struct E(usize);

#[derive(Debug, Clone, Component, Default)]
pub struct NonZeroE;

impl PredicateFilter for NonZeroE {
type Data = &'static E;
fn filter_predicate(item: crate::query::QueryItem<Self::Data>) -> bool {
item.0 != 0
}
}

#[test]
fn query_predicate() {
let mut world = World::new();
world.spawn((A(1), E(0)));
world.spawn((A(2), E(1)));
world.spawn((A(3), E(2)));

let values = world
.query_filtered::<&A, Predicate<NonZeroE>>()
.iter(&mut world)
.collect::<Vec<_>>();
assert_eq!(values, vec![&A(2), &A(3)]);
}
}
92 changes: 92 additions & 0 deletions crates/bevy_ecs/src/query/predicate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use std::marker::PhantomData;

use super::{QueryData, QueryFilter, QueryItem, WorldQuery};

pub trait PredicateFilter {
type Data: QueryData;
fn filter_predicate(item: QueryItem<Self::Data>) -> bool;
}

pub struct Predicate<P>(PhantomData<P>);

unsafe impl<D: QueryData, P: PredicateFilter<Data = D>> WorldQuery for Predicate<P> {
type Item<'a> = D::Item<'a>;

type Fetch<'a> = D::Fetch<'a>;

type State = D::State;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
D::shrink(item)
}

unsafe fn init_fetch<'w>(
world: crate::world::unsafe_world_cell::UnsafeWorldCell<'w>,
state: &Self::State,
last_run: crate::component::Tick,
this_run: crate::component::Tick,
) -> Self::Fetch<'w> {
D::init_fetch(world, state, last_run, this_run)
}

const IS_DENSE: bool = D::IS_DENSE;

unsafe fn set_archetype<'w>(
fetch: &mut Self::Fetch<'w>,
state: &Self::State,
archetype: &'w crate::archetype::Archetype,
table: &'w crate::storage::Table,
) {
D::set_archetype(fetch, state, archetype, table)
}

unsafe fn set_table<'w>(
fetch: &mut Self::Fetch<'w>,
state: &Self::State,
table: &'w crate::storage::Table,
) {
D::set_table(fetch, state, table)
}

unsafe fn fetch<'w>(
fetch: &mut Self::Fetch<'w>,
entity: crate::entity::Entity,
table_row: crate::storage::TableRow,
) -> Self::Item<'w> {
D::fetch(fetch, entity, table_row)
}

fn update_component_access(
state: &Self::State,
access: &mut super::FilteredAccess<crate::component::ComponentId>,
) {
D::update_component_access(state, access)
}

fn init_state(world: &mut crate::prelude::World) -> Self::State {
D::init_state(world)
}

fn get_state(components: &crate::component::Components) -> Option<Self::State> {
D::get_state(components)
}

fn matches_component_set(
state: &Self::State,
set_contains_id: &impl Fn(crate::component::ComponentId) -> bool,
) -> bool {
D::matches_component_set(state, set_contains_id)
}
}

impl<D: QueryData, P: PredicateFilter<Data = D>> QueryFilter for Predicate<P> {
const IS_ARCHETYPAL: bool = false;

unsafe fn filter_fetch(
fetch: &mut Self::Fetch<'_>,
entity: crate::entity::Entity,
table_row: crate::storage::TableRow,
) -> bool {
P::filter_predicate(Self::fetch(fetch, entity, table_row))
}
}
Loading