Skip to content

Commit cfe8db3

Browse files
committed
Rename Fetch and FilterFetch macros to WorldQuery
1 parent 0a8ba72 commit cfe8db3

File tree

5 files changed

+153
-132
lines changed

5 files changed

+153
-132
lines changed

crates/bevy_ecs/macros/src/fetch.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use proc_macro2::{Ident, Span};
33
use quote::{quote, ToTokens};
44
use syn::{
55
parse::{Parse, ParseStream},
6-
parse_macro_input, parse_quote,
6+
parse_quote,
77
punctuated::Punctuated,
88
Attribute, Data, DataStruct, DeriveInput, Field, Fields, GenericArgument, GenericParam,
99
ImplGenerics, Lifetime, LifetimeDef, Path, PathArguments, ReturnType, Token, Type,
@@ -18,25 +18,23 @@ struct FetchStructAttributes {
1818
pub read_only_derive_args: Punctuated<syn::NestedMeta, syn::token::Comma>,
1919
}
2020

21+
pub static FILTER_ATTRIBUTE_NAME: &str = "filter";
2122
static MUTABLE_ATTRIBUTE_NAME: &str = "mutable";
2223
static READ_ONLY_DERIVE_ATTRIBUTE_NAME: &str = "read_only_derive";
2324

2425
mod field_attr_keywords {
2526
syn::custom_keyword!(ignore);
2627
}
2728

28-
static FETCH_ATTRIBUTE_NAME: &str = "fetch";
29-
static FILTER_FETCH_ATTRIBUTE_NAME: &str = "filter_fetch";
30-
31-
pub fn derive_fetch_impl(input: TokenStream) -> TokenStream {
32-
let ast = parse_macro_input!(input as DeriveInput);
29+
pub static WORLD_QUERY_ATTRIBUTE_NAME: &str = "world_query";
3330

31+
pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream {
3432
let mut fetch_struct_attributes = FetchStructAttributes::default();
3533
for attr in &ast.attrs {
3634
if !attr
3735
.path
3836
.get_ident()
39-
.map_or(false, |ident| ident == FETCH_ATTRIBUTE_NAME)
37+
.map_or(false, |ident| ident == WORLD_QUERY_ATTRIBUTE_NAME)
4038
{
4139
continue;
4240
}
@@ -74,7 +72,7 @@ pub fn derive_fetch_impl(input: TokenStream) -> TokenStream {
7472
}
7573
Ok(())
7674
})
77-
.unwrap_or_else(|_| panic!("Invalid `{}` attribute format", FETCH_ATTRIBUTE_NAME));
75+
.unwrap_or_else(|_| panic!("Invalid `{}` attribute format", WORLD_QUERY_ATTRIBUTE_NAME));
7876
}
7977

8078
let FetchImplTokens {
@@ -101,7 +99,11 @@ pub fn derive_fetch_impl(input: TokenStream) -> TokenStream {
10199
field_types: _,
102100
query_types,
103101
fetch_init_types,
104-
} = fetch_impl_tokens(&ast, FETCH_ATTRIBUTE_NAME, fetch_struct_attributes.mutable);
102+
} = fetch_impl_tokens(
103+
&ast,
104+
WORLD_QUERY_ATTRIBUTE_NAME,
105+
fetch_struct_attributes.mutable,
106+
);
105107

106108
if !has_world_lifetime {
107109
panic!("Expected a struct with a lifetime");
@@ -319,9 +321,7 @@ pub fn derive_fetch_impl(input: TokenStream) -> TokenStream {
319321
tokens
320322
}
321323

322-
pub fn derive_filter_fetch_impl(input: TokenStream) -> TokenStream {
323-
let ast = parse_macro_input!(input as DeriveInput);
324-
324+
pub fn derive_world_query_filter_impl(ast: DeriveInput) -> TokenStream {
325325
let FetchImplTokens {
326326
struct_name,
327327
struct_name_read_only: _,
@@ -346,7 +346,7 @@ pub fn derive_filter_fetch_impl(input: TokenStream) -> TokenStream {
346346
field_types,
347347
query_types: _,
348348
fetch_init_types: _,
349-
} = fetch_impl_tokens(&ast, FILTER_FETCH_ATTRIBUTE_NAME, false);
349+
} = fetch_impl_tokens(&ast, WORLD_QUERY_ATTRIBUTE_NAME, false);
350350

351351
if has_world_lifetime {
352352
panic!("Expected a struct without a lifetime");

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ extern crate proc_macro;
33
mod component;
44
mod fetch;
55

6-
use crate::fetch::{derive_fetch_impl, derive_filter_fetch_impl};
6+
use crate::fetch::{
7+
derive_world_query_filter_impl, derive_world_query_impl, FILTER_ATTRIBUTE_NAME,
8+
WORLD_QUERY_ATTRIBUTE_NAME,
9+
};
710
use bevy_macro_utils::{derive_label, get_named_struct_fields, BevyManifest};
811
use proc_macro::TokenStream;
912
use proc_macro2::Span;
@@ -428,15 +431,45 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
428431
}
429432

430433
/// Implement `WorldQuery` to use a struct as a parameter in a query
431-
#[proc_macro_derive(Fetch, attributes(fetch))]
432-
pub fn derive_fetch(input: TokenStream) -> TokenStream {
433-
derive_fetch_impl(input)
434-
}
434+
#[proc_macro_derive(WorldQuery, attributes(world_query))]
435+
pub fn derive_world_query(input: TokenStream) -> TokenStream {
436+
let ast = parse_macro_input!(input as DeriveInput);
437+
438+
let mut is_filter = false;
435439

436-
/// Implement `FilterFetch` to use a struct as a filter parameter in a query
437-
#[proc_macro_derive(FilterFetch, attributes(filter_fetch))]
438-
pub fn derive_filter_fetch(input: TokenStream) -> TokenStream {
439-
derive_filter_fetch_impl(input)
440+
for attr in &ast.attrs {
441+
if !attr
442+
.path
443+
.get_ident()
444+
.map_or(false, |ident| ident == WORLD_QUERY_ATTRIBUTE_NAME)
445+
{
446+
continue;
447+
}
448+
attr.parse_args_with(|input: ParseStream| {
449+
let meta = input.parse_terminated::<syn::Meta, syn::token::Comma>(syn::Meta::parse)?;
450+
for meta in meta {
451+
let ident = meta.path().get_ident();
452+
if ident.map_or(false, |ident| ident == FILTER_ATTRIBUTE_NAME) {
453+
if let syn::Meta::Path(_) = meta {
454+
is_filter = true;
455+
} else {
456+
panic!(
457+
"The `{}` attribute is expected to have no value or arguments",
458+
FILTER_ATTRIBUTE_NAME
459+
);
460+
}
461+
}
462+
}
463+
Ok(())
464+
})
465+
.unwrap_or_else(|_| panic!("Invalid `{}` attribute format", WORLD_QUERY_ATTRIBUTE_NAME));
466+
}
467+
468+
if is_filter {
469+
derive_world_query_filter_impl(ast)
470+
} else {
471+
derive_world_query_impl(ast)
472+
}
440473
}
441474

442475
#[proc_macro_derive(SystemLabel)]

0 commit comments

Comments
 (0)