Skip to content

Commit 9c00443

Browse files
authored
Remove States::variants and remove enum-only restriction its derive (#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.
1 parent 95813b8 commit 9c00443

File tree

3 files changed

+5
-34
lines changed

3 files changed

+5
-34
lines changed

crates/bevy_ecs/macros/src/states.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,21 @@
1-
use proc_macro::{Span, TokenStream};
1+
use proc_macro::TokenStream;
22
use quote::{format_ident, quote};
3-
use syn::{parse_macro_input, Data::Enum, DeriveInput};
3+
use syn::{parse_macro_input, DeriveInput};
44

55
use crate::bevy_ecs_path;
66

77
pub fn derive_states(input: TokenStream) -> TokenStream {
88
let ast = parse_macro_input!(input as DeriveInput);
9-
let error = || {
10-
syn::Error::new(
11-
Span::call_site().into(),
12-
"derive(States) only supports fieldless enums",
13-
)
14-
.into_compile_error()
15-
.into()
16-
};
17-
let Enum(enumeration) = ast.data else {
18-
return error();
19-
};
20-
if enumeration.variants.iter().any(|v| !v.fields.is_empty()) {
21-
return error();
22-
}
23-
249
let generics = ast.generics;
2510
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
2611

2712
let mut trait_path = bevy_ecs_path();
2813
trait_path.segments.push(format_ident!("schedule").into());
2914
trait_path.segments.push(format_ident!("States").into());
3015
let struct_name = &ast.ident;
31-
let idents = enumeration.variants.iter().map(|v| &v.ident);
32-
let len = idents.len();
3316

3417
quote! {
35-
impl #impl_generics #trait_path for #struct_name #ty_generics #where_clause {
36-
type Iter = std::array::IntoIter<Self, #len>;
37-
38-
fn variants() -> Self::Iter {
39-
[#(Self::#idents,)*].into_iter()
40-
}
41-
}
18+
impl #impl_generics #trait_path for #struct_name #ty_generics #where_clause {}
4219
}
4320
.into()
4421
}

crates/bevy_ecs/src/schedule/condition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ mod tests {
11901190
.distributive_run_if(resource_changed_or_removed::<State<TestState>>())
11911191
.distributive_run_if(resource_removed::<State<TestState>>())
11921192
.distributive_run_if(state_exists::<TestState>())
1193-
.distributive_run_if(in_state(TestState::A))
1193+
.distributive_run_if(in_state(TestState::A).or_else(in_state(TestState::B)))
11941194
.distributive_run_if(state_changed::<TestState>())
11951195
.distributive_run_if(on_event::<TestEvent>())
11961196
.distributive_run_if(any_with_component::<TestComponent>())

crates/bevy_ecs/src/schedule/state.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ pub use bevy_ecs_macros::States;
4040
/// }
4141
///
4242
/// ```
43-
pub trait States: 'static + Send + Sync + Clone + PartialEq + Eq + Hash + Debug + Default {
44-
/// The type returned when iterating over all [`variants`](States::variants) of this type.
45-
type Iter: Iterator<Item = Self>;
46-
47-
/// Returns an iterator over all the state variants.
48-
fn variants() -> Self::Iter;
49-
}
43+
pub trait States: 'static + Send + Sync + Clone + PartialEq + Eq + Hash + Debug + Default {}
5044

5145
/// The label of a [`Schedule`](super::Schedule) that runs whenever [`State<S>`]
5246
/// enters this state.

0 commit comments

Comments
 (0)