bevy/crates/bevy_render/macros/src/extract_component.rs
raldone01 760d0a3100
Use one BevyManifest instance in proc macros (#16766)
# Objective

- Minor consistency improvement in proc macro code.
- Remove `get_path_direct` since it was only used once anyways and
doesn't add much.

## Solution
- Possibly a minor performance improvement since the `Cargo.toml` wont
be parsed as often.

## Testing

- I don't think it breaks anything.
- This is my first time working on bevy itself. Is there a script to do
a quick verify of my pr?

## Other PR

Similar to #7536 but has no extra dependencies.

Co-authored-by: François Mockers <mockersf@gmail.com>
2024-12-15 15:00:05 +00:00

52 lines
1.5 KiB
Rust

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, parse_quote, DeriveInput, Path};
pub fn derive_extract_component(input: TokenStream) -> TokenStream {
let mut ast = parse_macro_input!(input as DeriveInput);
let bevy_render_path: Path = crate::bevy_render_path();
let bevy_ecs_path: Path = bevy_macro_utils::BevyManifest::shared()
.maybe_get_path("bevy_ecs")
.expect("bevy_ecs should be found in manifest");
ast.generics
.make_where_clause()
.predicates
.push(parse_quote! { Self: Clone });
let struct_name = &ast.ident;
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
let filter = if let Some(attr) = ast
.attrs
.iter()
.find(|a| a.path().is_ident("extract_component_filter"))
{
let filter = match attr.parse_args::<syn::Type>() {
Ok(filter) => filter,
Err(e) => return e.to_compile_error().into(),
};
quote! {
#filter
}
} else {
quote! {
()
}
};
TokenStream::from(quote! {
impl #impl_generics #bevy_render_path::extract_component::ExtractComponent for #struct_name #type_generics #where_clause {
type QueryData = &'static Self;
type QueryFilter = #filter;
type Out = Self;
fn extract_component(item: #bevy_ecs_path::query::QueryItem<'_, Self::QueryData>) -> Option<Self::Out> {
Some(item.clone())
}
}
})
}