
There are cases where we want an enum variant name. Right now the only way to do that with rust's std is to derive Debug, but this will also print out the variant's fields. This creates the unfortunate situation where we need to manually write out each variant's string name (ex: in #1963), which is both boilerplate-ey and error-prone. Crates such as `strum` exist for this reason, but it includes a lot of code and complexity that we don't need. This adds a dead-simple `EnumVariantMeta` derive that exposes `enum_variant_index` and `enum_variant_name` functions. This allows us to make cases like #1963 much cleaner (see the second commit). We might also be able to reuse this logic for `bevy_reflect` enum derives.
63 lines
2.3 KiB
Rust
63 lines
2.3 KiB
Rust
extern crate proc_macro;
|
|
|
|
mod app_plugin;
|
|
mod bevy_main;
|
|
mod bytes;
|
|
mod enum_variant_meta;
|
|
mod modules;
|
|
mod render_resource;
|
|
mod render_resources;
|
|
mod resource;
|
|
mod shader_defs;
|
|
|
|
use proc_macro::TokenStream;
|
|
|
|
/// Derives the FromResources trait. Each field must also implement the FromResources trait or this
|
|
/// will fail. FromResources is automatically implemented for types that implement Default.
|
|
#[proc_macro_derive(FromResources, attributes(as_crate))]
|
|
pub fn derive_from_resources(input: TokenStream) -> TokenStream {
|
|
resource::derive_from_resources(input)
|
|
}
|
|
|
|
/// Derives the Bytes trait. Each field must also implements Bytes or this will fail.
|
|
#[proc_macro_derive(Bytes, attributes(as_crate))]
|
|
pub fn derive_bytes(input: TokenStream) -> TokenStream {
|
|
bytes::derive_bytes(input)
|
|
}
|
|
|
|
/// Derives the RenderResources trait. Each field must implement RenderResource or this will fail.
|
|
/// You can ignore fields using `#[render_resources(ignore)]`.
|
|
#[proc_macro_derive(RenderResources, attributes(render_resources, as_crate))]
|
|
pub fn derive_render_resources(input: TokenStream) -> TokenStream {
|
|
render_resources::derive_render_resources(input)
|
|
}
|
|
|
|
/// Derives the RenderResource trait. The type must also implement `Bytes` or this will fail.
|
|
#[proc_macro_derive(RenderResource, attributes(as_crate))]
|
|
pub fn derive_render_resource(input: TokenStream) -> TokenStream {
|
|
render_resource::derive_render_resource(input)
|
|
}
|
|
|
|
/// Derives the ShaderDefs trait. Each field must implement ShaderDef or this will fail.
|
|
/// You can ignore fields using `#[shader_defs(ignore)]`.
|
|
#[proc_macro_derive(ShaderDefs, attributes(shader_def, as_crate))]
|
|
pub fn derive_shader_defs(input: TokenStream) -> TokenStream {
|
|
shader_defs::derive_shader_defs(input)
|
|
}
|
|
|
|
/// Generates a dynamic plugin entry point function for the given `Plugin` type.
|
|
#[proc_macro_derive(DynamicPlugin)]
|
|
pub fn derive_dynamic_plugin(input: TokenStream) -> TokenStream {
|
|
app_plugin::derive_dynamic_plugin(input)
|
|
}
|
|
|
|
#[proc_macro_attribute]
|
|
pub fn bevy_main(attr: TokenStream, item: TokenStream) -> TokenStream {
|
|
bevy_main::bevy_main(attr, item)
|
|
}
|
|
|
|
#[proc_macro_derive(EnumVariantMeta, attributes(as_crate))]
|
|
pub fn derive_enum_variant_meta(input: TokenStream) -> TokenStream {
|
|
enum_variant_meta::derive_enum_variant_meta(input)
|
|
}
|