Skip to content

Commit

Permalink
EnumVariantMeta derive (bevyengine#1972) - 1248a63
Browse files Browse the repository at this point in the history
  • Loading branch information
cart authored and vabrador committed Sep 15, 2021
1 parent 2f8ad43 commit 057c0f0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 4 deletions.
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
5 changes: 3 additions & 2 deletions crates/bevy_render/src/mesh/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use bevy_ecs::{
};
use bevy_math::*;
use bevy_reflect::TypeUuid;
use std::borrow::Cow;
use bevy_utils::EnumVariantMeta;
use std::{borrow::Cow, collections::BTreeMap};

use crate::pipeline::{InputStepMode, VertexAttribute, VertexBufferLayout};
use bevy_utils::{HashMap, HashSet};
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 {
Float32(Vec<f32>),
Sint32(Vec<i32>),
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ license = "MIT"
keywords = ["bevy"]

[dependencies]
bevy_derive = { path = "../bevy_derive", version = "0.5.0" }
ahash = "0.7.0"
tracing = {version = "0.1", features = ["release_max_level_info"]}
instant = { version = "0.1", features = ["wasm-bindgen"] }
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_utils/src/enum_variant_meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub use bevy_derive::EnumVariantMeta;

pub trait EnumVariantMeta {
fn enum_variant_index(&self) -> usize;
fn enum_variant_name(&self) -> &'static str;
}
8 changes: 6 additions & 2 deletions crates/bevy_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
mod enum_variant_meta;
pub use enum_variant_meta::*;

pub use ahash::AHasher;
use ahash::RandomState;
pub use instant::{Duration, Instant};
use std::{future::Future, pin::Pin};
pub use tracing;
pub use uuid::Uuid;

use ahash::RandomState;
use std::{future::Future, pin::Pin};

#[cfg(not(target_arch = "wasm32"))]
pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

Expand Down

0 comments on commit 057c0f0

Please sign in to comment.