diff --git a/crates/bevy_ecs/Cargo.toml b/crates/bevy_ecs/Cargo.toml index 9112683c22e6c..cfcea53b3644c 100644 --- a/crates/bevy_ecs/Cargo.toml +++ b/crates/bevy_ecs/Cargo.toml @@ -27,6 +27,7 @@ fixedbitset = "0.4.2" rustc-hash = "1.1" downcast-rs = "1.2" serde = { version = "1", features = ["derive"] } +thiserror = "1.0" [dev-dependencies] rand = "0.8" diff --git a/crates/bevy_ecs/src/world/error.rs b/crates/bevy_ecs/src/world/error.rs new file mode 100644 index 0000000000000..c5426b6cbd5d3 --- /dev/null +++ b/crates/bevy_ecs/src/world/error.rs @@ -0,0 +1,8 @@ +use thiserror::Error; + +use crate::schedule::BoxedScheduleLabel; + +/// The error type returned by `World::try_run_schedule` if the provided schedule does not exist. +#[derive(Error, Debug)] +#[error("The schedule with the label {0:?} was not found.")] +pub struct TryRunScheduleError(pub BoxedScheduleLabel); diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 0832141f4bf02..88ab3dbb6214b 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1,4 +1,5 @@ mod entity_ref; +pub mod error; mod spawn_batch; pub mod unsafe_world_cell; mod world_cell; @@ -20,6 +21,7 @@ use crate::{ schedule::{Schedule, ScheduleLabel, Schedules}, storage::{ResourceData, Storages}, system::Resource, + world::error::TryRunScheduleError, }; use bevy_ptr::{OwningPtr, Ptr}; use bevy_utils::tracing::warn; @@ -1714,6 +1716,47 @@ impl World { schedules.insert(label, schedule); } + /// Attempts to run the [`Schedule`] associated with the `label` a single time, + /// and returns a [`TryRunScheduleError`] if the schedule does not exist. + /// + /// The [`Schedule`] is fetched from the [`Schedules`] resource of the world by its label, + /// and system state is cached. + /// + /// For simple testing use cases, call [`Schedule::run(&mut world)`](Schedule::run) instead. + pub fn try_run_schedule( + &mut self, + label: impl ScheduleLabel, + ) -> Result<(), TryRunScheduleError> { + self.try_run_schedule_ref(&label) + } + + /// Attempts to run the [`Schedule`] associated with the `label` a single time, + /// and returns a [`TryRunScheduleError`] if the schedule does not exist. + /// + /// Unlike the `try_run_schedule` method, this method takes the label by reference, which can save a clone. + /// + /// The [`Schedule`] is fetched from the [`Schedules`] resource of the world by its label, + /// and system state is cached. + /// + /// For simple testing use cases, call [`Schedule::run(&mut world)`](Schedule::run) instead. + pub fn try_run_schedule_ref( + &mut self, + label: &dyn ScheduleLabel, + ) -> Result<(), TryRunScheduleError> { + let Some((extracted_label, mut schedule)) = self.resource_mut::().remove_entry(label) else { + return Err(TryRunScheduleError(label.dyn_clone())); + }; + + // TODO: move this span to Schedule::run + #[cfg(feature = "trace")] + let _span = bevy_utils::tracing::info_span!("schedule", name = ?extracted_label).entered(); + schedule.run(self); + self.resource_mut::() + .insert(extracted_label, schedule); + + Ok(()) + } + /// Runs the [`Schedule`] associated with the `label` a single time. /// /// The [`Schedule`] is fetched from the [`Schedules`] resource of the world by its label, @@ -1741,17 +1784,8 @@ impl World { /// /// Panics if the requested schedule does not exist, or the [`Schedules`] resource was not added. pub fn run_schedule_ref(&mut self, label: &dyn ScheduleLabel) { - let (extracted_label, mut schedule) = self - .resource_mut::() - .remove_entry(label) - .unwrap_or_else(|| panic!("The schedule with the label {label:?} was not found.")); - - // TODO: move this span to Schedule::run - #[cfg(feature = "trace")] - let _span = bevy_utils::tracing::info_span!("schedule", name = ?extracted_label).entered(); - schedule.run(self); - self.resource_mut::() - .insert(extracted_label, schedule); + self.try_run_schedule_ref(label) + .unwrap_or_else(|e| panic!("{}", e)) } }