Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - EnumVariantMeta derive #1972

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions crates/bevy_derive/src/enum_variant_meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::modules::{get_modules, get_path};
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput};

pub fn derive_enum_variant_meta(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let variants = match &ast.data {
Data::Enum(v) => &v.variants,
_ => panic!("Expected an enum."),
};

let modules = get_modules(&ast.attrs);
let bevy_util_path = get_path(&modules.bevy_utils);

let generics = ast.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let struct_name = &ast.ident;
let idents = variants.iter().map(|v| &v.ident);
let names = variants.iter().map(|v| v.ident.to_string());
let indices = 0..names.len();

TokenStream::from(quote! {
impl #impl_generics #bevy_util_path::EnumVariantMeta for #struct_name#ty_generics #where_clause {
fn enum_variant_index(&self) -> usize {
match self {
#(#struct_name::#idents {..} => #indices,)*
}
}
fn enum_variant_name(&self) -> &'static str {
static variants: &[&str] = &[
#(#names,)*
];
let index = self.enum_variant_index();
variants[index]
}
}
})
}
6 changes: 6 additions & 0 deletions crates/bevy_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate proc_macro;
mod app_plugin;
mod bevy_main;
mod bytes;
mod enum_variant_meta;
mod modules;
mod render_resource;
mod render_resources;
Expand Down Expand Up @@ -54,3 +55,8 @@ pub fn derive_dynamic_plugin(input: TokenStream) -> TokenStream {
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)
}
3 changes: 3 additions & 0 deletions crates/bevy_derive/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Modules {
pub bevy_render: String,
pub bevy_asset: String,
pub bevy_core: String,
pub bevy_utils: String,
pub bevy_app: String,
}

Expand All @@ -16,6 +17,7 @@ impl Modules {
bevy_asset: format!("{}::asset", name),
bevy_render: format!("{}::render", name),
bevy_core: format!("{}::core", name),
bevy_utils: format!("{}::utils", name),
bevy_app: format!("{}::app", name),
}
}
Expand All @@ -25,6 +27,7 @@ impl Modules {
bevy_asset: "bevy_asset".to_string(),
bevy_render: "bevy_render".to_string(),
bevy_core: "bevy_core".to_string(),
bevy_utils: "bevy_utils".to_string(),
bevy_app: "bevy_app".to_string(),
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/mesh/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use bevy_ecs::{
};
use bevy_math::*;
use bevy_reflect::TypeUuid;
use bevy_utils::EnumVariantMeta;
use std::{borrow::Cow, collections::BTreeMap};

use crate::pipeline::{InputStepMode, VertexAttribute, VertexBufferLayout};
Expand All @@ -24,7 +25,7 @@ pub const INDEX_BUFFER_ASSET_INDEX: u64 = 0;
pub const VERTEX_ATTRIBUTE_BUFFER_ID: u64 = 10;

/// An array where each entry describes a property of a single vertex.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, EnumVariantMeta)]
pub enum VertexAttributeValues {
Float(Vec<f32>),
Int(Vec<i32>),
Expand Down
Loading