Path: blob/main/crates/bevy_ecs/macros/src/resource.rs
30636 views
use bevy_ecs_macro_logic::component::{DeriveComponent, StorageAttribute, StorageTy};1use bevy_macro_utils::fq_std::FQOption;2use proc_macro2::TokenStream;3use quote::quote;4use syn::{DeriveInput, Path};56pub fn derive_resource(ast: &mut DeriveInput) -> TokenStream {7let bevy_ecs: Path = crate::bevy_ecs_path();8let mut derive_component = match DeriveComponent::parse(ast, StorageAttribute::Disallowed) {9Ok(value) => value,10Err(e) => return e.into_compile_error(),11};1213let struct_name = &ast.ident;14let (_, type_generics, _) = &ast.generics.split_for_impl();1516// We add the component_id existence check here to avoid recursive init during required components initialization.17derive_component.additional_requires.push(quote! {18let resource_component_id = if let #FQOption::Some(id) = required_components.components_registrator().component_id::<#struct_name #type_generics>() {19id20} else {21required_components.components_registrator().register_component::<#struct_name #type_generics>()22};23required_components.register_required::<#bevy_ecs::resource::IsResource>(move || #bevy_ecs::resource::IsResource::new(resource_component_id));24});2526let component_impl = match derive_component.impl_component(ast, &bevy_ecs, StorageTy::SparseSet)27{28Ok(value) => value,29Err(err) => return err.into_compile_error(),30};3132let struct_name = &ast.ident;33let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();3435quote! {36#component_impl37impl #impl_generics #bevy_ecs::resource::Resource for #struct_name #type_generics #where_clause {38}39}40}414243