-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Split WorldQuery into WorldQueryData and WorldQueryFilter #9918
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
alice-i-cecile
merged 14 commits into
bevyengine:main
from
wainwrightmark:split_worldquery
Nov 28, 2023
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0c18048
Split WorldQuery into WorldQueryData and WorldQueryFilter
wainwrightmark d352f25
don't use internal imports in custom_query_param
wainwrightmark 11ff527
Removed T from AddedFetch and ChangedFetch
wainwrightmark 79e8e93
Made EntityRef use UnsafeWorldCell
wainwrightmark 4f673ae
Improved documentation in world_query and filter
wainwrightmark 93e2b4e
changed comment for EntityRef ReadOnlyWorldQueryData impl
wainwrightmark b76e1ba
Update crates/bevy_ecs/src/query/world_query.rs
wainwrightmark 63c8b89
Update crates/bevy_ecs/src/query/filter.rs
wainwrightmark 03b6353
Update crates/bevy_ecs/src/query/filter.rs
wainwrightmark ee559c4
Merge branch 'split_worldquery' of https://github.com/wainwrightmark/…
wainwrightmark 3d1283f
Merge branch 'main' into split_worldquery
wainwrightmark 4feac78
improved formatting in filter.rs
wainwrightmark 1e2f825
move shrink back to world_query
wainwrightmark 851358f
Merge remote-tracking branch 'origin/main' into split_worldquery
wainwrightmark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
use proc_macro2::Ident; | ||
use quote::quote; | ||
use syn::{Attribute, Fields, ImplGenerics, TypeGenerics, Visibility, WhereClause}; | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
pub(crate) fn item_struct( | ||
path: &syn::Path, | ||
fields: &Fields, | ||
derive_macro_call: &proc_macro2::TokenStream, | ||
struct_name: &Ident, | ||
visibility: &Visibility, | ||
item_struct_name: &Ident, | ||
field_types: &Vec<proc_macro2::TokenStream>, | ||
user_impl_generics_with_world: &ImplGenerics, | ||
field_attrs: &Vec<Vec<Attribute>>, | ||
field_visibilities: &Vec<Visibility>, | ||
field_idents: &Vec<proc_macro2::TokenStream>, | ||
user_ty_generics: &TypeGenerics, | ||
user_ty_generics_with_world: &TypeGenerics, | ||
user_where_clauses_with_world: Option<&WhereClause>, | ||
) -> proc_macro2::TokenStream { | ||
let item_attrs = quote!( | ||
#[doc = "Automatically generated [`WorldQuery`] item type for [`"] | ||
#[doc = stringify!(#struct_name)] | ||
#[doc = "`], returned when iterating over query results."] | ||
#[automatically_derived] | ||
); | ||
|
||
match fields { | ||
syn::Fields::Named(_) => quote! { | ||
#derive_macro_call | ||
#item_attrs | ||
#visibility struct #item_struct_name #user_impl_generics_with_world #user_where_clauses_with_world { | ||
#(#(#field_attrs)* #field_visibilities #field_idents: <#field_types as #path::query::WorldQuery>::Item<'__w>,)* | ||
} | ||
}, | ||
syn::Fields::Unnamed(_) => quote! { | ||
#derive_macro_call | ||
#item_attrs | ||
#[automatically_derived] | ||
#visibility struct #item_struct_name #user_impl_generics_with_world #user_where_clauses_with_world( | ||
#( #field_visibilities <#field_types as #path::query::WorldQuery>::Item<'__w>, )* | ||
); | ||
}, | ||
syn::Fields::Unit => quote! { | ||
#item_attrs | ||
#visibility type #item_struct_name #user_ty_generics_with_world = #struct_name #user_ty_generics; | ||
}, | ||
} | ||
} | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
pub(crate) fn world_query_impl( | ||
path: &syn::Path, | ||
struct_name: &Ident, | ||
visibility: &Visibility, | ||
item_struct_name: &Ident, | ||
fetch_struct_name: &Ident, | ||
field_types: &Vec<proc_macro2::TokenStream>, | ||
user_impl_generics: &ImplGenerics, | ||
user_impl_generics_with_world: &ImplGenerics, | ||
field_idents: &Vec<proc_macro2::TokenStream>, | ||
user_ty_generics: &TypeGenerics, | ||
user_ty_generics_with_world: &TypeGenerics, | ||
named_field_idents: &Vec<Ident>, | ||
marker_name: &Ident, | ||
state_struct_name: &Ident, | ||
user_where_clauses: Option<&WhereClause>, | ||
user_where_clauses_with_world: Option<&WhereClause>, | ||
) -> proc_macro2::TokenStream { | ||
quote! { | ||
#[doc(hidden)] | ||
#[doc = "Automatically generated internal [`WorldQuery`] fetch type for [`"] | ||
#[doc = stringify!(#struct_name)] | ||
#[doc = "`], used to define the world data accessed by this query."] | ||
#[automatically_derived] | ||
#visibility struct #fetch_struct_name #user_impl_generics_with_world #user_where_clauses_with_world { | ||
#(#named_field_idents: <#field_types as #path::query::WorldQuery>::Fetch<'__w>,)* | ||
#marker_name: &'__w (), | ||
} | ||
|
||
impl #user_impl_generics_with_world Clone for #fetch_struct_name #user_ty_generics_with_world | ||
#user_where_clauses_with_world { | ||
fn clone(&self) -> Self { | ||
Self { | ||
#(#named_field_idents: self.#named_field_idents.clone(),)* | ||
#marker_name: &(), | ||
} | ||
} | ||
} | ||
|
||
// SAFETY: `update_component_access` and `update_archetype_component_access` are called on every field | ||
unsafe impl #user_impl_generics #path::query::WorldQuery | ||
for #struct_name #user_ty_generics #user_where_clauses { | ||
|
||
type Item<'__w> = #item_struct_name #user_ty_generics_with_world; | ||
type Fetch<'__w> = #fetch_struct_name #user_ty_generics_with_world; | ||
type State = #state_struct_name #user_ty_generics; | ||
|
||
fn shrink<'__wlong: '__wshort, '__wshort>( | ||
item: <#struct_name #user_ty_generics as #path::query::WorldQuery>::Item<'__wlong> | ||
) -> <#struct_name #user_ty_generics as #path::query::WorldQuery>::Item<'__wshort> { | ||
#item_struct_name { | ||
#( | ||
#field_idents: <#field_types>::shrink(item.#field_idents), | ||
)* | ||
} | ||
} | ||
|
||
unsafe fn init_fetch<'__w>( | ||
_world: #path::world::unsafe_world_cell::UnsafeWorldCell<'__w>, | ||
state: &Self::State, | ||
_last_run: #path::component::Tick, | ||
_this_run: #path::component::Tick, | ||
) -> <Self as #path::query::WorldQuery>::Fetch<'__w> { | ||
#fetch_struct_name { | ||
#(#named_field_idents: | ||
<#field_types>::init_fetch( | ||
_world, | ||
&state.#named_field_idents, | ||
_last_run, | ||
_this_run, | ||
), | ||
)* | ||
#marker_name: &(), | ||
} | ||
} | ||
|
||
const IS_DENSE: bool = true #(&& <#field_types>::IS_DENSE)*; | ||
|
||
/// SAFETY: we call `set_archetype` for each member that implements `Fetch` | ||
#[inline] | ||
unsafe fn set_archetype<'__w>( | ||
_fetch: &mut <Self as #path::query::WorldQuery>::Fetch<'__w>, | ||
_state: &Self::State, | ||
_archetype: &'__w #path::archetype::Archetype, | ||
_table: &'__w #path::storage::Table | ||
) { | ||
#(<#field_types>::set_archetype(&mut _fetch.#named_field_idents, &_state.#named_field_idents, _archetype, _table);)* | ||
} | ||
|
||
/// SAFETY: we call `set_table` for each member that implements `Fetch` | ||
#[inline] | ||
unsafe fn set_table<'__w>( | ||
_fetch: &mut <Self as #path::query::WorldQuery>::Fetch<'__w>, | ||
_state: &Self::State, | ||
_table: &'__w #path::storage::Table | ||
) { | ||
#(<#field_types>::set_table(&mut _fetch.#named_field_idents, &_state.#named_field_idents, _table);)* | ||
} | ||
|
||
/// SAFETY: we call `fetch` for each member that implements `Fetch`. | ||
#[inline(always)] | ||
unsafe fn fetch<'__w>( | ||
_fetch: &mut <Self as #path::query::WorldQuery>::Fetch<'__w>, | ||
_entity: #path::entity::Entity, | ||
_table_row: #path::storage::TableRow, | ||
) -> <Self as #path::query::WorldQuery>::Item<'__w> { | ||
Self::Item { | ||
#(#field_idents: <#field_types>::fetch(&mut _fetch.#named_field_idents, _entity, _table_row),)* | ||
} | ||
} | ||
|
||
fn update_component_access(state: &Self::State, _access: &mut #path::query::FilteredAccess<#path::component::ComponentId>) { | ||
#( <#field_types>::update_component_access(&state.#named_field_idents, _access); )* | ||
} | ||
|
||
fn update_archetype_component_access( | ||
state: &Self::State, | ||
_archetype: &#path::archetype::Archetype, | ||
_access: &mut #path::query::Access<#path::archetype::ArchetypeComponentId> | ||
) { | ||
#( | ||
<#field_types>::update_archetype_component_access(&state.#named_field_idents, _archetype, _access); | ||
)* | ||
} | ||
|
||
fn init_state(world: &mut #path::world::World) -> #state_struct_name #user_ty_generics { | ||
#state_struct_name { | ||
#(#named_field_idents: <#field_types>::init_state(world),)* | ||
} | ||
} | ||
|
||
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))* | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any way to avoid the sheer number of arguments here and below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite possibly. I will have a go at factoring all the arguments into a small number of structs instead.