-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea2ecd4
commit 1291879
Showing
7 changed files
with
50 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
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,38 @@ | ||
use proc_macro::{Span, TokenStream}; | ||
use quote::quote; | ||
use syn::{parse_macro_input, Data::Enum, DeriveInput}; | ||
|
||
pub fn derive_states(input: TokenStream) -> TokenStream { | ||
let ast = parse_macro_input!(input as DeriveInput); | ||
let error = || { | ||
syn::Error::new( | ||
Span::call_site().into(), | ||
"derive(States) only supports fieldless enums", | ||
) | ||
.into_compile_error() | ||
.into() | ||
}; | ||
let Enum(enumeration) = ast.data else { | ||
return error(); | ||
}; | ||
if enumeration.variants.iter().any(|v| !v.fields.is_empty()) { | ||
return error(); | ||
} | ||
|
||
let generics = ast.generics; | ||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); | ||
|
||
let struct_name = &ast.ident; | ||
let idents = enumeration.variants.iter().map(|v| &v.ident); | ||
let len = idents.len(); | ||
|
||
quote! { | ||
impl #impl_generics bevy::ecs::schedule_v3::States for #struct_name #ty_generics #where_clause { | ||
type Iter = std::array::IntoIter<Self, #len>; | ||
|
||
fn variants() -> Self::Iter { | ||
[#(Self::#idents,)*].into_iter() | ||
} | ||
} | ||
}.into() | ||
} |
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