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

Allow non-static references as system Input in systems that are ran directly #9584

Closed
ItsDoot opened this issue Aug 26, 2023 · 3 comments · Fixed by #15184
Closed

Allow non-static references as system Input in systems that are ran directly #9584

ItsDoot opened this issue Aug 26, 2023 · 3 comments · Fixed by #15184
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Complex Quite challenging from either a design or technical perspective. Ask for help!

Comments

@ItsDoot
Copy link
Contributor

ItsDoot commented Aug 26, 2023

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 the System::In associated type, preventing one from passing (non-'static) references as input to a system via System::run:

fn run(&mut self, input: Self::In, world: &mut World) -> Self::Out {

Being able to pass non-'static references to one-shot systems would likely allow us to create full systems as egui widgets, an extension of #5522's rudimentary WidgetSystem into full-featured (exclusive) Systems, 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.

@ItsDoot ItsDoot added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Aug 26, 2023
@ItsDoot
Copy link
Contributor Author

ItsDoot commented Aug 26, 2023

Here's a sample showing that 'static is required:

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 'static bounds on the impl block.

@ItsDoot
Copy link
Contributor Author

ItsDoot commented Aug 26, 2023

Dang, seems like we're blocked by rust-lang/rust#81823 for now, if the solution requires GATs.

@nicopap nicopap added A-ECS Entities, components, systems, and events S-Blocked This cannot move forward until something else changes and removed S-Needs-Triage This issue needs to be labelled labels Aug 26, 2023
@alice-i-cecile alice-i-cecile added D-Complex Quite challenging from either a design or technical perspective. Ask for help! C-Usability A targeted quality-of-life change that makes Bevy easier to use and removed C-Feature A new feature, making something new possible labels Aug 30, 2023
@SkiFire13
Copy link
Contributor

SkiFire13 commented Mar 30, 2024

GATs may not be needed if we move the In associated type to a generic of System, kinda like how it's done for the Fn* traits. This way you would be able to have a for<'a> System<&'a Foo, Out = &'a Bar> which would be callable with non 'static inputs and even get non-'static outputs that borrow from the input. A downside of this is that some things may not be able to take advantage of it (e.g. System::register_system, though I don't see how it would work with GATs either).

Edit: quickly tried this and found a big problem: SystemParamFunction needs to contain the In parameter in order to avoid conflicting implementations, but in turn this makes it non-'static if the input is not 'static. This doesn't seem solvable unless the input is also abstracted in a way similar to how SystemParams are, which I guess is going to be very unergonomic.

github-merge-queue bot pushed a commit that referenced this issue Sep 23, 2024
# 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]>
@ItsDoot ItsDoot removed the S-Blocked This cannot move forward until something else changes label Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Complex Quite challenging from either a design or technical perspective. Ask for help!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants