diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index ae1efa7694334..33cc26ca739c9 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -162,9 +162,7 @@ impl SubApp { /// Runs the `SubApp`'s default schedule. pub fn run(&mut self) { - self.app - .world - .run_schedule_ref(&*self.app.main_schedule_label); + self.app.world.run_schedule(&*self.app.main_schedule_label); self.app.world.clear_trackers(); } @@ -241,7 +239,7 @@ impl App { { #[cfg(feature = "trace")] let _bevy_frame_update_span = info_span!("main app").entered(); - self.world.run_schedule_ref(&*self.main_schedule_label); + self.world.run_schedule(&*self.main_schedule_label); } for (_label, sub_app) in self.sub_apps.iter_mut() { #[cfg(feature = "trace")] diff --git a/crates/bevy_app/src/main_schedule.rs b/crates/bevy_app/src/main_schedule.rs index 2042b9e01f4e8..c940fda5337b9 100644 --- a/crates/bevy_app/src/main_schedule.rs +++ b/crates/bevy_app/src/main_schedule.rs @@ -143,7 +143,7 @@ impl Main { world.resource_scope(|world, order: Mut| { for label in &order.labels { - let _ = world.try_run_schedule_ref(&**label); + let _ = world.try_run_schedule(&**label); } }); } diff --git a/crates/bevy_ecs/src/schedule/set.rs b/crates/bevy_ecs/src/schedule/set.rs index 1e00a039a5792..590dfc6ee6f0f 100644 --- a/crates/bevy_ecs/src/schedule/set.rs +++ b/crates/bevy_ecs/src/schedule/set.rs @@ -201,7 +201,7 @@ mod tests { let boxed: Box = Box::new(A); world.insert_resource(Flag(false)); - world.run_schedule_ref(&boxed); + world.run_schedule(&boxed); assert!(world.resource::().0); world.insert_resource(Flag(false)); diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index c14b4ab8d4e90..943bb04c7f6ee 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1734,30 +1734,10 @@ impl World { /// For other use cases, see the example on [`World::schedule_scope`]. pub fn try_schedule_scope( &mut self, - label: impl ScheduleLabel, - f: impl FnOnce(&mut World, &mut Schedule) -> R, - ) -> Result { - self.try_schedule_scope_ref(&label, f) - } - - /// Temporarily removes the schedule associated with `label` from the world, - /// runs user code, and finally re-adds the schedule. - /// This returns a [`TryRunScheduleError`] if there is no schedule - /// associated with `label`. - /// - /// 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 cases where you just need to call the schedule once, - /// consider using [`World::try_run_schedule_ref`] instead. - /// For other use cases, see the example on [`World::schedule_scope`]. - pub fn try_schedule_scope_ref( - &mut self, - label: &dyn ScheduleLabel, + label: impl AsRef, f: impl FnOnce(&mut World, &mut Schedule) -> R, ) -> Result { + let label = label.as_ref(); let Some((extracted_label, mut schedule)) = self.get_resource_mut::().and_then(|mut s| s.remove_entry(label)) else { @@ -1818,33 +1798,10 @@ impl World { /// If the requested schedule does not exist. pub fn schedule_scope( &mut self, - label: impl ScheduleLabel, - f: impl FnOnce(&mut World, &mut Schedule) -> R, - ) -> R { - self.schedule_scope_ref(&label, f) - } - - /// Temporarily removes the schedule associated with `label` from the world, - /// runs user code, and finally re-adds the schedule. - /// - /// Unlike the `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 cases where you just need to call the schedule, - /// consider using [`World::run_schedule_ref`] instead. - /// For other use cases, see the example on [`World::schedule_scope`]. - /// - /// # Panics - /// - /// If the requested schedule does not exist. - pub fn schedule_scope_ref( - &mut self, - label: &dyn ScheduleLabel, + label: impl AsRef, f: impl FnOnce(&mut World, &mut Schedule) -> R, ) -> R { - self.try_schedule_scope_ref(label, f) + self.try_schedule_scope(label, f) .unwrap_or_else(|e| panic!("{e}")) } @@ -1857,25 +1814,9 @@ impl World { /// 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, + label: impl AsRef, ) -> Result<(), TryRunScheduleError> { - self.try_schedule_scope_ref(label, |world, sched| sched.run(world)) + self.try_schedule_scope(label, |world, sched| sched.run(world)) } /// Runs the [`Schedule`] associated with the `label` a single time. @@ -1888,8 +1829,8 @@ impl World { /// # Panics /// /// If the requested schedule does not exist. - pub fn run_schedule(&mut self, label: impl ScheduleLabel) { - self.run_schedule_ref(&label); + pub fn run_schedule(&mut self, label: impl AsRef) { + self.schedule_scope(label, |world, sched| sched.run(world)); } /// Runs the [`Schedule`] associated with the `label` a single time. @@ -1904,8 +1845,9 @@ impl World { /// # Panics /// /// If the requested schedule does not exist. + #[deprecated = "Use `World::run_schedule` instead."] pub fn run_schedule_ref(&mut self, label: &dyn ScheduleLabel) { - self.schedule_scope_ref(label, |world, sched| sched.run(world)); + self.schedule_scope(label, |world, sched| sched.run(world)); } } diff --git a/crates/bevy_macro_utils/src/lib.rs b/crates/bevy_macro_utils/src/lib.rs index 212a78ce3d69b..0aeea189448b4 100644 --- a/crates/bevy_macro_utils/src/lib.rs +++ b/crates/bevy_macro_utils/src/lib.rs @@ -191,6 +191,12 @@ pub fn derive_boxed_label(input: syn::DeriveInput, trait_path: &syn::Path) -> To ::std::hash::Hash::hash(self, &mut state); } } + + impl #impl_generics ::std::convert::AsRef for #ident #ty_generics #where_clause { + fn as_ref(&self) -> &dyn #trait_path { + self + } + } }) .into() } diff --git a/crates/bevy_utils/src/label.rs b/crates/bevy_utils/src/label.rs index 3c0fb762d9619..6eff79c0bb389 100644 --- a/crates/bevy_utils/src/label.rs +++ b/crates/bevy_utils/src/label.rs @@ -111,6 +111,13 @@ macro_rules! define_boxed_label { } } + impl ::std::convert::AsRef for dyn $label_trait_name { + #[inline] + fn as_ref(&self) -> &Self { + self + } + } + impl Clone for Box { fn clone(&self) -> Self { self.dyn_clone()