bevy/crates/bevy_ecs/src/system
Zeenobit 1e170d2e90
Add RunSystem (#9366)
Add a `RunSystem` extension trait to allow for immediate execution of
systems on a `World` for debugging and/or testing purposes.

# Objective

Fixes #6184

Initially, I made this CL as `ApplyCommands`. After a discussion with
@cart , we decided a more generic implementation would be better to
support all systems. This is the new revised CL. Sorry for the long
delay! 😅

This CL allows users to do this:
```rust
use bevy::prelude::*;
use bevy::ecs::system::RunSystem;

struct T(usize);

impl Resource for T {}

fn system(In(n): In<usize>, mut commands: Commands) -> usize {
    commands.insert_resource(T(n));
    n + 1
}

let mut world = World::default();
let n = world.run_system_with(1, system);
assert_eq!(n, 2);
assert_eq!(world.resource::<T>().0, 1);
```

## Solution

This is implemented as a trait extension and not included in any
preludes to ensure it's being used consciously.
Internally, it just initializes and runs a systems, and applies any
deferred parameters all "in place".
The trait has 2 functions (one of which calls the other by default):
- `run_system_with` is the general implementation, which allows user to
pass system input parameters
- `run_system` is the ergonomic wrapper for systems with no input
parameter (to avoid having the user pass `()` as input).

~~Additionally, this trait is also implemented for `&mut App`. I added
this mainly for ergonomics (`app.run_system` vs.
`app.world.run_system`).~~ (Removed based on feedback)

---------

Co-authored-by: Pascal Hertleif <killercup@gmail.com>
2023-08-11 20:41:48 +00:00
..
commands Add/fix track_caller attribute on panicking entity accessor methods (#8951) 2023-06-26 18:35:11 +00:00
combinator.rs Implement Clone for CombinatorSystem (#8826) 2023-06-12 19:44:51 +00:00
exclusive_function_system.rs Document every public item in bevy_ecs (#8731) 2023-06-10 23:23:48 +00:00
exclusive_system_param.rs Document every public item in bevy_ecs (#8731) 2023-06-10 23:23:48 +00:00
function_system.rs Migrate the rest of the engine to UnsafeWorldCell (#8833) 2023-06-15 01:31:56 +00:00
mod.rs Implement WorldQuery for EntityRef (#6960) 2023-06-22 21:20:00 +00:00
query.rs replace parens with square brackets when referencing _mut on Query docs #9200 (#9223) 2023-07-20 21:41:07 +00:00
system_param.rs Add a paragraph to the lifetimeless module doc (#9312) 2023-08-02 22:01:56 +00:00
system.rs Add RunSystem (#9366) 2023-08-11 20:41:48 +00:00