-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Allow non-static references as system Input in systems that are ran directly #9584
Comments
Here's a sample showing that use bevy::{ecs::system::BoxedSystem, prelude::*};
pub struct Callback<In, Out> {
initialized: bool,
system: BoxedSystem<In, Out>,
}
impl<In: 'static, Out: 'static> Callback<In, Out> {
pub fn new<M>(system: impl IntoSystem<In, Out, M>) -> Self {
Self {
initialized: false,
system: Box::new(IntoSystem::into_system(system)),
}
}
pub fn is_initialized(&self) -> bool {
self.initialized
}
pub fn run(&mut self, world: &mut World, input: In) -> Out {
if !self.initialized {
self.system.initialize(world);
}
let output = self.system.run(input, world);
self.system.apply_deferred(world);
output
}
} Try removing the |
Dang, seems like we're blocked by rust-lang/rust#81823 for now, if the solution requires GATs. |
GATs may not be needed if we move the Edit: quickly tried this and found a big problem: |
# Objective - Fixes #14924 - Closes #9584 ## Solution - We introduce a new trait, `SystemInput`, that serves as a type function from the `'static` form of the input, to its lifetime'd version, similarly to `SystemParam` or `WorldQuery`. - System functions now take the lifetime'd wrapped version, `SystemInput::Param<'_>`, which prevents the issue presented in #14924 (i.e. `InRef<T>`). - Functions for running systems now take the lifetime'd unwrapped version, `SystemInput::Inner<'_>` (i.e. `&T`). - Due to the above change, system piping had to be re-implemented as a standalone type, rather than `CombinatorSystem` as it was previously. - Removes the `Trigger<'static, E, B>` transmute in observer runner code. ## Testing - All current tests pass. - Added additional tests and doc-tests. --- ## Showcase ```rust let mut world = World::new(); let mut value = 2; // Currently possible: fn square(In(input): In<usize>) -> usize { input * input } value = world.run_system_once_with(value, square); // Now possible: fn square_mut(InMut(input): InMut<usize>) { *input *= *input; } world.run_system_once_with(&mut value, square_mut); // Or: fn square_ref(InRef(input): InRef<usize>) -> usize { *input * *input } value = world.run_system_once_with(&value, square_ref); ``` ## Migration Guide - All current explicit usages of the following types must be changed in the way specified: - `SystemId<I, O>` to `SystemId<In<I>, O>` - `System<In = T>` to `System<In = In<T>>` - `IntoSystem<I, O, M>` to `IntoSystem<In<I>, O, M>` - `Condition<M, T>` to `Condition<M, In<T>>` - `In<Trigger<E, B>>` is no longer a valid input parameter type. Use `Trigger<E, B>` directly, instead. --------- Co-authored-by: Giacomo Stevanato <[email protected]>
What problem does this solve or what need does it fill?
Directly ran systems (i.e. one-shot systems) currently effectively require a
'static
bound on theSystem::In
associated type, preventing one from passing (non-'static
) references as input to a system viaSystem::run
:bevy/crates/bevy_ecs/src/system/system.rs
Line 63 in 365cf31
Being able to pass non-
'static
references to one-shot systems would likely allow us to create full systems asegui
widgets, an extension of #5522's rudimentaryWidgetSystem
into full-featured (exclusive)System
s, for example.Given that
System::In
is only ever used as a function argument, it should be possible to implement this in a way that doesn't lose trait objects? I did some experimental implementing of this recently by turning it into a GAT, but ran into the trait object issue and haven't attempted since.What solution would you like?
For systems that are ran directly (one-shot systems), allow passing non-
'static
references as system input.What alternative(s) have you considered?
Clone objects that need to be passed in. This doesn't work for all cases, though.
The text was updated successfully, but these errors were encountered: