-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
SystemRunner param and macro syntax #21811
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
Open
ecoskey
wants to merge
10
commits into
bevyengine:main
Choose a base branch
from
ecoskey:feature/system_runner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e6e32ca
Add SystemRunner
ecoskey 4e1b139
Simplify system building with BuilderSystem
ecoskey 66b6a4f
add unwrap method to SystemInput
ecoskey 38c9b29
system composition macro
ecoskey c2d996a
cleanup
ecoskey b986a71
docs and example
ecoskey e0c905c
fix docs
ecoskey 2b4849f
fix ci
ecoskey 6578cfc
fix docs
ecoskey cf12de9
fix docs
ecoskey 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 hidden or 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 hidden or 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 hidden or 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,128 @@ | ||
| use bevy_macro_utils::ensure_no_collision; | ||
| use proc_macro::TokenStream; | ||
| use quote::{format_ident, quote}; | ||
| use syn::{ | ||
| parse::{Parse, ParseStream}, | ||
| parse_macro_input, parse_quote, parse_quote_spanned, | ||
| spanned::Spanned, | ||
| visit_mut::VisitMut, | ||
| Expr, ExprMacro, Ident, Pat, Path, Token, | ||
| }; | ||
|
|
||
| use crate::bevy_ecs_path; | ||
|
|
||
| struct ExpandSystemCalls { | ||
| call_ident: Ident, | ||
| systems_ident: Ident, | ||
| system_paths: Vec<Path>, | ||
| } | ||
|
|
||
| struct SystemCall { | ||
| path: Path, | ||
| _comma: Option<Token![,]>, | ||
| input: Option<Box<Expr>>, | ||
| } | ||
|
|
||
| impl Parse for SystemCall { | ||
| fn parse(input: ParseStream) -> syn::Result<Self> { | ||
| let path = input.parse()?; | ||
| let comma: Option<Token![,]> = input.parse()?; | ||
| let input = comma.as_ref().and_then(|_| input.parse().ok()); | ||
| Ok(Self { | ||
| path, | ||
| _comma: comma, | ||
| input, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl VisitMut for ExpandSystemCalls { | ||
| fn visit_expr_mut(&mut self, i: &mut Expr) { | ||
| if let Expr::Macro(ExprMacro { attrs: _, mac }) = i | ||
| && mac.path.is_ident(&self.call_ident) | ||
| { | ||
| let call = match mac.parse_body::<SystemCall>() { | ||
| Ok(call) => call, | ||
| Err(err) => { | ||
| *i = Expr::Verbatim(err.into_compile_error()); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| let call_index = match self.system_paths.iter().position(|p| p == &call.path) { | ||
| Some(i) => i, | ||
| None => { | ||
| let len = self.system_paths.len(); | ||
| self.system_paths.push(call.path.clone()); | ||
| len | ||
| } | ||
| }; | ||
|
|
||
| let systems_ident = &self.systems_ident; | ||
| let system_accessor = format_ident!("p{}", call_index); | ||
| let expr: Expr = match &call.input { | ||
| Some(input) => { | ||
| parse_quote_spanned!(mac.span()=> #systems_ident.#system_accessor().run_with(#input)) | ||
| } | ||
| None => { | ||
| parse_quote_spanned!(mac.span()=> #systems_ident.#system_accessor().run()) | ||
| } | ||
| }; | ||
| *i = expr; | ||
| } else { | ||
| syn::visit_mut::visit_expr_mut(self, i); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn compose(input: TokenStream, has_input: bool) -> TokenStream { | ||
| let bevy_ecs_path = bevy_ecs_path(); | ||
| let call_ident = format_ident!("run"); | ||
| let systems_ident = ensure_no_collision(format_ident!("__systems"), input.clone()); | ||
| let mut expr_closure = parse_macro_input!(input as syn::ExprClosure); | ||
|
|
||
| let mut visitor = ExpandSystemCalls { | ||
| call_ident, | ||
| systems_ident: systems_ident.clone(), | ||
| system_paths: Vec::new(), | ||
| }; | ||
|
|
||
| syn::visit_mut::visit_expr_closure_mut(&mut visitor, &mut expr_closure); | ||
|
|
||
| let runner_types: Vec<syn::Type> = visitor | ||
| .system_paths | ||
| .iter() | ||
| .map(|path| parse_quote_spanned!(path.span()=> #bevy_ecs_path::system::SystemRunner<_, _, _>)) | ||
| .collect(); | ||
|
|
||
| let param_count = if has_input { | ||
| if expr_closure.inputs.is_empty() { | ||
| return TokenStream::from( | ||
| syn::Error::new_spanned( | ||
| &expr_closure.inputs, | ||
| "closure must have at least one parameter", | ||
| ) | ||
| .into_compile_error(), | ||
| ); | ||
| } | ||
| expr_closure.inputs.len() - 1 | ||
| } else { | ||
| expr_closure.inputs.len() | ||
| }; | ||
|
|
||
| let mut builders: Vec<Expr> = | ||
| vec![parse_quote!(#bevy_ecs_path::system::ParamBuilder); param_count]; | ||
| let system_builders: Vec<Expr> = visitor.system_paths.iter().map(|path| parse_quote_spanned!(path.span()=> #bevy_ecs_path::system::ParamBuilder::system(#path))).collect(); | ||
| builders.push(parse_quote!(#bevy_ecs_path::system::ParamSetBuilder((#(#system_builders,)*)))); | ||
|
|
||
| expr_closure.inputs.push(Pat::Type( | ||
| parse_quote!(mut #systems_ident: #bevy_ecs_path::system::ParamSet<(#(#runner_types,)*)>), | ||
| )); | ||
|
|
||
| TokenStream::from(quote! { | ||
| #bevy_ecs_path::system::SystemParamBuilder::build_system( | ||
| (#(#builders,)*), | ||
| #expr_closure | ||
| ) | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Would it make sense to accept a function definition instead of or in addition to a closure? That would make it easier for these systems to have names. Something like
For that matter, would it make sense to use an attribute macro, like
?
... Oh, maybe not, because what you'd really want that to expand to is
const system_name: impl System = const { ... };, but there's no way to write the type for theconst.