forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove States::variants and remove enum-only restriction its derive (b…
…evyengine#9945) # Objective The `States::variants` method was once used to construct `OnExit` and `OnEnter` schedules for every possible value of a given `States` type. [Since the switch to lazily initialized schedules](https://github.com/bevyengine/bevy/pull/8028/files#diff-b2fba3a0c86e496085ce7f0e3f1de5960cb754c7d215ed0f087aa556e529f97f), we no longer need to track every possible value. This also opens the door to `States` types that aren't enums. ## Solution - Remove the unused `States::variants` method and its associated type. - Remove the enum-only restriction on derived States types. --- ## Changelog - Removed `States::variants` and its associated type. - Derived `States` can now be datatypes other than enums. ## Migration Guide - `States::variants` no longer exists. If you relied on this function, consider using a library that provides enum iterators.
- Loading branch information
Showing
3 changed files
with
5 additions
and
34 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 |
---|---|---|
@@ -1,44 +1,21 @@ | ||
use proc_macro::{Span, TokenStream}; | ||
use proc_macro::TokenStream; | ||
use quote::{format_ident, quote}; | ||
use syn::{parse_macro_input, Data::Enum, DeriveInput}; | ||
use syn::{parse_macro_input, DeriveInput}; | ||
|
||
use crate::bevy_ecs_path; | ||
|
||
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 mut trait_path = bevy_ecs_path(); | ||
trait_path.segments.push(format_ident!("schedule").into()); | ||
trait_path.segments.push(format_ident!("States").into()); | ||
let struct_name = &ast.ident; | ||
let idents = enumeration.variants.iter().map(|v| &v.ident); | ||
let len = idents.len(); | ||
|
||
quote! { | ||
impl #impl_generics #trait_path for #struct_name #ty_generics #where_clause { | ||
type Iter = std::array::IntoIter<Self, #len>; | ||
|
||
fn variants() -> Self::Iter { | ||
[#(Self::#idents,)*].into_iter() | ||
} | ||
} | ||
impl #impl_generics #trait_path for #struct_name #ty_generics #where_clause {} | ||
} | ||
.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