-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
192: Custom derive for Component trait r=torkleyy This PR introduces an optional `specs_derive` crate containing a custom derive macro for defining new components in a less verbose way. The ergonomics loosely resemble the [`derivative`](https://github.com/mcarton/rust-derivative) crate. ### Before ```rust #[derive(Debug)] struct Pos(f32, f32, f32); impl Component for Pos { type Storage = VecStorage<Pos>; } ``` ### After ```rust #[derive(Component, Debug)] struct Pos(f32, f32, f32); ``` The macro will store components in `VecStorage`s by default. To specify a different storage type, you may use the `#[component]` attribute. ```rust #[derive(Component, Debug)] #[component(HashMapStorage)] struct Pos(f32, f32, f32); ``` I also included a revised `basic.rs` example called `basic_derive.rs` to demonstrate its usage, but this can probably be omitted from the PR if people ultimately prefer implementing `Component` the explicit way. **EDIT**: Use `DenseVecStorage` by default.
- Loading branch information
Showing
15 changed files
with
170 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ Cargo.lock | |
# Generated by mdbook | ||
/book/book/ | ||
|
||
# Generated by rustfmt | ||
*.bk | ||
|
||
# IDEs / Editor | ||
*.iml | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "specs-derive" | ||
version = "0.1.0" | ||
authors = ["Eyal Kalderon <[email protected]>"] | ||
description = "Custom derive macro for Specs components" | ||
documentation = "https://docs.rs/specs-derive" | ||
repository = "https://github.com/slide-rs/specs/specs-derive" | ||
keywords = ["gamedev", "parallel", "specs", "ecs", "derive"] | ||
license = "MIT/Apache-2.0" | ||
|
||
[dependencies] | ||
syn = "0.11" | ||
quote = "0.3" | ||
|
||
[lib] | ||
proc-macro = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! Implements the `#[derive(Component)]` macro and `#[component]` attribute for | ||
//! [Specs][sp]. | ||
//! | ||
//! [sp]: https://slide-rs.github.io/specs-website/ | ||
|
||
extern crate proc_macro; | ||
#[macro_use] | ||
extern crate quote; | ||
extern crate syn; | ||
|
||
use proc_macro::TokenStream; | ||
use syn::{Ident, MacroInput, MetaItem, NestedMetaItem}; | ||
use quote::Tokens; | ||
|
||
/// Custom derive macro for the `Component` trait. | ||
/// | ||
/// ## Example | ||
/// | ||
/// ``` | ||
/// #[derive(Component, Debug)] | ||
/// struct Pos(f32, f32, f32); | ||
/// ``` | ||
/// | ||
/// The macro will store components in `DenseVecStorage`s by default. To specify | ||
/// a different storage type, you may use the `#[component]` attribute. | ||
/// | ||
/// ``` | ||
/// #[derive(Component, Debug)] | ||
/// #[component(HashMapStorage)] | ||
/// struct Pos(f32, f32, f32); | ||
/// ``` | ||
#[proc_macro_derive(Component, attributes(component))] | ||
pub fn component(input: TokenStream) -> TokenStream { | ||
let s = input.to_string(); | ||
let ast = syn::parse_derive_input(&s).unwrap(); | ||
let gen = impl_component(&ast); | ||
gen.parse().unwrap() | ||
} | ||
|
||
fn impl_component(ast: &MacroInput) -> Tokens { | ||
let name = &ast.ident; | ||
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); | ||
|
||
let storage = ast.attrs.first() | ||
.and_then(|attr| match attr.value { | ||
MetaItem::List(ref ident, ref items) if ident == "component" => items.first(), | ||
_ => None, | ||
}) | ||
.and_then(|attr| match *attr { | ||
NestedMetaItem::MetaItem(ref item) => Some(item), | ||
_ => None, | ||
}) | ||
.and_then(|attr| match *attr { | ||
MetaItem::Word(ref ident) => Some(ident), | ||
_ => None, | ||
}) | ||
.cloned() | ||
.unwrap_or(Ident::new("::specs::DenseVecStorage")); | ||
|
||
quote! { | ||
impl #impl_generics ::specs::Component for #name #ty_generics #where_clause { | ||
type Storage = #storage<#name>; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.