Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_ecs/macros/src/resource.rs
30636 views
1
use bevy_ecs_macro_logic::component::{DeriveComponent, StorageAttribute, StorageTy};
2
use bevy_macro_utils::fq_std::FQOption;
3
use proc_macro2::TokenStream;
4
use quote::quote;
5
use syn::{DeriveInput, Path};
6
7
pub fn derive_resource(ast: &mut DeriveInput) -> TokenStream {
8
let bevy_ecs: Path = crate::bevy_ecs_path();
9
let mut derive_component = match DeriveComponent::parse(ast, StorageAttribute::Disallowed) {
10
Ok(value) => value,
11
Err(e) => return e.into_compile_error(),
12
};
13
14
let struct_name = &ast.ident;
15
let (_, type_generics, _) = &ast.generics.split_for_impl();
16
17
// We add the component_id existence check here to avoid recursive init during required components initialization.
18
derive_component.additional_requires.push(quote! {
19
let resource_component_id = if let #FQOption::Some(id) = required_components.components_registrator().component_id::<#struct_name #type_generics>() {
20
id
21
} else {
22
required_components.components_registrator().register_component::<#struct_name #type_generics>()
23
};
24
required_components.register_required::<#bevy_ecs::resource::IsResource>(move || #bevy_ecs::resource::IsResource::new(resource_component_id));
25
});
26
27
let component_impl = match derive_component.impl_component(ast, &bevy_ecs, StorageTy::SparseSet)
28
{
29
Ok(value) => value,
30
Err(err) => return err.into_compile_error(),
31
};
32
33
let struct_name = &ast.ident;
34
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
35
36
quote! {
37
#component_impl
38
impl #impl_generics #bevy_ecs::resource::Resource for #struct_name #type_generics #where_clause {
39
}
40
}
41
}
42
43