bevy/crates/bevy_ecs/src/query
Chris Russell 6e918f56d8
Have System::run_unsafe return Result. (#19145)
# Objective

Allow combinator and pipe systems to delay validation of the second
system, while still allowing the second system to be skipped.

Fixes #18796

Allow fallible systems to be used as one-shot systems, reporting errors
to the error handler when used through commands.

Fixes #19722

Allow fallible systems to be used as run conditions, including when used
with combinators. Alternative to #19580.

Always validate parameters when calling the safe
`run_without_applying_deferred`, `run`, and `run_readonly` methods on a
`System`.

## Solution

Have `System::run_unsafe` return a `Result`.  

We want pipe systems to run the first system before validating the
second, since the first system may affect whether the second system has
valid parameters. But if the second system skips then we have no output
value to return! So, pipe systems must return a `Result` that indicates
whether the second system ran.

But if we just make pipe systems have `Out = Result<B::Out>`, then
chaining `a.pipe(b).pipe(c)` becomes difficult. `c` would need to accept
the `Result` from `a.pipe(b)`, which means it would likely need to
return `Result` itself, giving `Result<Result<Out>>`!

Instead, we make *all* systems return a `Result`! We move the handling
of fallible systems from `IntoScheduleConfigs` and `IntoObserverSystem`
to `SystemParamFunction` and `ExclusiveSystemParamFunction`, so that an
infallible system can be wrapped before being passed to a combinator.

As a side effect, this enables fallible systems to be used as run
conditions and one-shot systems.

Now that the safe `run_without_applying_deferred`, `run`, and
`run_readonly` methods return a `Result`, we can have them perform
parameter validation themselves instead of requiring each caller to
remember to call them. `run_unsafe` will continue to not validate
parameters, since it is used in the multi-threaded executor when we want
to validate and run in separate tasks.

Note that this makes type inference a little more brittle. A function
that returns `Result<T>` can be considered either a fallible system
returning `T` or an infallible system returning `Result<T>` (and this is
important to continue supporting `pipe`-based error handling)! So there
are some cases where the output type of a system can no longer be
inferred. It will work fine when directly adding to a schedule, since
then the output type is fixed to `()` (or `bool` for run conditions).
And it will work fine when `pipe`ing to a system with a typed input
parameter.

I used a dedicated `RunSystemError` for the error type instead of plain
`BevyError` so that skipping a system does not box an error or capture a
backtrace.
2025-07-03 21:48:09 +00:00
..
access.rs ECS: put strings only used for debug behind a feature (#19558) 2025-06-18 20:15:25 +00:00
builder.rs Let FilteredEntity(Ref|Mut) receive access when nested. (#18236) 2025-05-05 23:23:46 +00:00
error.rs ECS: put strings only used for debug behind a feature (#19558) 2025-06-18 20:15:25 +00:00
fetch.rs Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
filter.rs Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
iter.rs Remove 's lifetime from WorldQuery::Fetch (#19720) 2025-06-19 00:58:21 +00:00
mod.rs Have System::run_unsafe return Result. (#19145) 2025-07-03 21:48:09 +00:00
par_iter.rs Let query items borrow from query state to avoid needing to clone (#15396) 2025-06-16 21:05:41 +00:00
state.rs Fix issue 19734: add dependency on bevy_utils for the bevy_ecs test. (#19738) 2025-06-21 15:05:04 +00:00
world_query.rs Remove 's lifetime from WorldQuery::Fetch (#19720) 2025-06-19 00:58:21 +00:00