
# Objective In simple cases we might want to derive the `ExtractComponent` trait. This adds symmetry to the existing `ExtractResource` derive. ## Solution Add an implementation of `#[derive(ExtractComponent)]`. The implementation is adapted from the existing `ExtractResource` derive macro. Additionally, there is an attribute called `extract_component_filter`. This allows specifying a query filter type used when extracting. If not specified, no filter (equal to `()`) is used. So: ```rust #[derive(Component, Clone, ExtractComponent)] #[extract_component_filter(With<Fuel>)] pub struct Car { pub wheels: usize, } ``` would expand to (a bit cleaned up here): ```rust impl ExtractComponent for Car { type Query = &'static Self; type Filter = With<Fuel>; type Out = Self; fn extract_component(item: QueryItem<'_, Self::Query>) -> Option<Self::Out> { Some(item.clone()) } } ``` --- ## Changelog - Added the ability to `#[derive(ExtractComponent)]` with an optional filter.
52 lines
1.5 KiB
Rust
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::default()
|
|
.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 Query = &'static Self;
|
|
|
|
type Filter = #filter;
|
|
type Out = Self;
|
|
|
|
fn extract_component(item: #bevy_ecs_path::query::QueryItem<'_, Self::Query>) -> Option<Self::Out> {
|
|
Some(item.clone())
|
|
}
|
|
}
|
|
})
|
|
}
|