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

[Merged by Bors] - Remove with bundle filter #2623

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub mod prelude {
change_detection::DetectChanges,
entity::Entity,
event::{EventReader, EventWriter},
query::{Added, ChangeTrackers, Changed, Or, QueryState, With, WithBundle, Without},
query::{Added, ChangeTrackers, Changed, Or, QueryState, With, Without},
schedule::{
AmbiguitySetLabel, ExclusiveSystemDescriptorCoercion, ParallelSystemDescriptorCoercion,
RunCriteria, RunCriteriaDescriptorCoercion, RunCriteriaLabel, RunCriteriaPiping,
Expand Down
101 changes: 0 additions & 101 deletions crates/bevy_ecs/src/query/filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
archetype::{Archetype, ArchetypeComponentId},
bundle::Bundle,
component::{Component, ComponentId, ComponentTicks, StorageType},
entity::Entity,
query::{Access, Fetch, FetchState, FilteredAccess, WorldQuery},
Expand Down Expand Up @@ -282,106 +281,6 @@ impl<'w, 's, T: Component> Fetch<'w, 's> for WithoutFetch<T> {
}
}

pub struct WithBundle<T: Bundle>(PhantomData<T>);

impl<T: Bundle> WorldQuery for WithBundle<T> {
type Fetch = WithBundleFetch<T>;
type State = WithBundleState<T>;
}

pub struct WithBundleFetch<T: Bundle> {
is_dense: bool,
marker: PhantomData<T>,
}

pub struct WithBundleState<T: Bundle> {
component_ids: Vec<ComponentId>,
is_dense: bool,
marker: PhantomData<T>,
}

// SAFETY: no component access or archetype component access
unsafe impl<T: Bundle> FetchState for WithBundleState<T> {
fn init(world: &mut World) -> Self {
let bundle_info = world.bundles.init_info::<T>(&mut world.components);
let components = &world.components;
Self {
component_ids: bundle_info.component_ids.clone(),
is_dense: !bundle_info.component_ids.iter().any(|id| unsafe {
components.get_info_unchecked(*id).storage_type() != StorageType::Table
}),
marker: PhantomData,
}
}

#[inline]
fn update_component_access(&self, access: &mut FilteredAccess<ComponentId>) {
for component_id in self.component_ids.iter().cloned() {
access.add_with(component_id);
}
}

#[inline]
fn update_archetype_component_access(
&self,
_archetype: &Archetype,
_access: &mut Access<ArchetypeComponentId>,
) {
}

fn matches_archetype(&self, archetype: &Archetype) -> bool {
self.component_ids.iter().all(|id| archetype.contains(*id))
}

fn matches_table(&self, table: &Table) -> bool {
self.component_ids.iter().all(|id| table.has_column(*id))
}
}

impl<'w, 's, T: Bundle> Fetch<'w, 's> for WithBundleFetch<T> {
type Item = bool;
type State = WithBundleState<T>;

unsafe fn init(
_world: &World,
state: &Self::State,
_last_change_tick: u32,
_change_tick: u32,
) -> Self {
Self {
is_dense: state.is_dense,
marker: PhantomData,
}
}

#[inline]
fn is_dense(&self) -> bool {
self.is_dense
}

#[inline]
unsafe fn set_table(&mut self, _state: &Self::State, _table: &Table) {}

#[inline]
unsafe fn set_archetype(
&mut self,
_state: &Self::State,
_archetype: &Archetype,
_tables: &Tables,
) {
}

#[inline]
unsafe fn archetype_fetch(&mut self, _archetype_index: usize) -> bool {
true
}

#[inline]
unsafe fn table_fetch(&mut self, _table_row: usize) -> bool {
true
}
}

/// A filter that tests if any of the given filters apply.
///
/// This is useful for example if a system with multiple components in a query only wants to run
Expand Down
89 changes: 47 additions & 42 deletions examples/ecs/query_bundle.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
use bevy::{log::LogPlugin, prelude::*};
// use bevy::{log::LogPlugin, prelude::*};

fn main() {
App::new()
.add_plugin(LogPlugin)
.add_startup_system(setup)
.add_system(log_names.label(LogNamesSystem))
.add_system(log_person_bundles.after(LogNamesSystem))
.run();
}
// fn main() {
// App::new()
// .add_plugin(LogPlugin)
// .add_startup_system(setup)
// .add_system(log_names.label(LogNamesSystem))
// .add_system(log_person_bundles.after(LogNamesSystem))
// .run();
// }

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
struct LogNamesSystem;
// #[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
// struct LogNamesSystem;

#[derive(Debug)]
struct Name(String);
// #[derive(Debug)]
// struct Name(String);

#[derive(Debug)]
struct Age(usize);
// #[derive(Debug)]
// struct Age(usize);

#[derive(Debug, Bundle)]
struct PersonBundle {
name: Name,
age: Age,
}
// #[derive(Debug, Bundle)]
// struct PersonBundle {
// name: Name,
// age: Age,
// }

/// Sets up two entities, one with a [Name] component as part of a bundle,
/// and one entity with [Name] only.
fn setup(mut commands: Commands) {
commands.spawn().insert(Name("Steve".to_string()));
commands.spawn().insert_bundle(PersonBundle {
name: Name("Bob".to_string()),
age: Age(40),
});
}
// /// Sets up two entities, one with a [Name] component as part of a bundle,
// /// and one entity with [Name] only.
// fn setup(mut commands: Commands) {
// commands.spawn().insert(Name("Steve".to_string()));
// commands.spawn().insert_bundle(PersonBundle {
// name: Name("Bob".to_string()),
// age: Age(40),
// });
// }

fn log_names(query: Query<&Name>) {
info!("Log all entities with `Name` component");
// this will necessarily have to print both components.
for name in query.iter() {
info!("{:?}", name);
}
}
fn log_person_bundles(query: Query<&Name, WithBundle<PersonBundle>>) {
info!("Log `Name` components from entities that have all components in `PersonBundle`.");
// this should only print `Name("Bob")`.
for name in query.iter() {
info!("{:?}", name);
}
// fn log_names(query: Query<&Name>) {
// info!("Log all entities with `Name` component");
// // this will necessarily have to print both components.
// for name in query.iter() {
// info!("{:?}", name);
// }
// }
// fn log_person_bundles(query: Query<&Name, WithBundle<PersonBundle>>) {
// info!("Log `Name` components from entities that have all components in `PersonBundle`.");
// // this should only print `Name("Bob")`.
// for name in query.iter() {
// info!("{:?}", name);
// }
// }

fn main() {
// See https://github.com/bevyengine/bevy/issues/2620
println!("Temporarily removed until querying bundles is more decided upon.")
}