Skip to content

Commit 06e0b06

Browse files
DJMcNabexjam
authored andcommitted
Make RunOnce a non-manual System impl (bevyengine#3922)
# Objective - `RunOnce` was a manual `System` implementation. - Adding run criteria to stages was yet to be systemyoten ## Solution - Make it a normal function - yeet ## Changelog - Replaced `RunOnce` with `ShouldRun::once` ## Migration guide The run criterion `RunOnce`, which would make the controlled systems run only once, has been replaced with a new run criterion function `ShouldRun::once`. Replace all instances of `RunOnce` with `ShouldRun::once`.
1 parent aaa7e72 commit 06e0b06

File tree

3 files changed

+23
-58
lines changed

3 files changed

+23
-58
lines changed

crates/bevy_app/src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy_ecs::{
44
event::Events,
55
prelude::{FromWorld, IntoExclusiveSystem},
66
schedule::{
7-
IntoSystemDescriptor, RunOnce, Schedule, Stage, StageLabel, State, StateData, SystemSet,
7+
IntoSystemDescriptor, Schedule, ShouldRun, Stage, StageLabel, State, StateData, SystemSet,
88
SystemStage,
99
},
1010
system::Resource,
@@ -591,7 +591,7 @@ impl App {
591591
.add_stage(
592592
StartupSchedule,
593593
Schedule::default()
594-
.with_run_criteria(RunOnce::default())
594+
.with_run_criteria(ShouldRun::once)
595595
.with_stage(StartupStage::PreStartup, SystemStage::parallel())
596596
.with_stage(StartupStage::Startup, SystemStage::parallel())
597597
.with_stage(StartupStage::PostStartup, SystemStage::parallel()),

crates/bevy_ecs/src/schedule/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use system_set::*;
2727

2828
use std::fmt::Debug;
2929

30-
use crate::{system::System, world::World};
30+
use crate::{system::IntoSystem, world::World};
3131
use bevy_utils::HashMap;
3232

3333
/// A container of [`Stage`]s set to be run in a linear order.
@@ -76,7 +76,7 @@ impl Schedule {
7676
}
7777

7878
#[must_use]
79-
pub fn with_run_criteria<S: System<In = (), Out = ShouldRun>>(mut self, system: S) -> Self {
79+
pub fn with_run_criteria<S: IntoSystem<(), ShouldRun, P>, P>(mut self, system: S) -> Self {
8080
self.set_run_criteria(system);
8181
self
8282
}
@@ -92,11 +92,9 @@ impl Schedule {
9292
self
9393
}
9494

95-
pub fn set_run_criteria<S: System<In = (), Out = ShouldRun>>(
96-
&mut self,
97-
system: S,
98-
) -> &mut Self {
99-
self.run_criteria.set(Box::new(system));
95+
pub fn set_run_criteria<S: IntoSystem<(), ShouldRun, P>, P>(&mut self, system: S) -> &mut Self {
96+
self.run_criteria
97+
.set(Box::new(IntoSystem::into_system(system)));
10098
self
10199
}
102100

crates/bevy_ecs/src/schedule/run_criteria.rs

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use crate::{
2-
archetype::ArchetypeComponentId,
3-
component::ComponentId,
4-
query::Access,
52
schedule::{BoxedRunCriteriaLabel, GraphNode, RunCriteriaLabel},
6-
system::{BoxedSystem, IntoSystem, System},
3+
system::{BoxedSystem, IntoSystem, Local},
74
world::World,
85
};
96
use std::borrow::Cow;
@@ -44,6 +41,21 @@ pub enum ShouldRun {
4441
NoAndCheckAgain,
4542
}
4643

44+
impl ShouldRun {
45+
/// A run criterion which returns [`ShouldRun::Yes`] exactly once.
46+
///
47+
/// This leads to the systems controlled by it only being
48+
/// executed one time only.
49+
pub fn once(mut ran: Local<bool>) -> ShouldRun {
50+
if *ran {
51+
ShouldRun::No
52+
} else {
53+
*ran = true;
54+
ShouldRun::Yes
55+
}
56+
}
57+
}
58+
4759
#[derive(Default)]
4860
pub(crate) struct BoxedRunCriteria {
4961
criteria_system: Option<BoxedSystem<(), ShouldRun>>,
@@ -324,48 +336,3 @@ impl RunCriteria {
324336
}
325337
}
326338
}
327-
328-
#[derive(Default)]
329-
pub struct RunOnce {
330-
ran: bool,
331-
archetype_component_access: Access<ArchetypeComponentId>,
332-
component_access: Access<ComponentId>,
333-
}
334-
335-
impl System for RunOnce {
336-
type In = ();
337-
type Out = ShouldRun;
338-
339-
fn name(&self) -> Cow<'static, str> {
340-
Cow::Borrowed(std::any::type_name::<RunOnce>())
341-
}
342-
343-
fn component_access(&self) -> &Access<ComponentId> {
344-
&self.component_access
345-
}
346-
347-
fn archetype_component_access(&self) -> &Access<ArchetypeComponentId> {
348-
&self.archetype_component_access
349-
}
350-
351-
fn is_send(&self) -> bool {
352-
true
353-
}
354-
355-
unsafe fn run_unsafe(&mut self, _input: (), _world: &World) -> ShouldRun {
356-
if self.ran {
357-
ShouldRun::No
358-
} else {
359-
self.ran = true;
360-
ShouldRun::Yes
361-
}
362-
}
363-
364-
fn apply_buffers(&mut self, _world: &mut World) {}
365-
366-
fn initialize(&mut self, _world: &mut World) {}
367-
368-
fn update_archetype_component_access(&mut self, _world: &World) {}
369-
370-
fn check_change_tick(&mut self, _change_tick: u32) {}
371-
}

0 commit comments

Comments
 (0)