Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify world schedule methods #8403

Merged
merged 12 commits into from
Apr 19, 2023
10 changes: 4 additions & 6 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -1025,7 +1023,7 @@ mod tests {
app.add_state::<AppState>()
.add_systems(OnEnter(AppState::MainMenu), (foo, bar));

app.world.run_schedule(OnEnter(AppState::MainMenu));
app.world.run_schedule(&OnEnter(AppState::MainMenu));
assert_eq!(app.world.entities().len(), 2);
}

Expand All @@ -1035,7 +1033,7 @@ mod tests {
app.add_systems(OnEnter(AppState::MainMenu), (foo, bar))
.add_state::<AppState>();

app.world.run_schedule(OnEnter(AppState::MainMenu));
app.world.run_schedule(&OnEnter(AppState::MainMenu));
assert_eq!(app.world.entities().len(), 2);
}
}
8 changes: 4 additions & 4 deletions crates/bevy_app/src/main_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ impl Main {
/// A system that runs the "main schedule"
pub fn run_main(world: &mut World, mut run_at_least_once: Local<bool>) {
if !*run_at_least_once {
let _ = world.try_run_schedule(PreStartup);
let _ = world.try_run_schedule(Startup);
let _ = world.try_run_schedule(PostStartup);
let _ = world.try_run_schedule(&PreStartup);
let _ = world.try_run_schedule(&Startup);
let _ = world.try_run_schedule(&PostStartup);
*run_at_least_once = true;
}

world.resource_scope(|world, order: Mut<MainScheduleOrder>| {
for label in &order.labels {
let _ = world.try_run_schedule_ref(&**label);
let _ = world.try_run_schedule(&**label);
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<S: States> NextState<S> {
/// Run the enter schedule (if it exists) for the current state.
pub fn run_enter_schedule<S: States>(world: &mut World) {
world
.try_run_schedule(OnEnter(world.resource::<State<S>>().0.clone()))
.try_run_schedule(&OnEnter(world.resource::<State<S>>().0.clone()))
.ok();
}

Expand All @@ -133,14 +133,14 @@ pub fn apply_state_transition<S: States>(world: &mut World) {
if *state_resource != entered {
let exited = mem::replace(&mut state_resource.0, entered.clone());
// Try to run the schedules if they exist.
world.try_run_schedule(OnExit(exited.clone())).ok();
world.try_run_schedule(&OnExit(exited.clone())).ok();
world
.try_run_schedule(OnTransition {
.try_run_schedule(&OnTransition {
from: exited,
to: entered.clone(),
})
.ok();
world.try_run_schedule(OnEnter(entered)).ok();
world.try_run_schedule(&OnEnter(entered)).ok();
}
}
}
86 changes: 5 additions & 81 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1733,27 +1733,6 @@ impl World {
/// consider using [`World::try_run_schedule`] instead.
/// For other use cases, see the example on [`World::schedule_scope`].
pub fn try_schedule_scope<R>(
&mut self,
label: impl ScheduleLabel,
f: impl FnOnce(&mut World, &mut Schedule) -> R,
) -> Result<R, TryRunScheduleError> {
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<R>(
&mut self,
label: &dyn ScheduleLabel,
f: impl FnOnce(&mut World, &mut Schedule) -> R,
Expand Down Expand Up @@ -1802,7 +1781,7 @@ impl World {
/// # world.add_schedule(schedule, MySchedule);
/// # fn tick_counter(mut counter: ResMut<Counter>) { counter.0 += 1; }
/// // Run the schedule five times.
/// world.schedule_scope(MySchedule, |world, schedule| {
/// world.schedule_scope(&MySchedule, |world, schedule| {
/// for _ in 0..5 {
/// schedule.run(world);
/// }
Expand All @@ -1817,34 +1796,11 @@ impl World {
///
/// If the requested schedule does not exist.
pub fn schedule_scope<R>(
&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<R>(
&mut self,
label: &dyn ScheduleLabel,
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}"))
}

Expand All @@ -1856,46 +1812,14 @@ 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,
) -> Result<(), TryRunScheduleError> {
self.try_schedule_scope_ref(label, |world, sched| sched.run(world))
}

/// Runs the [`Schedule`] associated with the `label` a single time.
///
/// 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.
///
/// # Panics
///
/// If the requested schedule does not exist.
pub fn run_schedule(&mut self, label: impl ScheduleLabel) {
self.run_schedule_ref(&label);
self.try_schedule_scope(label, |world, sched| sched.run(world))
}

/// Runs the [`Schedule`] associated with the `label` a single time.
///
/// 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.
///
Expand All @@ -1904,8 +1828,8 @@ impl World {
/// # Panics
///
/// If the requested schedule does not exist.
pub fn run_schedule_ref(&mut self, label: &dyn ScheduleLabel) {
self.schedule_scope_ref(label, |world, sched| sched.run(world));
pub fn run_schedule(&mut self, label: &dyn ScheduleLabel) {
self.schedule_scope(label, |world, sched| sched.run(world));
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn extract(main_world: &mut World, render_app: &mut App) {
let inserted_world = std::mem::replace(main_world, scratch_world.0);
render_app.world.insert_resource(MainWorld(inserted_world));

render_app.world.run_schedule(ExtractSchedule);
render_app.world.run_schedule(&ExtractSchedule);

// move the app world back, as if nothing happened.
let inserted_world = render_app.world.remove_resource::<MainWorld>().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_time/src/fixed_timestep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn run_fixed_update_schedule(world: &mut World) {
fixed_time.tick(delta_time);

// Run the schedule until we run out of accumulated time
let _ = world.try_schedule_scope(FixedUpdate, |world, schedule| {
let _ = world.try_schedule_scope(&FixedUpdate, |world, schedule| {
while world.resource_mut::<FixedTime>().expend().is_ok() {
schedule.run(world);
}
Expand Down