-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Support SystemInput
tuples up to 8 elements
#16814
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
use core::ops::{Deref, DerefMut}; | ||
|
||
use variadics_please::all_tuples; | ||
|
||
use crate::{bundle::Bundle, prelude::Trigger, system::System}; | ||
|
||
/// Trait for types that can be used as input to [`System`]s. | ||
|
@@ -11,6 +13,9 @@ use crate::{bundle::Bundle, prelude::Trigger, system::System}; | |
/// - [`InMut<T>`]: For mutable references to values | ||
/// - [`Trigger<E, B>`]: For [`ObserverSystem`]s | ||
/// - [`StaticSystemInput<I>`]: For arbitrary [`SystemInput`]s in generic contexts | ||
/// - Tuples of [`SystemInput`]s up to 8 elements | ||
/// | ||
/// For advanced usecases, you can implement this trait for your own types. | ||
/// | ||
/// [`ObserverSystem`]: crate::system::ObserverSystem | ||
pub trait SystemInput: Sized { | ||
|
@@ -31,14 +36,6 @@ pub trait SystemInput: Sized { | |
/// Shorthand way to get the [`System::In`] for a [`System`] as a [`SystemInput::Inner`]. | ||
pub type SystemIn<'a, S> = <<S as System>::In as SystemInput>::Inner<'a>; | ||
|
||
/// [`SystemInput`] type for systems that take no input. | ||
impl SystemInput for () { | ||
type Param<'i> = (); | ||
type Inner<'i> = (); | ||
|
||
fn wrap(_this: Self::Inner<'_>) -> Self::Param<'_> {} | ||
} | ||
|
||
/// A [`SystemInput`] type which denotes that a [`System`] receives | ||
/// an input value of type `T` from its caller. | ||
/// | ||
|
@@ -227,3 +224,66 @@ impl<'a, I: SystemInput> SystemInput for StaticSystemInput<'a, I> { | |
StaticSystemInput(this) | ||
} | ||
} | ||
|
||
macro_rules! impl_system_input_tuple { | ||
($(#[$meta:meta])* $($name:ident),*) => { | ||
$(#[$meta])* | ||
impl<$($name: SystemInput),*> SystemInput for ($($name,)*) { | ||
type Param<'i> = ($($name::Param<'i>,)*); | ||
type Inner<'i> = ($($name::Inner<'i>,)*); | ||
|
||
#[allow(non_snake_case)] | ||
#[allow(clippy::unused_unit)] | ||
fn wrap(this: Self::Inner<'_>) -> Self::Param<'_> { | ||
let ($($name,)*) = this; | ||
($($name::wrap($name),)*) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
all_tuples!( | ||
#[doc(fake_variadic)] | ||
impl_system_input_tuple, | ||
0, | ||
8, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a problem with this PR specifically, but I feel like Bevy should probably just have a constant somewhere for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fair, I just think its way less reasonable to have large-arity input tuples when even using system input at all is somewhat uncommon. So I'd prefer if we could (potentially) save some compilation time by not implementing for arities that are highly unlikely to be used in practice. |
||
I | ||
); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{ | ||
system::{In, InMut, InRef, IntoSystem, System}, | ||
world::World, | ||
}; | ||
|
||
#[test] | ||
fn two_tuple() { | ||
fn by_value((In(a), In(b)): (In<usize>, In<usize>)) -> usize { | ||
a + b | ||
} | ||
fn by_ref((InRef(a), InRef(b)): (InRef<usize>, InRef<usize>)) -> usize { | ||
*a + *b | ||
} | ||
fn by_mut((InMut(a), In(b)): (InMut<usize>, In<usize>)) { | ||
*a += b; | ||
} | ||
|
||
let mut world = World::new(); | ||
let mut by_value = IntoSystem::into_system(by_value); | ||
let mut by_ref = IntoSystem::into_system(by_ref); | ||
let mut by_mut = IntoSystem::into_system(by_mut); | ||
|
||
by_value.initialize(&mut world); | ||
by_ref.initialize(&mut world); | ||
by_mut.initialize(&mut world); | ||
|
||
let mut a = 12; | ||
let b = 24; | ||
|
||
assert_eq!(by_value.run((a, b), &mut world), 36); | ||
assert_eq!(by_ref.run((&a, &b), &mut world), 36); | ||
by_mut.run((&mut a, b), &mut world); | ||
assert_eq!(a, 36); | ||
} | ||
} |
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.
This should also be documented on
In
.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.
In
used to be the only way to pass input into systems, but given it's now just one of the multipleSystemInput
types, I don't thinkIn
needs to be treated any differently from the others. I have added an extra note on all of the input types to check the trait for more information, however. I also added an example to the docs of using tuples.