Skip to content

Commit 9fd867a

Browse files
authored
Simplify world schedule methods (#8403)
# Objective Methods for interacting with world schedules currently have two variants: one that takes `impl ScheduleLabel` and one that takes `&dyn ScheduleLabel`. Operations such as `run_schedule` or `schedule_scope` only use the label by reference, so there is little reason to have an owned variant of these functions. ## Solution Decrease maintenance burden by merging the `ref` variants of these functions with the owned variants. --- ## Changelog - Deprecated `World::run_schedule_ref`. It is now redundant, since `World::run_schedule` can take values by reference. ## Migration Guide The method `World::run_schedule_ref` has been deprecated, and will be removed in the next version of Bevy. Use `run_schedule` instead.
1 parent fe852fd commit 9fd867a

File tree

6 files changed

+27
-74
lines changed

6 files changed

+27
-74
lines changed

crates/bevy_app/src/app.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ impl SubApp {
162162

163163
/// Runs the `SubApp`'s default schedule.
164164
pub fn run(&mut self) {
165-
self.app
166-
.world
167-
.run_schedule_ref(&*self.app.main_schedule_label);
165+
self.app.world.run_schedule(&*self.app.main_schedule_label);
168166
self.app.world.clear_trackers();
169167
}
170168

@@ -241,7 +239,7 @@ impl App {
241239
{
242240
#[cfg(feature = "trace")]
243241
let _bevy_frame_update_span = info_span!("main app").entered();
244-
self.world.run_schedule_ref(&*self.main_schedule_label);
242+
self.world.run_schedule(&*self.main_schedule_label);
245243
}
246244
for (_label, sub_app) in self.sub_apps.iter_mut() {
247245
#[cfg(feature = "trace")]

crates/bevy_app/src/main_schedule.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl Main {
143143

144144
world.resource_scope(|world, order: Mut<MainScheduleOrder>| {
145145
for label in &order.labels {
146-
let _ = world.try_run_schedule_ref(&**label);
146+
let _ = world.try_run_schedule(&**label);
147147
}
148148
});
149149
}

crates/bevy_ecs/src/schedule/set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ mod tests {
201201
let boxed: Box<dyn ScheduleLabel> = Box::new(A);
202202

203203
world.insert_resource(Flag(false));
204-
world.run_schedule_ref(&boxed);
204+
world.run_schedule(&boxed);
205205
assert!(world.resource::<Flag>().0);
206206

207207
world.insert_resource(Flag(false));

crates/bevy_ecs/src/world/mod.rs

+10-68
Original file line numberDiff line numberDiff line change
@@ -1734,30 +1734,10 @@ impl World {
17341734
/// For other use cases, see the example on [`World::schedule_scope`].
17351735
pub fn try_schedule_scope<R>(
17361736
&mut self,
1737-
label: impl ScheduleLabel,
1738-
f: impl FnOnce(&mut World, &mut Schedule) -> R,
1739-
) -> Result<R, TryRunScheduleError> {
1740-
self.try_schedule_scope_ref(&label, f)
1741-
}
1742-
1743-
/// Temporarily removes the schedule associated with `label` from the world,
1744-
/// runs user code, and finally re-adds the schedule.
1745-
/// This returns a [`TryRunScheduleError`] if there is no schedule
1746-
/// associated with `label`.
1747-
///
1748-
/// Unlike the `try_run_schedule` method, this method takes the label by reference, which can save a clone.
1749-
///
1750-
/// The [`Schedule`] is fetched from the [`Schedules`] resource of the world by its label,
1751-
/// and system state is cached.
1752-
///
1753-
/// For simple cases where you just need to call the schedule once,
1754-
/// consider using [`World::try_run_schedule_ref`] instead.
1755-
/// For other use cases, see the example on [`World::schedule_scope`].
1756-
pub fn try_schedule_scope_ref<R>(
1757-
&mut self,
1758-
label: &dyn ScheduleLabel,
1737+
label: impl AsRef<dyn ScheduleLabel>,
17591738
f: impl FnOnce(&mut World, &mut Schedule) -> R,
17601739
) -> Result<R, TryRunScheduleError> {
1740+
let label = label.as_ref();
17611741
let Some((extracted_label, mut schedule))
17621742
= self.get_resource_mut::<Schedules>().and_then(|mut s| s.remove_entry(label))
17631743
else {
@@ -1818,33 +1798,10 @@ impl World {
18181798
/// If the requested schedule does not exist.
18191799
pub fn schedule_scope<R>(
18201800
&mut self,
1821-
label: impl ScheduleLabel,
1822-
f: impl FnOnce(&mut World, &mut Schedule) -> R,
1823-
) -> R {
1824-
self.schedule_scope_ref(&label, f)
1825-
}
1826-
1827-
/// Temporarily removes the schedule associated with `label` from the world,
1828-
/// runs user code, and finally re-adds the schedule.
1829-
///
1830-
/// Unlike the `run_schedule` method, this method takes the label by reference, which can save a clone.
1831-
///
1832-
/// The [`Schedule`] is fetched from the [`Schedules`] resource of the world by its label,
1833-
/// and system state is cached.
1834-
///
1835-
/// For simple cases where you just need to call the schedule,
1836-
/// consider using [`World::run_schedule_ref`] instead.
1837-
/// For other use cases, see the example on [`World::schedule_scope`].
1838-
///
1839-
/// # Panics
1840-
///
1841-
/// If the requested schedule does not exist.
1842-
pub fn schedule_scope_ref<R>(
1843-
&mut self,
1844-
label: &dyn ScheduleLabel,
1801+
label: impl AsRef<dyn ScheduleLabel>,
18451802
f: impl FnOnce(&mut World, &mut Schedule) -> R,
18461803
) -> R {
1847-
self.try_schedule_scope_ref(label, f)
1804+
self.try_schedule_scope(label, f)
18481805
.unwrap_or_else(|e| panic!("{e}"))
18491806
}
18501807

@@ -1857,25 +1814,9 @@ impl World {
18571814
/// For simple testing use cases, call [`Schedule::run(&mut world)`](Schedule::run) instead.
18581815
pub fn try_run_schedule(
18591816
&mut self,
1860-
label: impl ScheduleLabel,
1861-
) -> Result<(), TryRunScheduleError> {
1862-
self.try_run_schedule_ref(&label)
1863-
}
1864-
1865-
/// Attempts to run the [`Schedule`] associated with the `label` a single time,
1866-
/// and returns a [`TryRunScheduleError`] if the schedule does not exist.
1867-
///
1868-
/// Unlike the `try_run_schedule` method, this method takes the label by reference, which can save a clone.
1869-
///
1870-
/// The [`Schedule`] is fetched from the [`Schedules`] resource of the world by its label,
1871-
/// and system state is cached.
1872-
///
1873-
/// For simple testing use cases, call [`Schedule::run(&mut world)`](Schedule::run) instead.
1874-
pub fn try_run_schedule_ref(
1875-
&mut self,
1876-
label: &dyn ScheduleLabel,
1817+
label: impl AsRef<dyn ScheduleLabel>,
18771818
) -> Result<(), TryRunScheduleError> {
1878-
self.try_schedule_scope_ref(label, |world, sched| sched.run(world))
1819+
self.try_schedule_scope(label, |world, sched| sched.run(world))
18791820
}
18801821

18811822
/// Runs the [`Schedule`] associated with the `label` a single time.
@@ -1888,8 +1829,8 @@ impl World {
18881829
/// # Panics
18891830
///
18901831
/// If the requested schedule does not exist.
1891-
pub fn run_schedule(&mut self, label: impl ScheduleLabel) {
1892-
self.run_schedule_ref(&label);
1832+
pub fn run_schedule(&mut self, label: impl AsRef<dyn ScheduleLabel>) {
1833+
self.schedule_scope(label, |world, sched| sched.run(world));
18931834
}
18941835

18951836
/// Runs the [`Schedule`] associated with the `label` a single time.
@@ -1904,8 +1845,9 @@ impl World {
19041845
/// # Panics
19051846
///
19061847
/// If the requested schedule does not exist.
1848+
#[deprecated = "Use `World::run_schedule` instead."]
19071849
pub fn run_schedule_ref(&mut self, label: &dyn ScheduleLabel) {
1908-
self.schedule_scope_ref(label, |world, sched| sched.run(world));
1850+
self.schedule_scope(label, |world, sched| sched.run(world));
19091851
}
19101852
}
19111853

crates/bevy_macro_utils/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ pub fn derive_boxed_label(input: syn::DeriveInput, trait_path: &syn::Path) -> To
191191
::std::hash::Hash::hash(self, &mut state);
192192
}
193193
}
194+
195+
impl #impl_generics ::std::convert::AsRef<dyn #trait_path> for #ident #ty_generics #where_clause {
196+
fn as_ref(&self) -> &dyn #trait_path {
197+
self
198+
}
199+
}
194200
})
195201
.into()
196202
}

crates/bevy_utils/src/label.rs

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ macro_rules! define_boxed_label {
111111
}
112112
}
113113

114+
impl ::std::convert::AsRef<dyn $label_trait_name> for dyn $label_trait_name {
115+
#[inline]
116+
fn as_ref(&self) -> &Self {
117+
self
118+
}
119+
}
120+
114121
impl Clone for Box<dyn $label_trait_name> {
115122
fn clone(&self) -> Self {
116123
self.dyn_clone()

0 commit comments

Comments
 (0)