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

Support SystemInput tuples up to 8 elements #16814

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 3 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
76 changes: 68 additions & 8 deletions crates/bevy_ecs/src/system/input.rs
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.
Expand All @@ -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
Copy link
Member

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.

Copy link
Contributor Author

@ItsDoot ItsDoot Dec 14, 2024

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 multiple SystemInput types, I don't think In 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.

///
/// For advanced usecases, you can implement this trait for your own types.
///
/// [`ObserverSystem`]: crate::system::ObserverSystem
pub trait SystemInput: Sized {
Expand All @@ -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.
///
Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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 LONGEST_REASONABLE_TUPLE, instead of each site picking a somewhat random value (I think I've seen 12, 15, and 16, now also 8).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
}
}
Loading