Skip to content
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

Add API for simple system ordering #5165

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,62 @@ pub(crate) fn bevy_ecs_path() -> syn::Path {
pub fn derive_component(input: TokenStream) -> TokenStream {
component::derive_component(input)
}

#[proc_macro]
pub fn impl_into_linked_system_set(_input: TokenStream) -> TokenStream {
let start = 0;
let end = 15;
let mut system_idents = Vec::with_capacity(end - start);
let mut params_idents = Vec::with_capacity(end - start);
let mut labels_idents = Vec::with_capacity(end - start);
let mut i_s = Vec::with_capacity(end - start);
for i in start..=end {
let system_ident = format_ident!("S{}", i);
let params_ident = format_ident!("P{}", i);
let labels_ident = format_ident!("label{}", i);
system_idents.push(system_ident);
params_idents.push(params_ident);
labels_idents.push(labels_ident);
// See https://docs.rs/quote/latest/quote/macro.quote.html#indexing-into-a-tuple-struct
i_s.push(Index::from(i));
}

let start = 2;
let implementations = (start..=end).map(|i| {
let system_idents = &system_idents[0..i];
let params_idents = &params_idents[0..i];

// This is the reason why this is a proc macro and not
// a macro passed to `all_tuples`.
//
// `macro_rules!` do not have a way to get a
// subset of a variadic.
let head_i = &i_s[0];
let tail_i = &i_s[1..i];
let init_i = &i_s[0..i - 1];
let labels_idents = &labels_idents[0..i - 1];

quote! {
impl<#(#system_idents),*, #(#params_idents),*>
IntoLinkedSystemSet<(#(#system_idents,)*), (#(#params_idents,)*)>
for (#(#system_idents,)*)
where
#(#params_idents: SystemParam + 'static,)*
#(#system_idents: SystemParamFunction<(), (), #params_idents, ()> + 'static,)*
{
fn link(self) -> SystemSet {
#(let #labels_idents = self.#init_i.as_system_label();)*
SystemSet::new()
.with_system(self.#head_i)
#(.with_system(self.#tail_i.after(#labels_idents)))*
}
}
}
});

TokenStream::from(quote! {
#(
#implementations
)*
})
}
7 changes: 4 additions & 3 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ pub mod prelude {
event::{EventReader, EventWriter},
query::{Added, AnyOf, ChangeTrackers, Changed, Or, QueryState, With, Without},
schedule::{
AmbiguitySetLabel, ExclusiveSystemDescriptorCoercion, ParallelSystemDescriptorCoercion,
RunCriteria, RunCriteriaDescriptorCoercion, RunCriteriaLabel, Schedule, Stage,
StageLabel, State, SystemLabel, SystemSet, SystemStage,
AmbiguitySetLabel, ExclusiveSystemDescriptorCoercion, IntoLinkedSystemSet,
IntoSequentialSystemSet, ParallelSystemDescriptorCoercion, RunCriteria,
RunCriteriaDescriptorCoercion, RunCriteriaLabel, Schedule, Stage, StageLabel, State,
SystemLabel, SystemSet, SystemStage,
},
system::{
Commands, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem, Local, NonSend,
Expand Down
83 changes: 82 additions & 1 deletion crates/bevy_ecs/src/schedule/system_set.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use bevy_ecs_macros::impl_into_linked_system_set;

use crate::schedule::{
AmbiguitySetLabel, BoxedAmbiguitySetLabel, BoxedSystemLabel, IntoRunCriteria,
IntoSystemDescriptor, RunCriteriaDescriptorOrLabel, State, StateData, SystemDescriptor,
SystemLabel,
};
use crate::system::AsSystemLabel;
use crate::system::{AsSystemLabel, SystemParam, SystemParamFunction};

use super::ParallelSystemDescriptorCoercion;

/// A builder for describing several systems at the same time.
#[derive(Default)]
Expand Down Expand Up @@ -138,3 +142,80 @@ impl SystemSet {
(run_criteria, systems)
}
}

/// A trait that provides the [`.link()`] method to tuples of systems.
///
/// [`.link()`]: IntoLinkedSystemSet::link
pub trait IntoLinkedSystemSet<S, P> {
/// A helper method to create system sets with automatic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A helper method to create system sets with automatic
/// A helper method to create system sets with sequential

/// ordering of execution.
///
/// This is only implemented for tuples of 2 to 15 systems.
///
/// ```
/// # use bevy_ecs::prelude::*;
/// #
/// # fn first_system() {}
/// # fn second_system() {}
/// # fn third_system() {}
/// # fn fourth_system() {}
///
/// // This expression...
///
/// (
/// first_system,
/// second_system,
/// third_system,
/// fourth_system,
/// ).link();
///
/// // ...is equal to:
///
/// SystemSet::new()
/// .with_system(first_system)
/// .with_system(second_system.after(first_system))
/// .with_system(third_system.after(second_system))
/// .with_system(fourth_system.after(third_system));
/// ```
fn link(self) -> SystemSet;
}

impl_into_linked_system_set!();

/// A trait that provides the [`.then()`] method to systems.
///
/// [`.then()`]: IntoSequentialSystemSet::then
pub trait IntoSequentialSystemSet<S0, S1, P0, P1> {
/// A helper method to create system sets with automatic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A helper method to create system sets with automatic
/// A helper method to create system sets with sequential

/// ordering of execution.
///
/// ```
/// # use bevy_ecs::prelude::*;
/// #
/// # fn first_system() {}
/// # fn second_system() {}
///
/// // This expression...
///
/// first_system.then(second_system);
///
/// // ...is equal to:
///
/// SystemSet::new()
/// .with_system(first_system)
/// .with_system(second_system.after(first_system));
/// ```
fn then(self, next_system: S1) -> SystemSet;
}

impl<S0, S1, P0, P1> IntoSequentialSystemSet<S0, S1, P0, P1> for S0
where
P0: SystemParam + 'static,
P1: SystemParam + 'static,
S0: SystemParamFunction<(), (), P0, ()> + 'static,
S1: SystemParamFunction<(), (), P1, ()> + 'static,
{
fn then(self, next_system: S1) -> SystemSet {
(self, next_system).link()
}
}