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 Apr 9, 2022
1 parent df1e9bb commit 4860cdf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 45 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 @@ -4,8 +4,8 @@ use bevy_ecs::{
event::Events,
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 @@ -591,7 +591,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
48 changes: 6 additions & 42 deletions crates/bevy_ecs/src/schedule/run_criteria.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
archetype::ArchetypeComponentId,
component::ComponentId,
query::Access,
schedule::{BoxedRunCriteriaLabel, GraphNode, RunCriteriaLabel},
system::{BoxedSystem, IntoSystem, System},
system::{BoxedSystem, IntoSystem},
world::World,
};
use std::borrow::Cow;
Expand Down Expand Up @@ -325,47 +322,14 @@ impl RunCriteria {
}
}

#[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 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 update_archetype_component_access(&mut self, _world: &World) {}

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

0 comments on commit 4860cdf

Please sign in to comment.