bevy/crates
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
..
bevy_a11y
bevy_animation Prevent AnimationGraph from serializing AssetIds. (#19615) 2025-06-30 22:26:05 +00:00
bevy_anti_aliasing Use RenderStartup in bevy_anti_aliasing. (#19897) 2025-07-02 22:38:44 +00:00
bevy_app
bevy_asset Add test that repros #11111 (different loader settings produce same asset) (#19094) 2025-07-02 22:39:04 +00:00
bevy_audio
bevy_color
bevy_core_pipeline Make render and compute pipeline descriptors defaultable. (#19903) 2025-07-02 18:47:27 +00:00
bevy_core_widgets Newtyped ScrollPosition (#19881) 2025-07-01 17:41:48 +00:00
bevy_derive
bevy_dev_tools
bevy_diagnostic
bevy_dylib
bevy_ecs Have System::run_unsafe return Result. (#19145) 2025-07-03 21:48:09 +00:00
bevy_encase_derive
bevy_feathers Feathers toggle switches. (#19928) 2025-07-03 01:09:31 +00:00
bevy_gilrs
bevy_gizmos Use RenderStartup in bevy_gizmos. (#19898) 2025-07-02 20:38:37 +00:00
bevy_gltf
bevy_image
bevy_input
bevy_input_focus
bevy_internal Light Textures (#18031) 2025-06-30 21:56:17 +00:00
bevy_log
bevy_macro_utils
bevy_math Add unit structs for each ease function (#18739) 2025-07-02 19:18:20 +00:00
bevy_mesh
bevy_mikktspace
bevy_pbr fix skin uniform buffer size (#19888) 2025-07-02 20:06:27 +00:00
bevy_picking
bevy_platform
bevy_ptr
bevy_reflect bevy_reflect: streamline generated FromReflect::from_reflect (#19906) 2025-07-02 14:59:20 +00:00
bevy_remote Add support for returning all Component and values to query method in the Bevy Remote Protocol (#19857) 2025-07-03 18:51:32 +00:00
bevy_render spec_v2: minor revisions (#19923) 2025-07-03 17:49:04 +00:00
bevy_scene
bevy_solari Make render and compute pipeline descriptors defaultable. (#19903) 2025-07-02 18:47:27 +00:00
bevy_sprite Fixing sprite pixel space point computation for sprites with zero custom_size (#19907) 2025-07-03 17:43:32 +00:00
bevy_state Fix docs typo (#19937) 2025-07-03 19:02:50 +00:00
bevy_tasks
bevy_text Replace Handle::Weak with Handle::Uuid. (#19896) 2025-07-02 14:40:35 +00:00
bevy_time
bevy_transform
bevy_ui Make render and compute pipeline descriptors defaultable. (#19903) 2025-07-02 18:47:27 +00:00
bevy_utils Have System::run_unsafe return Result. (#19145) 2025-07-03 21:48:09 +00:00
bevy_window bevy_window: fix compilation without default features (#19870) 2025-06-30 20:51:52 +00:00
bevy_winit