-
Notifications
You must be signed in to change notification settings - Fork 220
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
Custom derive for Component trait #192
Merged
+170
−52
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6ce59f3
Create specs_derive crate, rustfmt codebase, ignore *.bk files
ebkalderon fb8d541
Address suggestions and concerns, reference #[derive] only in book
ebkalderon 9587538
Fix missing and moved imports, remove rebasing leftovers
ebkalderon 2912cdd
Rename specs_derive crate to specs-derive
ebkalderon 2017538
Make specs-derive optional
ebkalderon 1578392
Remove extra macro_use directive
ebkalderon 086f698
Update changelog, remove leftover re-export line
ebkalderon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <ebkalderon@gmail.com>"] | ||
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, forgot to reorder this one.