Skip to content

Commit

Permalink
Make RunOnce not a manual system implementation
Browse files Browse the repository at this point in the history
Can we make `System` sealed please?
  • Loading branch information
DJMcNab committed Feb 14, 2022
1 parent 29419a3 commit edd2c67
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 44 deletions.
6 changes: 3 additions & 3 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub use bevy_derive::AppLabel;
use bevy_ecs::{
prelude::{FromWorld, IntoExclusiveSystem},
schedule::{
IntoSystemDescriptor, RunOnce, Schedule, Stage, StageLabel, State, StateData, SystemSet,
SystemStage,
run_once_criteria, IntoSystemDescriptor, Schedule, Stage, StageLabel, State, StateData,
SystemSet, SystemStage,
},
system::Resource,
world::World,
Expand Down Expand Up @@ -594,7 +594,7 @@ impl App {
.add_stage(
StartupSchedule,
Schedule::default()
.with_run_criteria(RunOnce::default())
.with_run_criteria(run_once_criteria())
.with_stage(StartupStage::PreStartup, SystemStage::parallel())
.with_stage(StartupStage::Startup, SystemStage::parallel())
.with_stage(StartupStage::PostStartup, SystemStage::parallel()),
Expand Down
47 changes: 6 additions & 41 deletions crates/bevy_ecs/src/schedule/run_criteria.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
archetype::{Archetype, ArchetypeComponentId, ArchetypeGeneration},
component::ComponentId,
query::Access,
archetype::ArchetypeGeneration,
schedule::{BoxedRunCriteriaLabel, GraphNode, RunCriteriaLabel},
system::{BoxedSystem, IntoSystem, System},
world::World,
Expand Down Expand Up @@ -399,47 +397,14 @@ where
}
}

#[derive(Default)]
pub struct RunOnce {
ran: bool,
archetype_component_access: Access<ArchetypeComponentId>,
component_access: Access<ComponentId>,
}

impl System for RunOnce {
type In = ();
type Out = ShouldRun;

fn name(&self) -> Cow<'static, str> {
Cow::Borrowed(std::any::type_name::<RunOnce>())
}

fn new_archetype(&mut self, _archetype: &Archetype) {}

fn component_access(&self) -> &Access<ComponentId> {
&self.component_access
}

fn archetype_component_access(&self) -> &Access<ArchetypeComponentId> {
&self.archetype_component_access
}

fn is_send(&self) -> bool {
true
}

unsafe fn run_unsafe(&mut self, _input: (), _world: &World) -> ShouldRun {
if self.ran {
pub fn run_once_criteria() -> impl FnMut() -> ShouldRun {
let mut ran = false;
move || {
if ran {
ShouldRun::No
} else {
self.ran = true;
ran = true;
ShouldRun::Yes
}
}

fn apply_buffers(&mut self, _world: &mut World) {}

fn initialize(&mut self, _world: &mut World) {}

fn check_change_tick(&mut self, _change_tick: u32) {}
}

0 comments on commit edd2c67

Please sign in to comment.